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

def active_profile
@active_profile ||= profiles.verified.find(&:active?)
if defined?(@active_profile)
if !@active_profile&.active
Comment thread
matthinz marked this conversation as resolved.
Outdated
remove_instance_variable(:@active_profile)
else
return @active_profile
end
end

@active_profile = profiles.verified.find(&:active?)
end

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

def pending_profile
return @pending_profile if defined?(@pending_profile)
if defined?(@pending_profile)
if @pending_profile&.active
remove_instance_variable(:@pending_profile)
else
return @pending_profile
end
end

@pending_profile = begin
pending = profiles.in_person_verification_pending.or(
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,
Comment thread
matthinz marked this conversation as resolved.
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
21 changes: 21 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@

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
end
end

Expand Down Expand Up @@ -629,6 +640,16 @@

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
Expand Down