Skip to content
10 changes: 8 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def active_identities
end

def active_profile
@active_profile ||= profiles.verified.find(&:active?)
return @active_profile if defined?(@active_profile) && @active_profile&.active
@active_profile = profiles.verified.find(&:active?)
end

def pending_profile?
Expand Down Expand Up @@ -149,7 +150,7 @@ def reinstate!
end

def pending_profile
return @pending_profile if defined?(@pending_profile)
return @pending_profile if defined?(@pending_profile) && !@pending_profile&.active

@pending_profile = begin
pending = profiles.in_person_verification_pending.or(
Expand Down Expand Up @@ -479,6 +480,11 @@ def send_email_to_all_addresses(user_mailer_template)
end
end

def reload(...)
remove_instance_variable(:@pending_profile) if defined?(@pending_profile)
super(...)
end

private

def lockout_period
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/idv/by_mail/enter_code_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
end

let(:user) { create(:user, :with_pending_gpo_profile, created_at: 2.days.ago) }
let(:pending_profile) { user.gpo_verification_pending_profile }
let!(:pending_profile) { user.gpo_verification_pending_profile }
let(:success) { true }

it 'uses the PII from the pending profile' do
Expand All @@ -189,7 +189,7 @@
errors: {},
pending_in_person_enrollment: false,
fraud_check_failed: false,
enqueued_at: user.pending_profile.gpo_confirmation_codes.last.code_sent_at,
enqueued_at: pending_profile.gpo_confirmation_codes.last.code_sent_at,
which_letter: 1,
letter_count: 1,
attempts: 1,
Expand Down Expand Up @@ -234,7 +234,7 @@
errors: {},
pending_in_person_enrollment: true,
fraud_check_failed: false,
enqueued_at: user.pending_profile.gpo_confirmation_codes.last.code_sent_at,
enqueued_at: pending_profile.gpo_confirmation_codes.last.code_sent_at,
which_letter: 1,
letter_count: 1,
attempts: 1,
Expand Down Expand Up @@ -265,7 +265,7 @@
errors: {},
pending_in_person_enrollment: false,
fraud_check_failed: true,
enqueued_at: user.pending_profile.gpo_confirmation_codes.last.code_sent_at,
enqueued_at: pending_profile.gpo_confirmation_codes.last.code_sent_at,
which_letter: 1,
letter_count: 1,
attempts: 1,
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/tasks/dev_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
expect(User.count).to eq 10
verified_user.reload
expect(verified_user.updated_at).to eq(verified_user_updated_at)
expect(verified_user.active_profile).to be(verified_user_profile)
expect(verified_user.active_profile).to eq(verified_user_profile)

unverified_user = User.last
expect(unverified_user.active_profile).to be_nil
Expand All @@ -99,7 +99,7 @@

verified_user.reload
expect(verified_user.updated_at).to eq(verified_user_updated_at)
expect(verified_user.active_profile).to be(verified_user_profile)
expect(verified_user.active_profile).to eq(verified_user_profile)
end
end

Expand Down
57 changes: 55 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,36 @@

expect(user.active_profile).to eq profile1
end

context 'when the active profile is deactivated' do
it 'is no longer returned' do
user = create(:user, :fully_registered)
create(:profile, :active, :verified, user: user, pii: { first_name: 'Jane' })

expect(user.active_profile).not_to be_nil
user.active_profile.deactivate(:password_reset)
expect(user.active_profile).to be_nil
end
end

context 'when there is no active profile' do
it 'does not cache the nil value' do
user = create(:user, :fully_registered)
profile = create(
:profile,
gpo_verification_pending_at: 2.days.ago,
created_at: 2.days.ago,
user: user,
)

expect(user.active_profile).to be_nil

profile.remove_gpo_deactivation_reason
profile.activate

expect(user.active_profile).to eql(profile)
end
end
end
end

Expand Down Expand Up @@ -607,8 +637,9 @@
end

describe '#pending_profile' do
let(:user) { User.new }

context 'when a pending profile exists' do
let(:user) { User.new }
let!(:pending) do
create(
:profile,
Expand All @@ -629,11 +660,20 @@

expect(user.pending_profile).to eq pending
end

it 'returns nil after the pending profile is activated' do
pending_profile = user.pending_profile
expect(pending_profile).not_to be_nil

pending_profile.remove_gpo_deactivation_reason
pending_profile.activate

expect(user.pending_profile).to be_nil
end
end

context 'when pending profile does not exist' do
it 'returns nil' do
user = User.new
create(
:profile,
deactivation_reason: :encryption_error,
Expand All @@ -642,6 +682,19 @@

expect(user.pending_profile).to be_nil
end

it 'caches nil until reload' do
expect(user.pending_profile).to be_nil
pending_profile = create(
:profile,
gpo_verification_pending_at: 2.days.ago,
created_at: 2.days.ago,
user: user,
)
expect(user.pending_profile).to be_nil
user.reload
expect(user.pending_profile).to eql(pending_profile)
end
end

context 'verification was cancelled for a pending profile' do
Expand Down