##// END OF EJS Templates
Use create!....
Jean-Philippe Lang -
r9334:4a3f03859592
parent child
Show More
@@ -1,109 +1,109
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 PrincipalTest < ActiveSupport::TestCase
20 class PrincipalTest < ActiveSupport::TestCase
21 fixtures :users, :projects, :members, :member_roles
21 fixtures :users, :projects, :members, :member_roles
22
22
23 def test_active_scope_should_return_groups_and_active_users
23 def test_active_scope_should_return_groups_and_active_users
24 result = Principal.active.all
24 result = Principal.active.all
25 assert_include Group.first, result
25 assert_include Group.first, result
26 assert_not_nil result.detect {|p| p.is_a?(User)}
26 assert_not_nil result.detect {|p| p.is_a?(User)}
27 assert_nil result.detect {|p| p.is_a?(User) && !p.active?}
27 assert_nil result.detect {|p| p.is_a?(User) && !p.active?}
28 assert_nil result.detect {|p| p.is_a?(AnonymousUser)}
28 assert_nil result.detect {|p| p.is_a?(AnonymousUser)}
29 end
29 end
30
30
31 def test_member_of_scope_should_return_the_union_of_all_members
31 def test_member_of_scope_should_return_the_union_of_all_members
32 projects = Project.find_all_by_id(1, 2)
32 projects = Project.find_all_by_id(1, 2)
33 assert_equal projects.map(&:principals).flatten.sort, Principal.member_of(projects).sort
33 assert_equal projects.map(&:principals).flatten.sort, Principal.member_of(projects).sort
34 end
34 end
35
35
36 def test_member_of_scope_should_be_empty_for_no_projects
36 def test_member_of_scope_should_be_empty_for_no_projects
37 assert_equal [], Principal.member_of([]).sort
37 assert_equal [], Principal.member_of([]).sort
38 end
38 end
39
39
40 def test_not_member_of_scope_should_return_users_that_have_no_memberships
40 def test_not_member_of_scope_should_return_users_that_have_no_memberships
41 projects = Project.find_all_by_id(1, 2)
41 projects = Project.find_all_by_id(1, 2)
42 expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
42 expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
43 assert_equal expected, Principal.not_member_of(projects).sort
43 assert_equal expected, Principal.not_member_of(projects).sort
44 end
44 end
45
45
46 def test_not_member_of_scope_should_be_empty_for_no_projects
46 def test_not_member_of_scope_should_be_empty_for_no_projects
47 assert_equal [], Principal.not_member_of([]).sort
47 assert_equal [], Principal.not_member_of([]).sort
48 end
48 end
49
49
50 context "#like" do
50 context "#like" do
51 setup do
51 setup do
52 Principal.generate!(:login => 'login')
52 Principal.create!(:login => 'login')
53 Principal.generate!(:login => 'login2')
53 Principal.create!(:login => 'login2')
54
54
55 Principal.generate!(:firstname => 'firstname')
55 Principal.create!(:firstname => 'firstname')
56 Principal.generate!(:firstname => 'firstname2')
56 Principal.create!(:firstname => 'firstname2')
57
57
58 Principal.generate!(:lastname => 'lastname')
58 Principal.create!(:lastname => 'lastname')
59 Principal.generate!(:lastname => 'lastname2')
59 Principal.create!(:lastname => 'lastname2')
60
60
61 Principal.generate!(:mail => 'mail@example.com')
61 Principal.create!(:mail => 'mail@example.com')
62 Principal.generate!(:mail => 'mail2@example.com')
62 Principal.create!(:mail => 'mail2@example.com')
63
63
64 @palmer = Principal.generate!(:firstname => 'David', :lastname => 'Palmer')
64 @palmer = Principal.create!(:firstname => 'David', :lastname => 'Palmer')
65 end
65 end
66
66
67 should "search login" do
67 should "search login" do
68 results = Principal.like('login')
68 results = Principal.like('login')
69
69
70 assert_equal 2, results.count
70 assert_equal 2, results.count
71 assert results.all? {|u| u.login.match(/login/) }
71 assert results.all? {|u| u.login.match(/login/) }
72 end
72 end
73
73
74 should "search firstname" do
74 should "search firstname" do
75 results = Principal.like('firstname')
75 results = Principal.like('firstname')
76
76
77 assert_equal 2, results.count
77 assert_equal 2, results.count
78 assert results.all? {|u| u.firstname.match(/firstname/) }
78 assert results.all? {|u| u.firstname.match(/firstname/) }
79 end
79 end
80
80
81 should "search lastname" do
81 should "search lastname" do
82 results = Principal.like('lastname')
82 results = Principal.like('lastname')
83
83
84 assert_equal 2, results.count
84 assert_equal 2, results.count
85 assert results.all? {|u| u.lastname.match(/lastname/) }
85 assert results.all? {|u| u.lastname.match(/lastname/) }
86 end
86 end
87
87
88 should "search mail" do
88 should "search mail" do
89 results = Principal.like('mail')
89 results = Principal.like('mail')
90
90
91 assert_equal 2, results.count
91 assert_equal 2, results.count
92 assert results.all? {|u| u.mail.match(/mail/) }
92 assert results.all? {|u| u.mail.match(/mail/) }
93 end
93 end
94
94
95 should "search firstname and lastname" do
95 should "search firstname and lastname" do
96 results = Principal.like('david palm')
96 results = Principal.like('david palm')
97
97
98 assert_equal 1, results.count
98 assert_equal 1, results.count
99 assert_equal @palmer, results.first
99 assert_equal @palmer, results.first
100 end
100 end
101
101
102 should "search lastname and firstname" do
102 should "search lastname and firstname" do
103 results = Principal.like('palmer davi')
103 results = Principal.like('palmer davi')
104
104
105 assert_equal 1, results.count
105 assert_equal 1, results.count
106 assert_equal @palmer, results.first
106 assert_equal @palmer, results.first
107 end
107 end
108 end
108 end
109 end
109 end
General Comments 0
You need to be logged in to leave comments. Login now