Skip to content

Commit

Permalink
[ARP-86023] ARP IPF: Use ActiveSupport::Testing::TimeHelpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nihil2501 committed Sep 7, 2024
1 parent 09d03d5 commit 7056d3c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 116 deletions.
16 changes: 11 additions & 5 deletions modules/accredited_representative_portal/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ def parsed_response
JSON.parse(response.body)
end
end
end

module AccreditedRepresentativePortal
module AuthenticationHelper
def login_as(representative_user, options = {})
access_token = options[:access_token] || create(:access_token, user_uuid: representative_user.uuid,
audience: ['arp'])
options[:access_token] ||=
create(
:access_token,
user_uuid: representative_user.uuid,
audience: ['arp']
)

cookies[SignIn::Constants::Auth::ACCESS_TOKEN_COOKIE_NAME] =
SignIn::AccessTokenJwtEncoder.new(access_token:).perform
SignIn::AccessTokenJwtEncoder
.new(**options.slice(:access_token))
.perform
end
end
end
Expand All @@ -28,4 +33,5 @@ def login_as(representative_user, options = {})
config.include AccreditedRepresentativePortal::AuthenticationHelper, type: :request
config.include AccreditedRepresentativePortal::AuthenticationHelper, type: :controller
config.include AccreditedRepresentativePortal::RequestHelper, type: :request
config.include ActiveSupport::Testing::TimeHelpers
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

RSpec.describe AccreditedRepresentativePortal::V0::InProgressFormsController, type: :request do
let(:representative_user) { create(:representative_user) }
let(:initial_request_date) { Time.utc(2022, 3, 4, 5, 6, 7) }
let(:form_id) { '21a' }
let(:headers) { { 'Content-Type' => 'application/json' } }

Expand All @@ -16,139 +15,139 @@
describe 'requests' do
context 'can make requests to InProgressForms controller' do
it 'can make requests to InProgressForms controller' do
form = build(
# Test for GET and DELETE of existing InProgressForm
travel_to Time.utc(2022, 3, 4, 5, 6, 7)

form = create(
:in_progress_form,
user_uuid: representative_user.uuid,
form_data: { field: 'value' },
form_id:
)

# Test for GET and DELETE of existing InProgressForm
Timecop.freeze(initial_request_date) do
form.save!

get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(parsed_response).to eq(
{
'formData' => {
'field' => 'value'
get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(parsed_response).to eq(
{
'formData' => {
'field' => 'value'
},
'metadata' => {
'version' => 1,
'return_url' => 'foo.com',
'submission' => {
'status' => false,
'error_message' => false,
'id' => false,
'timestamp' => false,
'has_attempted_submit' => false
},
'metadata' => {
'version' => 1,
'return_url' => 'foo.com',
'submission' => {
'status' => false,
'error_message' => false,
'id' => false,
'timestamp' => false,
'has_attempted_submit' => false
},
'createdAt' => 1_646_370_367,
'expiresAt' => 1_651_554_367,
'lastUpdated' => 1_646_370_367,
'inProgressFormId' => InProgressForm.last.id
}
'createdAt' => 1_646_370_367,
'expiresAt' => 1_651_554_367,
'lastUpdated' => 1_646_370_367,
'inProgressFormId' => InProgressForm.last.id
}
)
}
)

delete("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(response).to have_http_status(:no_content)
end
delete("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(response).to have_http_status(:no_content)

# Test for PUT when InProgressForm does not exist
Timecop.freeze(initial_request_date + 1.day) do
put(
"/accredited_representative_portal/v0/in_progress_forms/#{form_id}",
params: { 'form_data' => { another_field: 'foo' } }.to_json,
headers:
)

expect(parsed_response).to eq(
{
'data' => {
'id' => '',
'type' => 'in_progress_forms',
'attributes' => {
'formId' => '21a',
'createdAt' => '2022-03-05T05:06:07.000Z',
'updatedAt' => '2022-03-05T05:06:07.000Z',
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_640_767,
'lastUpdated' => 1_646_456_767,
'inProgressFormId' => InProgressForm.last.id
}
travel 1.day

put(
"/accredited_representative_portal/v0/in_progress_forms/#{form_id}",
params: { 'form_data' => { another_field: 'foo' } }.to_json,
headers:
)

expect(parsed_response).to eq(
{
'data' => {
'id' => '',
'type' => 'in_progress_forms',
'attributes' => {
'formId' => '21a',
'createdAt' => '2022-03-05T05:06:07.000Z',
'updatedAt' => '2022-03-05T05:06:07.000Z',
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_640_767,
'lastUpdated' => 1_646_456_767,
'inProgressFormId' => InProgressForm.last.id
}
}
}
)
}
)

get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(parsed_response).to eq(
{
'formData' => {
'another_field' => 'foo'
},
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_640_767,
'lastUpdated' => 1_646_456_767,
'inProgressFormId' => InProgressForm.last.id
}
get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(parsed_response).to eq(
{
'formData' => {
'another_field' => 'foo'
},
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_640_767,
'lastUpdated' => 1_646_456_767,
'inProgressFormId' => InProgressForm.last.id
}
)
end
}
)

# Test for PUT and DELETE when InProgressForm does exist
Timecop.freeze(initial_request_date + 2.days) do
put(
"/accredited_representative_portal/v0/in_progress_forms/#{form_id}",
params: { 'form_data' => { another_field: 'foo', sample_field: 'sample' } }.to_json,
headers:
)

expect(parsed_response).to eq(
{
'data' => {
'id' => '',
'type' => 'in_progress_forms',
'attributes' => {
'formId' => '21a',
'createdAt' => '2022-03-05T05:06:07.000Z',
'updatedAt' => '2022-03-06T05:06:07.000Z',
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_727_167,
'lastUpdated' => 1_646_543_167,
'inProgressFormId' => InProgressForm.last.id
}
travel 1.day

put(
"/accredited_representative_portal/v0/in_progress_forms/#{form_id}",
params: { 'form_data' => { another_field: 'foo', sample_field: 'sample' } }.to_json,
headers:
)

expect(parsed_response).to eq(
{
'data' => {
'id' => '',
'type' => 'in_progress_forms',
'attributes' => {
'formId' => '21a',
'createdAt' => '2022-03-05T05:06:07.000Z',
'updatedAt' => '2022-03-06T05:06:07.000Z',
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_727_167,
'lastUpdated' => 1_646_543_167,
'inProgressFormId' => InProgressForm.last.id
}
}
}
)

get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(parsed_response).to eq(
{
'formData' => {
'another_field' => 'foo',
'sample_field' => 'sample'
},
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_727_167,
'lastUpdated' => 1_646_543_167,
'inProgressFormId' => InProgressForm.last.id
}
}
)

get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(parsed_response).to eq(
{
'formData' => {
'another_field' => 'foo',
'sample_field' => 'sample'
},
'metadata' => {
'createdAt' => 1_646_456_767,
'expiresAt' => 1_651_727_167,
'lastUpdated' => 1_646_543_167,
'inProgressFormId' => InProgressForm.last.id
}
)
}
)

delete("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(response).to have_http_status(:no_content)

delete("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(response).to have_http_status(:no_content)
get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(response.body).to eq({}.to_json)

get("/accredited_representative_portal/v0/in_progress_forms/#{form_id}")
expect(response.body).to eq({}.to_json)
end
travel_back
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
)
end

it 'responds with the user and their in progress form', run_at: '2024-09-06T16:19:34-04:00' do
around do |example|
travel_to '2024-09-06T16:19:34-04:00'
example.run
travel_back
end

it 'responds with the user and their in progress form' do
get '/accredited_representative_portal/v0/user'

expect(response).to have_http_status(:ok)
Expand Down

0 comments on commit 7056d3c

Please sign in to comment.