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