diff --git a/app/handlers/create_external_user_credentials.rb b/app/handlers/create_external_user_credentials.rb index 1394662e6..4e12e4b71 100644 --- a/app/handlers/create_external_user_credentials.rb +++ b/app/handlers/create_external_user_credentials.rb @@ -53,7 +53,7 @@ def handle password_confirmation: signup_params.password ) - run Newflow::CreateEmailForUser, email: signup_params.email, user: outputs.user + run Newflow::CreateEmailForUser, email: signup_params.email, user: outputs.user, show_pin: false agree_to_terms if signup_params.terms_accepted end diff --git a/app/mailers/newflow_mailer.rb b/app/mailers/newflow_mailer.rb index 75e327e8e..47afe9c9b 100644 --- a/app/mailers/newflow_mailer.rb +++ b/app/mailers/newflow_mailer.rb @@ -15,8 +15,9 @@ def reset_password_email(user:, email_address:) subject: "Reset your OpenStax password" end - def signup_email_confirmation(email_address:) - @should_show_pin = ConfirmByPin.sequential_failure_for(email_address).attempts_remaining? + def signup_email_confirmation(email_address:, show_pin: true) + @should_show_pin = show_pin != false && + ConfirmByPin.sequential_failure_for(email_address).attempts_remaining? @email_value = email_address.value @confirmation_pin = email_address.confirmation_pin @confirmation_code = email_address.confirmation_code diff --git a/app/routines/newflow/create_email_for_user.rb b/app/routines/newflow/create_email_for_user.rb index 05014c85f..e6ef5d37d 100644 --- a/app/routines/newflow/create_email_for_user.rb +++ b/app/routines/newflow/create_email_for_user.rb @@ -5,7 +5,7 @@ class CreateEmailForUser protected ############### - def exec(email:, user:, is_school_issued: nil) + def exec(email:, user:, is_school_issued: nil, show_pin: true) @email = EmailAddress.find_or_create_by(value: email&.downcase, user_id: user.id) @email.is_school_issued = is_school_issued @@ -17,7 +17,9 @@ def exec(email:, user:, is_school_issued: nil) event_type: :email_added_to_user, event_data: { email: @email } ) - NewflowMailer.signup_email_confirmation(email_address: @email).deliver_later + NewflowMailer.signup_email_confirmation( + email_address: @email, show_pin: show_pin + ).deliver_later end @email.save diff --git a/app/views/newflow_mailer/signup_email_confirmation.html.erb b/app/views/newflow_mailer/signup_email_confirmation.html.erb index 3afbd7612..2ccd542ac 100644 --- a/app/views/newflow_mailer/signup_email_confirmation.html.erb +++ b/app/views/newflow_mailer/signup_email_confirmation.html.erb @@ -89,7 +89,7 @@

- Verify your email by clicking the button below or use your pin: <%= @confirmation_pin %> + Verify your email by clicking the button below<% if @should_show_pin %> or use your pin: <%= @confirmation_pin %><% end %>

diff --git a/spec/handlers/create_external_user_credentials_spec.rb b/spec/handlers/create_external_user_credentials_spec.rb index 43f82faac..9eb9da7fd 100644 --- a/spec/handlers/create_external_user_credentials_spec.rb +++ b/spec/handlers/create_external_user_credentials_spec.rb @@ -49,7 +49,7 @@ it 'sends a confirmation email' do expect_any_instance_of(NewflowMailer).to( receive(:signup_email_confirmation).with( - hash_including({ email_address: an_instance_of(EmailAddress) }) + hash_including({ email_address: an_instance_of(EmailAddress), show_pin: false }) ) ) handler_call diff --git a/spec/mailers/newflow_mailer_spec.rb b/spec/mailers/newflow_mailer_spec.rb index dabac3510..aabb49abb 100644 --- a/spec/mailers/newflow_mailer_spec.rb +++ b/spec/mailers/newflow_mailer_spec.rb @@ -2,9 +2,17 @@ module Newflow describe NewflowMailer, type: :mailer do + let(:pin) { '123456' } + let(:code) { '1234' } + let(:confirm_url) { "http://localhost:2999/i/verify_email_by_code/#{code}" } let(:user) { FactoryBot.create :user, first_name: 'John', last_name: 'Doe', suffix: 'Jr.' } - let(:email) { FactoryBot.create :email_address, value: 'to@example.org', - user_id: user.id, confirmation_code: '1234', confirmation_pin: '123456' } + let(:email) { + FactoryBot.create :email_address, + value: 'to@example.org', + user_id: user.id, + confirmation_code: code, + confirmation_pin: pin + } describe 'sends email confirmation' do it 'has basic header and from info and greeting' do @@ -15,14 +23,34 @@ module Newflow expect(mail.body.encoded).to include("Welcome to OpenStax!") end - it "has PIN info" do - allow(ConfirmByPin).to receive(:sequential_failure_for) { Hashie::Mash.new('attempts_remaining?' => true)} + context 'when show_pin is not sent' do + it 'includes PIN info in the email' do + mail = NewflowMailer.signup_email_confirmation(email_address: email) - mail = NewflowMailer.signup_email_confirmation email_address: email + expect(mail.subject).to eq("[OpenStax] Your OpenStax account PIN has arrived: #{pin}") + expect(mail.body.encoded).to include("#{pin}") + end + end + + context 'when show_pin is nil' do + it 'includes PIN info in the email' do + mail = NewflowMailer.signup_email_confirmation(email_address: email, show_pin: nil) + + expect(mail.subject).to eq("[OpenStax] Your OpenStax account PIN has arrived: #{pin}") + expect(mail.body.encoded).to include("#{pin}") + end + end + + context 'when show_pin is false' do + it 'excludes the pin code from the email' do + mail = NewflowMailer.signup_email_confirmation(email_address: email, show_pin: false) - expect(mail.subject).to eq("[OpenStax] Your OpenStax account PIN has arrived: 123456") - expect(mail.body.encoded).to include('123456') + expect(mail.subject).to eq("[OpenStax] Confirm your email address") + expect(mail.body.encoded).to include("#{pin}") + end end end end