@@ -0,0 +1,45 | |||||
|
1 | # Mocks out OpenID | |||
|
2 | # | |||
|
3 | # http://www.northpub.com/articles/2007/04/02/testing-openid-support | |||
|
4 | module OpenIdAuthentication | |||
|
5 | ||||
|
6 | EXTENSION_FIELDS = {'email' => 'user@somedomain.com', | |||
|
7 | 'nickname' => 'cool_user', | |||
|
8 | 'country' => 'US', | |||
|
9 | 'postcode' => '12345', | |||
|
10 | 'fullname' => 'Cool User', | |||
|
11 | 'dob' => '1970-04-01', | |||
|
12 | 'language' => 'en', | |||
|
13 | 'timezone' => 'America/New_York'} | |||
|
14 | ||||
|
15 | protected | |||
|
16 | ||||
|
17 | def authenticate_with_open_id(identity_url = params[:openid_url], options = {}) #:doc: | |||
|
18 | if User.find_by_identity_url(identity_url) || identity_url.include?('good') | |||
|
19 | # Don't process registration fields unless it is requested. | |||
|
20 | unless identity_url.include?('blank') || (options[:required].nil? && options[:optional].nil?) | |||
|
21 | extension_response_fields = {} | |||
|
22 | ||||
|
23 | options[:required].each do |field| | |||
|
24 | extension_response_fields[field.to_s] = EXTENSION_FIELDS[field.to_s] | |||
|
25 | end unless options[:required].nil? | |||
|
26 | ||||
|
27 | options[:optional].each do |field| | |||
|
28 | extension_response_fields[field.to_s] = EXTENSION_FIELDS[field.to_s] | |||
|
29 | end unless options[:optional].nil? | |||
|
30 | end | |||
|
31 | ||||
|
32 | yield Result[:successful], identity_url , extension_response_fields | |||
|
33 | else | |||
|
34 | logger.info "OpenID authentication failed: #{identity_url}" | |||
|
35 | yield Result[:failed], identity_url, nil | |||
|
36 | end | |||
|
37 | end | |||
|
38 | ||||
|
39 | private | |||
|
40 | ||||
|
41 | def add_simple_registration_fields(open_id_response, fields) | |||
|
42 | open_id_response.add_extension_arg('sreg', 'required', [ fields[:required] ].flatten * ',') if fields[:required] | |||
|
43 | open_id_response.add_extension_arg('sreg', 'optional', [ fields[:optional] ].flatten * ',') if fields[:optional] | |||
|
44 | end | |||
|
45 | end |
@@ -1,84 +1,98 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 | require 'account_controller' |
|
19 | require 'account_controller' | |
20 |
|
20 | |||
21 | # Re-raise errors caught by the controller. |
|
21 | # Re-raise errors caught by the controller. | |
22 | class AccountController; def rescue_action(e) raise e end; end |
|
22 | class AccountController; def rescue_action(e) raise e end; end | |
23 |
|
23 | |||
24 | class AccountControllerTest < Test::Unit::TestCase |
|
24 | class AccountControllerTest < Test::Unit::TestCase | |
25 | fixtures :users, :roles |
|
25 | fixtures :users, :roles | |
26 |
|
26 | |||
27 | def setup |
|
27 | def setup | |
28 | @controller = AccountController.new |
|
28 | @controller = AccountController.new | |
29 | @request = ActionController::TestRequest.new |
|
29 | @request = ActionController::TestRequest.new | |
30 | @response = ActionController::TestResponse.new |
|
30 | @response = ActionController::TestResponse.new | |
31 | User.current = nil |
|
31 | User.current = nil | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def test_show |
|
34 | def test_show | |
35 | get :show, :id => 2 |
|
35 | get :show, :id => 2 | |
36 | assert_response :success |
|
36 | assert_response :success | |
37 | assert_template 'show' |
|
37 | assert_template 'show' | |
38 | assert_not_nil assigns(:user) |
|
38 | assert_not_nil assigns(:user) | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | def test_show_inactive |
|
41 | def test_show_inactive | |
42 | get :show, :id => 5 |
|
42 | get :show, :id => 5 | |
43 | assert_response 404 |
|
43 | assert_response 404 | |
44 | assert_nil assigns(:user) |
|
44 | assert_nil assigns(:user) | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | def test_login_should_redirect_to_back_url_param |
|
47 | def test_login_should_redirect_to_back_url_param | |
48 | # request.uri is "test.host" in test environment |
|
48 | # request.uri is "test.host" in test environment | |
49 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' |
|
49 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' | |
50 | assert_redirected_to '/issues/show/1' |
|
50 | assert_redirected_to '/issues/show/1' | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | def test_login_should_not_redirect_to_another_host |
|
53 | def test_login_should_not_redirect_to_another_host | |
54 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' |
|
54 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' | |
55 | assert_redirected_to '/my/page' |
|
55 | assert_redirected_to '/my/page' | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def test_login_with_wrong_password |
|
58 | def test_login_with_wrong_password | |
59 | post :login, :username => 'admin', :password => 'bad' |
|
59 | post :login, :username => 'admin', :password => 'bad' | |
60 | assert_response :success |
|
60 | assert_response :success | |
61 | assert_template 'login' |
|
61 | assert_template 'login' | |
62 | assert_tag 'div', |
|
62 | assert_tag 'div', | |
63 | :attributes => { :class => "flash error" }, |
|
63 | :attributes => { :class => "flash error" }, | |
64 | :content => /Invalid user or password/ |
|
64 | :content => /Invalid user or password/ | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
|
67 | def test_login_with_openid | |||
|
68 | post :login, :openid_url => 'http://openid.example.com/good_user' | |||
|
69 | assert_redirected_to 'my/page' | |||
|
70 | end | |||
|
71 | ||||
|
72 | def test_login_with_openid_with_new_user_created | |||
|
73 | ||||
|
74 | end | |||
|
75 | ||||
|
76 | ||||
|
77 | def test_login_with_openid_with_new_user_with_conflict | |||
|
78 | ||||
|
79 | end | |||
|
80 | ||||
67 | def test_autologin |
|
81 | def test_autologin | |
68 | Setting.autologin = "7" |
|
82 | Setting.autologin = "7" | |
69 | Token.delete_all |
|
83 | Token.delete_all | |
70 | post :login, :username => 'admin', :password => 'admin', :autologin => 1 |
|
84 | post :login, :username => 'admin', :password => 'admin', :autologin => 1 | |
71 | assert_redirected_to 'my/page' |
|
85 | assert_redirected_to 'my/page' | |
72 | token = Token.find :first |
|
86 | token = Token.find :first | |
73 | assert_not_nil token |
|
87 | assert_not_nil token | |
74 | assert_equal User.find_by_login('admin'), token.user |
|
88 | assert_equal User.find_by_login('admin'), token.user | |
75 | assert_equal 'autologin', token.action |
|
89 | assert_equal 'autologin', token.action | |
76 | end |
|
90 | end | |
77 |
|
91 | |||
78 | def test_logout |
|
92 | def test_logout | |
79 | @request.session[:user_id] = 2 |
|
93 | @request.session[:user_id] = 2 | |
80 | get :logout |
|
94 | get :logout | |
81 | assert_redirected_to '' |
|
95 | assert_redirected_to '' | |
82 | assert_nil @request.session[:user_id] |
|
96 | assert_nil @request.session[:user_id] | |
83 | end |
|
97 | end | |
84 | end |
|
98 | end |
@@ -1,67 +1,68 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | ENV["RAILS_ENV"] ||= "test" |
|
18 | ENV["RAILS_ENV"] ||= "test" | |
19 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
|
19 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | |
20 | require 'test_help' |
|
20 | require 'test_help' | |
21 | require File.expand_path(File.dirname(__FILE__) + '/helper_testcase') |
|
21 | require File.expand_path(File.dirname(__FILE__) + '/helper_testcase') | |
|
22 | load File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb') | |||
22 |
|
23 | |||
23 | class Test::Unit::TestCase |
|
24 | class Test::Unit::TestCase | |
24 | # Transactional fixtures accelerate your tests by wrapping each test method |
|
25 | # Transactional fixtures accelerate your tests by wrapping each test method | |
25 | # in a transaction that's rolled back on completion. This ensures that the |
|
26 | # in a transaction that's rolled back on completion. This ensures that the | |
26 | # test database remains unchanged so your fixtures don't have to be reloaded |
|
27 | # test database remains unchanged so your fixtures don't have to be reloaded | |
27 | # between every test method. Fewer database queries means faster tests. |
|
28 | # between every test method. Fewer database queries means faster tests. | |
28 | # |
|
29 | # | |
29 | # Read Mike Clark's excellent walkthrough at |
|
30 | # Read Mike Clark's excellent walkthrough at | |
30 | # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting |
|
31 | # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting | |
31 | # |
|
32 | # | |
32 | # Every Active Record database supports transactions except MyISAM tables |
|
33 | # Every Active Record database supports transactions except MyISAM tables | |
33 | # in MySQL. Turn off transactional fixtures in this case; however, if you |
|
34 | # in MySQL. Turn off transactional fixtures in this case; however, if you | |
34 | # don't care one way or the other, switching from MyISAM to InnoDB tables |
|
35 | # don't care one way or the other, switching from MyISAM to InnoDB tables | |
35 | # is recommended. |
|
36 | # is recommended. | |
36 | self.use_transactional_fixtures = true |
|
37 | self.use_transactional_fixtures = true | |
37 |
|
38 | |||
38 | # Instantiated fixtures are slow, but give you @david where otherwise you |
|
39 | # Instantiated fixtures are slow, but give you @david where otherwise you | |
39 | # would need people(:david). If you don't want to migrate your existing |
|
40 | # would need people(:david). If you don't want to migrate your existing | |
40 | # test cases which use the @david style and don't mind the speed hit (each |
|
41 | # test cases which use the @david style and don't mind the speed hit (each | |
41 | # instantiated fixtures translates to a database query per test method), |
|
42 | # instantiated fixtures translates to a database query per test method), | |
42 | # then set this back to true. |
|
43 | # then set this back to true. | |
43 | self.use_instantiated_fixtures = false |
|
44 | self.use_instantiated_fixtures = false | |
44 |
|
45 | |||
45 | # Add more helper methods to be used by all tests here... |
|
46 | # Add more helper methods to be used by all tests here... | |
46 |
|
47 | |||
47 | def log_user(login, password) |
|
48 | def log_user(login, password) | |
48 | get "/account/login" |
|
49 | get "/account/login" | |
49 | assert_equal nil, session[:user_id] |
|
50 | assert_equal nil, session[:user_id] | |
50 | assert_response :success |
|
51 | assert_response :success | |
51 | assert_template "account/login" |
|
52 | assert_template "account/login" | |
52 | post "/account/login", :username => login, :password => password |
|
53 | post "/account/login", :username => login, :password => password | |
53 | assert_redirected_to "my/page" |
|
54 | assert_redirected_to "my/page" | |
54 | assert_equal login, User.find(session[:user_id]).login |
|
55 | assert_equal login, User.find(session[:user_id]).login | |
55 | end |
|
56 | end | |
56 |
|
57 | |||
57 | def test_uploaded_file(name, mime) |
|
58 | def test_uploaded_file(name, mime) | |
58 | ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime) |
|
59 | ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime) | |
59 | end |
|
60 | end | |
60 |
|
61 | |||
61 | # Use a temporary directory for attachment related tests |
|
62 | # Use a temporary directory for attachment related tests | |
62 | def set_tmp_attachments_directory |
|
63 | def set_tmp_attachments_directory | |
63 | Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test") |
|
64 | Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test") | |
64 | Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments") |
|
65 | Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments") | |
65 | Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments" |
|
66 | Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments" | |
66 | end |
|
67 | end | |
67 | end |
|
68 | end |
General Comments 0
You need to be logged in to leave comments.
Login now