Chris@909
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@119
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
19
|
Chris@0
|
20 begin
|
Chris@0
|
21 require 'mocha'
|
Chris@0
|
22 rescue
|
Chris@0
|
23 # Won't run some tests
|
Chris@0
|
24 end
|
Chris@0
|
25
|
Chris@0
|
26 class AccountTest < ActionController::IntegrationTest
|
Chris@0
|
27 fixtures :users, :roles
|
Chris@0
|
28
|
Chris@0
|
29 # Replace this with your real tests.
|
Chris@0
|
30 def test_login
|
Chris@0
|
31 get "my/page"
|
Chris@0
|
32 assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
|
Chris@0
|
33 log_user('jsmith', 'jsmith')
|
Chris@909
|
34
|
Chris@0
|
35 get "my/account"
|
Chris@0
|
36 assert_response :success
|
Chris@909
|
37 assert_template "my/account"
|
Chris@0
|
38 end
|
Chris@909
|
39
|
Chris@0
|
40 def test_autologin
|
Chris@0
|
41 user = User.find(1)
|
Chris@0
|
42 Setting.autologin = "7"
|
Chris@0
|
43 Token.delete_all
|
Chris@909
|
44
|
Chris@0
|
45 # User logs in with 'autologin' checked
|
Chris@0
|
46 post '/login', :username => user.login, :password => 'admin', :autologin => 1
|
chris@37
|
47 assert_redirected_to '/my/page'
|
Chris@0
|
48 token = Token.find :first
|
Chris@0
|
49 assert_not_nil token
|
Chris@0
|
50 assert_equal user, token.user
|
Chris@0
|
51 assert_equal 'autologin', token.action
|
Chris@0
|
52 assert_equal user.id, session[:user_id]
|
Chris@0
|
53 assert_equal token.value, cookies['autologin']
|
Chris@909
|
54
|
Chris@0
|
55 # Session is cleared
|
Chris@0
|
56 reset!
|
Chris@0
|
57 User.current = nil
|
Chris@0
|
58 # Clears user's last login timestamp
|
Chris@0
|
59 user.update_attribute :last_login_on, nil
|
Chris@0
|
60 assert_nil user.reload.last_login_on
|
Chris@909
|
61
|
Chris@0
|
62 # User comes back with his autologin cookie
|
Chris@0
|
63 cookies[:autologin] = token.value
|
Chris@0
|
64 get '/my/page'
|
Chris@0
|
65 assert_response :success
|
Chris@0
|
66 assert_template 'my/page'
|
Chris@0
|
67 assert_equal user.id, session[:user_id]
|
Chris@0
|
68 assert_not_nil user.reload.last_login_on
|
Chris@0
|
69 end
|
Chris@909
|
70
|
Chris@0
|
71 def test_lost_password
|
Chris@0
|
72 Token.delete_all
|
Chris@909
|
73
|
Chris@0
|
74 get "account/lost_password"
|
Chris@0
|
75 assert_response :success
|
Chris@0
|
76 assert_template "account/lost_password"
|
Chris@1115
|
77 assert_select 'input[name=mail]'
|
Chris@909
|
78
|
Chris@0
|
79 post "account/lost_password", :mail => 'jSmith@somenet.foo'
|
Chris@0
|
80 assert_redirected_to "/login"
|
Chris@909
|
81
|
Chris@0
|
82 token = Token.find(:first)
|
Chris@0
|
83 assert_equal 'recovery', token.action
|
Chris@0
|
84 assert_equal 'jsmith@somenet.foo', token.user.mail
|
Chris@0
|
85 assert !token.expired?
|
Chris@909
|
86
|
Chris@0
|
87 get "account/lost_password", :token => token.value
|
Chris@0
|
88 assert_response :success
|
Chris@0
|
89 assert_template "account/password_recovery"
|
Chris@1115
|
90 assert_select 'input[type=hidden][name=token][value=?]', token.value
|
Chris@1115
|
91 assert_select 'input[name=new_password]'
|
Chris@1115
|
92 assert_select 'input[name=new_password_confirmation]'
|
Chris@909
|
93
|
Chris@1115
|
94 post "account/lost_password", :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
|
Chris@0
|
95 assert_redirected_to "/login"
|
Chris@0
|
96 assert_equal 'Password was successfully updated.', flash[:notice]
|
Chris@909
|
97
|
Chris@1115
|
98 log_user('jsmith', 'newpass123')
|
Chris@909
|
99 assert_equal 0, Token.count
|
Chris@0
|
100 end
|
Chris@909
|
101
|
Chris@0
|
102 def test_register_with_automatic_activation
|
Chris@0
|
103 Setting.self_registration = '3'
|
Chris@909
|
104
|
Chris@0
|
105 get 'account/register'
|
Chris@0
|
106 assert_response :success
|
Chris@0
|
107 assert_template 'account/register'
|
Chris@909
|
108
|
Chris@1115
|
109 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
|
Chris@1115
|
110 :password => "newpass123", :password_confirmation => "newpass123"}
|
chris@37
|
111 assert_redirected_to '/my/account'
|
Chris@0
|
112 follow_redirect!
|
Chris@0
|
113 assert_response :success
|
Chris@0
|
114 assert_template 'my/account'
|
Chris@909
|
115
|
Chris@0
|
116 user = User.find_by_login('newuser')
|
Chris@0
|
117 assert_not_nil user
|
Chris@0
|
118 assert user.active?
|
Chris@0
|
119 assert_not_nil user.last_login_on
|
Chris@0
|
120 end
|
Chris@909
|
121
|
Chris@0
|
122 def test_register_with_manual_activation
|
Chris@0
|
123 Setting.self_registration = '2'
|
Chris@909
|
124
|
Chris@1115
|
125 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
|
Chris@1115
|
126 :password => "newpass123", :password_confirmation => "newpass123"}
|
Chris@0
|
127 assert_redirected_to '/login'
|
Chris@0
|
128 assert !User.find_by_login('newuser').active?
|
Chris@0
|
129 end
|
Chris@909
|
130
|
Chris@0
|
131 def test_register_with_email_activation
|
Chris@0
|
132 Setting.self_registration = '1'
|
Chris@0
|
133 Token.delete_all
|
Chris@909
|
134
|
Chris@1115
|
135 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
|
Chris@1115
|
136 :password => "newpass123", :password_confirmation => "newpass123"}
|
Chris@0
|
137 assert_redirected_to '/login'
|
Chris@0
|
138 assert !User.find_by_login('newuser').active?
|
Chris@909
|
139
|
Chris@0
|
140 token = Token.find(:first)
|
Chris@0
|
141 assert_equal 'register', token.action
|
Chris@0
|
142 assert_equal 'newuser@foo.bar', token.user.mail
|
Chris@0
|
143 assert !token.expired?
|
Chris@909
|
144
|
Chris@0
|
145 get 'account/activate', :token => token.value
|
Chris@0
|
146 assert_redirected_to '/login'
|
Chris@1115
|
147 log_user('newuser', 'newpass123')
|
Chris@0
|
148 end
|
Chris@909
|
149
|
Chris@0
|
150 def test_onthefly_registration
|
Chris@0
|
151 # disable registration
|
Chris@0
|
152 Setting.self_registration = '0'
|
Chris@0
|
153 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
|
Chris@909
|
154
|
Chris@1115
|
155 post '/login', :username => 'foo', :password => 'bar'
|
chris@37
|
156 assert_redirected_to '/my/page'
|
Chris@909
|
157
|
Chris@0
|
158 user = User.find_by_login('foo')
|
Chris@0
|
159 assert user.is_a?(User)
|
Chris@0
|
160 assert_equal 66, user.auth_source_id
|
Chris@0
|
161 assert user.hashed_password.blank?
|
Chris@0
|
162 end
|
Chris@909
|
163
|
Chris@0
|
164 def test_onthefly_registration_with_invalid_attributes
|
Chris@0
|
165 # disable registration
|
Chris@0
|
166 Setting.self_registration = '0'
|
Chris@0
|
167 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
|
Chris@909
|
168
|
Chris@1115
|
169 post '/login', :username => 'foo', :password => 'bar'
|
Chris@0
|
170 assert_response :success
|
Chris@0
|
171 assert_template 'account/register'
|
Chris@0
|
172 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
|
Chris@0
|
173 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
|
Chris@0
|
174 assert_no_tag :input, :attributes => { :name => 'user[login]' }
|
Chris@0
|
175 assert_no_tag :input, :attributes => { :name => 'user[password]' }
|
Chris@909
|
176
|
Chris@0
|
177 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
|
Chris@0
|
178 assert_redirected_to '/my/account'
|
Chris@909
|
179
|
Chris@0
|
180 user = User.find_by_login('foo')
|
Chris@0
|
181 assert user.is_a?(User)
|
Chris@0
|
182 assert_equal 66, user.auth_source_id
|
Chris@0
|
183 assert user.hashed_password.blank?
|
Chris@0
|
184 end
|
Chris@0
|
185 end
|