-
Notifications
You must be signed in to change notification settings - Fork 7
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 #6611 from ministryofjustice/CTSKF-690CCCD---Set-u…
…p-access-to-MAAT-API Ctskf 690 cccd set up access to maat api
- Loading branch information
Showing
14 changed files
with
217 additions
and
2 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
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 |