Chris@1115
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 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@1464
|
20 class Redmine::ApiTest::MembershipsTest < Redmine::ApiTest::Base
|
Chris@1115
|
21 fixtures :projects, :users, :roles, :members, :member_roles
|
Chris@1115
|
22
|
Chris@1115
|
23 def setup
|
Chris@1115
|
24 Setting.rest_api_enabled = '1'
|
Chris@1115
|
25 end
|
Chris@1115
|
26
|
Chris@1464
|
27 test "GET /projects/:project_id/memberships.xml should return memberships" do
|
Chris@1464
|
28 get '/projects/1/memberships.xml', {}, credentials('jsmith')
|
Chris@1115
|
29
|
Chris@1464
|
30 assert_response :success
|
Chris@1464
|
31 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
32 assert_tag :tag => 'memberships',
|
Chris@1464
|
33 :attributes => {:type => 'array'},
|
Chris@1464
|
34 :child => {
|
Chris@1464
|
35 :tag => 'membership',
|
Chris@1464
|
36 :child => {
|
Chris@1464
|
37 :tag => 'id',
|
Chris@1464
|
38 :content => '2',
|
Chris@1464
|
39 :sibling => {
|
Chris@1464
|
40 :tag => 'user',
|
Chris@1464
|
41 :attributes => {:id => '3', :name => 'Dave Lopper'},
|
Chris@1464
|
42 :sibling => {
|
Chris@1464
|
43 :tag => 'roles',
|
Chris@1115
|
44 :child => {
|
Chris@1464
|
45 :tag => 'role',
|
Chris@1464
|
46 :attributes => {:id => '2', :name => 'Developer'}
|
Chris@1115
|
47 }
|
Chris@1115
|
48 }
|
Chris@1464
|
49 }
|
Chris@1464
|
50 }
|
Chris@1464
|
51 }
|
Chris@1464
|
52 end
|
Chris@1115
|
53
|
Chris@1464
|
54 test "GET /projects/:project_id/memberships.json should return memberships" do
|
Chris@1464
|
55 get '/projects/1/memberships.json', {}, credentials('jsmith')
|
Chris@1115
|
56
|
Chris@1464
|
57 assert_response :success
|
Chris@1464
|
58 assert_equal 'application/json', @response.content_type
|
Chris@1464
|
59 json = ActiveSupport::JSON.decode(response.body)
|
Chris@1464
|
60 assert_equal({
|
Chris@1464
|
61 "memberships" =>
|
Chris@1464
|
62 [{"id"=>1,
|
Chris@1464
|
63 "project" => {"name"=>"eCookbook", "id"=>1},
|
Chris@1464
|
64 "roles" => [{"name"=>"Manager", "id"=>1}],
|
Chris@1464
|
65 "user" => {"name"=>"John Smith", "id"=>2}},
|
Chris@1464
|
66 {"id"=>2,
|
Chris@1464
|
67 "project" => {"name"=>"eCookbook", "id"=>1},
|
Chris@1464
|
68 "roles" => [{"name"=>"Developer", "id"=>2}],
|
Chris@1464
|
69 "user" => {"name"=>"Dave Lopper", "id"=>3}}],
|
Chris@1464
|
70 "limit" => 25,
|
Chris@1464
|
71 "total_count" => 2,
|
Chris@1464
|
72 "offset" => 0},
|
Chris@1464
|
73 json)
|
Chris@1464
|
74 end
|
Chris@1115
|
75
|
Chris@1517
|
76 test "GET /projects/:project_id/memberships.xml should succeed for closed project" do
|
Chris@1517
|
77 project = Project.find(1)
|
Chris@1517
|
78 project.close
|
Chris@1517
|
79 assert !project.reload.active?
|
Chris@1517
|
80 get '/projects/1/memberships.json', {}, credentials('jsmith')
|
Chris@1517
|
81 assert_response :success
|
Chris@1517
|
82 end
|
Chris@1517
|
83
|
Chris@1464
|
84 test "POST /projects/:project_id/memberships.xml should create the membership" do
|
Chris@1464
|
85 assert_difference 'Member.count' do
|
Chris@1464
|
86 post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
|
Chris@1115
|
87
|
Chris@1464
|
88 assert_response :created
|
Chris@1115
|
89 end
|
Chris@1115
|
90 end
|
Chris@1115
|
91
|
Chris@1464
|
92 test "POST /projects/:project_id/memberships.xml with invalid parameters should return errors" do
|
Chris@1464
|
93 assert_no_difference 'Member.count' do
|
Chris@1464
|
94 post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
|
Chris@1115
|
95
|
Chris@1464
|
96 assert_response :unprocessable_entity
|
Chris@1464
|
97 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
98 assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
|
Chris@1464
|
99 end
|
Chris@1464
|
100 end
|
Chris@1464
|
101
|
Chris@1464
|
102 test "GET /memberships/:id.xml should return the membership" do
|
Chris@1464
|
103 get '/memberships/2.xml', {}, credentials('jsmith')
|
Chris@1464
|
104
|
Chris@1464
|
105 assert_response :success
|
Chris@1464
|
106 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
107 assert_tag :tag => 'membership',
|
Chris@1464
|
108 :child => {
|
Chris@1464
|
109 :tag => 'id',
|
Chris@1464
|
110 :content => '2',
|
Chris@1464
|
111 :sibling => {
|
Chris@1464
|
112 :tag => 'user',
|
Chris@1464
|
113 :attributes => {:id => '3', :name => 'Dave Lopper'},
|
Chris@1464
|
114 :sibling => {
|
Chris@1464
|
115 :tag => 'roles',
|
Chris@1115
|
116 :child => {
|
Chris@1464
|
117 :tag => 'role',
|
Chris@1464
|
118 :attributes => {:id => '2', :name => 'Developer'}
|
Chris@1115
|
119 }
|
Chris@1464
|
120 }
|
Chris@1464
|
121 }
|
Chris@1464
|
122 }
|
Chris@1464
|
123 end
|
Chris@1115
|
124
|
Chris@1464
|
125 test "GET /memberships/:id.json should return the membership" do
|
Chris@1464
|
126 get '/memberships/2.json', {}, credentials('jsmith')
|
Chris@1115
|
127
|
Chris@1464
|
128 assert_response :success
|
Chris@1464
|
129 assert_equal 'application/json', @response.content_type
|
Chris@1464
|
130 json = ActiveSupport::JSON.decode(response.body)
|
Chris@1464
|
131 assert_equal(
|
Chris@1464
|
132 {"membership" => {
|
Chris@1464
|
133 "id" => 2,
|
Chris@1464
|
134 "project" => {"name"=>"eCookbook", "id"=>1},
|
Chris@1464
|
135 "roles" => [{"name"=>"Developer", "id"=>2}],
|
Chris@1464
|
136 "user" => {"name"=>"Dave Lopper", "id"=>3}}
|
Chris@1464
|
137 },
|
Chris@1464
|
138 json)
|
Chris@1464
|
139 end
|
Chris@1464
|
140
|
Chris@1464
|
141 test "PUT /memberships/:id.xml should update the membership" do
|
Chris@1464
|
142 assert_not_equal [1,2], Member.find(2).role_ids.sort
|
Chris@1464
|
143 assert_no_difference 'Member.count' do
|
Chris@1464
|
144 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
|
Chris@1464
|
145
|
Chris@1464
|
146 assert_response :ok
|
Chris@1464
|
147 assert_equal '', @response.body
|
Chris@1115
|
148 end
|
Chris@1464
|
149 member = Member.find(2)
|
Chris@1464
|
150 assert_equal [1,2], member.role_ids.sort
|
Chris@1464
|
151 end
|
Chris@1115
|
152
|
Chris@1464
|
153 test "PUT /memberships/:id.xml with invalid parameters should return errors" do
|
Chris@1464
|
154 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
|
Chris@1115
|
155
|
Chris@1464
|
156 assert_response :unprocessable_entity
|
Chris@1464
|
157 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
158 assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
|
Chris@1464
|
159 end
|
Chris@1115
|
160
|
Chris@1464
|
161 test "DELETE /memberships/:id.xml should destroy the membership" do
|
Chris@1464
|
162 assert_difference 'Member.count', -1 do
|
Chris@1464
|
163 delete '/memberships/2.xml', {}, credentials('jsmith')
|
Chris@1115
|
164
|
Chris@1464
|
165 assert_response :ok
|
Chris@1464
|
166 assert_equal '', @response.body
|
Chris@1115
|
167 end
|
Chris@1464
|
168 assert_nil Member.find_by_id(2)
|
Chris@1464
|
169 end
|
Chris@1115
|
170
|
Chris@1464
|
171 test "DELETE /memberships/:id.xml should respond with 422 on failure" do
|
Chris@1464
|
172 assert_no_difference 'Member.count' do
|
Chris@1464
|
173 # A membership with an inherited role can't be deleted
|
Chris@1464
|
174 Member.find(2).member_roles.first.update_attribute :inherited_from, 99
|
Chris@1464
|
175 delete '/memberships/2.xml', {}, credentials('jsmith')
|
Chris@1115
|
176
|
Chris@1464
|
177 assert_response :unprocessable_entity
|
Chris@1115
|
178 end
|
Chris@1115
|
179 end
|
Chris@1115
|
180 end
|