annotate test/functional/account_controller_test.rb @ 669:202986dd17e4 live bibliography_plugin_alpha

Merge from branch "cannam_integration"
author Chris Cannam
date Fri, 09 Sep 2011 16:56:21 +0100
parents af80e5618e9b
children cbb26bc654de
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 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@0 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@0 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@117 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19 require 'account_controller'
Chris@0 20
Chris@0 21 # Re-raise errors caught by the controller.
Chris@0 22 class AccountController; def rescue_action(e) raise e end; end
Chris@0 23
Chris@0 24 class AccountControllerTest < ActionController::TestCase
Chris@0 25 fixtures :users, :roles
Chris@0 26
Chris@0 27 def setup
Chris@0 28 @controller = AccountController.new
Chris@0 29 @request = ActionController::TestRequest.new
Chris@0 30 @response = ActionController::TestResponse.new
Chris@0 31 User.current = nil
Chris@0 32 end
Chris@0 33
Chris@0 34 def test_login_should_redirect_to_back_url_param
Chris@0 35 # request.uri is "test.host" in test environment
Chris@0 36 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
Chris@0 37 assert_redirected_to '/issues/show/1'
Chris@0 38 end
Chris@0 39
Chris@0 40 def test_login_should_not_redirect_to_another_host
Chris@0 41 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
Chris@0 42 assert_redirected_to '/my/page'
Chris@0 43 end
Chris@0 44
Chris@0 45 def test_login_with_wrong_password
Chris@0 46 post :login, :username => 'admin', :password => 'bad'
Chris@0 47 assert_response :success
Chris@0 48 assert_template 'login'
Chris@0 49 assert_tag 'div',
Chris@0 50 :attributes => { :class => "flash error" },
Chris@0 51 :content => /Invalid user or password/
Chris@0 52 end
Chris@0 53
Chris@0 54 if Object.const_defined?(:OpenID)
Chris@0 55
Chris@0 56 def test_login_with_openid_for_existing_user
Chris@0 57 Setting.self_registration = '3'
Chris@0 58 Setting.openid = '1'
Chris@0 59 existing_user = User.new(:firstname => 'Cool',
Chris@0 60 :lastname => 'User',
Chris@0 61 :mail => 'user@somedomain.com',
Chris@0 62 :identity_url => 'http://openid.example.com/good_user')
Chris@0 63 existing_user.login = 'cool_user'
Chris@0 64 assert existing_user.save!
Chris@0 65
Chris@0 66 post :login, :openid_url => existing_user.identity_url
chris@37 67 assert_redirected_to '/my/page'
Chris@0 68 end
Chris@0 69
Chris@14 70 def test_login_with_invalid_openid_provider
Chris@14 71 Setting.self_registration = '0'
Chris@14 72 Setting.openid = '1'
Chris@14 73 post :login, :openid_url => 'http;//openid.example.com/good_user'
Chris@14 74 assert_redirected_to home_url
Chris@14 75 end
Chris@14 76
Chris@0 77 def test_login_with_openid_for_existing_non_active_user
Chris@0 78 Setting.self_registration = '2'
Chris@0 79 Setting.openid = '1'
Chris@0 80 existing_user = User.new(:firstname => 'Cool',
Chris@0 81 :lastname => 'User',
Chris@0 82 :mail => 'user@somedomain.com',
Chris@0 83 :identity_url => 'http://openid.example.com/good_user',
Chris@0 84 :status => User::STATUS_REGISTERED)
Chris@0 85 existing_user.login = 'cool_user'
Chris@0 86 assert existing_user.save!
Chris@0 87
Chris@0 88 post :login, :openid_url => existing_user.identity_url
chris@37 89 assert_redirected_to '/login'
Chris@0 90 end
Chris@0 91
Chris@0 92 def test_login_with_openid_with_new_user_created
Chris@0 93 Setting.self_registration = '3'
Chris@0 94 Setting.openid = '1'
Chris@0 95 post :login, :openid_url => 'http://openid.example.com/good_user'
chris@37 96 assert_redirected_to '/my/account'
Chris@0 97 user = User.find_by_login('cool_user')
Chris@0 98 assert user
Chris@0 99 assert_equal 'Cool', user.firstname
Chris@0 100 assert_equal 'User', user.lastname
Chris@0 101 end
Chris@0 102
Chris@0 103 def test_login_with_openid_with_new_user_and_self_registration_off
Chris@0 104 Setting.self_registration = '0'
Chris@0 105 Setting.openid = '1'
Chris@0 106 post :login, :openid_url => 'http://openid.example.com/good_user'
Chris@0 107 assert_redirected_to home_url
Chris@0 108 user = User.find_by_login('cool_user')
Chris@0 109 assert ! user
Chris@0 110 end
Chris@0 111
Chris@0 112 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
Chris@0 113 Setting.self_registration = '1'
Chris@0 114 Setting.openid = '1'
Chris@0 115 post :login, :openid_url => 'http://openid.example.com/good_user'
chris@37 116 assert_redirected_to '/login'
Chris@0 117 user = User.find_by_login('cool_user')
Chris@0 118 assert user
Chris@0 119
Chris@0 120 token = Token.find_by_user_id_and_action(user.id, 'register')
Chris@0 121 assert token
Chris@0 122 end
Chris@0 123
Chris@0 124 def test_login_with_openid_with_new_user_created_with_manual_activation
Chris@0 125 Setting.self_registration = '2'
Chris@0 126 Setting.openid = '1'
Chris@0 127 post :login, :openid_url => 'http://openid.example.com/good_user'
chris@37 128 assert_redirected_to '/login'
Chris@0 129 user = User.find_by_login('cool_user')
Chris@0 130 assert user
Chris@0 131 assert_equal User::STATUS_REGISTERED, user.status
Chris@0 132 end
Chris@0 133
Chris@0 134 def test_login_with_openid_with_new_user_with_conflict_should_register
Chris@0 135 Setting.self_registration = '3'
Chris@0 136 Setting.openid = '1'
Chris@0 137 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
Chris@0 138 existing_user.login = 'cool_user'
Chris@0 139 assert existing_user.save!
Chris@0 140
Chris@0 141 post :login, :openid_url => 'http://openid.example.com/good_user'
Chris@0 142 assert_response :success
Chris@0 143 assert_template 'register'
Chris@0 144 assert assigns(:user)
Chris@0 145 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
Chris@0 146 end
Chris@0 147
Chris@0 148 def test_setting_openid_should_return_true_when_set_to_true
Chris@0 149 Setting.openid = '1'
Chris@0 150 assert_equal true, Setting.openid?
Chris@0 151 end
Chris@0 152
Chris@0 153 else
Chris@0 154 puts "Skipping openid tests."
Chris@0 155 end
Chris@0 156
Chris@0 157 def test_logout
Chris@0 158 @request.session[:user_id] = 2
Chris@0 159 get :logout
chris@37 160 assert_redirected_to '/'
Chris@0 161 assert_nil @request.session[:user_id]
Chris@0 162 end
Chris@14 163
Chris@14 164 context "GET #register" do
Chris@14 165 context "with self registration on" do
Chris@14 166 setup do
Chris@14 167 Setting.self_registration = '3'
Chris@14 168 get :register
Chris@14 169 end
Chris@14 170
Chris@14 171 should_respond_with :success
Chris@14 172 should_render_template :register
Chris@14 173 should_assign_to :user
Chris@14 174 end
Chris@14 175
Chris@14 176 context "with self registration off" do
Chris@14 177 setup do
Chris@14 178 Setting.self_registration = '0'
Chris@14 179 get :register
Chris@14 180 end
Chris@14 181
Chris@14 182 should_redirect_to('/') { home_url }
Chris@14 183 end
Chris@14 184 end
Chris@14 185
Chris@14 186 # See integration/account_test.rb for the full test
Chris@14 187 context "POST #register" do
Chris@14 188 context "with self registration on automatic" do
Chris@14 189 setup do
Chris@14 190 Setting.self_registration = '3'
Chris@14 191 post :register, :user => {
Chris@14 192 :login => 'register',
Chris@14 193 :password => 'test',
Chris@14 194 :password_confirmation => 'test',
Chris@14 195 :firstname => 'John',
Chris@14 196 :lastname => 'Doe',
Chris@14 197 :mail => 'register@example.com'
Chris@14 198 }
Chris@14 199 end
Chris@14 200
Chris@14 201 should_respond_with :redirect
Chris@14 202 should_assign_to :user
Chris@14 203 should_redirect_to('my page') { {:controller => 'my', :action => 'account'} }
Chris@14 204
Chris@14 205 should_create_a_new_user { User.last(:conditions => {:login => 'register'}) }
Chris@14 206
Chris@14 207 should 'set the user status to active' do
Chris@14 208 user = User.last(:conditions => {:login => 'register'})
Chris@14 209 assert user
Chris@14 210 assert_equal User::STATUS_ACTIVE, user.status
Chris@14 211 end
Chris@14 212 end
Chris@14 213
Chris@14 214 context "with self registration off" do
Chris@14 215 setup do
Chris@14 216 Setting.self_registration = '0'
Chris@14 217 post :register
Chris@14 218 end
Chris@14 219
Chris@14 220 should_redirect_to('/') { home_url }
Chris@14 221 end
Chris@14 222 end
Chris@14 223
Chris@0 224 end