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