diff --git a/app/controllers/sign_up/completions_controller.rb b/app/controllers/sign_up/completions_controller.rb index 6dd5af26a6f..33af693179a 100644 --- a/app/controllers/sign_up/completions_controller.rb +++ b/app/controllers/sign_up/completions_controller.rb @@ -81,7 +81,7 @@ def sign_user_out_and_instruct_to_go_back_to_mobile_app end def analytics_attributes(page_occurence) - { + attributes = { ial2: sp_session[:ial2], ialmax: sp_session[:ialmax], service_provider_name: decorated_sp_session.sp_name, @@ -91,6 +91,19 @@ def analytics_attributes(page_occurence) in_account_creation_flow: user_session[:in_account_creation_flow] || false, needs_completion_screen_reason: needs_completion_screen_reason, } + + if page_occurence.present? && DisposableDomain.disposable?(email_domain) + attributes[:disposable_email_domain] = email_domain + end + + attributes + end + + def email_domain + @email_domain ||= begin + email_address = current_user.email_addresses.take.email + Mail::Address.new(email_address).domain + end end def track_completion_event(last_page) diff --git a/app/models/disposable_domain.rb b/app/models/disposable_domain.rb index f32d48d914d..082199f0970 100644 --- a/app/models/disposable_domain.rb +++ b/app/models/disposable_domain.rb @@ -1,2 +1,9 @@ class DisposableDomain < ApplicationRecord + class << self + def disposable?(domain) + return false if !domain.is_a?(String) || domain.empty? + + exists?(name: domain) + end + end end diff --git a/spec/controllers/sign_up/completions_controller_spec.rb b/spec/controllers/sign_up/completions_controller_spec.rb index 60095800142..45c93fb7335 100644 --- a/spec/controllers/sign_up/completions_controller_spec.rb +++ b/spec/controllers/sign_up/completions_controller_spec.rb @@ -1,6 +1,8 @@ require 'rails_helper' RSpec.describe SignUp::CompletionsController do + let(:temporary_email) { 'name@temporary.com' } + describe '#show' do let(:current_sp) { create(:service_provider) } @@ -22,8 +24,10 @@ end context 'IAL1' do - let(:user) { create(:user, :fully_registered) } + let(:user) { create(:user, :fully_registered, email: temporary_email) } + before do + DisposableDomain.create(name: 'temporary.com') stub_sign_in(user) subject.session[:sp] = { issuer: current_sp.issuer, @@ -255,11 +259,47 @@ patch :update expect(response).to redirect_to account_path end + + context 'with a disposable email address' do + let(:user) { create(:user, :fully_registered, email: temporary_email) } + + it 'logs disposable domain' do + DisposableDomain.create(name: 'temporary.com') + stub_sign_in(user) + subject.session[:sp] = { + ial2: false, + issuer: 'foo', + request_url: 'http://example.com', + } + subject.user_session[:in_account_creation_flow] = true + + patch :update + + expect(@analytics).to have_received(:track_event).with( + 'User registration: complete', + ial2: false, + ialmax: nil, + service_provider_name: subject.decorated_sp_session.sp_name, + page_occurence: 'agency-page', + needs_completion_screen_reason: :new_sp, + sp_request_requested_attributes: nil, + sp_session_requested_attributes: nil, + in_account_creation_flow: true, + disposable_email_domain: 'temporary.com', + ) + end + end end context 'IAL2' do it 'tracks analytics' do - user = create(:user, :fully_registered, profiles: [create(:profile, :verified, :active)]) + DisposableDomain.create(name: 'temporary.com') + user = create( + :user, + :fully_registered, + profiles: [create(:profile, :verified, :active)], + email: temporary_email, + ) stub_sign_in(user) sp = create(:service_provider, issuer: 'https://awesome') subject.session[:sp] = { @@ -282,6 +322,7 @@ sp_request_requested_attributes: nil, sp_session_requested_attributes: ['email'], in_account_creation_flow: true, + disposable_email_domain: 'temporary.com', ) end diff --git a/spec/models/disposable_domain_spec.rb b/spec/models/disposable_domain_spec.rb new file mode 100644 index 00000000000..3572d1b0859 --- /dev/null +++ b/spec/models/disposable_domain_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.describe DisposableDomain do + let(:domain) { 'temporary.com' } + + describe '.disposable?' do + before do + DisposableDomain.create(name: domain) + end + + context 'when the domain exists' do + it 'returns true' do + expect(DisposableDomain.disposable?(domain)).to eq true + end + end + + context 'when the domain does not exist' do + it 'returns false' do + expect(DisposableDomain.disposable?('example.com')).to eq false + end + end + + context 'with bad data' do + it 'returns false' do + expect(DisposableDomain.disposable?('')).to eq false + expect(DisposableDomain.disposable?(nil)).to eq false + expect(DisposableDomain.disposable?({})).to eq false + end + end + end +end