From ea85daadc4633fea1c1e39925b19c0a39004006c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 16:06:51 +0200 Subject: [PATCH] Added UsersController#confirm_email with route and some specs --- app/controllers/users_controller.rb | 9 ++++++++ app/views/notifier/confirm_email.html.haml | 2 +- app/views/notifier/confirm_email.text.haml | 2 +- config/locales/diaspora/en.yml | 3 +++ config/routes.rb | 1 + spec/controllers/users_controller_spec.rb | 27 ++++++++++++++++++++++ spec/models/user_spec.rb | 6 ++--- 7 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 954e4b123..ccb80eb05 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -150,4 +150,13 @@ class UsersController < ApplicationController tar_path = PhotoMover::move_photos(current_user) send_data( File.open(tar_path).read, :filename => "#{current_user.id}.tar" ) end + + def confirm_email + if current_user.confirm_email(params[:token]) + flash[:notice] = I18n.t('users.confirm_email.email_confirmed', :email => current_user.email) + elsif current_user.unconfirmed_email.present? + flash[:error] = I18n.t('users.confirm_email.email_not_confirmed') + end + redirect_to edit_user_path + end end diff --git a/app/views/notifier/confirm_email.html.haml b/app/views/notifier/confirm_email.html.haml index 88a880d68..ffa324f67 100644 --- a/app/views/notifier/confirm_email.html.haml +++ b/app/views/notifier/confirm_email.html.haml @@ -3,7 +3,7 @@ %p != t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email) %br - = link_to root_url, root_url + = link_to confirm_email_url(:token => @receiver.confirm_email_token), confirm_email_url(:token => @receiver.confirm_email_token) %br %br diff --git a/app/views/notifier/confirm_email.text.haml b/app/views/notifier/confirm_email.text.haml index 8a86f0afd..f692ff73e 100644 --- a/app/views/notifier/confirm_email.text.haml +++ b/app/views/notifier/confirm_email.text.haml @@ -1,7 +1,7 @@ != t('notifier.hello', :name => @receiver.profile.first_name) != t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email) -!= root_url +!= confirm_email_url(:token => @receiver.confirm_email_token) != "#{t('notifier.love')} \n" != t('notifier.diaspora') diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 48b29f141..9e9317893 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -735,6 +735,9 @@ en: unconfirmed_email_not_changed: "E-Mail Change Failed" public: does_not_exist: "User %{username} does not exist!" + confirm_email: + email_confirmed: "E-Mail %{email} activated" + email_not_confirmed: "E-Mail could not be activated. Wrong link?" webfinger: fetch_failed: "failed to fetch webfinger profile for %{profile_url}" diff --git a/config/routes.rb b/config/routes.rb index 8b4772333..962d3b1ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,6 +48,7 @@ Diaspora::Application.routes.draw do get 'public/:username' => :public, :as => 'users_public' match 'getting_started' => :getting_started, :as => 'getting_started' get 'getting_started_completed' => :getting_started_completed + get 'confirm_email/:token' => :confirm_email, :as => 'confirm_email' end # This is a hack to overide a route created by devise. diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 824a28f75..6efe5c0f1 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -180,4 +180,31 @@ describe UsersController do response.should redirect_to new_user_session_path end end + + describe '#confirm_email' do + before do + @user.update_attribute(:unconfirmed_email, 'my@newemail.com') + end + + it 'redirects to to the user edit page' do + get 'confirm_email', :token => @user.confirm_email_token + response.should redirect_to edit_user_path + end + + it 'confirms email' do + get 'confirm_email', :token => @user.confirm_email_token + @user.reload + @user.email.should eql('my@newemail.com') + request.flash[:notice].should eql(I18n.t('users.confirm_email.email_confirmed', :email => 'my@newemail.com')) + request.flash[:error].should be_blank + end + + it 'does NOT confirm email with wrong token' do + get 'confirm_email', :token => @user.confirm_email_token.reverse + @user.reload + @user.email.should_not eql('my@newemail.com') + request.flash[:error].should eql(I18n.t('users.confirm_email.email_not_confirmed')) + request.flash[:notice].should be_blank + end + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 86ad4a530..cceaf0360 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -686,7 +686,7 @@ describe User do alice.mail_confirm_email.should eql(false) end end - + describe '#confirm_email' do context 'on user with unconfirmed email' do before do @@ -729,14 +729,14 @@ describe User do user.unconfirmed_email.should eql(nil) user.confirm_email_token.should eql(nil) end - + it 'returns false and does not change anything on blank token' do user.confirm_email("").should eql(false) user.email.should_not eql("alice@newmail.com") user.unconfirmed_email.should eql(nil) user.confirm_email_token.should eql(nil) end - + it 'returns false and does not change anything on blank token' do user.confirm_email(nil).should eql(false) user.email.should_not eql("alice@newmail.com")