@@ -1,132 +1,132 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class SessionStartTest < ActionController::TestCase |
|
20 | class SessionStartTest < ActionController::TestCase | |
21 | tests AccountController |
|
21 | tests AccountController | |
22 |
|
22 | |||
23 | fixtures :users |
|
23 | fixtures :users | |
24 |
|
24 | |||
25 | def test_login_should_set_session_timestamps |
|
25 | def test_login_should_set_session_timestamps | |
26 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
26 | post :login, :username => 'jsmith', :password => 'jsmith' | |
27 | assert_response 302 |
|
27 | assert_response 302 | |
28 | assert_equal 2, session[:user_id] |
|
28 | assert_equal 2, session[:user_id] | |
29 | assert_not_nil session[:ctime] |
|
29 | assert_not_nil session[:ctime] | |
30 | assert_not_nil session[:atime] |
|
30 | assert_not_nil session[:atime] | |
31 | end |
|
31 | end | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | class SessionsTest < ActionController::TestCase |
|
34 | class SessionsTest < ActionController::TestCase | |
35 | include Redmine::I18n |
|
35 | include Redmine::I18n | |
36 | tests WelcomeController |
|
36 | tests WelcomeController | |
37 |
|
37 | |||
38 | fixtures :users |
|
38 | fixtures :users, :email_addresses | |
39 |
|
39 | |||
40 | def test_atime_from_user_session_should_be_updated |
|
40 | def test_atime_from_user_session_should_be_updated | |
41 | created = 2.hours.ago.utc.to_i |
|
41 | created = 2.hours.ago.utc.to_i | |
42 | get :index, {}, {:user_id => 2, :ctime => created, :atime => created} |
|
42 | get :index, {}, {:user_id => 2, :ctime => created, :atime => created} | |
43 | assert_response :success |
|
43 | assert_response :success | |
44 | assert_equal created, session[:ctime] |
|
44 | assert_equal created, session[:ctime] | |
45 | assert_not_equal created, session[:atime] |
|
45 | assert_not_equal created, session[:atime] | |
46 | assert session[:atime] > created |
|
46 | assert session[:atime] > created | |
47 | end |
|
47 | end | |
48 |
|
48 | |||
49 | def test_user_session_should_not_be_reset_if_lifetime_and_timeout_disabled |
|
49 | def test_user_session_should_not_be_reset_if_lifetime_and_timeout_disabled | |
50 | with_settings :session_lifetime => '0', :session_timeout => '0' do |
|
50 | with_settings :session_lifetime => '0', :session_timeout => '0' do | |
51 | get :index, {}, {:user_id => 2} |
|
51 | get :index, {}, {:user_id => 2} | |
52 | assert_response :success |
|
52 | assert_response :success | |
53 | end |
|
53 | end | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def test_user_session_without_ctime_should_be_reset_if_lifetime_enabled |
|
56 | def test_user_session_without_ctime_should_be_reset_if_lifetime_enabled | |
57 | with_settings :session_lifetime => '720' do |
|
57 | with_settings :session_lifetime => '720' do | |
58 | get :index, {}, {:user_id => 2} |
|
58 | get :index, {}, {:user_id => 2} | |
59 | assert_redirected_to '/login' |
|
59 | assert_redirected_to '/login' | |
60 | end |
|
60 | end | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def test_user_session_with_expired_ctime_should_be_reset_if_lifetime_enabled |
|
63 | def test_user_session_with_expired_ctime_should_be_reset_if_lifetime_enabled | |
64 | with_settings :session_timeout => '720' do |
|
64 | with_settings :session_timeout => '720' do | |
65 | get :index, {}, {:user_id => 2, :atime => 2.days.ago.utc.to_i} |
|
65 | get :index, {}, {:user_id => 2, :atime => 2.days.ago.utc.to_i} | |
66 | assert_redirected_to '/login' |
|
66 | assert_redirected_to '/login' | |
67 | end |
|
67 | end | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def test_user_session_with_valid_ctime_should_not_be_reset_if_lifetime_enabled |
|
70 | def test_user_session_with_valid_ctime_should_not_be_reset_if_lifetime_enabled | |
71 | with_settings :session_timeout => '720' do |
|
71 | with_settings :session_timeout => '720' do | |
72 | get :index, {}, {:user_id => 2, :atime => 3.hours.ago.utc.to_i} |
|
72 | get :index, {}, {:user_id => 2, :atime => 3.hours.ago.utc.to_i} | |
73 | assert_response :success |
|
73 | assert_response :success | |
74 | end |
|
74 | end | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def test_user_session_without_atime_should_be_reset_if_timeout_enabled |
|
77 | def test_user_session_without_atime_should_be_reset_if_timeout_enabled | |
78 | with_settings :session_timeout => '60' do |
|
78 | with_settings :session_timeout => '60' do | |
79 | get :index, {}, {:user_id => 2} |
|
79 | get :index, {}, {:user_id => 2} | |
80 | assert_redirected_to '/login' |
|
80 | assert_redirected_to '/login' | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def test_user_session_with_expired_atime_should_be_reset_if_timeout_enabled |
|
84 | def test_user_session_with_expired_atime_should_be_reset_if_timeout_enabled | |
85 | with_settings :session_timeout => '60' do |
|
85 | with_settings :session_timeout => '60' do | |
86 | get :index, {}, {:user_id => 2, :atime => 4.hours.ago.utc.to_i} |
|
86 | get :index, {}, {:user_id => 2, :atime => 4.hours.ago.utc.to_i} | |
87 | assert_redirected_to '/login' |
|
87 | assert_redirected_to '/login' | |
88 | end |
|
88 | end | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | def test_user_session_with_valid_atime_should_not_be_reset_if_timeout_enabled |
|
91 | def test_user_session_with_valid_atime_should_not_be_reset_if_timeout_enabled | |
92 | with_settings :session_timeout => '60' do |
|
92 | with_settings :session_timeout => '60' do | |
93 | get :index, {}, {:user_id => 2, :atime => 10.minutes.ago.utc.to_i} |
|
93 | get :index, {}, {:user_id => 2, :atime => 10.minutes.ago.utc.to_i} | |
94 | assert_response :success |
|
94 | assert_response :success | |
95 | end |
|
95 | end | |
96 | end |
|
96 | end | |
97 |
|
97 | |||
98 | def test_expired_user_session_should_be_restarted_if_autologin |
|
98 | def test_expired_user_session_should_be_restarted_if_autologin | |
99 | with_settings :session_lifetime => '720', :session_timeout => '60', :autologin => 7 do |
|
99 | with_settings :session_lifetime => '720', :session_timeout => '60', :autologin => 7 do | |
100 | token = Token.create!(:user_id => 2, :action => 'autologin', :created_on => 1.day.ago) |
|
100 | token = Token.create!(:user_id => 2, :action => 'autologin', :created_on => 1.day.ago) | |
101 | @request.cookies['autologin'] = token.value |
|
101 | @request.cookies['autologin'] = token.value | |
102 | created = 2.hours.ago.utc.to_i |
|
102 | created = 2.hours.ago.utc.to_i | |
103 |
|
103 | |||
104 | get :index, {}, {:user_id => 2, :ctime => created, :atime => created} |
|
104 | get :index, {}, {:user_id => 2, :ctime => created, :atime => created} | |
105 | assert_equal 2, session[:user_id] |
|
105 | assert_equal 2, session[:user_id] | |
106 | assert_response :success |
|
106 | assert_response :success | |
107 | assert_not_equal created, session[:ctime] |
|
107 | assert_not_equal created, session[:ctime] | |
108 | assert session[:ctime] >= created |
|
108 | assert session[:ctime] >= created | |
109 | end |
|
109 | end | |
110 | end |
|
110 | end | |
111 |
|
111 | |||
112 | def test_expired_user_session_should_set_locale |
|
112 | def test_expired_user_session_should_set_locale | |
113 | set_language_if_valid 'it' |
|
113 | set_language_if_valid 'it' | |
114 | user = User.find(2) |
|
114 | user = User.find(2) | |
115 | user.language = 'fr' |
|
115 | user.language = 'fr' | |
116 | user.save! |
|
116 | user.save! | |
117 |
|
117 | |||
118 | with_settings :session_timeout => '60' do |
|
118 | with_settings :session_timeout => '60' do | |
119 | get :index, {}, {:user_id => user.id, :atime => 4.hours.ago.utc.to_i} |
|
119 | get :index, {}, {:user_id => user.id, :atime => 4.hours.ago.utc.to_i} | |
120 | assert_redirected_to '/login' |
|
120 | assert_redirected_to '/login' | |
121 | assert_include "Veuillez vous reconnecter", flash[:error] |
|
121 | assert_include "Veuillez vous reconnecter", flash[:error] | |
122 | assert_equal :fr, current_language |
|
122 | assert_equal :fr, current_language | |
123 | end |
|
123 | end | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def test_anonymous_session_should_not_be_reset |
|
126 | def test_anonymous_session_should_not_be_reset | |
127 | with_settings :session_lifetime => '720', :session_timeout => '60' do |
|
127 | with_settings :session_lifetime => '720', :session_timeout => '60' do | |
128 | get :index |
|
128 | get :index | |
129 | assert_response :success |
|
129 | assert_response :success | |
130 | end |
|
130 | end | |
131 | end |
|
131 | end | |
132 | end |
|
132 | end |
@@ -1,135 +1,136 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Redmine - project management software |
|
3 | # Redmine - project management software | |
4 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
4 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; either version 2 |
|
8 | # as published by the Free Software Foundation; either version 2 | |
9 | # of the License, or (at your option) any later version. |
|
9 | # of the License, or (at your option) any later version. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 |
|
19 | |||
20 | require File.expand_path('../../test_helper', __FILE__) |
|
20 | require File.expand_path('../../test_helper', __FILE__) | |
21 |
|
21 | |||
22 | class PrincipalTest < ActiveSupport::TestCase |
|
22 | class PrincipalTest < ActiveSupport::TestCase | |
23 | fixtures :users, :projects, :members, :member_roles, :roles |
|
23 | fixtures :users, :projects, :members, :member_roles, :roles, | |
|
24 | :email_addresses | |||
24 |
|
25 | |||
25 | def test_active_scope_should_return_groups_and_active_users |
|
26 | def test_active_scope_should_return_groups_and_active_users | |
26 | result = Principal.active.to_a |
|
27 | result = Principal.active.to_a | |
27 | assert_include Group.first, result |
|
28 | assert_include Group.first, result | |
28 | assert_not_nil result.detect {|p| p.is_a?(User)} |
|
29 | assert_not_nil result.detect {|p| p.is_a?(User)} | |
29 | assert_nil result.detect {|p| p.is_a?(User) && !p.active?} |
|
30 | assert_nil result.detect {|p| p.is_a?(User) && !p.active?} | |
30 | assert_nil result.detect {|p| p.is_a?(AnonymousUser)} |
|
31 | assert_nil result.detect {|p| p.is_a?(AnonymousUser)} | |
31 | end |
|
32 | end | |
32 |
|
33 | |||
33 | def test_visible_scope_for_admin_should_return_all_principals |
|
34 | def test_visible_scope_for_admin_should_return_all_principals | |
34 | admin = User.generate! {|u| u.admin = true} |
|
35 | admin = User.generate! {|u| u.admin = true} | |
35 | assert_equal Principal.count, Principal.visible(admin).count |
|
36 | assert_equal Principal.count, Principal.visible(admin).count | |
36 | end |
|
37 | end | |
37 |
|
38 | |||
38 | def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_active_principals |
|
39 | def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_active_principals | |
39 | Role.non_member.update! :users_visibility => 'all' |
|
40 | Role.non_member.update! :users_visibility => 'all' | |
40 | user = User.generate! |
|
41 | user = User.generate! | |
41 |
|
42 | |||
42 | expected = Principal.active |
|
43 | expected = Principal.active | |
43 | assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort |
|
44 | assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort | |
44 | end |
|
45 | end | |
45 |
|
46 | |||
46 | def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_members_of_visible_projects_and_self |
|
47 | def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_members_of_visible_projects_and_self | |
47 | Role.non_member.update! :users_visibility => 'members_of_visible_projects' |
|
48 | Role.non_member.update! :users_visibility => 'members_of_visible_projects' | |
48 | user = User.generate! |
|
49 | user = User.generate! | |
49 |
|
50 | |||
50 | expected = Project.visible(user).map(&:member_principals).flatten.map(&:principal).uniq << user |
|
51 | expected = Project.visible(user).map(&:member_principals).flatten.map(&:principal).uniq << user | |
51 | assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort |
|
52 | assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort | |
52 | end |
|
53 | end | |
53 |
|
54 | |||
54 | def test_member_of_scope_should_return_the_union_of_all_members |
|
55 | def test_member_of_scope_should_return_the_union_of_all_members | |
55 | projects = Project.find([1]) |
|
56 | projects = Project.find([1]) | |
56 | assert_equal [3, 2], Principal.member_of(projects).sort.map(&:id) |
|
57 | assert_equal [3, 2], Principal.member_of(projects).sort.map(&:id) | |
57 | projects = Project.find([1, 2]) |
|
58 | projects = Project.find([1, 2]) | |
58 | assert_equal [3, 2, 8, 11], Principal.member_of(projects).sort.map(&:id) |
|
59 | assert_equal [3, 2, 8, 11], Principal.member_of(projects).sort.map(&:id) | |
59 | end |
|
60 | end | |
60 |
|
61 | |||
61 | def test_member_of_scope_should_be_empty_for_no_projects |
|
62 | def test_member_of_scope_should_be_empty_for_no_projects | |
62 | assert_equal [], Principal.member_of([]).sort |
|
63 | assert_equal [], Principal.member_of([]).sort | |
63 | end |
|
64 | end | |
64 |
|
65 | |||
65 | def test_not_member_of_scope_should_return_users_that_have_no_memberships |
|
66 | def test_not_member_of_scope_should_return_users_that_have_no_memberships | |
66 | [[1], [1, 2]].each do |ids| |
|
67 | [[1], [1, 2]].each do |ids| | |
67 | projects = Project.find(ids) |
|
68 | projects = Project.find(ids) | |
68 | assert_equal ids.size, projects.count |
|
69 | assert_equal ids.size, projects.count | |
69 | expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort |
|
70 | expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort | |
70 | assert_equal expected, Principal.not_member_of(projects).sort |
|
71 | assert_equal expected, Principal.not_member_of(projects).sort | |
71 | end |
|
72 | end | |
72 | end |
|
73 | end | |
73 |
|
74 | |||
74 | def test_not_member_of_scope_should_be_empty_for_no_projects |
|
75 | def test_not_member_of_scope_should_be_empty_for_no_projects | |
75 | assert_equal [], Principal.not_member_of([]).sort |
|
76 | assert_equal [], Principal.not_member_of([]).sort | |
76 | end |
|
77 | end | |
77 |
|
78 | |||
78 | def test_sorted_scope_should_sort_users_before_groups |
|
79 | def test_sorted_scope_should_sort_users_before_groups | |
79 | scope = Principal.where(:type => ['User', 'Group']) |
|
80 | scope = Principal.where(:type => ['User', 'Group']) | |
80 | users = scope.select {|p| p.is_a?(User)}.sort |
|
81 | users = scope.select {|p| p.is_a?(User)}.sort | |
81 | groups = scope.select {|p| p.is_a?(Group)}.sort |
|
82 | groups = scope.select {|p| p.is_a?(Group)}.sort | |
82 |
|
83 | |||
83 | assert_equal (users + groups).map(&:name).map(&:downcase), |
|
84 | assert_equal (users + groups).map(&:name).map(&:downcase), | |
84 | scope.sorted.map(&:name).map(&:downcase) |
|
85 | scope.sorted.map(&:name).map(&:downcase) | |
85 | end |
|
86 | end | |
86 |
|
87 | |||
87 | test "like scope should search login" do |
|
88 | test "like scope should search login" do | |
88 | results = Principal.like('jsmi') |
|
89 | results = Principal.like('jsmi') | |
89 |
|
90 | |||
90 | assert results.any? |
|
91 | assert results.any? | |
91 | assert results.all? {|u| u.login.match(/jsmi/i) } |
|
92 | assert results.all? {|u| u.login.match(/jsmi/i) } | |
92 | end |
|
93 | end | |
93 |
|
94 | |||
94 | test "like scope should search firstname" do |
|
95 | test "like scope should search firstname" do | |
95 | results = Principal.like('john') |
|
96 | results = Principal.like('john') | |
96 |
|
97 | |||
97 | assert results.any? |
|
98 | assert results.any? | |
98 | assert results.all? {|u| u.firstname.match(/john/i) } |
|
99 | assert results.all? {|u| u.firstname.match(/john/i) } | |
99 | end |
|
100 | end | |
100 |
|
101 | |||
101 | test "like scope should search lastname" do |
|
102 | test "like scope should search lastname" do | |
102 | results = Principal.like('smi') |
|
103 | results = Principal.like('smi') | |
103 |
|
104 | |||
104 | assert results.any? |
|
105 | assert results.any? | |
105 | assert results.all? {|u| u.lastname.match(/smi/i) } |
|
106 | assert results.all? {|u| u.lastname.match(/smi/i) } | |
106 | end |
|
107 | end | |
107 |
|
108 | |||
108 | test "like scope should search mail" do |
|
109 | test "like scope should search mail" do | |
109 | results = Principal.like('somenet') |
|
110 | results = Principal.like('somenet') | |
110 |
|
111 | |||
111 | assert results.any? |
|
112 | assert results.any? | |
112 | assert results.all? {|u| u.mail.match(/somenet/i) } |
|
113 | assert results.all? {|u| u.mail.match(/somenet/i) } | |
113 | end |
|
114 | end | |
114 |
|
115 | |||
115 | test "like scope should search firstname and lastname" do |
|
116 | test "like scope should search firstname and lastname" do | |
116 | results = Principal.like('john smi') |
|
117 | results = Principal.like('john smi') | |
117 |
|
118 | |||
118 | assert_equal 1, results.count |
|
119 | assert_equal 1, results.count | |
119 | assert_equal User.find(2), results.first |
|
120 | assert_equal User.find(2), results.first | |
120 | end |
|
121 | end | |
121 |
|
122 | |||
122 | test "like scope should search lastname and firstname" do |
|
123 | test "like scope should search lastname and firstname" do | |
123 | results = Principal.like('smith joh') |
|
124 | results = Principal.like('smith joh') | |
124 |
|
125 | |||
125 | assert_equal 1, results.count |
|
126 | assert_equal 1, results.count | |
126 | assert_equal User.find(2), results.first |
|
127 | assert_equal User.find(2), results.first | |
127 | end |
|
128 | end | |
128 |
|
129 | |||
129 | def test_like_scope_with_cyrillic_name |
|
130 | def test_like_scope_with_cyrillic_name | |
130 | user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') |
|
131 | user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') | |
131 | results = Principal.like('Собо') |
|
132 | results = Principal.like('Собо') | |
132 | assert_equal 1, results.count |
|
133 | assert_equal 1, results.count | |
133 | assert_equal user, results.first |
|
134 | assert_equal user, results.first | |
134 | end |
|
135 | end | |
135 | end |
|
136 | end |
General Comments 0
You need to be logged in to leave comments.
Login now