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@1464
|
76 test "POST /projects/:project_id/memberships.xml should create the membership" do
|
Chris@1464
|
77 assert_difference 'Member.count' do
|
Chris@1464
|
78 post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
|
Chris@1115
|
79
|
Chris@1464
|
80 assert_response :created
|
Chris@1115
|
81 end
|
Chris@1115
|
82 end
|
Chris@1115
|
83
|
Chris@1464
|
84 test "POST /projects/:project_id/memberships.xml with invalid parameters should return errors" do
|
Chris@1464
|
85 assert_no_difference 'Member.count' do
|
Chris@1464
|
86 post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
|
Chris@1115
|
87
|
Chris@1464
|
88 assert_response :unprocessable_entity
|
Chris@1464
|
89 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
90 assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
|
Chris@1464
|
91 end
|
Chris@1464
|
92 end
|
Chris@1464
|
93
|
Chris@1464
|
94 test "GET /memberships/:id.xml should return the membership" do
|
Chris@1464
|
95 get '/memberships/2.xml', {}, credentials('jsmith')
|
Chris@1464
|
96
|
Chris@1464
|
97 assert_response :success
|
Chris@1464
|
98 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
99 assert_tag :tag => 'membership',
|
Chris@1464
|
100 :child => {
|
Chris@1464
|
101 :tag => 'id',
|
Chris@1464
|
102 :content => '2',
|
Chris@1464
|
103 :sibling => {
|
Chris@1464
|
104 :tag => 'user',
|
Chris@1464
|
105 :attributes => {:id => '3', :name => 'Dave Lopper'},
|
Chris@1464
|
106 :sibling => {
|
Chris@1464
|
107 :tag => 'roles',
|
Chris@1115
|
108 :child => {
|
Chris@1464
|
109 :tag => 'role',
|
Chris@1464
|
110 :attributes => {:id => '2', :name => 'Developer'}
|
Chris@1115
|
111 }
|
Chris@1464
|
112 }
|
Chris@1464
|
113 }
|
Chris@1464
|
114 }
|
Chris@1464
|
115 end
|
Chris@1115
|
116
|
Chris@1464
|
117 test "GET /memberships/:id.json should return the membership" do
|
Chris@1464
|
118 get '/memberships/2.json', {}, credentials('jsmith')
|
Chris@1115
|
119
|
Chris@1464
|
120 assert_response :success
|
Chris@1464
|
121 assert_equal 'application/json', @response.content_type
|
Chris@1464
|
122 json = ActiveSupport::JSON.decode(response.body)
|
Chris@1464
|
123 assert_equal(
|
Chris@1464
|
124 {"membership" => {
|
Chris@1464
|
125 "id" => 2,
|
Chris@1464
|
126 "project" => {"name"=>"eCookbook", "id"=>1},
|
Chris@1464
|
127 "roles" => [{"name"=>"Developer", "id"=>2}],
|
Chris@1464
|
128 "user" => {"name"=>"Dave Lopper", "id"=>3}}
|
Chris@1464
|
129 },
|
Chris@1464
|
130 json)
|
Chris@1464
|
131 end
|
Chris@1464
|
132
|
Chris@1464
|
133 test "PUT /memberships/:id.xml should update the membership" do
|
Chris@1464
|
134 assert_not_equal [1,2], Member.find(2).role_ids.sort
|
Chris@1464
|
135 assert_no_difference 'Member.count' do
|
Chris@1464
|
136 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
|
Chris@1464
|
137
|
Chris@1464
|
138 assert_response :ok
|
Chris@1464
|
139 assert_equal '', @response.body
|
Chris@1115
|
140 end
|
Chris@1464
|
141 member = Member.find(2)
|
Chris@1464
|
142 assert_equal [1,2], member.role_ids.sort
|
Chris@1464
|
143 end
|
Chris@1115
|
144
|
Chris@1464
|
145 test "PUT /memberships/:id.xml with invalid parameters should return errors" do
|
Chris@1464
|
146 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
|
Chris@1115
|
147
|
Chris@1464
|
148 assert_response :unprocessable_entity
|
Chris@1464
|
149 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
150 assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
|
Chris@1464
|
151 end
|
Chris@1115
|
152
|
Chris@1464
|
153 test "DELETE /memberships/:id.xml should destroy the membership" do
|
Chris@1464
|
154 assert_difference 'Member.count', -1 do
|
Chris@1464
|
155 delete '/memberships/2.xml', {}, credentials('jsmith')
|
Chris@1115
|
156
|
Chris@1464
|
157 assert_response :ok
|
Chris@1464
|
158 assert_equal '', @response.body
|
Chris@1115
|
159 end
|
Chris@1464
|
160 assert_nil Member.find_by_id(2)
|
Chris@1464
|
161 end
|
Chris@1115
|
162
|
Chris@1464
|
163 test "DELETE /memberships/:id.xml should respond with 422 on failure" do
|
Chris@1464
|
164 assert_no_difference 'Member.count' do
|
Chris@1464
|
165 # A membership with an inherited role can't be deleted
|
Chris@1464
|
166 Member.find(2).member_roles.first.update_attribute :inherited_from, 99
|
Chris@1464
|
167 delete '/memberships/2.xml', {}, credentials('jsmith')
|
Chris@1115
|
168
|
Chris@1464
|
169 assert_response :unprocessable_entity
|
Chris@1115
|
170 end
|
Chris@1115
|
171 end
|
Chris@1115
|
172 end
|