##// END OF EJS Templates
Additional test for memberships API....
Jean-Philippe Lang -
r9313:67f90df175ba
parent child
Show More
@@ -1,190 +1,198
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 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 ApiTest::MembershipsTest < ActionController::IntegrationTest
20 class ApiTest::MembershipsTest < ActionController::IntegrationTest
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 context "/projects/:project_id/memberships" do
27 context "/projects/:project_id/memberships" do
28 context "GET" do
28 context "GET" do
29 context "xml" do
29 context "xml" do
30 should "return memberships" do
30 should "return memberships" do
31 get '/projects/1/memberships.xml', {}, credentials('jsmith')
31 get '/projects/1/memberships.xml', {}, credentials('jsmith')
32
32
33 assert_response :success
33 assert_response :success
34 assert_equal 'application/xml', @response.content_type
34 assert_equal 'application/xml', @response.content_type
35 assert_tag :tag => 'memberships',
35 assert_tag :tag => 'memberships',
36 :attributes => {:type => 'array'},
36 :attributes => {:type => 'array'},
37 :child => {
37 :child => {
38 :tag => 'membership',
38 :tag => 'membership',
39 :child => {
39 :child => {
40 :tag => 'id',
40 :tag => 'id',
41 :content => '2',
41 :content => '2',
42 :sibling => {
42 :sibling => {
43 :tag => 'user',
43 :tag => 'user',
44 :attributes => {:id => '3', :name => 'Dave Lopper'},
44 :attributes => {:id => '3', :name => 'Dave Lopper'},
45 :sibling => {
45 :sibling => {
46 :tag => 'roles',
46 :tag => 'roles',
47 :child => {
47 :child => {
48 :tag => 'role',
48 :tag => 'role',
49 :attributes => {:id => '2', :name => 'Developer'}
49 :attributes => {:id => '2', :name => 'Developer'}
50 }
50 }
51 }
51 }
52 }
52 }
53 }
53 }
54 }
54 }
55 end
55 end
56 end
56 end
57
57
58 context "json" do
58 context "json" do
59 should "return memberships" do
59 should "return memberships" do
60 get '/projects/1/memberships.json', {}, credentials('jsmith')
60 get '/projects/1/memberships.json', {}, credentials('jsmith')
61
61
62 assert_response :success
62 assert_response :success
63 assert_equal 'application/json', @response.content_type
63 assert_equal 'application/json', @response.content_type
64 json = ActiveSupport::JSON.decode(response.body)
64 json = ActiveSupport::JSON.decode(response.body)
65 assert_equal({
65 assert_equal({
66 "memberships" =>
66 "memberships" =>
67 [{"id"=>1,
67 [{"id"=>1,
68 "project" => {"name"=>"eCookbook", "id"=>1},
68 "project" => {"name"=>"eCookbook", "id"=>1},
69 "roles" => [{"name"=>"Manager", "id"=>1}],
69 "roles" => [{"name"=>"Manager", "id"=>1}],
70 "user" => {"name"=>"John Smith", "id"=>2}},
70 "user" => {"name"=>"John Smith", "id"=>2}},
71 {"id"=>2,
71 {"id"=>2,
72 "project" => {"name"=>"eCookbook", "id"=>1},
72 "project" => {"name"=>"eCookbook", "id"=>1},
73 "roles" => [{"name"=>"Developer", "id"=>2}],
73 "roles" => [{"name"=>"Developer", "id"=>2}],
74 "user" => {"name"=>"Dave Lopper", "id"=>3}}],
74 "user" => {"name"=>"Dave Lopper", "id"=>3}}],
75 "limit" => 25,
75 "limit" => 25,
76 "total_count" => 2,
76 "total_count" => 2,
77 "offset" => 0},
77 "offset" => 0},
78 json)
78 json)
79 end
79 end
80 end
80 end
81 end
81 end
82
82
83 context "POST" do
83 context "POST" do
84 context "xml" do
84 context "xml" do
85 should "create membership" do
85 should "create membership" do
86 assert_difference 'Member.count' do
86 assert_difference 'Member.count' do
87 post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
87 post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
88
88
89 assert_response :created
89 assert_response :created
90 end
90 end
91 end
91 end
92
92
93 should "return errors on failure" do
93 should "return errors on failure" do
94 assert_no_difference 'Member.count' do
94 assert_no_difference 'Member.count' do
95 post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
95 post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
96
96
97 assert_response :unprocessable_entity
97 assert_response :unprocessable_entity
98 assert_equal 'application/xml', @response.content_type
98 assert_equal 'application/xml', @response.content_type
99 assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
99 assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
100 end
100 end
101 end
101 end
102 end
102 end
103 end
103 end
104 end
104 end
105
105
106 context "/memberships/:id" do
106 context "/memberships/:id" do
107 context "GET" do
107 context "GET" do
108 context "xml" do
108 context "xml" do
109 should "return the membership" do
109 should "return the membership" do
110 get '/memberships/2.xml', {}, credentials('jsmith')
110 get '/memberships/2.xml', {}, credentials('jsmith')
111
111
112 assert_response :success
112 assert_response :success
113 assert_equal 'application/xml', @response.content_type
113 assert_equal 'application/xml', @response.content_type
114 assert_tag :tag => 'membership',
114 assert_tag :tag => 'membership',
115 :child => {
115 :child => {
116 :tag => 'id',
116 :tag => 'id',
117 :content => '2',
117 :content => '2',
118 :sibling => {
118 :sibling => {
119 :tag => 'user',
119 :tag => 'user',
120 :attributes => {:id => '3', :name => 'Dave Lopper'},
120 :attributes => {:id => '3', :name => 'Dave Lopper'},
121 :sibling => {
121 :sibling => {
122 :tag => 'roles',
122 :tag => 'roles',
123 :child => {
123 :child => {
124 :tag => 'role',
124 :tag => 'role',
125 :attributes => {:id => '2', :name => 'Developer'}
125 :attributes => {:id => '2', :name => 'Developer'}
126 }
126 }
127 }
127 }
128 }
128 }
129 }
129 }
130 end
130 end
131 end
131 end
132
132
133 context "json" do
133 context "json" do
134 should "return the membership" do
134 should "return the membership" do
135 get '/memberships/2.json', {}, credentials('jsmith')
135 get '/memberships/2.json', {}, credentials('jsmith')
136
136
137 assert_response :success
137 assert_response :success
138 assert_equal 'application/json', @response.content_type
138 assert_equal 'application/json', @response.content_type
139 json = ActiveSupport::JSON.decode(response.body)
139 json = ActiveSupport::JSON.decode(response.body)
140 assert_equal(
140 assert_equal(
141 {"membership" => {
141 {"membership" => {
142 "id" => 2,
142 "id" => 2,
143 "project" => {"name"=>"eCookbook", "id"=>1},
143 "project" => {"name"=>"eCookbook", "id"=>1},
144 "roles" => [{"name"=>"Developer", "id"=>2}],
144 "roles" => [{"name"=>"Developer", "id"=>2}],
145 "user" => {"name"=>"Dave Lopper", "id"=>3}}
145 "user" => {"name"=>"Dave Lopper", "id"=>3}}
146 },
146 },
147 json)
147 json)
148 end
148 end
149 end
149 end
150 end
150 end
151
151
152 context "PUT" do
152 context "PUT" do
153 context "xml" do
153 context "xml" do
154 should "update membership" do
154 should "update membership" do
155 assert_not_equal [1,2], Member.find(2).role_ids.sort
155 assert_not_equal [1,2], Member.find(2).role_ids.sort
156 assert_no_difference 'Member.count' do
156 assert_no_difference 'Member.count' do
157 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
157 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
158
158
159 assert_response :ok
159 assert_response :ok
160 end
160 end
161 member = Member.find(2)
161 member = Member.find(2)
162 assert_equal [1,2], member.role_ids.sort
162 assert_equal [1,2], member.role_ids.sort
163 end
163 end
164
165 should "return errors on failure" do
166 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
167
168 assert_response :unprocessable_entity
169 assert_equal 'application/xml', @response.content_type
170 assert_tag 'errors', :child => {:tag => 'error', :content => "member_roles is invalid"}
171 end
164 end
172 end
165 end
173 end
166
174
167 context "DELETE" do
175 context "DELETE" do
168 context "xml" do
176 context "xml" do
169 should "destroy membership" do
177 should "destroy membership" do
170 assert_difference 'Member.count', -1 do
178 assert_difference 'Member.count', -1 do
171 delete '/memberships/2.xml', {}, credentials('jsmith')
179 delete '/memberships/2.xml', {}, credentials('jsmith')
172
180
173 assert_response :ok
181 assert_response :ok
174 end
182 end
175 assert_nil Member.find_by_id(2)
183 assert_nil Member.find_by_id(2)
176 end
184 end
177
185
178 should "respond with 422 on failure" do
186 should "respond with 422 on failure" do
179 assert_no_difference 'Member.count' do
187 assert_no_difference 'Member.count' do
180 # A membership with an inherited role can't be deleted
188 # A membership with an inherited role can't be deleted
181 Member.find(2).member_roles.first.update_attribute :inherited_from, 99
189 Member.find(2).member_roles.first.update_attribute :inherited_from, 99
182 delete '/memberships/2.xml', {}, credentials('jsmith')
190 delete '/memberships/2.xml', {}, credentials('jsmith')
183
191
184 assert_response :unprocessable_entity
192 assert_response :unprocessable_entity
185 end
193 end
186 end
194 end
187 end
195 end
188 end
196 end
189 end
197 end
190 end
198 end
General Comments 0
You need to be logged in to leave comments. Login now