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