Commit bb3c6485603dc5c1c31b775153e3d46c2b748f97
Committed by
Joenio Costa
1 parent
64e4639c
Exists in
master
and in
29 other branches
Forgot password checks for the user in his environment
(ActionItem1578)
Showing
5 changed files
with
29 additions
and
6 deletions
Show diff stats
app/models/change_password.rb
... | ... | @@ -5,7 +5,7 @@ class ChangePassword < Task |
5 | 5 | self[:data] ||= {} |
6 | 6 | end |
7 | 7 | |
8 | - attr_accessor :login, :email, :password, :password_confirmation | |
8 | + attr_accessor :login, :email, :password, :password_confirmation, :environment_id | |
9 | 9 | |
10 | 10 | def self.human_attribute_name(attrib) |
11 | 11 | case attrib.to_sym |
... | ... | @@ -25,15 +25,15 @@ class ChangePassword < Task |
25 | 25 | ################################################### |
26 | 26 | # validations for creating a ChangePassword task |
27 | 27 | |
28 | - validates_presence_of :login, :email, :on => :create | |
28 | + validates_presence_of :login, :email, :environment_id, :on => :create | |
29 | 29 | |
30 | 30 | validates_presence_of :requestor_id |
31 | 31 | |
32 | 32 | validates_format_of :email, :on => :create, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |obj| !obj.email.blank? }) |
33 | 33 | |
34 | 34 | validates_each :login, :on => :create do |data,attr,value| |
35 | - unless data.login.blank? | |
36 | - user = User.find_by_login(data.login) | |
35 | + unless data.login.blank? || data.email.blank? | |
36 | + user = User.find_by_login_and_environment_id(data.login, data.environment_id) | |
37 | 37 | if user.nil? |
38 | 38 | data.errors.add(:login, _('%{fn} is not a valid username.')) |
39 | 39 | else | ... | ... |
app/views/account/forgot_password.rhtml
test/functional/account_controller_test.rb
... | ... | @@ -265,7 +265,7 @@ class AccountControllerTest < Test::Unit::TestCase |
265 | 265 | |
266 | 266 | should 'require password confirmation correctly to enter new pasword' do |
267 | 267 | user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') |
268 | - change = ChangePassword.create!(:login => 'testuser', :email => 'testuser@example.com') | |
268 | + change = ChangePassword.create!(:login => 'testuser', :email => 'testuser@example.com', :environment_id => Environment.default.id) | |
269 | 269 | |
270 | 270 | post :new_password, :code => change.code, :change_password => { :password => 'onepass', :password_confirmation => 'another_pass' } |
271 | 271 | assert_response :success | ... | ... |
test/integration/forgot_password_test.rb
... | ... | @@ -19,7 +19,7 @@ class ForgotPasswordTest < ActionController::IntegrationTest |
19 | 19 | assert_response :success |
20 | 20 | assert_tag :tag => 'form', :attributes => { :action => '/account/forgot_password', :method => 'post' } |
21 | 21 | |
22 | - post '/account/forgot_password', :change_password => { :login => 'forgotten', :email => 'forgotten@localhost.localdomain' } | |
22 | + post '/account/forgot_password', :change_password => { :login => 'forgotten', :email => 'forgotten@localhost.localdomain', :environment_id => Environment.default.id } | |
23 | 23 | |
24 | 24 | assert_response :success |
25 | 25 | assert_template 'password_recovery_sent' | ... | ... |
test/unit/change_password_test.rb
... | ... | @@ -14,6 +14,8 @@ class ChangePasswordTest < Test::Unit::TestCase |
14 | 14 | |
15 | 15 | data = ChangePassword.new |
16 | 16 | data.login = 'unexisting' |
17 | + data.email = 'example@example.com' | |
18 | + data.environment_id = Environment.default.id | |
17 | 19 | data.valid? |
18 | 20 | assert data.errors.invalid?(:login) |
19 | 21 | end |
... | ... | @@ -35,6 +37,7 @@ class ChangePasswordTest < Test::Unit::TestCase |
35 | 37 | data = ChangePassword.new |
36 | 38 | data.login = 'testuser' |
37 | 39 | data.email = 'wrong@example.com' |
40 | + data.environment_id = Environment.default.id | |
38 | 41 | |
39 | 42 | data.valid? |
40 | 43 | assert !data.errors.invalid?(:login) |
... | ... | @@ -48,6 +51,7 @@ class ChangePasswordTest < Test::Unit::TestCase |
48 | 51 | data = ChangePassword.new |
49 | 52 | data.login = 'testuser' |
50 | 53 | data.email = 'test@example.com' |
54 | + data.environment_id = Environment.default.id | |
51 | 55 | |
52 | 56 | data.valid? |
53 | 57 | assert !data.errors.invalid?(:login) |
... | ... | @@ -60,6 +64,7 @@ class ChangePasswordTest < Test::Unit::TestCase |
60 | 64 | change = ChangePassword.new |
61 | 65 | change.login = 'testuser' |
62 | 66 | change.email = 'test@example.com' |
67 | + change.environment_id = Environment.default.id | |
63 | 68 | change.save! |
64 | 69 | |
65 | 70 | change.status = Task::Status::FINISHED |
... | ... | @@ -80,6 +85,7 @@ class ChangePasswordTest < Test::Unit::TestCase |
80 | 85 | change = ChangePassword.new |
81 | 86 | change.login = 'testuser' |
82 | 87 | change.email = 'test@example.com' |
88 | + change.environment_id = Environment.default.id | |
83 | 89 | change.save! |
84 | 90 | |
85 | 91 | change.expects(:requestor).returns(person).at_least_once |
... | ... | @@ -98,6 +104,7 @@ class ChangePasswordTest < Test::Unit::TestCase |
98 | 104 | change = ChangePassword.new |
99 | 105 | change.login = 'testuser' |
100 | 106 | change.email = 'test@example.com' |
107 | + change.environment_id = Environment.default.id | |
101 | 108 | change.save! |
102 | 109 | |
103 | 110 | assert_nothing_raised do |
... | ... | @@ -111,4 +118,18 @@ class ChangePasswordTest < Test::Unit::TestCase |
111 | 118 | assert_equal t1.permission, t2.permission |
112 | 119 | end |
113 | 120 | |
121 | + should 'search for user in the correct environment' do | |
122 | + e1 = Environment.create!(:id => 1, :name => "environment1") | |
123 | + e2 = Environment.create!(:id => 2, :name => "environment2") | |
124 | + p1 = create_user('sample-user', :password => 'test', :password_confirmation => 'test', :email => 'sample-user@e1.com', :environment => e1).person | |
125 | + p2 = create_user('sample-user', :password => 'test', :password_confirmation => 'test', :email => 'sample-user@e2.com', :environment => e2).person | |
126 | + | |
127 | + change = ChangePassword.new | |
128 | + change.login = 'sample-user' | |
129 | + change.email = 'sample-user@e2.com' | |
130 | + change.environment_id = e2.id | |
131 | + | |
132 | + assert change.valid? | |
133 | + end | |
134 | + | |
114 | 135 | end | ... | ... |