Fix an error where creating an invalid group would fail.
[gitorious:pkong23s-mainline.git] / test / unit / team_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2012 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18
19 require "test_helper"
20 class TeamTest < ActiveSupport::TestCase
21   def setup
22     LdapGroup.any_instance.stubs(:validate_ldap_dns)
23   end
24
25   context "LDAP backend" do
26     setup do
27       @old_klass = Team.group_implementation
28       Team.group_implementation = LdapGroup
29     end
30
31     teardown do
32       Team.group_implementation = @old_klass
33     end
34
35     should "supply pagination" do
36       LdapGroup.expects(:paginate)
37       Team.paginate_all
38     end
39
40     should "find by name" do
41       assert_not_nil(Team.find_by_name!("FirstLdapGroup"))
42     end
43
44     should "return a new instance" do
45       assert_kind_of LdapGroup, Team.new_group
46     end
47
48     should "create a new instance" do
49       group = Team.create_group(ldap_group_params, User.first)
50       assert_kind_of LdapGroup, group
51     end
52
53     should "return a group even if it's invalid" do
54       name = "This is not valid"
55       group = Team.create_group({:group => {:name => name}}, User.first)
56       assert !group.valid?
57       assert_equal name, group.name
58     end
59
60
61
62     should "destroy a group if user is admin" do
63       assert_nothing_raised do
64         group = ldap_groups(:first_ldap_group)
65         Team.destroy_group(group.name, group.creator)
66       end
67     end
68
69     should "not let others than the group creator destroy it" do
70       assert_raises Team::DestroyGroupError do
71         group = ldap_groups(:first_ldap_group)
72         Team.destroy_group(group.name, users(:moe))
73       end
74     end
75
76     should "treat group creator as admin" do
77       group = ldap_groups(:first_ldap_group)
78       user = group.creator
79       assert Team.by_admin(user).include?(group)
80     end
81
82     should "find by id" do
83       group = ldap_groups(:first_ldap_group)
84       assert_equal group, Team.find(group.id)
85     end
86   end
87
88   context "Group backend" do
89     setup do
90       @old_klass = Team.group_implementation
91       Team.group_implementation = Group
92     end
93
94     teardown do
95       Team.group_implementation = @old_klass
96     end
97
98     should "create a new instance" do
99       group = Team.create_group(normal_group_params, User.first)
100       assert group.valid?
101     end
102
103     should "return a group even if it's invalid" do
104       name = "This is not valid"
105       group = Team.create_group({:group => {:name => name}}, User.first)
106       assert !group.valid?
107       assert_equal name, group.name
108     end
109
110     should "list all groups for which a user is admin" do
111       user = users(:johan)
112       groups = user.groups.select{|g| admin?(user, g) }
113       assert_equal groups, Team.by_admin(user)
114     end
115
116     should "find by id" do
117       group = Group.first
118       assert_equal(group, Team.find(group.id))
119     end
120   end
121
122   context "Accessing groups" do
123     setup {@group = groups(:a_team)}
124
125     should "return events" do
126       @group.expects(:events).returns([])
127       Team.events(@group,nil)
128     end
129
130     should "return memberships" do
131       assert_kind_of Array, Team.memberships(@group)
132     end
133   end
134
135
136   def ldap_group_params
137     {:ldap_group => {:name => "Testing", :description => "4fun only"}}
138   end
139
140   def normal_group_params
141     {:group => {:name => "Testing", :description => "4fun only"}}
142   end
143 end