Skip to content

Commit

Permalink
New test for RefreshDomain interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
suprjinx committed Sep 12, 2024
1 parent 12e7833 commit d0c9bd3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .devcontainer/app_reg_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"ownerDelegatedRequestsToTeam": true,
"autoApprovedGroups": "group1",
"autoApprovedServiceAccounts": "[email protected]"
},
{
"id": "example2.com",
"fullyQualifiedDomainName": "example2.com",
"ownerDelegatedRequestsToTeam": true,
"autoApprovedGroups": "group1",
"autoApprovedServiceAccounts": "[email protected]"
}
]
}
6 changes: 4 additions & 2 deletions app/interactors/authorize_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ class AuthorizeRequest

def call
context.request.fqdns.each do |fqdn|
domain = Domain.where(fqdn: fqdn).first!
raise AuthError unless domain.users_array.include?(context.identity.subject) ||
domain = Domain.where(fqdn: fqdn).first
raise AuthError.new("Common or alt name not recognized") unless domain
raise AuthError.new("No subject or group authorization") unless
domain.users_array.include?(context.identity.subject) ||
(domain.group_delegation? && (domain.groups_array & context.identity.groups).any?)
end
nil
Expand Down
6 changes: 3 additions & 3 deletions app/interactors/refresh_domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ class RefreshDomain
include Interactor

def call
domain_info = Services::DomainOwnershipService.new.get_domain_info(context.request.fqdn)
domain_record = Domain.first_or_create(fqdn: context.request.fqdn)
domain_info = Services::DomainOwnershipService.new.get_domain_info(context.request.common_name)
domain_record = Domain.find_or_create_by!(fqdn: context.request.common_name)
if !domain_info
domain_record.delete
domain_record.destroy!
return
end

Expand Down
41 changes: 41 additions & 0 deletions test/interactors/refresh_domain_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "test_helper"

class RefreshDomainTest < ActiveSupport::TestCase
def setup
@domain = domains(:owner_match)
@identity = Identity.new(subject: @domain.users_array.first)
@cr = CertIssueRequest.new(common_name: @domain.fqdn)
@interactor = RefreshDomain
end

test ".call updates db record with service response when 200" do
rslt = @interactor.call(identity: @identity, request: @cr)
assert rslt.success?
reloaded = Domain.where(fqdn: @domain.fqdn).first!
assert_not_equal @domain.users, reloaded.users
assert_not_equal @domain.groups, reloaded.groups
assert_not_equal @domain.group_delegation, reloaded.group_delegation
end

test ".call deletes db record when service 404" do
@domain = domains(:no_match) # this fixture should have no match
@cr = CertIssueRequest.new(common_name: @domain.fqdn)
rslt = @interactor.call(identity: @identity, request: @cr)
assert rslt.success?
reloaded = Domain.where(fqdn: @domain.fqdn).first
assert_nil reloaded
end

test ".call leaves db record as-is when service has error" do
mock = Services::DomainOwnershipService.new
err = ->(_){ raise Faraday::TimeoutError.new }

Check failure on line 31 in test/interactors/refresh_domain_test.rb

View workflow job for this annotation

GitHub Actions / build_test

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
mock.stub(:get_domain_info, err) do

Check failure on line 32 in test/interactors/refresh_domain_test.rb

View workflow job for this annotation

GitHub Actions / build_test

Layout/TrailingWhitespace: Trailing whitespace detected.
Services::DomainOwnershipService.stub :new, mock do
rslt = @interactor.call(identity: @identity, request: @cr)
assert rslt.success?
reloaded = Domain.where(fqdn: @domain.fqdn).first!
assert_equal @domain.users, reloaded.users
end
end
end
end
6 changes: 3 additions & 3 deletions test/lib/services/domain_ownership_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

class DomainOwnershipServiceTest < ActiveSupport::TestCase
setup do
@subject = Services::DomainOwnershipService.new
@service = Services::DomainOwnershipService.new
end

test "#get_domain_info fetches from configured api server" do
domain_info = @subject.get_domain_info(domains(:owner_match).fqdn)
domain_info = @service.get_domain_info(domains(:owner_match).fqdn)
assert_not_nil domain_info
assert_equal "group1", domain_info.groups
assert_equal "[email protected]", domain_info.users
Expand All @@ -15,7 +15,7 @@ class DomainOwnershipServiceTest < ActiveSupport::TestCase
end

test "#get_domain_info returns nil for unmatched fqdn" do
domain_info = @subject.get_domain_info(domains(:group_match).fqdn)
domain_info = @service.get_domain_info(domains(:no_match).fqdn)
assert_nil domain_info
end
end

0 comments on commit d0c9bd3

Please sign in to comment.