From cf8a5bcaec99cc197ff556793febb8317e1db220 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Sat, 9 Sep 2017 15:55:07 +0200 Subject: [PATCH] add verified/unverified labels to profile emails. added "Resend confirmation email" for unverified emails --- app/controllers/profiles/emails_controller.rb | 10 +++++++ app/services/emails/confirm_service.rb | 7 +++++ app/views/profiles/emails/index.html.haml | 13 +++++++--- config/routes/profile.rb | 6 ++++- .../profiles/emails_controller_spec.rb | 15 +++++++++++ spec/features/profiles/emails_spec.rb | 26 +++++++++++++++---- spec/services/emails/confirm_service_spec.rb | 16 ++++++++++++ 7 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 app/services/emails/confirm_service.rb create mode 100644 spec/services/emails/confirm_service_spec.rb diff --git a/app/controllers/profiles/emails_controller.rb b/app/controllers/profiles/emails_controller.rb index 60426e4f14f..152b71e687d 100644 --- a/app/controllers/profiles/emails_controller.rb +++ b/app/controllers/profiles/emails_controller.rb @@ -24,6 +24,16 @@ class Profiles::EmailsController < Profiles::ApplicationController end end + def resend_confirmation_instructions + @email = current_user.emails.find(params[:id]) + if @email && Emails::ConfirmService.new(current_user, email: @email.email).execute + flash[:notice] = "Confirmation email sent to #{@email.email}" + else + flash[:alert] = "There was a problem sending the confirmation email" + end + redirect_to profile_emails_url + end + private def email_params diff --git a/app/services/emails/confirm_service.rb b/app/services/emails/confirm_service.rb new file mode 100644 index 00000000000..45845ccecc5 --- /dev/null +++ b/app/services/emails/confirm_service.rb @@ -0,0 +1,7 @@ +module Emails + class ConfirmService < ::Emails::BaseService + def execute + Email.find_by_email!(@email).resend_confirmation_instructions + end + end +end diff --git a/app/views/profiles/emails/index.html.haml b/app/views/profiles/emails/index.html.haml index f6f5a363194..1b02603543d 100644 --- a/app/views/profiles/emails/index.html.haml +++ b/app/views/profiles/emails/index.html.haml @@ -32,7 +32,7 @@ All email addresses will be used to identify your commits. %ul.well-list %li - = @primary + = render partial: 'shared/email_with_badge', locals: { email: @primary, verified: current_user.confirmed? } %span.pull-right %span.label.label-success Primary email - if @primary === current_user.public_email @@ -41,12 +41,17 @@ %span.label.label-info Notification email - @emails.each do |email| %li - = email.email + = render partial: 'shared/email_with_badge', locals: { email: email.email, verified: email.confirmed? } %span.pull-right - if email.email === current_user.public_email %span.label.label-info Public email - if email.email === current_user.notification_email %span.label.label-info Notification email - unless email.confirmed? - %span.label.label-warning Unconfirmed - = link_to 'Remove', profile_email_path(email), data: { confirm: 'Are you sure?'}, method: :delete, class: 'btn btn-sm btn-warning prepend-left-10' + - confirm_title = "#{email.confirmation_sent_at ? 'Resend' : 'Send'} confirmation email" + = link_to confirm_title, resend_confirmation_instructions_profile_email_path(email), method: :put, class: 'btn btn-sm btn-warning prepend-left-10' + + = link_to profile_email_path(email), data: { confirm: 'Are you sure?'}, method: :delete, class: 'btn btn-sm btn-danger prepend-left-10' do + %span.sr-only Remove + = icon('trash') + diff --git a/config/routes/profile.rb b/config/routes/profile.rb index 3e4e6111ab8..ea4dd9b67ec 100644 --- a/config/routes/profile.rb +++ b/config/routes/profile.rb @@ -28,7 +28,11 @@ resource :profile, only: [:show, :update] do put :revoke end end - resources :emails, only: [:index, :create, :destroy] + resources :emails, only: [:index, :create, :destroy] do + member do + put :resend_confirmation_instructions + end + end resources :chat_names, only: [:index, :new, :create, :destroy] do collection do delete :deny diff --git a/spec/controllers/profiles/emails_controller_spec.rb b/spec/controllers/profiles/emails_controller_spec.rb index 00862f12b7e..cf510aa5589 100644 --- a/spec/controllers/profiles/emails_controller_spec.rb +++ b/spec/controllers/profiles/emails_controller_spec.rb @@ -17,4 +17,19 @@ describe Profiles::EmailsController do expect(ActionMailer::Base.deliveries.last.subject).to match "Confirmation instructions" end end + + describe '#resend_confirmation_instructions' do + let(:email_params) { {email: "add_email@example.com" } } + + it 'resends an email confirmation' do + email = user.emails.create(email: 'add_email@example.com') + expect {put(:resend_confirmation_instructions, { id: email })}.to change { ActionMailer::Base.deliveries.size } + expect(ActionMailer::Base.deliveries.last.to).to eq [email_params[:email]] + expect(ActionMailer::Base.deliveries.last.subject).to match "Confirmation instructions" + end + + it 'unable to resend an email confirmation' do + expect {put(:resend_confirmation_instructions, { id: 1 })}.to_not change { ActionMailer::Base.deliveries.size } + end + end end diff --git a/spec/features/profiles/emails_spec.rb b/spec/features/profiles/emails_spec.rb index 98b8c607cb0..a3e3ea4e7a2 100644 --- a/spec/features/profiles/emails_spec.rb +++ b/spec/features/profiles/emails_spec.rb @@ -17,8 +17,8 @@ feature 'Profile > Emails' do click_button('Add email address') expect(page).to have_content('my@email.com Unverified') - expect(page).to have_content('user1@example.org Verified') - expect(page).to have_content('Resend Confirmation Email') + expect(page).to have_content("#{user.email} Verified") + expect(page).to have_content('Resend confirmation email') end scenario 'does not add a duplicate email' do @@ -43,14 +43,30 @@ feature 'Profile > Emails' do scenario 'User confirms email' do email = user.emails.create(email: 'my@email.com') visit profile_emails_path - expect(page).to have_content("my@email.com Unverified") + expect(page).to have_content("#{email.email} Unverified") email.confirm expect(email.confirmed?).to be_truthy visit profile_emails_path - expect(page).to have_content("my@email.com Verified") + expect(page).to have_content("#{email.email} Verified") + end + + scenario 'User re-sends confirmation email' do + email = user.emails.create(email: 'my@email.com') + visit profile_emails_path + + expect { click_link("Resend confirmation email") }.to change { ActionMailer::Base.deliveries.size } + expect(page).to have_content("Confirmation email sent to #{email.email}") + end + + scenario 'old unconfirmed emails show Send Confirmation button' do + email = user.emails.create(email: 'my@email.com') + email.update_attribute(:confirmation_sent_at, nil) + visit profile_emails_path + + expect(page).to_not have_content('Resend confirmation email') + expect(page).to have_content('Send confirmation email') end - scenario '' end diff --git a/spec/services/emails/confirm_service_spec.rb b/spec/services/emails/confirm_service_spec.rb new file mode 100644 index 00000000000..2f348589136 --- /dev/null +++ b/spec/services/emails/confirm_service_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Emails::ConfirmService do + let(:user) { create(:user) } + let(:opts) { { email: 'new@email.com' } } + + subject(:service) { described_class.new(user, opts) } + + describe '#execute' do + it 'sends a confirmation email again' do + email = user.emails.create(email: opts[:email]) + mail = service.execute + expect(mail.subject).to eq('Confirmation instructions') + end + end +end