Skip to content

Commit

Permalink
Add better auth error propogation from interactor to controller; plus…
Browse files Browse the repository at this point in the history
… unit test (#23)

* Add better auth error propogation from interactor to controller; added
interactor unit test

* Use a concern to make interactors fail on error
  • Loading branch information
suprjinx authored Sep 9, 2024
1 parent 785cc05 commit bebd047
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
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
1 change: 1 addition & 0 deletions app/interactors/authenticate_identity.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class AuthenticateIdentity
include Interactor
include FailOnError

before do
token = context.request.headers["Authorization"]
Expand Down
1 change: 1 addition & 0 deletions app/interactors/authorize_request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class AuthorizeRequest
include Interactor
include FailOnError

def call
Services::DomainOwnershipService.new.authorize!(context.identity, context.request)
Expand Down
6 changes: 0 additions & 6 deletions app/interactors/check_policy.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/interactors/fail_on_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module FailOnError
extend ActiveSupport::Concern

included do
around do |interactor|
interactor.call
rescue => e
context.fail!(error: e)
end
end
end
1 change: 1 addition & 0 deletions app/interactors/obtain_cert.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ObtainCert
include Interactor
include FailOnError

def call
if cert = Services::CertificateService.new.issue_cert(context.request)
Expand Down
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

0 comments on commit bebd047

Please sign in to comment.