Skip to content

Commit

Permalink
Add serializer for PowerOfAttorneyRequest with controller and tests
Browse files Browse the repository at this point in the history
- Implement `PowerOfAttorneyRequestSerializer` to standardize JSON:API output
- Add request specs for controller endpoints (`index` and `show`)
- Add serializer specs to ensure proper formatting of resolution details
- Normalize response keys for consistency in test expectations
  • Loading branch information
ojbucao committed Dec 19, 2024
1 parent e5c36db commit e7856e0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,21 @@
module AccreditedRepresentativePortal
module V0
class PowerOfAttorneyRequestsController < ApplicationController
POA_REQUEST_ITEM_MOCK_DATA = {
status: 'Pending',
declinedReason: nil,
powerOfAttorneyCode: '091',
submittedAt: '2024-04-30T11:03:17Z',
acceptedOrDeclinedAt: nil,
isAddressChangingAuthorized: false,
isTreatmentDisclosureAuthorized: true,
veteran: {
firstName: 'Jon',
middleName: nil,
lastName: 'Smith',
participantId: '6666666666666'
},
representative: {
email: '[email protected]',
firstName: 'Jane',
lastName: 'Doe'
},
claimant: {
firstName: 'Sam',
lastName: 'Smith',
participantId: '777777777777777',
relationshipToVeteran: 'Child'
},
claimantAddress: {
city: 'Hartford',
state: 'CT',
zip: '06107',
country: 'GU',
militaryPostOffice: nil,
militaryPostalCode: nil
}
}.freeze

POA_REQUEST_LIST_MOCK_DATA = [
POA_REQUEST_ITEM_MOCK_DATA,
POA_REQUEST_ITEM_MOCK_DATA,
POA_REQUEST_ITEM_MOCK_DATA
].freeze

def index
render json: POA_REQUEST_LIST_MOCK_DATA
requests = PowerOfAttorneyRequest.includes(:resolution)
render json: PowerOfAttorneyRequestSerializer.new(requests).serializable_hash, status: :ok
end

def show
render json: POA_REQUEST_ITEM_MOCK_DATA
request = PowerOfAttorneyRequest.includes(:resolution).find(params[:id])
render json: PowerOfAttorneyRequestSerializer.new(request).serializable_hash, status: :ok
rescue ActiveRecord::RecordNotFound
render json: { error: 'Record not found' }, status: :not_found
end

private

def authenticate; end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ module AccreditedRepresentativePortal
class PowerOfAttorneyRequestSerializer
include JSONAPI::Serializer

attributes :id, :claimant_id, :created_at
attributes :id, :claimant_id

attribute :created_at do |object|
object.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
end

attribute :resolution do |object|
next nil unless object.resolution

{
id: object.resolution.id,
type: object.resolution.resolving_type&.demodulize&.underscore,
created_at: object.resolution.created_at.iso8601,
created_at: object.resolution.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
reason: object.resolution&.reason,
creator_id: object.resolution.resolving.try(:creator_id)
}.compact
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,58 @@

RSpec.describe AccreditedRepresentativePortal::V0::PowerOfAttorneyRequestsController, type: :request do
let(:test_user) { create(:representative_user) }
let(:poa_request_details_id) { '123' }
let(:poa_request_details_mock_data) do
{
'status' => 'Pending',
'declinedReason' => nil,
'powerOfAttorneyCode' => '091',
'submittedAt' => '2024-04-30T11:03:17Z',
'acceptedOrDeclinedAt' => nil,
'isAddressChangingAuthorized' => false,
'isTreatmentDisclosureAuthorized' => true,
'veteran' => { 'firstName' => 'Jon', 'middleName' => nil, 'lastName' => 'Smith',
'participantId' => '6666666666666' },
'representative' => { 'email' => '[email protected]', 'firstName' => 'Jane', 'lastName' => 'Doe' },
'claimant' => { 'firstName' => 'Sam', 'lastName' => 'Smith', 'participantId' => '777777777777777',
'relationshipToVeteran' => 'Child' },
'claimantAddress' => { 'city' => 'Hartford', 'state' => 'CT', 'zip' => '06107', 'country' => 'GU',
'militaryPostOffice' => nil, 'militaryPostalCode' => nil }
}
end
let(:poa_request_list_mock_data) do
[poa_request_details_mock_data, poa_request_details_mock_data, poa_request_details_mock_data]
end
let(:poa_request) { create(:power_of_attorney_request) }
let(:poa_requests) { create_list(:power_of_attorney_request, 3) }

before do
Flipper.enable(:accredited_representative_portal_pilot)
login_as(test_user)
end

describe 'GET /accredited_representative_portal/v0/power_of_attorney_requests' do
it 'returns the list of a power of attorney request' do
it 'returns the list of power of attorney requests' do
poa_requests

get('/accredited_representative_portal/v0/power_of_attorney_requests')

expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to eq(poa_request_list_mock_data)
parsed_response = JSON.parse(response.body)

expected_response = AccreditedRepresentativePortal::PowerOfAttorneyRequestSerializer
.new(poa_requests)
.serializable_hash

expect(deep_stringify(parsed_response)).to eq(deep_stringify(expected_response))
end
end

describe 'GET /accredited_representative_portal/v0/power_of_attorney_requests/:id' do
it 'returns the details of a power of attorney request' do
get("/accredited_representative_portal/v0/power_of_attorney_requests/#{poa_request_details_id}")
it 'returns the details of a specific power of attorney request' do
get("/accredited_representative_portal/v0/power_of_attorney_requests/#{poa_request.id}")

expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to include(poa_request_details_mock_data)
parsed_response = JSON.parse(response.body)

expected_response = AccreditedRepresentativePortal::PowerOfAttorneyRequestSerializer
.new(poa_request)
.serializable_hash

expect(deep_stringify(parsed_response)).to eq(deep_stringify(expected_response))
end
end

def deep_stringify(value)
case value
when Hash
value.each_with_object({}) do |(k, v), result|
result[k.to_s] = deep_stringify(v) # Stringify keys and recursively process values
end
when Array
value.map { |v| deep_stringify(v) } # Recursively process arrays
when Symbol
value.to_s # Convert symbols to strings
else
value # Leave other primitives (e.g., strings, numbers, nil) as is
end
end
end
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# frozen_string_literal: true

# spec/serializers/power_of_attorney_request_serializer_spec.rb
require 'rails_helper'

RSpec.describe AccreditedRepresentativePortal::PowerOfAttorneyRequestSerializer do
let(:request) { create(:power_of_attorney_request, created_at: '2024-12-17T00:30:55Z') }

subject { described_class.new(request).serializable_hash[:data][:attributes] }

let(:request) { create(:power_of_attorney_request, created_at: '2024-12-17T00:30:55Z') }

context 'when resolution is nil' do
it 'returns the request without resolution' do
expect(subject).to eq({
id: request.id,
claimant_id: request.claimant_id,
created_at: '2024-12-17T00:30:55Z',
resolution: nil
})
id: request.id,
claimant_id: request.claimant_id,
created_at: '2024-12-17T00:30:55.000Z',
resolution: nil
})
end
end

Expand All @@ -26,11 +28,11 @@

it 'includes resolution details with type expiration and reason' do
expect(subject[:resolution]).to eq({
id: resolution.id,
type: 'power_of_attorney_request_expiration',
created_at: resolution.created_at.iso8601,
reason: 'Test reason for resolution'
})
id: resolution.id,
type: 'power_of_attorney_request_expiration',
created_at: resolution.created_at.iso8601(3),
reason: 'Test reason for resolution'
})
end
end

Expand All @@ -43,12 +45,12 @@

it 'includes resolution details with type decision and creator_id' do
expect(subject[:resolution]).to eq({
id: resolution.id,
type: 'power_of_attorney_request_decision',
created_at: resolution.created_at.iso8601,
reason: 'Test reason for resolution',
creator_id: resolution.resolving.creator_id
})
id: resolution.id,
type: 'power_of_attorney_request_decision',
created_at: resolution.created_at.iso8601(3),
reason: 'Test reason for resolution',
creator_id: resolution.resolving.creator_id
})
end
end

Expand All @@ -62,12 +64,12 @@

it 'includes resolution details with type decision, reason, and creator_id' do
expect(subject[:resolution]).to eq({
id: resolution.id,
type: 'power_of_attorney_request_decision',
created_at: resolution.created_at.iso8601,
reason: "Didn't authorize treatment record disclosure",
creator_id: resolution.resolving.creator_id
})
id: resolution.id,
type: 'power_of_attorney_request_decision',
created_at: resolution.created_at.iso8601(3),
reason: "Didn't authorize treatment record disclosure",
creator_id: resolution.resolving.creator_id
})
end
end
end

0 comments on commit e7856e0

Please sign in to comment.