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