##// END OF EJS Templates
Adds a test for adding a group membership with the REST API (#17904)....
Jean-Philippe Lang -
r13062:8b678837a353
parent child
Show More
@@ -1,180 +1,190
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class Redmine::ApiTest::MembershipsTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :users, :roles, :members, :member_roles
22 22
23 23 def setup
24 24 Setting.rest_api_enabled = '1'
25 25 end
26 26
27 27 test "GET /projects/:project_id/memberships.xml should return memberships" do
28 28 get '/projects/1/memberships.xml', {}, credentials('jsmith')
29 29
30 30 assert_response :success
31 31 assert_equal 'application/xml', @response.content_type
32 32 assert_tag :tag => 'memberships',
33 33 :attributes => {:type => 'array'},
34 34 :child => {
35 35 :tag => 'membership',
36 36 :child => {
37 37 :tag => 'id',
38 38 :content => '2',
39 39 :sibling => {
40 40 :tag => 'user',
41 41 :attributes => {:id => '3', :name => 'Dave Lopper'},
42 42 :sibling => {
43 43 :tag => 'roles',
44 44 :child => {
45 45 :tag => 'role',
46 46 :attributes => {:id => '2', :name => 'Developer'}
47 47 }
48 48 }
49 49 }
50 50 }
51 51 }
52 52 end
53 53
54 54 test "GET /projects/:project_id/memberships.json should return memberships" do
55 55 get '/projects/1/memberships.json', {}, credentials('jsmith')
56 56
57 57 assert_response :success
58 58 assert_equal 'application/json', @response.content_type
59 59 json = ActiveSupport::JSON.decode(response.body)
60 60 assert_equal({
61 61 "memberships" =>
62 62 [{"id"=>1,
63 63 "project" => {"name"=>"eCookbook", "id"=>1},
64 64 "roles" => [{"name"=>"Manager", "id"=>1}],
65 65 "user" => {"name"=>"John Smith", "id"=>2}},
66 66 {"id"=>2,
67 67 "project" => {"name"=>"eCookbook", "id"=>1},
68 68 "roles" => [{"name"=>"Developer", "id"=>2}],
69 69 "user" => {"name"=>"Dave Lopper", "id"=>3}}],
70 70 "limit" => 25,
71 71 "total_count" => 2,
72 72 "offset" => 0},
73 73 json)
74 74 end
75 75
76 76 test "GET /projects/:project_id/memberships.xml should succeed for closed project" do
77 77 project = Project.find(1)
78 78 project.close
79 79 assert !project.reload.active?
80 80 get '/projects/1/memberships.json', {}, credentials('jsmith')
81 81 assert_response :success
82 82 end
83 83
84 84 test "POST /projects/:project_id/memberships.xml should create the membership" do
85 85 assert_difference 'Member.count' do
86 86 post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
87 87
88 88 assert_response :created
89 89 end
90 90 end
91 91
92 test "POST /projects/:project_id/memberships.xml should create the group membership" do
93 group = Group.find(11)
94
95 assert_difference 'Member.count', 1 + group.users.count do
96 post '/projects/1/memberships.xml', {:membership => {:user_id => 11, :role_ids => [2,3]}}, credentials('jsmith')
97
98 assert_response :created
99 end
100 end
101
92 102 test "POST /projects/:project_id/memberships.xml with invalid parameters should return errors" do
93 103 assert_no_difference 'Member.count' do
94 104 post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
95 105
96 106 assert_response :unprocessable_entity
97 107 assert_equal 'application/xml', @response.content_type
98 108 assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
99 109 end
100 110 end
101 111
102 112 test "GET /memberships/:id.xml should return the membership" do
103 113 get '/memberships/2.xml', {}, credentials('jsmith')
104 114
105 115 assert_response :success
106 116 assert_equal 'application/xml', @response.content_type
107 117 assert_tag :tag => 'membership',
108 118 :child => {
109 119 :tag => 'id',
110 120 :content => '2',
111 121 :sibling => {
112 122 :tag => 'user',
113 123 :attributes => {:id => '3', :name => 'Dave Lopper'},
114 124 :sibling => {
115 125 :tag => 'roles',
116 126 :child => {
117 127 :tag => 'role',
118 128 :attributes => {:id => '2', :name => 'Developer'}
119 129 }
120 130 }
121 131 }
122 132 }
123 133 end
124 134
125 135 test "GET /memberships/:id.json should return the membership" do
126 136 get '/memberships/2.json', {}, credentials('jsmith')
127 137
128 138 assert_response :success
129 139 assert_equal 'application/json', @response.content_type
130 140 json = ActiveSupport::JSON.decode(response.body)
131 141 assert_equal(
132 142 {"membership" => {
133 143 "id" => 2,
134 144 "project" => {"name"=>"eCookbook", "id"=>1},
135 145 "roles" => [{"name"=>"Developer", "id"=>2}],
136 146 "user" => {"name"=>"Dave Lopper", "id"=>3}}
137 147 },
138 148 json)
139 149 end
140 150
141 151 test "PUT /memberships/:id.xml should update the membership" do
142 152 assert_not_equal [1,2], Member.find(2).role_ids.sort
143 153 assert_no_difference 'Member.count' do
144 154 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
145 155
146 156 assert_response :ok
147 157 assert_equal '', @response.body
148 158 end
149 159 member = Member.find(2)
150 160 assert_equal [1,2], member.role_ids.sort
151 161 end
152 162
153 163 test "PUT /memberships/:id.xml with invalid parameters should return errors" do
154 164 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
155 165
156 166 assert_response :unprocessable_entity
157 167 assert_equal 'application/xml', @response.content_type
158 168 assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
159 169 end
160 170
161 171 test "DELETE /memberships/:id.xml should destroy the membership" do
162 172 assert_difference 'Member.count', -1 do
163 173 delete '/memberships/2.xml', {}, credentials('jsmith')
164 174
165 175 assert_response :ok
166 176 assert_equal '', @response.body
167 177 end
168 178 assert_nil Member.find_by_id(2)
169 179 end
170 180
171 181 test "DELETE /memberships/:id.xml should respond with 422 on failure" do
172 182 assert_no_difference 'Member.count' do
173 183 # A membership with an inherited role can't be deleted
174 184 Member.find(2).member_roles.first.update_attribute :inherited_from, 99
175 185 delete '/memberships/2.xml', {}, credentials('jsmith')
176 186
177 187 assert_response :unprocessable_entity
178 188 end
179 189 end
180 190 end
General Comments 0
You need to be logged in to leave comments. Login now