##// END OF EJS Templates
Fixes Setting.openid? (#2764)....
Jean-Philippe Lang -
r2419:9525e5f147a8
parent child
Show More
@@ -1,168 +1,168
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 class Setting < ActiveRecord::Base
18 class Setting < ActiveRecord::Base
19
19
20 DATE_FORMATS = [
20 DATE_FORMATS = [
21 '%Y-%m-%d',
21 '%Y-%m-%d',
22 '%d/%m/%Y',
22 '%d/%m/%Y',
23 '%d.%m.%Y',
23 '%d.%m.%Y',
24 '%d-%m-%Y',
24 '%d-%m-%Y',
25 '%m/%d/%Y',
25 '%m/%d/%Y',
26 '%d %b %Y',
26 '%d %b %Y',
27 '%d %B %Y',
27 '%d %B %Y',
28 '%b %d, %Y',
28 '%b %d, %Y',
29 '%B %d, %Y'
29 '%B %d, %Y'
30 ]
30 ]
31
31
32 TIME_FORMATS = [
32 TIME_FORMATS = [
33 '%H:%M',
33 '%H:%M',
34 '%I:%M %p'
34 '%I:%M %p'
35 ]
35 ]
36
36
37 ENCODINGS = %w(US-ASCII
37 ENCODINGS = %w(US-ASCII
38 windows-1250
38 windows-1250
39 windows-1251
39 windows-1251
40 windows-1252
40 windows-1252
41 windows-1253
41 windows-1253
42 windows-1254
42 windows-1254
43 windows-1255
43 windows-1255
44 windows-1256
44 windows-1256
45 windows-1257
45 windows-1257
46 windows-1258
46 windows-1258
47 windows-31j
47 windows-31j
48 ISO-2022-JP
48 ISO-2022-JP
49 ISO-2022-KR
49 ISO-2022-KR
50 ISO-8859-1
50 ISO-8859-1
51 ISO-8859-2
51 ISO-8859-2
52 ISO-8859-3
52 ISO-8859-3
53 ISO-8859-4
53 ISO-8859-4
54 ISO-8859-5
54 ISO-8859-5
55 ISO-8859-6
55 ISO-8859-6
56 ISO-8859-7
56 ISO-8859-7
57 ISO-8859-8
57 ISO-8859-8
58 ISO-8859-9
58 ISO-8859-9
59 ISO-8859-13
59 ISO-8859-13
60 ISO-8859-15
60 ISO-8859-15
61 KOI8-R
61 KOI8-R
62 UTF-8
62 UTF-8
63 UTF-16
63 UTF-16
64 UTF-16BE
64 UTF-16BE
65 UTF-16LE
65 UTF-16LE
66 EUC-JP
66 EUC-JP
67 Shift_JIS
67 Shift_JIS
68 GB18030
68 GB18030
69 GBK
69 GBK
70 ISCII91
70 ISCII91
71 EUC-KR
71 EUC-KR
72 Big5
72 Big5
73 Big5-HKSCS
73 Big5-HKSCS
74 TIS-620)
74 TIS-620)
75
75
76 cattr_accessor :available_settings
76 cattr_accessor :available_settings
77 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
77 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
78 Redmine::Plugin.all.each do |plugin|
78 Redmine::Plugin.all.each do |plugin|
79 next unless plugin.settings
79 next unless plugin.settings
80 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
80 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
81 end
81 end
82
82
83 validates_uniqueness_of :name
83 validates_uniqueness_of :name
84 validates_inclusion_of :name, :in => @@available_settings.keys
84 validates_inclusion_of :name, :in => @@available_settings.keys
85 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
85 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
86
86
87 # Hash used to cache setting values
87 # Hash used to cache setting values
88 @cached_settings = {}
88 @cached_settings = {}
89 @cached_cleared_on = Time.now
89 @cached_cleared_on = Time.now
90
90
91 def value
91 def value
92 v = read_attribute(:value)
92 v = read_attribute(:value)
93 # Unserialize serialized settings
93 # Unserialize serialized settings
94 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
94 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
95 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
95 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
96 v
96 v
97 end
97 end
98
98
99 def value=(v)
99 def value=(v)
100 v = v.to_yaml if v && @@available_settings[name]['serialized']
100 v = v.to_yaml if v && @@available_settings[name]['serialized']
101 write_attribute(:value, v.to_s)
101 write_attribute(:value, v.to_s)
102 end
102 end
103
103
104 # Returns the value of the setting named name
104 # Returns the value of the setting named name
105 def self.[](name)
105 def self.[](name)
106 v = @cached_settings[name]
106 v = @cached_settings[name]
107 v ? v : (@cached_settings[name] = find_or_default(name).value)
107 v ? v : (@cached_settings[name] = find_or_default(name).value)
108 end
108 end
109
109
110 def self.[]=(name, v)
110 def self.[]=(name, v)
111 setting = find_or_default(name)
111 setting = find_or_default(name)
112 setting.value = (v ? v : "")
112 setting.value = (v ? v : "")
113 @cached_settings[name] = nil
113 @cached_settings[name] = nil
114 setting.save
114 setting.save
115 setting.value
115 setting.value
116 end
116 end
117
117
118 # Defines getter and setter for each setting
118 # Defines getter and setter for each setting
119 # Then setting values can be read using: Setting.some_setting_name
119 # Then setting values can be read using: Setting.some_setting_name
120 # or set using Setting.some_setting_name = "some value"
120 # or set using Setting.some_setting_name = "some value"
121 @@available_settings.each do |name, params|
121 @@available_settings.each do |name, params|
122 src = <<-END_SRC
122 src = <<-END_SRC
123 def self.#{name}
123 def self.#{name}
124 self[:#{name}]
124 self[:#{name}]
125 end
125 end
126
126
127 def self.#{name}?
127 def self.#{name}?
128 self[:#{name}].to_i > 0
128 self[:#{name}].to_i > 0
129 end
129 end
130
130
131 def self.#{name}=(value)
131 def self.#{name}=(value)
132 self[:#{name}] = value
132 self[:#{name}] = value
133 end
133 end
134 END_SRC
134 END_SRC
135 class_eval src, __FILE__, __LINE__
135 class_eval src, __FILE__, __LINE__
136 end
136 end
137
137
138 # Helper that returns an array based on per_page_options setting
138 # Helper that returns an array based on per_page_options setting
139 def self.per_page_options_array
139 def self.per_page_options_array
140 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
140 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
141 end
141 end
142
142
143 def self.openid?
143 def self.openid?
144 Object.const_defined?(:OpenID) && self['openid'].to_s == '1'
144 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
145 end
145 end
146
146
147 # Checks if settings have changed since the values were read
147 # Checks if settings have changed since the values were read
148 # and clears the cache hash if it's the case
148 # and clears the cache hash if it's the case
149 # Called once per request
149 # Called once per request
150 def self.check_cache
150 def self.check_cache
151 settings_updated_on = Setting.maximum(:updated_on)
151 settings_updated_on = Setting.maximum(:updated_on)
152 if settings_updated_on && @cached_cleared_on <= settings_updated_on
152 if settings_updated_on && @cached_cleared_on <= settings_updated_on
153 @cached_settings.clear
153 @cached_settings.clear
154 @cached_cleared_on = Time.now
154 @cached_cleared_on = Time.now
155 logger.info "Settings cache cleared." if logger
155 logger.info "Settings cache cleared." if logger
156 end
156 end
157 end
157 end
158
158
159 private
159 private
160 # Returns the Setting instance for the setting named name
160 # Returns the Setting instance for the setting named name
161 # (record found in database or new record with default value)
161 # (record found in database or new record with default value)
162 def self.find_or_default(name)
162 def self.find_or_default(name)
163 name = name.to_s
163 name = name.to_s
164 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
164 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
165 setting = find_by_name(name)
165 setting = find_by_name(name)
166 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
166 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
167 end
167 end
168 end
168 end
@@ -1,161 +1,166
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 if Object.const_defined?(:OpenID)
67 if Object.const_defined?(:OpenID)
68
68
69 def test_login_with_openid_for_existing_user
69 def test_login_with_openid_for_existing_user
70 Setting.self_registration = '3'
70 Setting.self_registration = '3'
71 Setting.openid = '1'
71 Setting.openid = '1'
72 existing_user = User.new(:firstname => 'Cool',
72 existing_user = User.new(:firstname => 'Cool',
73 :lastname => 'User',
73 :lastname => 'User',
74 :mail => 'user@somedomain.com',
74 :mail => 'user@somedomain.com',
75 :identity_url => 'http://openid.example.com/good_user')
75 :identity_url => 'http://openid.example.com/good_user')
76 existing_user.login = 'cool_user'
76 existing_user.login = 'cool_user'
77 assert existing_user.save!
77 assert existing_user.save!
78
78
79 post :login, :openid_url => existing_user.identity_url
79 post :login, :openid_url => existing_user.identity_url
80 assert_redirected_to 'my/page'
80 assert_redirected_to 'my/page'
81 end
81 end
82
82
83 def test_login_with_openid_with_new_user_created
83 def test_login_with_openid_with_new_user_created
84 Setting.self_registration = '3'
84 Setting.self_registration = '3'
85 Setting.openid = '1'
85 Setting.openid = '1'
86 post :login, :openid_url => 'http://openid.example.com/good_user'
86 post :login, :openid_url => 'http://openid.example.com/good_user'
87 assert_redirected_to 'my/account'
87 assert_redirected_to 'my/account'
88 user = User.find_by_login('cool_user')
88 user = User.find_by_login('cool_user')
89 assert user
89 assert user
90 assert_equal 'Cool', user.firstname
90 assert_equal 'Cool', user.firstname
91 assert_equal 'User', user.lastname
91 assert_equal 'User', user.lastname
92 end
92 end
93
93
94 def test_login_with_openid_with_new_user_and_self_registration_off
94 def test_login_with_openid_with_new_user_and_self_registration_off
95 Setting.self_registration = '0'
95 Setting.self_registration = '0'
96 Setting.openid = '1'
96 Setting.openid = '1'
97 post :login, :openid_url => 'http://openid.example.com/good_user'
97 post :login, :openid_url => 'http://openid.example.com/good_user'
98 assert_redirected_to home_url
98 assert_redirected_to home_url
99 user = User.find_by_login('cool_user')
99 user = User.find_by_login('cool_user')
100 assert ! user
100 assert ! user
101 end
101 end
102
102
103 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
103 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
104 Setting.self_registration = '1'
104 Setting.self_registration = '1'
105 Setting.openid = '1'
105 Setting.openid = '1'
106 post :login, :openid_url => 'http://openid.example.com/good_user'
106 post :login, :openid_url => 'http://openid.example.com/good_user'
107 assert_redirected_to 'login'
107 assert_redirected_to 'login'
108 user = User.find_by_login('cool_user')
108 user = User.find_by_login('cool_user')
109 assert user
109 assert user
110
110
111 token = Token.find_by_user_id_and_action(user.id, 'register')
111 token = Token.find_by_user_id_and_action(user.id, 'register')
112 assert token
112 assert token
113 end
113 end
114
114
115 def test_login_with_openid_with_new_user_created_with_manual_activation
115 def test_login_with_openid_with_new_user_created_with_manual_activation
116 Setting.self_registration = '2'
116 Setting.self_registration = '2'
117 Setting.openid = '1'
117 Setting.openid = '1'
118 post :login, :openid_url => 'http://openid.example.com/good_user'
118 post :login, :openid_url => 'http://openid.example.com/good_user'
119 assert_redirected_to 'login'
119 assert_redirected_to 'login'
120 user = User.find_by_login('cool_user')
120 user = User.find_by_login('cool_user')
121 assert user
121 assert user
122 assert_equal User::STATUS_REGISTERED, user.status
122 assert_equal User::STATUS_REGISTERED, user.status
123 end
123 end
124
124
125 def test_login_with_openid_with_new_user_with_conflict_should_register
125 def test_login_with_openid_with_new_user_with_conflict_should_register
126 Setting.self_registration = '3'
126 Setting.self_registration = '3'
127 Setting.openid = '1'
127 Setting.openid = '1'
128 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
128 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
129 existing_user.login = 'cool_user'
129 existing_user.login = 'cool_user'
130 assert existing_user.save!
130 assert existing_user.save!
131
131
132 post :login, :openid_url => 'http://openid.example.com/good_user'
132 post :login, :openid_url => 'http://openid.example.com/good_user'
133 assert_response :success
133 assert_response :success
134 assert_template 'register'
134 assert_template 'register'
135 assert assigns(:user)
135 assert assigns(:user)
136 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
136 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
137 end
137 end
138
138
139 def test_setting_openid_should_return_true_when_set_to_true
140 Setting.openid = '1'
141 assert_equal true, Setting.openid?
142 end
143
139 else
144 else
140 puts "Skipping openid tests."
145 puts "Skipping openid tests."
141 end
146 end
142
147
143
148
144 def test_autologin
149 def test_autologin
145 Setting.autologin = "7"
150 Setting.autologin = "7"
146 Token.delete_all
151 Token.delete_all
147 post :login, :username => 'admin', :password => 'admin', :autologin => 1
152 post :login, :username => 'admin', :password => 'admin', :autologin => 1
148 assert_redirected_to 'my/page'
153 assert_redirected_to 'my/page'
149 token = Token.find :first
154 token = Token.find :first
150 assert_not_nil token
155 assert_not_nil token
151 assert_equal User.find_by_login('admin'), token.user
156 assert_equal User.find_by_login('admin'), token.user
152 assert_equal 'autologin', token.action
157 assert_equal 'autologin', token.action
153 end
158 end
154
159
155 def test_logout
160 def test_logout
156 @request.session[:user_id] = 2
161 @request.session[:user_id] = 2
157 get :logout
162 get :logout
158 assert_redirected_to ''
163 assert_redirected_to ''
159 assert_nil @request.session[:user_id]
164 assert_nil @request.session[:user_id]
160 end
165 end
161 end
166 end
General Comments 0
You need to be logged in to leave comments. Login now