|
7 | 7 | let(:test_site_id) { '12345678' } |
8 | 8 | let(:test_post_id) { 98_765 } |
9 | 9 | let(:second_post_id) { 54_321 } |
10 | | - let(:api_url) { "https://public-api.wordpress.com/wp/v2/sites/#{test_site_id}/a8c_cdn_build/#{test_post_id}" } |
11 | | - let(:second_api_url) { "https://public-api.wordpress.com/wp/v2/sites/#{test_site_id}/a8c_cdn_build/#{second_post_id}" } |
12 | | - let(:visibility_term_url) { "https://public-api.wordpress.com/wp/v2/sites/#{test_site_id}/visibility" } |
| 10 | + let(:api_url) { "https://public-api.wordpress.com/wpcom/v2/sites/#{test_site_id}/a8c-cdn/builds/#{test_post_id}" } |
| 11 | + let(:second_api_url) { "https://public-api.wordpress.com/wpcom/v2/sites/#{test_site_id}/a8c-cdn/builds/#{second_post_id}" } |
13 | 12 | let(:test_api_token) { 'test_api_token' } |
14 | 13 |
|
15 | | - let(:external_term_id) { 21_293 } |
16 | | - let(:internal_term_id) { 1_316 } |
17 | | - |
18 | 14 | let(:stub_success_response) do |
19 | 15 | { |
20 | 16 | id: test_post_id, |
21 | | - title: { rendered: 'WordPress.com Studio 1.7.5' }, |
22 | | - status: 'publish', |
23 | | - visibility: [external_term_id], |
24 | | - class_list: ['visibility-external'] |
| 17 | + post_status: 'publish', |
| 18 | + release_notes: 'Release 1.7.5', |
| 19 | + visibility: 'External', |
| 20 | + product: 'WordPress.com Studio' |
25 | 21 | }.to_json |
26 | 22 | end |
27 | 23 |
|
|
31 | 27 |
|
32 | 28 | describe 'updating visibility' do |
33 | 29 | it 'successfully updates the visibility to external' do |
34 | | - stub_request(:get, visibility_term_url) |
35 | | - .with(query: { 'slug' => 'external' }) |
36 | | - .to_return( |
37 | | - status: 200, |
38 | | - body: [{ 'id' => external_term_id, 'name' => 'External', 'slug' => 'external' }].to_json, |
39 | | - headers: { 'Content-Type' => 'application/json' } |
40 | | - ) |
41 | | - |
42 | 30 | stub_request(:post, api_url) |
43 | 31 | .to_return( |
44 | 32 | status: 200, |
|
62 | 50 | expect(req.headers['Authorization']).to eq("Bearer #{test_api_token}") |
63 | 51 | expect(req.headers['Content-Type']).to eq('application/json') |
64 | 52 | body = JSON.parse(req.body) |
65 | | - expect(body['visibility']).to eq([external_term_id]) |
| 53 | + expect(body['visibility']).to eq('External') |
66 | 54 | true |
67 | 55 | end |
68 | 56 | ) |
69 | 57 | end |
70 | 58 |
|
71 | 59 | it 'successfully updates the visibility to internal' do |
72 | | - stub_request(:get, visibility_term_url) |
73 | | - .with(query: { 'slug' => 'internal' }) |
74 | | - .to_return( |
75 | | - status: 200, |
76 | | - body: [{ 'id' => internal_term_id, 'name' => 'Internal', 'slug' => 'internal' }].to_json, |
77 | | - headers: { 'Content-Type' => 'application/json' } |
78 | | - ) |
79 | | - |
80 | 60 | internal_response = { |
81 | 61 | id: test_post_id, |
82 | | - visibility: [internal_term_id], |
83 | | - class_list: ['visibility-internal'] |
| 62 | + post_status: 'publish', |
| 63 | + visibility: 'Internal' |
84 | 64 | }.to_json |
85 | 65 |
|
86 | 66 | stub_request(:post, api_url) |
|
102 | 82 | expect(WebMock).to( |
103 | 83 | have_requested(:post, api_url).with do |req| |
104 | 84 | body = JSON.parse(req.body) |
105 | | - expect(body['visibility']).to eq([internal_term_id]) |
| 85 | + expect(body['visibility']).to eq('Internal') |
106 | 86 | true |
107 | 87 | end |
108 | 88 | ) |
109 | 89 | end |
110 | 90 | end |
111 | 91 |
|
112 | 92 | describe 'batch updating multiple posts' do |
113 | | - it 'updates all posts with a single visibility term lookup' do |
114 | | - stub_request(:get, visibility_term_url) |
115 | | - .with(query: { 'slug' => 'external' }) |
116 | | - .to_return( |
117 | | - status: 200, |
118 | | - body: [{ 'id' => external_term_id, 'name' => 'External', 'slug' => 'external' }].to_json, |
119 | | - headers: { 'Content-Type' => 'application/json' } |
120 | | - ) |
121 | | - |
| 93 | + it 'updates each post with a single request' do |
122 | 94 | stub_request(:post, api_url) |
123 | 95 | .to_return( |
124 | 96 | status: 200, |
|
143 | 115 | expect(results.size).to eq(2) |
144 | 116 | expect(results).to eq([test_post_id, second_post_id]) |
145 | 117 |
|
146 | | - # Visibility term lookup should have been called only once |
147 | | - expect(WebMock).to have_requested(:get, visibility_term_url).with(query: { 'slug' => 'external' }).once |
148 | 118 | expect(WebMock).to have_requested(:post, api_url).once |
149 | 119 | expect(WebMock).to have_requested(:post, second_api_url).once |
150 | 120 | end |
|
155 | 125 | stub_request(:post, api_url) |
156 | 126 | .to_return( |
157 | 127 | status: 200, |
158 | | - body: { id: test_post_id, status: 'draft' }.to_json, |
| 128 | + body: { id: test_post_id, post_status: 'draft' }.to_json, |
159 | 129 | headers: { 'Content-Type' => 'application/json' } |
160 | 130 | ) |
161 | 131 |
|
|
171 | 141 | expect(WebMock).to( |
172 | 142 | have_requested(:post, api_url).with do |req| |
173 | 143 | body = JSON.parse(req.body) |
174 | | - expect(body['status']).to eq('draft') |
| 144 | + expect(body['post_status']).to eq('draft') |
175 | 145 | true |
176 | 146 | end |
177 | 147 | ) |
|
180 | 150 |
|
181 | 151 | describe 'updating multiple fields' do |
182 | 152 | it 'successfully updates both visibility and post_status' do |
183 | | - stub_request(:get, visibility_term_url) |
184 | | - .with(query: { 'slug' => 'external' }) |
185 | | - .to_return( |
186 | | - status: 200, |
187 | | - body: [{ 'id' => external_term_id, 'name' => 'External', 'slug' => 'external' }].to_json, |
188 | | - headers: { 'Content-Type' => 'application/json' } |
189 | | - ) |
190 | | - |
191 | 153 | stub_request(:post, api_url) |
192 | 154 | .to_return( |
193 | 155 | status: 200, |
|
208 | 170 | expect(WebMock).to( |
209 | 171 | have_requested(:post, api_url).with do |req| |
210 | 172 | body = JSON.parse(req.body) |
211 | | - expect(body['visibility']).to eq([external_term_id]) |
212 | | - expect(body['status']).to eq('publish') |
| 173 | + expect(body['visibility']).to eq('External') |
| 174 | + expect(body['post_status']).to eq('publish') |
213 | 175 | true |
214 | 176 | end |
215 | 177 | ) |
|
221 | 183 | stub_request(:post, api_url) |
222 | 184 | .to_return( |
223 | 185 | status: 403, |
224 | | - body: { code: 'rest_forbidden', message: 'You are not authorized.' }.to_json, |
| 186 | + body: { code: 'rest_forbidden', message: 'You do not have permission to update builds.' }.to_json, |
225 | 187 | headers: { 'Content-Type' => 'application/json' } |
226 | 188 | ) |
227 | 189 |
|
|
235 | 197 | end.to raise_error(FastlaneCore::Interface::FastlaneError, "Update of Apps CDN build metadata failed for post #{test_post_id}") |
236 | 198 | end |
237 | 199 |
|
238 | | - it 'handles server errors properly' do |
| 200 | + it 'handles a non-existent build post properly' do |
239 | 201 | stub_request(:post, api_url) |
240 | 202 | .to_return( |
241 | | - status: 500, |
242 | | - body: 'Internal Server Error', |
243 | | - headers: { 'Content-Type' => 'text/plain' } |
| 203 | + status: 404, |
| 204 | + body: { code: 'rest_build_not_found', message: 'Build not found.' }.to_json, |
| 205 | + headers: { 'Content-Type' => 'application/json' } |
244 | 206 | ) |
245 | 207 |
|
246 | 208 | expect do |
247 | 209 | run_described_fastlane_action( |
248 | 210 | site_id: test_site_id, |
249 | 211 | api_token: test_api_token, |
250 | 212 | post_ids: [test_post_id], |
251 | | - post_status: 'publish' |
| 213 | + visibility: :external |
252 | 214 | ) |
253 | 215 | end.to raise_error(FastlaneCore::Interface::FastlaneError, "Update of Apps CDN build metadata failed for post #{test_post_id}") |
254 | 216 | end |
255 | 217 |
|
256 | | - it 'handles visibility term lookup failure' do |
257 | | - stub_request(:get, visibility_term_url) |
258 | | - .with(query: { 'slug' => 'external' }) |
| 218 | + it 'handles server errors properly' do |
| 219 | + stub_request(:post, api_url) |
259 | 220 | .to_return( |
260 | | - status: 200, |
261 | | - body: [].to_json, |
262 | | - headers: { 'Content-Type' => 'application/json' } |
| 221 | + status: 500, |
| 222 | + body: 'Internal Server Error', |
| 223 | + headers: { 'Content-Type' => 'text/plain' } |
263 | 224 | ) |
264 | 225 |
|
265 | 226 | expect do |
266 | 227 | run_described_fastlane_action( |
267 | 228 | site_id: test_site_id, |
268 | 229 | api_token: test_api_token, |
269 | 230 | post_ids: [test_post_id], |
270 | | - visibility: :external |
| 231 | + post_status: 'publish' |
271 | 232 | ) |
272 | | - end.to raise_error(FastlaneCore::Interface::FastlaneError, "No visibility term found for 'external'") |
| 233 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, "Update of Apps CDN build metadata failed for post #{test_post_id}") |
273 | 234 | end |
274 | 235 | end |
275 | 236 |
|
|
0 commit comments