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