annotate test/integration/api_test/groups_test.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1115 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1115 3 #
Chris@1115 4 # This program is free software; you can redistribute it and/or
Chris@1115 5 # modify it under the terms of the GNU General Public License
Chris@1115 6 # as published by the Free Software Foundation; either version 2
Chris@1115 7 # of the License, or (at your option) any later version.
Chris@1115 8 #
Chris@1115 9 # This program is distributed in the hope that it will be useful,
Chris@1115 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1115 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1115 12 # GNU General Public License for more details.
Chris@1115 13 #
Chris@1115 14 # You should have received a copy of the GNU General Public License
Chris@1115 15 # along with this program; if not, write to the Free Software
Chris@1115 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1115 17
Chris@1115 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@1115 19
Chris@1295 20 class Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
Chris@1115 21 fixtures :users, :groups_users
Chris@1115 22
Chris@1115 23 def setup
Chris@1115 24 Setting.rest_api_enabled = '1'
Chris@1115 25 end
Chris@1115 26
Chris@1115 27 context "GET /groups" do
Chris@1115 28 context ".xml" do
Chris@1115 29 should "require authentication" do
Chris@1115 30 get '/groups.xml'
Chris@1115 31 assert_response 401
Chris@1115 32 end
Chris@1115 33
Chris@1115 34 should "return groups" do
Chris@1115 35 get '/groups.xml', {}, credentials('admin')
Chris@1115 36 assert_response :success
Chris@1115 37 assert_equal 'application/xml', response.content_type
Chris@1115 38
Chris@1115 39 assert_select 'groups' do
Chris@1115 40 assert_select 'group' do
Chris@1115 41 assert_select 'name', :text => 'A Team'
Chris@1115 42 assert_select 'id', :text => '10'
Chris@1115 43 end
Chris@1115 44 end
Chris@1115 45 end
Chris@1115 46 end
Chris@1115 47
Chris@1115 48 context ".json" do
Chris@1115 49 should "require authentication" do
Chris@1115 50 get '/groups.json'
Chris@1115 51 assert_response 401
Chris@1115 52 end
Chris@1115 53
Chris@1115 54 should "return groups" do
Chris@1115 55 get '/groups.json', {}, credentials('admin')
Chris@1115 56 assert_response :success
Chris@1115 57 assert_equal 'application/json', response.content_type
Chris@1115 58
Chris@1115 59 json = MultiJson.load(response.body)
Chris@1115 60 groups = json['groups']
Chris@1115 61 assert_kind_of Array, groups
Chris@1115 62 group = groups.detect {|g| g['name'] == 'A Team'}
Chris@1115 63 assert_not_nil group
Chris@1115 64 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
Chris@1115 65 end
Chris@1115 66 end
Chris@1115 67 end
Chris@1115 68
Chris@1115 69 context "GET /groups/:id" do
Chris@1115 70 context ".xml" do
Chris@1115 71 should "return the group with its users" do
Chris@1115 72 get '/groups/10.xml', {}, credentials('admin')
Chris@1115 73 assert_response :success
Chris@1115 74 assert_equal 'application/xml', response.content_type
Chris@1115 75
Chris@1115 76 assert_select 'group' do
Chris@1115 77 assert_select 'name', :text => 'A Team'
Chris@1115 78 assert_select 'id', :text => '10'
Chris@1115 79 end
Chris@1115 80 end
Chris@1115 81
Chris@1115 82 should "include users if requested" do
Chris@1115 83 get '/groups/10.xml?include=users', {}, credentials('admin')
Chris@1115 84 assert_response :success
Chris@1115 85 assert_equal 'application/xml', response.content_type
Chris@1115 86
Chris@1115 87 assert_select 'group' do
Chris@1115 88 assert_select 'users' do
Chris@1115 89 assert_select 'user', Group.find(10).users.count
Chris@1115 90 assert_select 'user[id=8]'
Chris@1115 91 end
Chris@1115 92 end
Chris@1115 93 end
Chris@1115 94
Chris@1115 95 should "include memberships if requested" do
Chris@1115 96 get '/groups/10.xml?include=memberships', {}, credentials('admin')
Chris@1115 97 assert_response :success
Chris@1115 98 assert_equal 'application/xml', response.content_type
Chris@1115 99
Chris@1115 100 assert_select 'group' do
Chris@1115 101 assert_select 'memberships'
Chris@1115 102 end
Chris@1115 103 end
Chris@1115 104 end
Chris@1115 105 end
Chris@1115 106
Chris@1115 107 context "POST /groups" do
Chris@1115 108 context "with valid parameters" do
Chris@1115 109 context ".xml" do
Chris@1115 110 should "create groups" do
Chris@1115 111 assert_difference('Group.count') do
Chris@1115 112 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
Chris@1115 113 assert_response :created
Chris@1115 114 assert_equal 'application/xml', response.content_type
Chris@1115 115 end
Chris@1115 116
Chris@1115 117 group = Group.order('id DESC').first
Chris@1115 118 assert_equal 'Test', group.name
Chris@1115 119 assert_equal [2, 3], group.users.map(&:id).sort
Chris@1115 120
Chris@1115 121 assert_select 'group' do
Chris@1115 122 assert_select 'name', :text => 'Test'
Chris@1115 123 end
Chris@1115 124 end
Chris@1115 125 end
Chris@1115 126 end
Chris@1115 127
Chris@1115 128 context "with invalid parameters" do
Chris@1115 129 context ".xml" do
Chris@1115 130 should "return errors" do
Chris@1115 131 assert_no_difference('Group.count') do
Chris@1115 132 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
Chris@1115 133 end
Chris@1115 134 assert_response :unprocessable_entity
Chris@1115 135 assert_equal 'application/xml', response.content_type
Chris@1115 136
Chris@1115 137 assert_select 'errors' do
Chris@1115 138 assert_select 'error', :text => /Name can't be blank/
Chris@1115 139 end
Chris@1115 140 end
Chris@1115 141 end
Chris@1115 142 end
Chris@1115 143 end
Chris@1115 144
Chris@1115 145 context "PUT /groups/:id" do
Chris@1115 146 context "with valid parameters" do
Chris@1115 147 context ".xml" do
Chris@1115 148 should "update the group" do
Chris@1115 149 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
Chris@1115 150 assert_response :ok
Chris@1115 151 assert_equal '', @response.body
Chris@1115 152
Chris@1115 153 group = Group.find(10)
Chris@1115 154 assert_equal 'New name', group.name
Chris@1115 155 assert_equal [2, 3], group.users.map(&:id).sort
Chris@1115 156 end
Chris@1115 157 end
Chris@1115 158 end
Chris@1115 159
Chris@1115 160 context "with invalid parameters" do
Chris@1115 161 context ".xml" do
Chris@1115 162 should "return errors" do
Chris@1115 163 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
Chris@1115 164 assert_response :unprocessable_entity
Chris@1115 165 assert_equal 'application/xml', response.content_type
Chris@1115 166
Chris@1115 167 assert_select 'errors' do
Chris@1115 168 assert_select 'error', :text => /Name can't be blank/
Chris@1115 169 end
Chris@1115 170 end
Chris@1115 171 end
Chris@1115 172 end
Chris@1115 173 end
Chris@1115 174
Chris@1115 175 context "DELETE /groups/:id" do
Chris@1115 176 context ".xml" do
Chris@1115 177 should "delete the group" do
Chris@1115 178 assert_difference 'Group.count', -1 do
Chris@1115 179 delete '/groups/10.xml', {}, credentials('admin')
Chris@1115 180 assert_response :ok
Chris@1115 181 assert_equal '', @response.body
Chris@1115 182 end
Chris@1115 183 end
Chris@1115 184 end
Chris@1115 185 end
Chris@1115 186
Chris@1115 187 context "POST /groups/:id/users" do
Chris@1115 188 context ".xml" do
Chris@1115 189 should "add user to the group" do
Chris@1115 190 assert_difference 'Group.find(10).users.count' do
Chris@1115 191 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
Chris@1115 192 assert_response :ok
Chris@1115 193 assert_equal '', @response.body
Chris@1115 194 end
Chris@1115 195 assert_include User.find(5), Group.find(10).users
Chris@1115 196 end
Chris@1115 197 end
Chris@1115 198 end
Chris@1115 199
Chris@1115 200 context "DELETE /groups/:id/users/:user_id" do
Chris@1115 201 context ".xml" do
Chris@1115 202 should "remove user from the group" do
Chris@1115 203 assert_difference 'Group.find(10).users.count', -1 do
Chris@1115 204 delete '/groups/10/users/8.xml', {}, credentials('admin')
Chris@1115 205 assert_response :ok
Chris@1115 206 assert_equal '', @response.body
Chris@1115 207 end
Chris@1115 208 assert_not_include User.find(8), Group.find(10).users
Chris@1115 209 end
Chris@1115 210 end
Chris@1115 211 end
Chris@1115 212 end