-
Notifications
You must be signed in to change notification settings - Fork 7
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
Ctskf 690 cccd set up access to maat api #6611
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f47b383
Create presenter classes that formats maat and representation orders …
Katy600 d4fcb7d
Create a connection interface to call maat API. Create an interface t…
Katy600 09e905a
Create table to display maat data
Katy600 41523eb
Add tests for MaatService
jrmhaig 876d141
Add tests for defendant presenters
jrmhaig 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module CaseWorkers | ||
class DefendantPresenter < BasePresenter | ||
presents :defendant | ||
|
||
def representation_orders | ||
defendant.representation_orders.map { |rep_order| RepresentationOrder.new(rep_order, @view) } | ||
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 @@ | ||
module CaseWorkers | ||
class RepresentationOrder < BasePresenter | ||
presents :representation_order | ||
def maat_details = @maat_details ||= MaatService.call(maat_reference:) | ||
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,9 @@ | ||
module ExternalUsers | ||
class DefendantPresenter < BasePresenter | ||
presents :defendant | ||
|
||
def representation_orders | ||
defendant.representation_orders.map { |rep_order| RepresentationOrder.new(rep_order, @view) } | ||
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,9 @@ | ||
module ExternalUsers | ||
class RepresentationOrder < BasePresenter | ||
presents :representation_order | ||
|
||
def maat_details | ||
{ case_number: '???' } | ||
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,17 @@ | ||
class MaatService | ||
def self.call(...) = new(...).call | ||
|
||
def initialize(**kwargs) | ||
@connection = MaatService::Connection.instance | ||
@maat_reference = kwargs[:maat_reference] | ||
end | ||
|
||
def call | ||
data = @connection.fetch(@maat_reference) | ||
|
||
{ | ||
case_number: data['caseId'], | ||
representation_order_date: data['crownRepOrderDate'] | ||
} | ||
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,37 @@ | ||
class MaatService | ||
class Connection | ||
include Singleton | ||
|
||
def fetch(maat_reference) | ||
JSON.parse(client.get("assessment/rep-orders/#{maat_reference}").body) | ||
rescue Faraday::ConnectionFailed | ||
{} | ||
end | ||
|
||
private | ||
|
||
def client | ||
@client ||= Faraday.new(Settings.maat_api_url, request: { timeout: 2 }) do |conn| | ||
conn.headers['Authorization'] = "Bearer #{oauth_token}" | ||
end | ||
end | ||
|
||
def oauth_token | ||
response = Faraday.post(Settings.maat_api_oauth_url, request_params, request_headers) | ||
JSON.parse(response.body)['access_token'] | ||
end | ||
|
||
def request_params | ||
{ | ||
client_id: Settings.maat_api_oauth_client_id, | ||
client_secret: Settings.maat_api_oauth_client_secret, | ||
scope: Settings.maat_api_oauth_scope, | ||
grant_type: 'client_credentials' | ||
} | ||
end | ||
|
||
def request_headers | ||
{ content_type: 'application/x-www-form-urlencoded' } | ||
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
- defendants.each_with_index do | defendant, index | | ||
.govuk-grid-column-one-half | ||
= render partial: 'shared/claim_defendant_details', locals: { defendant: defendant, index: index += 1 } | ||
= render partial: 'shared/claim_defendant_details', locals: { defendant: present(defendant, CaseWorkers::DefendantPresenter), index: index += 1 } |
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 @@ | ||
= govuk_table do |table| | ||
- table.with_caption(size: 's', text: 'Details from MAAT (beta)') | ||
|
||
- table.with_body do |body| | ||
- body.with_row do |row| | ||
- row.with_cell(header: true, text: 'Case number') | ||
- row.with_cell(text: representation_order.maat_details[:case_number]) | ||
|
||
- body.with_row do |row| | ||
- row.with_cell(header: true, text: 'Date') | ||
- row.with_cell(text: representation_order.maat_details[:representation_order_date]) |
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,20 @@ | ||
RSpec.describe CaseWorkers::DefendantPresenter do | ||
subject(:defendant_presenter) { described_class.new(defendant, view) } | ||
|
||
describe '#representation_orders' do | ||
subject(:representation_orders) { defendant_presenter.representation_orders } | ||
|
||
context 'when there are representation orders' do | ||
let(:defendant) { build(:defendant, representation_orders: build_list(:representation_order, 3)) } | ||
|
||
it { expect(representation_orders.length).to eq 3 } | ||
it { is_expected.to all(be_a(CaseWorkers::RepresentationOrder)) } | ||
end | ||
|
||
context 'when there are no representation orders' do | ||
let(:defendant) { build(:defendant, representation_orders: []) } | ||
|
||
it { is_expected.to be_empty } | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
spec/presenters/external_users/defendant_presenter_spec.rb
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,20 @@ | ||
RSpec.describe ExternalUsers::DefendantPresenter do | ||
subject(:defendant_presenter) { described_class.new(defendant, view) } | ||
|
||
describe '#representation_orders' do | ||
subject(:representation_orders) { defendant_presenter.representation_orders } | ||
|
||
context 'when there are representation orders' do | ||
let(:defendant) { build(:defendant, representation_orders: build_list(:representation_order, 3)) } | ||
|
||
it { expect(representation_orders.length).to eq 3 } | ||
it { is_expected.to all(be_a(ExternalUsers::RepresentationOrder)) } | ||
end | ||
|
||
context 'when there are no representation orders' do | ||
let(:defendant) { build(:defendant, representation_orders: []) } | ||
|
||
it { is_expected.to be_empty } | ||
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,61 @@ | ||
RSpec.describe MaatService do | ||
before do | ||
allow(Settings).to receive_messages(maat_api_url: 'https://example.com', maat_api_oauth_url: 'https://example.com/oauth') | ||
stub_request(:post, 'https://example.com/oauth') | ||
.to_return(body: { access_token: 'test_token' }.to_json) | ||
end | ||
|
||
describe '.call' do | ||
subject { described_class.call(maat_reference:) } | ||
|
||
let(:maat_reference) { '9876543' } | ||
let(:connection) { MaatService::Connection.instance } | ||
|
||
context 'with a valid MAAT reference' do | ||
before do | ||
stub_request(:get, "https://example.com/assessment/rep-orders/#{maat_reference}") | ||
.to_return(body: { caseId: 'TEST12345678', crownRepOrderDate: '2024-03-28' }.to_json) | ||
end | ||
|
||
it { is_expected.to eq({ case_number: 'TEST12345678', representation_order_date: '2024-03-28' }) } | ||
end | ||
|
||
context 'with a valid MAAT reference with no date' do | ||
before do | ||
stub_request(:get, "https://example.com/assessment/rep-orders/#{maat_reference}") | ||
.to_return(body: { caseId: 'TEST12345678' }.to_json) | ||
end | ||
|
||
it { is_expected.to eq({ case_number: 'TEST12345678', representation_order_date: nil }) } | ||
end | ||
|
||
context 'with an unknown MAAT reference' do | ||
before do | ||
stub_request(:get, "https://example.com/assessment/rep-orders/#{maat_reference}") | ||
.to_return(status: 404, body: { message: "No Rep Order found for ID: #{maat_reference}" }.to_json) | ||
end | ||
|
||
it { is_expected.to eq({ case_number: nil, representation_order_date: nil }) } | ||
end | ||
|
||
context 'when unauthorized' do | ||
before do | ||
stub_request(:get, "https://example.com/assessment/rep-orders/#{maat_reference}") | ||
.to_return(status: 401, body: { message: 'Unauthorized' }.to_json) | ||
end | ||
|
||
it { is_expected.to eq({ case_number: nil, representation_order_date: nil }) } | ||
end | ||
|
||
context 'when the OAuth settings are not correct' do | ||
before do | ||
stub_request(:post, 'https://example.com/oauth') | ||
.to_return(status: 400, body: { error: 'invalid_client' }.to_json) | ||
stub_request(:get, "https://example.com/assessment/rep-orders/#{maat_reference}") | ||
.to_return(status: 401, body: { message: 'Unauthorized' }.to_json) | ||
end | ||
|
||
it { is_expected.to eq({ case_number: nil, representation_order_date: nil }) } | ||
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.
Will this still be in beta once released?
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.
This will not actually be displayed yet as this PR is just to get the connection code in the codebase, so that it can be used and built on. I think, however, that when we get to the point of displaying information it should be indicated that it is beta or experimental for a period of time.