Skip to content

Commit

Permalink
Merge pull request #582 from communitiesuk/null-fields
Browse files Browse the repository at this point in the history
Fix Sentry NoMethodError issue and add application name to DB
  • Loading branch information
aamircodes authored Mar 19, 2024
2 parents 05bbded + d3f0b9b commit fcefe2e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 13 deletions.
9 changes: 8 additions & 1 deletion app/controllers/token_based_resume_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ class TokenBasedResumeController < ApplicationController
add_flash_types :error

def session_expired
send_email
@application = UnaccompaniedMinor.find_by_reference(session[:app_reference])

if @application && @application.email.present?
send_email
else
Rails.logger.info "User hasn't created any account yet and the session has timed out due to inactivity."
end

reset_session
render "token-based-resume/session_expired"
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/unaccompanied_minor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ def after_initialize
end

def prepare_transfer
@transferred_at = Time.zone.now
self.fullname = "#{given_name} #{family_name}"
self.transferred_at = Time.zone.now
save!(validate: false)

UnaccompaniedMinorTransferAdapter.to_json(as_json)
Expand Down
36 changes: 36 additions & 0 deletions spec/controllers/token_based_resume_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@
end
end

describe "User session times out before creating an account" do
given_name = "".freeze
email = nil

uam = UnaccompaniedMinor.new
uam.save!

uuid = "test-uuid".freeze
magic_link = "http://test.host/sponsor-a-child/resume-application?uuid=#{uuid}".freeze

let(:unaccompanied_minor) { instance_double("UnaccompaniedMinor") }
let(:message_delivery) { instance_double("ActionMailer::MessageDelivery") }

before do
allow(SecureRandom).to receive(:uuid).and_return(uuid)
allow(GovNotifyMailer).to receive(:send_save_and_return_email).and_return(message_delivery)
allow(message_delivery).to receive(:deliver_later)
allow(UnaccompaniedMinor).to receive(:find_by_reference).and_return(uam)
end

it "dosn't call the emailer" do
get :session_expired

expect(GovNotifyMailer).not_to have_received(:send_save_and_return_email).with(given_name, magic_link, email)
expect(response).to render_template("token-based-resume/session_expired")
end

it "Rails logger receives a message" do
allow(Rails.logger).to receive(:info)

get :session_expired

expect(Rails.logger).to have_received(:info).with("User hasn't created any account yet and the session has timed out due to inactivity.")
end
end

describe "User tries to resume their application after email sent" do
given_name = "First".freeze
email = "test@example.com".freeze
Expand Down
40 changes: 40 additions & 0 deletions spec/models/unaccompanied_minor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,44 @@ def populate_incomplete_other_adults_section(uam)
expect(app.as_json[:reference]).to start_with("SPON-")
end
end

describe "prepare_transfer function" do
app = described_class.new(given_name: "John", "family_name": "Doe")

context "when prepare_transfer hasn't been called" do
it "has transferred_at equal to nil" do
expect(app.transferred_at).to be_nil
end

it "has fullname equal to be nil" do
expect(app.fullname).to be_nil
end

it "is not submitted" do
expect(app.is_submitted?).to eq(false)
end
end

context "when calling prepare_transfer" do
before { app.prepare_transfer }

it "sets transferred_at to current time" do
expect(app.transferred_at).not_to be_nil
expect(app.transferred_at).to be_within(1.second).of(Time.zone.now)
end

it "sets fullname" do
expect(app.fullname).to eq("John Doe")
end

it "is submittted" do
expect(app.is_submitted?).to eq(true)
end

it "returns JSON of the application" do
expect(JSON.parse(app.prepare_transfer)).to include("given_name" => "John")
expect(JSON.parse(app.prepare_transfer)).to include("family_name" => "Doe")
end
end
end
end
38 changes: 27 additions & 11 deletions spec/system/token_based_resume_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
require "rails_helper"

RSpec.describe TokenBasedResumeController, type: :system do
let(:texter) { instance_double("Notifications::Client") }
let(:application_token) { instance_double("ApplicationToken") }
let(:task_list_content) { "Apply for approval to provide a safe home for a child from Ukraine" }
let(:sms_code) { 123_456 }
let(:already_expired) { Time.zone.now.utc - 1.hour }
let(:created_at) { Time.zone.now.utc }
let(:magic_id) { "e5c4fe58-a8ca-4e6f-aaa6-7e0a381eb3dc" }
let(:expiry_time) { Time.zone.now.utc + 1.hour }
let(:uam) { UnaccompaniedMinor.new }
let(:email) { "test@example.com" }
let(:email_scrambled) { "t***@example.com" }
let(:spencer_scrambled) { "s**************@example.com" }
let(:email_scrambled) { "t***@example.com" }
let(:email) { "test@example.com" }
let(:uam) { UnaccompaniedMinor.new }
let(:expiry_time) { Time.zone.now.utc + 1.hour }
let(:magic_id) { "e5c4fe58-a8ca-4e6f-aaa6-7e0a381eb3dc" }
let(:created_at) { Time.zone.now.utc }
let(:already_expired) { Time.zone.now.utc - 1.hour }
let(:sms_code) { 123_456 }
let(:task_list_content) { "Apply for approval to provide a safe home for a child from Ukraine" }
let(:application_token) { instance_double("ApplicationToken") }
let(:texter) { instance_double("Notifications::Client") }

before do
driven_by(:rack_test_user_agent)
allow(Notifications::Client).to receive(:new).and_return(texter)
allow(texter).to receive(:send_sms)
end

describe "User hasn't created an account and has been timed out" do
let(:email) { nil }

it "shows time out page if user refresh time out page" do
new_application = UnaccompaniedMinor.new
new_application.save!

page.set_rack_session(app_reference: new_application.reference)

visit "/sponsor-a-child/session-expired"
visit current_path

expect(page).to have_content("Your session has timed out due to inactivity")
end
end

describe "User has been timed out" do
it "shows time out page and copy" do
new_application = UnaccompaniedMinor.new
Expand Down

0 comments on commit fcefe2e

Please sign in to comment.