From cd404b9210837755e95315646bd0be3fb4ab1929 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 10:25:31 +0200 Subject: [PATCH 1/7] Added spec for valid User#email validation --- spec/models/user_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2cad8c7db..b08ab0378 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -113,6 +113,11 @@ describe User do alice.email = eve.email alice.should_not be_valid end + + it "requires a vaild email address" do + alice.email = "somebody@anywhere" + alice.should_not be_valid + end end describe "of language" do From 5b408ecca3acab65cfc7007d897b5dc22a3602e2 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 11:39:38 +0200 Subject: [PATCH 2/7] Added User#unconfirmed_email and #confirm_email_token with specs --- app/models/user.rb | 10 +++ ...01083310_add_unconfirmed_email_to_users.rb | 9 +++ ...091059_add_confirm_email_token_to_users.rb | 9 +++ db/schema.rb | 4 +- spec/models/user_spec.rb | 81 +++++++++++++++++++ 5 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20110601083310_add_unconfirmed_email_to_users.rb create mode 100644 db/migrate/20110601091059_add_confirm_email_token_to_users.rb diff --git a/app/models/user.rb b/app/models/user.rb index 82534c1e4..d56ab6001 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,6 +23,7 @@ class User < ActiveRecord::Base validates_format_of :username, :with => /\A[A-Za-z0-9_]+\z/ validates_length_of :username, :maximum => 32 validates_inclusion_of :language, :in => AVAILABLE_LANGUAGE_CODES + validates_format_of :unconfirmed_email, :with => Devise.email_regexp, :allow_blank => true validates_presence_of :person, :unless => proc {|user| user.invitation_token.present?} validates_associated :person @@ -43,6 +44,7 @@ class User < ActiveRecord::Base before_save do person.save if person && person.changed? end + before_save :guard_unconfirmed_email attr_accessible :getting_started, :password, :password_confirmation, :language, :disable_mail @@ -349,4 +351,12 @@ class User < ActiveRecord::Base mentioned_person.delete end end + + def guard_unconfirmed_email + self.unconfirmed_email = nil if unconfirmed_email.blank? || unconfirmed_email == email + + if unconfirmed_email_changed? + self.confirm_email_token = unconfirmed_email ? ActiveSupport::SecureRandom.hex(15) : nil + end + end end diff --git a/db/migrate/20110601083310_add_unconfirmed_email_to_users.rb b/db/migrate/20110601083310_add_unconfirmed_email_to_users.rb new file mode 100644 index 000000000..9c295e557 --- /dev/null +++ b/db/migrate/20110601083310_add_unconfirmed_email_to_users.rb @@ -0,0 +1,9 @@ +class AddUnconfirmedEmailToUsers < ActiveRecord::Migration + def self.up + add_column :users, :unconfirmed_email, :string, :default => nil, :null => true + end + + def self.down + remove_column :users, :unconfirmed_email + end +end diff --git a/db/migrate/20110601091059_add_confirm_email_token_to_users.rb b/db/migrate/20110601091059_add_confirm_email_token_to_users.rb new file mode 100644 index 000000000..4c71e97b2 --- /dev/null +++ b/db/migrate/20110601091059_add_confirm_email_token_to_users.rb @@ -0,0 +1,9 @@ +class AddConfirmEmailTokenToUsers < ActiveRecord::Migration + def self.up + add_column :users, :confirm_email_token, :string, :limit => 30 + end + + def self.down + remove_column :users, :confirm_email_token + end +end diff --git a/db/schema.rb b/db/schema.rb index 4ccf06d4b..31eae27d8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110527135552) do +ActiveRecord::Schema.define(:version => 20110601091059) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -369,6 +369,8 @@ ActiveRecord::Schema.define(:version => 20110527135552) do t.integer "invited_by_id" t.string "invited_by_type" t.string "authentication_token", :limit => 30 + t.string "unconfirmed_email" + t.string "confirm_email_token", :limit => 30 end add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b08ab0378..2b2747fc8 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -34,6 +34,7 @@ describe User do new_user.id.should_not == alice.id end end + describe "validation" do describe "of associated person" do it "fails if person is not valid" do @@ -119,6 +120,26 @@ describe User do alice.should_not be_valid end end + + describe "of unconfirmed_email" do + it "unconfirmed_email address can be nil/blank" do + alice.unconfirmed_email = nil + alice.should be_valid + alice.unconfirmed_email = "" + alice.should be_valid + end + + it "does NOT require a unique unconfirmed_email address" do + eve.update_attribute :unconfirmed_email, "new@email.com" + alice.unconfirmed_email = "new@email.com" + alice.should be_valid + end + + it "requires a vaild unconfirmed_email address" do + alice.unconfirmed_email = "somebody@anywhere" + alice.should_not be_valid + end + end describe "of language" do after do @@ -593,4 +614,64 @@ describe User do end end end + + context 'change email' do + let(:user){ alice } + + describe "#unconfirmed_email" do + it "is nil by default" do + user.unconfirmed_email.should eql(nil) + end + + it "forces blank to nil" do + user.unconfirmed_email = "" + user.save! + user.unconfirmed_email.should eql(nil) + end + + it "is ignored if it equals email" do + user.unconfirmed_email = user.email + user.save! + user.unconfirmed_email.should eql(nil) + end + + it "allows change to valid new email" do + user.unconfirmed_email = "alice@newmail.com" + user.save! + user.unconfirmed_email.should eql("alice@newmail.com") + end + end + + describe "#confirm_email_token" do + it "is nil by default" do + user.confirm_email_token.should eql(nil) + end + + it "is autofilled when unconfirmed_email is set to new email" do + user.unconfirmed_email = "alice@newmail.com" + user.save! + user.confirm_email_token.should_not be_blank + user.confirm_email_token.size.should eql(30) + end + + it "is set back to nil when unconfirmed_email is empty" do + user.unconfirmed_email = "alice@newmail.com" + user.save! + user.confirm_email_token.should_not be_blank + user.unconfirmed_email = nil + user.save! + user.confirm_email_token.should eql(nil) + end + + it "generates new token on every new unconfirmed_email" do + user.unconfirmed_email = "alice@newmail.com" + user.save! + first_token = user.confirm_email_token + user.unconfirmed_email = "alice@andanotherone.com" + user.save! + user.confirm_email_token.should_not eql(first_token) + user.confirm_email_token.size.should eql(30) + end + end + end end From 602382e24e8d67978f5d7441748c65a83dce88de Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 13:01:34 +0200 Subject: [PATCH 3/7] Extended UsersController#update method for changing password with minimal specs, added form to view and locales (en). --- app/controllers/users_controller.rb | 7 ++++++ app/views/users/edit.html.haml | 11 ++++++++-- config/locales/diaspora/en.yml | 4 ++++ spec/controllers/users_controller_spec.rb | 26 +++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 88076acfa..858bccf2d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -53,6 +53,13 @@ class UsersController < ApplicationController unless u[:a_ids] == ["home"] @user.aspects.where(:id => u[:a_ids]).update_all(:open => true) end + elsif u[:email] + @user.unconfirmed_email = u[:email] + if @user.save + flash[:notice] = I18n.t 'users.update.unconfirmed_email_changed' + else + flash[:error] = I18n.t 'users.update.unconfirmed_email_not_changed' + end end end diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml index 54f960817..1721df878 100644 --- a/app/views/users/edit.html.haml +++ b/app/views/users/edit.html.haml @@ -22,8 +22,15 @@ .span-5.last %h3 = t('.your_email') - %p - = current_user.email + = form_for 'user', :url => user_path, :html => { :method => :put } do |f| + = f.error_messages + %p + = f.text_field :email, :value => @user.unconfirmed_email || @user.email + = f.submit t('.change_email') + %br + - if @user.unconfirmed_email.present? + %p= t('.email_awaiting_confirmation', :email => @user.email, :unconfirmed_email => @user.unconfirmed_email) + %br %br %br diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 782aa46d1..012f4e579 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -688,6 +688,7 @@ en: close_account: "Close Account" change_language: "Change Language" change_password: "Change Password" + change_email: "Change E-Mail" new_password: "New Password" current_password: "Current password" download_xml: "download my xml" @@ -703,6 +704,7 @@ en: private_message: "...you receive a private message?" liked: "...someone likes your post?" change: "Change" + email_awaiting_confirmation: "We have sent you an activation link to %{unconfirmed_email}. Till you follow this link and activate the new address, we will continue to use your original address %{email}." destroy: "Account successfully closed." getting_started: welcome: "Welcome to Diaspora!" @@ -726,6 +728,8 @@ en: language_changed: "Language Changed" language_not_changed: "Language Change Failed" email_notifications_changed: "Email notifications changed" + unconfirmed_email_changed: "E-Mail Changed. Needs activation." + unconfirmed_email_not_changed: "E-Mail Change Failed" public: does_not_exist: "User %{username} does not exist!" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index dc99406a8..8e070a0d7 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -108,6 +108,32 @@ describe UsersController do end end + describe 'email' do + it 'allow the user to change his (unconfirmed) email' do + put(:update, :id => @user.id, :user => { :email => "my@newemail.com"}) + @user.reload + @user.unconfirmed_email.should eql("my@newemail.com") + end + + it 'informs the user about success' do + put(:update, :id => @user.id, :user => { :email => "my@newemail.com"}) + request.flash[:notice].should eql(I18n.t('users.update.unconfirmed_email_changed')) + request.flash[:error].should be_blank + end + + it 'informs the user about failure' do + put(:update, :id => @user.id, :user => { :email => "my@newemailcom"}) + request.flash[:error].should eql(I18n.t('users.update.unconfirmed_email_not_changed')) + request.flash[:notice].should be_blank + end + + it 'allow the user to change his (unconfirmed) email to blank (= abort confirmation)' do + put(:update, :id => @user.id, :user => { :email => ""}) + @user.reload + @user.unconfirmed_email.should eql(nil) + end + end + describe 'email settings' do it 'lets the user turn off mail' do par = {:id => @user.id, :user => {:email_preferences => {'mentioned' => 'true'}}} From 37ccbce153bda81b23d1d6456f2a39edc9fd8fc9 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 14:43:36 +0200 Subject: [PATCH 4/7] Added confirmation email + specs + User#mail_confirm_email + call User#mail_confirm_email in UsersController#update --- app/controllers/users_controller.rb | 1 + app/mailers/notifier.rb | 12 ++++++++++ app/models/jobs/mail_confirm_email.rb | 8 +++++++ app/models/user.rb | 6 +++++ app/views/notifier/confirm_email.html.haml | 12 ++++++++++ app/views/notifier/confirm_email.text.haml | 7 ++++++ config/locales/diaspora/en.yml | 3 +++ spec/controllers/users_controller_spec.rb | 9 +++++++ spec/mailers/notifier_spec.rb | 28 ++++++++++++++++++++++ spec/models/user_spec.rb | 13 ++++++++++ 10 files changed, 99 insertions(+) create mode 100644 app/models/jobs/mail_confirm_email.rb create mode 100644 app/views/notifier/confirm_email.html.haml create mode 100644 app/views/notifier/confirm_email.text.haml diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 858bccf2d..954e4b123 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -56,6 +56,7 @@ class UsersController < ApplicationController elsif u[:email] @user.unconfirmed_email = u[:email] if @user.save + @user.mail_confirm_email flash[:notice] = I18n.t 'users.update.unconfirmed_email_changed' else flash[:error] = I18n.t 'users.update.unconfirmed_email_not_changed' diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index c7b10bcf3..45fd635c2 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -115,6 +115,18 @@ class Notifier < ActionMailer::Base end end + def confirm_email(receiver_id) + @receiver = User.find_by_id(receiver_id) + + attachments.inline['logo_caps.png'] = ATTACHMENT + + I18n.with_locale(@receiver.language) do + mail(:to => "\"#{@receiver.name}\" <#{@receiver.unconfirmed_email}>", + :subject => I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @receiver.unconfirmed_email), + :host => AppConfig[:pod_uri].host) + end + end + private def log_mail recipient_id, sender_id, type log_string = "event=mail mail_type=#{type} recipient_id=#{recipient_id} sender_id=#{sender_id}" diff --git a/app/models/jobs/mail_confirm_email.rb b/app/models/jobs/mail_confirm_email.rb new file mode 100644 index 000000000..6c8bc3248 --- /dev/null +++ b/app/models/jobs/mail_confirm_email.rb @@ -0,0 +1,8 @@ +module Job + class MailConfirmEmail < Base + @queue = :mail + def self.perform_delegate(user_id) + Notifier.confirm_email(user_id).deliver + end + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index d56ab6001..ee74711c1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -206,6 +206,12 @@ class User < ActiveRecord::Base end end + def mail_confirm_email + return false if unconfirmed_email.blank? + Resque.enqueue(Job::MailConfirmEmail, id) + true + end + ######### Posts and Such ############### def retract(post) if post.respond_to?(:relayable?) && post.relayable? diff --git a/app/views/notifier/confirm_email.html.haml b/app/views/notifier/confirm_email.html.haml new file mode 100644 index 000000000..88a880d68 --- /dev/null +++ b/app/views/notifier/confirm_email.html.haml @@ -0,0 +1,12 @@ +%p + = t('notifier.hello', :name => @receiver.profile.first_name) +%p + != t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email) + %br + = link_to root_url, root_url + + %br + %br + = t('notifier.love') + %br + = t('notifier.diaspora') \ No newline at end of file diff --git a/app/views/notifier/confirm_email.text.haml b/app/views/notifier/confirm_email.text.haml new file mode 100644 index 000000000..8a86f0afd --- /dev/null +++ b/app/views/notifier/confirm_email.text.haml @@ -0,0 +1,7 @@ +!= t('notifier.hello', :name => @receiver.profile.first_name) + +!= t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email) +!= root_url + +!= "#{t('notifier.love')} \n" +!= t('notifier.diaspora') diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 012f4e579..48b29f141 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -401,6 +401,9 @@ en: subject: "%{name} has just liked your post" liked: "%{name} has just liked your post: " sign_in: "Sign in to view it" + confirm_email: + subject: "Please activate your new e-mail address %{unconfirmed_email}" + click_link: "To activate your new e-mail address %{unconfirmed_email}, please click this link:" people: zero: "no people" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 8e070a0d7..824a28f75 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -109,6 +109,10 @@ describe UsersController do end describe 'email' do + before do + Resque.stub!(:enqueue) + end + it 'allow the user to change his (unconfirmed) email' do put(:update, :id => @user.id, :user => { :email => "my@newemail.com"}) @user.reload @@ -132,6 +136,11 @@ describe UsersController do @user.reload @user.unconfirmed_email.should eql(nil) end + + it 'sends out activation email on success' do + Resque.should_receive(:enqueue).with(Job::MailConfirmEmail, @user.id).once + put(:update, :id => @user.id, :user => { :email => "my@newemail.com"}) + end end describe 'email settings' do diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 40c062067..dca03ed5b 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -207,5 +207,33 @@ describe Notifier do end + describe ".confirm_email" do + before do + user.update_attribute(:unconfirmed_email, "my@newemail.com") + end + + let!(:confirm_email) { Notifier.confirm_email(user.id) } + + it 'goes to the right person' do + confirm_email.to.should == [user.unconfirmed_email] + end + + it 'has the unconfirmed emil in the subject' do + confirm_email.subject.should include(user.unconfirmed_email) + end + + it 'has the unconfirmed emil in the body' do + confirm_email.body.encoded.should include(user.unconfirmed_email) + end + + it 'has the receivers name in the body' do + confirm_email.body.encoded.should include(user.person.profile.first_name) + end + + it 'has the activation link in the body' do + pending + end + + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2b2747fc8..3bf33bd6e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -673,5 +673,18 @@ describe User do user.confirm_email_token.size.should eql(30) end end + + describe '#mail_confirm_email' do + it 'enqueues a mail job on user with unconfirmed email' do + user.update_attribute(:unconfirmed_email, "alice@newmail.com") + Resque.should_receive(:enqueue).with(Job::MailConfirmEmail, alice.id).once + alice.mail_confirm_email.should eql(true) + end + + it 'enqueues NO mail job on user without unconfirmed email' do + Resque.should_not_receive(:enqueue).with(Job::MailConfirmEmail, alice.id) + alice.mail_confirm_email.should eql(false) + end + end end end From f42055ae2442d9270cc5b25856d67c49ea5652b4 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 15:14:57 +0200 Subject: [PATCH 5/7] User#confirm_email with specs --- app/models/user.rb | 6 ++++ spec/models/user_spec.rb | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index ee74711c1..47bc0fe95 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -93,6 +93,12 @@ class User < ActiveRecord::Base true end + def confirm_email(token) + return false if token.blank? || token != confirm_email_token + self.email = unconfirmed_email + save + end + ######### Aspects ###################### def move_contact(person, to_aspect, from_aspect) return true if to_aspect == from_aspect diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3bf33bd6e..86ad4a530 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -686,5 +686,64 @@ 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 + user.update_attribute(:unconfirmed_email, "alice@newmail.com") + end + + it 'confirms email and set the unconfirmed_email to email on valid token' do + user.confirm_email(user.confirm_email_token).should eql(true) + user.email.should 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 wrong token' do + user.confirm_email(user.confirm_email_token.reverse).should eql(false) + user.email.should_not eql("alice@newmail.com") + user.unconfirmed_email.should_not eql(nil) + user.confirm_email_token.should_not 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_not eql(nil) + user.confirm_email_token.should_not 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") + user.unconfirmed_email.should_not eql(nil) + user.confirm_email_token.should_not eql(nil) + end + end + + context 'on user without unconfirmed email' do + it 'returns false and does not change anything on any token' do + user.confirm_email("12345"*6).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("").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") + user.unconfirmed_email.should eql(nil) + user.confirm_email_token.should eql(nil) + end + end + end end end From ea85daadc4633fea1c1e39925b19c0a39004006c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 16:06:51 +0200 Subject: [PATCH 6/7] 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") From 0af9aa7716be72e3e83b6f65fcfebab5bb6a8e86 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jun 2011 16:26:11 +0200 Subject: [PATCH 7/7] Filled out pending sepc for Notifier.confirm_email --- spec/mailers/notifier_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index dca03ed5b..99fbf051f 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -231,7 +231,7 @@ describe Notifier do end it 'has the activation link in the body' do - pending + confirm_email.body.encoded.should include(confirm_email_url(:token => user.confirm_email_token)) end end