Skip to content

Commit

Permalink
Merge pull request #1288 from openstax/CORE-542-assignable-skip-pin-e…
Browse files Browse the repository at this point in the history
…mail

skip displaying the pin code text when creating external users
  • Loading branch information
jivey authored Dec 16, 2024
2 parents 308baa9 + 34f163d commit e1cf4e5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/handlers/create_external_user_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/mailers/newflow_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions app/routines/newflow/create_email_for_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<td style="font-family: Arial, Helvetica, sans-serif;padding-top: 10px;padding-right: 30px;padding-bottom: 20px;padding-left: 30px;">
<p style="color: rgb(0, 0, 0); font-family: Helvetica, Arial, Helvetica, sans-serif; font-weight: normal; margin: 0px; line-height: 2;">
<div style="text-align: center">
Verify your email by clicking the button below or use your pin: <b id='pin'><%= @confirmation_pin %></b>
Verify your email by clicking the button below<% if @should_show_pin %> or use your pin: <b id='pin'><%= @confirmation_pin %></b><% end %>
</div>
</p>
</td>
Expand Down
2 changes: 1 addition & 1 deletion spec/handlers/create_external_user_credentials_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 36 additions & 8 deletions spec/mailers/newflow_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
user_id: user.id, confirmation_code: '1234', confirmation_pin: '123456' }
let(:email) {
FactoryBot.create :email_address,
value: '[email protected]',
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
Expand All @@ -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("<a href=\"#{confirm_url}\"")
expect(mail.body.encoded).to include("use your pin: <b id='pin'>#{pin}</b>")
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("<a href=\"#{confirm_url}\"")
expect(mail.body.encoded).to include("use your pin: <b id='pin'>#{pin}</b>")
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('<a href="http://localhost:2999/i/verify_email_by_code/1234"')
expect(mail.body.encoded).to include('use your pin: <b id=\'pin\'>123456</b>')
expect(mail.subject).to eq("[OpenStax] Confirm your email address")
expect(mail.body.encoded).to include("<a href=\"#{confirm_url}\"")
expect(mail.body.encoded).not_to include("use your pin: <b id='pin'>#{pin}</b>")
end
end
end
end
Expand Down

0 comments on commit e1cf4e5

Please sign in to comment.