-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from suprjinx/interactors
Interactors
- Loading branch information
Showing
14 changed files
with
159 additions
and
7 deletions.
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 call" 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 "unsuccessful call" 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 call" 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 call" 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 |
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