Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better auth error propogation from interactor to controller; plus unit test #23

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/certificates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def create
if !req.valid?
raise BadRequestError.new req.errors.full_messages
end
result = IssueCert.call(request: req, identity: @identity)
result = IssueCert.call(request: req, identity: identity)
if result.failure?
raise StandardError.new result.message
raise (result.error || StandardError.new(result.message))
end
@cert = result.cert
end
Expand Down
3 changes: 3 additions & 0 deletions app/interactors/authorize_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ class AuthorizeRequest

def call
Services::DomainOwnershipService.new.authorize!(context.identity, context.request)
rescue AuthError => e
context.error = e
context.fail!
end
end
33 changes: 33 additions & 0 deletions test/interactors/authorize_request_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "test_helper"

class AuthorizeRequestTest < ActiveSupport::TestCase
def setup
@domain = domains(:group_match)
@identity = Identity.new(subject: @domain.owner)
@cr = CertIssueRequest.new(common_name: @domain.fqdn)
@interactor = AuthorizeRequest
end

test "successful call" do
request = CertIssueRequest.new(common_name: @domain.fqdn)
srv = Minitest::Mock.new
srv.expect :authorize!, nil, [ @identity, @cr ]
Services::DomainOwnershipService.stub :new, srv do
context = @interactor.call(identity: @identity, request: @cr)
assert context.success?
end
end

test "unsuccessful call" do
request = CertIssueRequest.new(common_name: @domain.fqdn)
srv = Services::DomainOwnershipService.new
Services::DomainOwnershipService.stub :new, srv do
err = ->(_, _) { raise AuthError.new "no can do" }
srv.stub :authorize!, err do
context = @interactor.call(identity: @identity, request: @cr)
assert_not context.success?
assert_kind_of AuthError, context.error
end
end
end
end