Commit fcce6c0fda79f715d9ba3a24d99c285fbca78eba
1 parent
f100e6e0
Exists in
master
and in
22 other branches
ActionItem78: checkpoint
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@630 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
7 changed files
with
101 additions
and
23 deletions
Show diff stats
app/controllers/public/account_controller.rb
| @@ -72,7 +72,7 @@ class AccountController < PublicController | @@ -72,7 +72,7 @@ class AccountController < PublicController | ||
| 72 | 72 | ||
| 73 | # posts back | 73 | # posts back |
| 74 | def forgot_password | 74 | def forgot_password |
| 75 | - @change_password = ChangePasswordData.new(params[:change_password]) | 75 | + @change_password = ChangePassword.new(params[:change_password]) |
| 76 | if request.post? | 76 | if request.post? |
| 77 | begin | 77 | begin |
| 78 | @change_password.confirm! | 78 | @change_password.confirm! |
app/models/change_password.rb
| 1 | +# TODO: send an e-mail with a hash code to the task after the ChangePassword is creatd -> override messages from #Task | ||
| 2 | + | ||
| 1 | class ChangePassword < Task | 3 | class ChangePassword < Task |
| 2 | 4 | ||
| 3 | - attr_accessor :login, :email | 5 | + serialize :data, Hash |
| 6 | + def data | ||
| 7 | + self[:data] ||= {} | ||
| 8 | + end | ||
| 9 | + | ||
| 10 | + attr_accessor :login, :email, :password, :password_confirmation | ||
| 11 | + | ||
| 12 | + ################################################### | ||
| 13 | + # validations for creating a ChangePassword task | ||
| 14 | + | ||
| 15 | + validates_presence_of :login, :email, :on => :create | ||
| 4 | 16 | ||
| 5 | - validates_presence_of :login, :email | ||
| 6 | - validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |obj| !obj.email.blank? }) | 17 | + validates_format_of :email, :on => :create, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |obj| !obj.email.blank? }) |
| 7 | 18 | ||
| 8 | - # | ||
| 9 | - validates_each :login do |data,attr,value| | 19 | + validates_each :login, :on => :create do |data,attr,value| |
| 10 | unless data.login.blank? | 20 | unless data.login.blank? |
| 11 | user = User.find_by_login(data.login) | 21 | user = User.find_by_login(data.login) |
| 12 | if user.nil? | 22 | if user.nil? |
| @@ -19,16 +29,24 @@ class ChangePassword < Task | @@ -19,16 +29,24 @@ class ChangePassword < Task | ||
| 19 | end | 29 | end |
| 20 | end | 30 | end |
| 21 | 31 | ||
| 22 | - def initialize(hash = nil) | ||
| 23 | - hash ||= {} | ||
| 24 | - self.login = hash[:login] || hash['login'] | ||
| 25 | - self.email = hash[:email] || hash['email'] | 32 | + before_create do |change_password| |
| 33 | + change_password.requestor = Person.find_by_identifier(change_password.login) | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + ################################################### | ||
| 37 | + # validations for updating a ChangePassword task | ||
| 38 | + | ||
| 39 | + # only require the new password when actually changing it. | ||
| 40 | + validates_presence_of :password, :on => :update | ||
| 41 | + | ||
| 42 | + def initialize(*args) | ||
| 43 | + super(*args) | ||
| 44 | + self[:data] = {} | ||
| 26 | end | 45 | end |
| 27 | 46 | ||
| 28 | - def confirm! | ||
| 29 | - raise ActiveRecord::RecordInvalid unless self.valid? | 47 | + def perform |
| 30 | user = User.find_by_login(self.login) | 48 | user = User.find_by_login(self.login) |
| 31 | - #ChangePassword.create!(:user_id => user.id) | 49 | + user.force_change_password!(self.password, self.password_confirmation) |
| 32 | end | 50 | end |
| 33 | 51 | ||
| 34 | end | 52 | end |
app/models/task.rb
| @@ -29,6 +29,10 @@ class Task < ActiveRecord::Base | @@ -29,6 +29,10 @@ class Task < ActiveRecord::Base | ||
| 29 | self.status ||= Task::Status::ACTIVE | 29 | self.status ||= Task::Status::ACTIVE |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | + after_create do |task| | ||
| 33 | + TaskMailer.deliver_task_created(task) | ||
| 34 | + end | ||
| 35 | + | ||
| 32 | # this method finished the task. It calls #perform, which must be overriden | 36 | # this method finished the task. It calls #perform, which must be overriden |
| 33 | # by subclasses. At the end a message (as returned by #finish_message) is | 37 | # by subclasses. At the end a message (as returned by #finish_message) is |
| 34 | # sent to the requestor with #notify_requestor. | 38 | # sent to the requestor with #notify_requestor. |
| @@ -53,6 +57,12 @@ class Task < ActiveRecord::Base | @@ -53,6 +57,12 @@ class Task < ActiveRecord::Base | ||
| 53 | end | 57 | end |
| 54 | end | 58 | end |
| 55 | 59 | ||
| 60 | + # The message that will be sent to the requestor of the task when the task is | ||
| 61 | + # created. | ||
| 62 | + def create_message | ||
| 63 | + _("The task was created at %s") % Time.now | ||
| 64 | + end | ||
| 65 | + | ||
| 56 | # The message that will be sent to the requestor of the task when its | 66 | # The message that will be sent to the requestor of the task when its |
| 57 | # finished. | 67 | # finished. |
| 58 | def finish_message | 68 | def finish_message |
app/models/task_mailer.rb
| 1 | class TaskMailer < ActionMailer::Base | 1 | class TaskMailer < ActionMailer::Base |
| 2 | 2 | ||
| 3 | def task_finished(task) | 3 | def task_finished(task) |
| 4 | - recipients task.requestor.email | ||
| 5 | - from task.requestor.environment.contact_email | ||
| 6 | - subject task.description | ||
| 7 | - body :requestor => task.requestor.name, | ||
| 8 | - :message => task.finish_message, | ||
| 9 | - :environment => task.requestor.environment.name, | ||
| 10 | - :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') | 4 | + send_message(task, task.finish_message) |
| 5 | + end | ||
| 6 | + | ||
| 7 | + def task_created(task) | ||
| 8 | + send_message(task, task.create_message) | ||
| 11 | end | 9 | end |
| 12 | 10 | ||
| 13 | def task_cancelled(task) | 11 | def task_cancelled(task) |
| 12 | + send_message(task, task.cancel_message) | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + protected | ||
| 16 | + | ||
| 17 | + def send_message(task, message) | ||
| 14 | recipients task.requestor.email | 18 | recipients task.requestor.email |
| 15 | from task.requestor.environment.contact_email | 19 | from task.requestor.environment.contact_email |
| 16 | subject task.description | 20 | subject task.description |
| 17 | body :requestor => task.requestor.name, | 21 | body :requestor => task.requestor.name, |
| 18 | - :message => task.cancel_message, | 22 | + :message => message, |
| 19 | :environment => task.requestor.environment.name, | 23 | :environment => task.requestor.environment.name, |
| 20 | :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') | 24 | :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') |
| 21 | end | 25 | end |
test/functional/account_controller_test.rb
| @@ -182,6 +182,14 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -182,6 +182,14 @@ class AccountControllerTest < Test::Unit::TestCase | ||
| 182 | assert_response :success | 182 | assert_response :success |
| 183 | end | 183 | end |
| 184 | 184 | ||
| 185 | + should 'respond to forgotten password change request' do | ||
| 186 | + flunk 'not implemented yet' | ||
| 187 | + end | ||
| 188 | + | ||
| 189 | + should 'provide interface for entering new password to replace forgotten one' do | ||
| 190 | + flunk 'not implemented yet' | ||
| 191 | + end | ||
| 192 | + | ||
| 185 | protected | 193 | protected |
| 186 | def create_user(options = {}) | 194 | def create_user(options = {}) |
| 187 | post :signup, :user => { :login => 'quire', :email => 'quire@example.com', | 195 | post :signup, :user => { :login => 'quire', :email => 'quire@example.com', |
test/unit/change_password_test.rb
| @@ -52,4 +52,33 @@ class ChangePasswordTest < Test::Unit::TestCase | @@ -52,4 +52,33 @@ class ChangePasswordTest < Test::Unit::TestCase | ||
| 52 | assert !data.errors.invalid?(:email) | 52 | assert !data.errors.invalid?(:email) |
| 53 | end | 53 | end |
| 54 | 54 | ||
| 55 | + should 'send a message with a URL so the user can enter the new password' do | ||
| 56 | + User.destroy_all | ||
| 57 | + User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') | ||
| 58 | + | ||
| 59 | + data = ChangePassword.new | ||
| 60 | + data.login = 'testuser' | ||
| 61 | + data.email = 'test@example.com' | ||
| 62 | + data.save! | ||
| 63 | + end | ||
| 64 | + | ||
| 65 | + should 'actually change password' do | ||
| 66 | + User.destroy_all | ||
| 67 | + User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') | ||
| 68 | + | ||
| 69 | + change = ChangePassword.new | ||
| 70 | + change.login = 'testuser' | ||
| 71 | + change.email = 'test@example.com' | ||
| 72 | + change.save! | ||
| 73 | + | ||
| 74 | + user = User.new | ||
| 75 | + user.expects(:force_change_password!).with('newpass', 'newpass') | ||
| 76 | + User.expects(:find_by_login).with('testuser').returns(user) | ||
| 77 | + | ||
| 78 | + change.password = 'newpass' | ||
| 79 | + change.password_confirmation = 'newpass' | ||
| 80 | + change.finish | ||
| 81 | + | ||
| 82 | + end | ||
| 83 | + | ||
| 55 | end | 84 | end |
test/unit/task_test.rb
| @@ -6,6 +6,8 @@ class TaskTest < Test::Unit::TestCase | @@ -6,6 +6,8 @@ class TaskTest < Test::Unit::TestCase | ||
| 6 | ActionMailer::Base.delivery_method = :test | 6 | ActionMailer::Base.delivery_method = :test |
| 7 | ActionMailer::Base.perform_deliveries = true | 7 | ActionMailer::Base.perform_deliveries = true |
| 8 | ActionMailer::Base.deliveries = [] | 8 | ActionMailer::Base.deliveries = [] |
| 9 | + | ||
| 10 | + TaskMailer.stubs(:deliver_task_created) | ||
| 9 | end | 11 | end |
| 10 | 12 | ||
| 11 | def test_relationship_with_requestor | 13 | def test_relationship_with_requestor |
| @@ -49,14 +51,14 @@ class TaskTest < Test::Unit::TestCase | @@ -49,14 +51,14 @@ class TaskTest < Test::Unit::TestCase | ||
| 49 | end | 51 | end |
| 50 | 52 | ||
| 51 | def test_should_notify_finish | 53 | def test_should_notify_finish |
| 52 | - TaskMailer.expects(:deliver_task_finished) | ||
| 53 | t = Task.create | 54 | t = Task.create |
| 55 | + TaskMailer.expects(:deliver_task_finished).with(t) | ||
| 54 | t.finish | 56 | t.finish |
| 55 | end | 57 | end |
| 56 | 58 | ||
| 57 | def test_should_notify_cancel | 59 | def test_should_notify_cancel |
| 58 | - TaskMailer.expects(:deliver_task_cancelled) | ||
| 59 | t = Task.create | 60 | t = Task.create |
| 61 | + TaskMailer.expects(:deliver_task_cancelled).with(t) | ||
| 60 | t.cancel | 62 | t.cancel |
| 61 | end | 63 | end |
| 62 | 64 | ||
| @@ -80,4 +82,11 @@ class TaskTest < Test::Unit::TestCase | @@ -80,4 +82,11 @@ class TaskTest < Test::Unit::TestCase | ||
| 80 | assert_kind_of String, Task.new.description | 82 | assert_kind_of String, Task.new.description |
| 81 | end | 83 | end |
| 82 | 84 | ||
| 85 | + should 'notify just after the task is created' do | ||
| 86 | + task = Task.new | ||
| 87 | + TaskMailer.expects(:deliver_task_created).with(task) | ||
| 88 | + task.save! | ||
| 89 | + end | ||
| 90 | + | ||
| 91 | + | ||
| 83 | end | 92 | end |