summaryrefslogtreecommitdiffstats
path: root/test/functional/groups_controller_test.rb
blob: e2f71d87fe9caa8c44a6bd368db2a5117493b860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

require File.dirname(__FILE__) + '/../test_helper'

class GroupsControllerTest < ActionController::TestCase
  
  should_render_in_global_context

  def setup
    @group = groups(:team_thunderbird)
  end
  
  context "Routing" do
    should "recognizes routes starting with plus as teams/show/<name>" do
      assert_generates("/+#{@group.to_param}", { :controller => "groups", 
        :action => "show", :id => @group.to_param})
      assert_recognizes({:controller => "groups", :action => "show", 
                         :id => @group.to_param}, "/+#{@group.to_param}")
    end
  end
  
  context "GET index" do
    should "it is successfull" do
      get :index
      assert_response :success
    end
  end
  
  context "GET show" do
    should "finds the requested group" do
      get :show, :id => @group.to_param
      assert_response :success
      assert_equal @group, assigns(:group)
    end
  end
  
  context 'GET to edit' do
    setup do
      login_as :mike
      get :edit, :id => @group.to_param
    end
    should_assign_to :group
    should_respond_with :success
  end
  
  context 'PUT do update' do
    should 'require user to be admin of group' do
      put :update, :id => @group.to_param, :group => {:description => "Unskilled and unprofessional"}
      assert_redirected_to :controller => 'sessions', :action => 'new'
    end
    
    should 'only update the description, not the name' do
      login_as :mike
      put :update, :id => @group.to_param, :group => {:name => 'hackers'}
      assert_redirected_to :action => 'show'
      assert_equal('team-thunderbird', @group.name)
    end
    
    should 'update successfully' do
      login_as :mike
      new_description = 'We save lives'
      put :update, :id => @group.to_param, :group => {:description => new_description}
      assert_redirected_to :action => 'show'
      assert_equal(new_description, @group.reload.description)
    end
  end
  
  context "creating a group" do
    should "requires login" do
      get :new
      assert_redirected_to (new_sessions_path)
    end
    
    should "GETs new successfully" do
      login_as :mike
      get :new
      assert_response :success
    end
    
    should "POST create creates a new group" do
      login_as :mike
      assert_difference("Group.count") do
        post :create, :group => {:name => "foo-hackers", :description => 'Hacking the foos for your bars'},
          :project => {:slug => projects(:johans).slug}
      end
      assert_not_equal nil, flash[:success]
      assert !assigns(:group).new_record?, 'assigns(:group).new_record? should be false'
      assert_equal "foo-hackers", assigns(:group).name
      assert_equal [users(:mike)], assigns(:group).members
    end
  end
  
  context "deleting a group" do
    setup do
      @group = groups(:team_thunderbird)
      @user = users(:mike)
      assert @group.admin?(@user)
    end
    
    should "be deletable if there's only one member" do
      assert_equal 1, @group.members.count
      login_as :mike
      @group.projects.destroy_all
    
      assert_difference("Group.count", -1) do
        delete :destroy, :id => @group.to_param
        assert_response :redirect
      end
      assert_redirected_to groups_path
      assert_match(/team was deleted/, flash[:success])
    end
    
    should "not be deletable if there's more than one member" do
      @group.add_member(users(:johan), Role.member)
      assert_equal 2, @group.members.count
      login_as :mike
      assert_no_difference("Group.count", -1) do
        delete :destroy, :id => @group.to_param
        assert_response :redirect
      end
      assert_redirected_to group_path(@group)
      assert_match(/team can't be deleted/, flash[:error])
    end
    
    should "be deletable if there's more than one member and user is site_admin" do
      assert users(:johan).site_admin?
      @group.add_member(users(:johan), Role.member)
      assert_equal 2, @group.members.count
      
      login_as :johan
      assert_difference("Group.count", -1) do
        delete :destroy, :id => @group.to_param
      end
      assert_response :redirect
      assert_redirected_to groups_path
      assert_match(/team was deleted/, flash[:success])
    end
    
    should 'be able to delete the team avatar' do
      login_as :mike
      @group.update_attribute(:avatar_file_name, "foo.png")
      assert @group.avatar?
      delete :avatar, :id => @group.to_param
      assert_redirected_to group_path(@group)
      assert !@group.reload.avatar?
    end
  end
end