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