-
Notifications
You must be signed in to change notification settings - Fork 4
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
Interactors #7
Merged
Merged
Interactors #7
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c3f823b
add interactor gem
suprjinx 1f316c3
introduced app interactors and switched controllers to use
suprjinx 8c103db
use context.message for AuthError
suprjinx 7d7a911
Add domain policy rules to request validation
suprjinx 50da259
Add placeholder for logging
suprjinx 5cd8d9d
Merge branch 'main' into interactors
suprjinx 67956bf
Add unit test for authenticate_identity interactor
suprjinx 3601bc4
Add unit test for obtain_cert interactor
suprjinx 54d12d0
Interactors automatically rescue, don't need this
suprjinx 4d5bf44
Added some tests for cert request validation
suprjinx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class AuthenticateIdentity | ||
include Interactor | ||
|
||
before do | ||
token = context.request.headers["Authorization"] | ||
context.token = token.split(" ").last if token | ||
end | ||
|
||
def call | ||
if identity = Services::AuthService.new.authenticate!(context.token) | ||
context.identity = identity | ||
else | ||
context.fail!(message: "Invalid token") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class CheckPolicy | ||
include Interactor | ||
|
||
def call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class IssueCert | ||
include Interactor::Organizer | ||
|
||
organize CheckPolicy, ObtainCert, Log | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Log | ||
include Interactor | ||
|
||
def call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class ObtainCert | ||
include Interactor | ||
|
||
def call | ||
if cert = Services::CertificateService.new.issue_cert(context.request) | ||
context.cert = cert | ||
else | ||
context.fail!(message: "Failed to issue certificate") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "test_helper" | ||
|
||
class AuthenticateIdentityTest < ActiveSupport::TestCase | ||
def setup | ||
@interactor = AuthenticateIdentity | ||
@identity = Identity.new(subject: "[email protected]", groups: [ "admin_group" ]) | ||
end | ||
|
||
test "successful authentication" do | ||
request = OpenStruct.new(headers: { "Authorization" => "Bearer valid_token" }) | ||
srv = Minitest::Mock.new | ||
srv.expect :authenticate!, @identity, [ "valid_token" ] | ||
Services::AuthService.stub :new, srv do | ||
context = @interactor.call(request: request) | ||
assert context.success? | ||
assert_equal @identity, context.identity | ||
end | ||
end | ||
|
||
test "failed authentication" do | ||
request = OpenStruct.new(headers: { "Authorization" => "Bearer invalid_token" }) | ||
srv = Minitest::Mock.new | ||
srv.expect :authenticate!, nil, [ "invalid_token" ] | ||
Services::AuthService.stub :new, srv do | ||
context = @interactor.call(request: request) | ||
assert_not context.success? | ||
assert_nil context.identity | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "test_helper" | ||
|
||
class ObtainCertTest < ActiveSupport::TestCase | ||
def setup | ||
@interactor = ObtainCert | ||
@cert = OpenStruct.new(certificate: "certificate", ca_chain: "ca_chain") | ||
end | ||
|
||
test "successful issue" do | ||
request = CertIssueRequest.new | ||
srv = Minitest::Mock.new | ||
srv.expect :issue_cert, @cert, [ request ] | ||
Services::CertificateService.stub :new, srv do | ||
context = @interactor.call(request: request) | ||
assert context.success? | ||
assert_equal @cert, context.cert | ||
end | ||
end | ||
|
||
test "unsuccessful issue" do | ||
request = CertIssueRequest.new | ||
srv = Minitest::Mock.new | ||
srv.expect :issue_cert, nil, [ request ] | ||
Services::CertificateService.stub :new, srv do | ||
context = @interactor.call(request: request) | ||
assert context.failure? | ||
assert_equal nil, context.cert | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get a little unit test on this bad boy?