Skip to content

Commit

Permalink
Added test for domain_ownership logic
Browse files Browse the repository at this point in the history
  • Loading branch information
suprjinx committed Aug 24, 2024
1 parent 4cadf7f commit 2f9d3b0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/lib/services/domain_ownership_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def authorize!(identity, cert_req)
(domain.group_delegation &&
(domain.groups & identity.groups).any?))
end
nil
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/domain_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class DomainInfo

attribute :owner, :string
attribute :groups, array: :string, default: []
attribute :group_delegation, :bool, default: false
attribute :group_delegation, :boolean, default: false
end
46 changes: 46 additions & 0 deletions test/lib/services/domain_ownership_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "test_helper"

class DomainOwnershipServiceTest < ActiveSupport::TestCase
def setup
@identity = Identity.new(subject: "[email protected]", groups: ["admin_group"])

Check failure on line 5 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 5 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
@domain = DomainInfo.new(owner: "[email protected]", group_delegation: false, groups: ["admin_group"])

Check failure on line 6 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 6 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
end

test "#authorize! with matching owner" do
ds = Services::DomainOwnershipService.new
ds.stub :get_domain_name, @domain do
assert_nil(ds.authorize!(@identity, CertIssueRequest.new))
end
end

test "#authorize! with non-matching owner" do
ds = Services::DomainOwnershipService.new
@domain.owner = "[email protected]"
ds.stub :get_domain_name, @domain do
assert_raises(AuthError) do
ds.authorize!(@identity, CertIssueRequest.new)
end
end
end

test "#authorize! with matching group" do
ds = Services::DomainOwnershipService.new
@domain.owner = "[email protected]"
@domain.group_delegation = true
ds.stub :get_domain_name, @domain do
assert_nil(ds.authorize!(@identity, CertIssueRequest.new))
end
end

test "#authorize! with non-matching group" do
ds = Services::DomainOwnershipService.new
@domain.owner = "[email protected]"
@identity.groups = ["different_group"]

Check failure on line 38 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 38 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
ds.stub :get_domain_name, @domain do
assert_raises(AuthError) do
ds.authorize!(@identity, CertIssueRequest.new)
end
end
end

Check failure on line 45 in test/lib/services/domain_ownership_service_test.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
end
1 change: 0 additions & 1 deletion test/models/cert_isssue_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def setup
assert_not @cert_issue_request.valid?
end


test "fqdns should return alt_names plus common_name" do
assert_equal [ "alt1.example.com", "alt2.example.com", "example.com" ], @cert_issue_request.fqdns
end
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require "minitest"
require "minitest/mock"

module ActiveSupport
class TestCase
Expand Down

0 comments on commit 2f9d3b0

Please sign in to comment.