Debian Codegraph @

tests/api-testing/REST/content.v1/Page.js

raw symbols SCIP
1 'use strict';
2
3 const { action, assert, REST, utils } = require( 'api-testing' );
4 const url = require( 'url' );
5
6 let pathPrefix = 'rest.php/content/v1';
7
8 // Parse a URL-ref, which may or may not contain a protocol and host.
9 // WHATWG URL currently doesn't support partial URLs, see https://github.com/whatwg/url/issues/531
10 function parseURL( ref ) {
11 const urlObj = new url.URL( ref, 'http://fake-host' );
12 const urlRec = {
13 protocol: urlObj.protocol,
14 host: urlObj.host,
15 hostname: urlObj.hostname,
16 port: urlObj.port,
17 pathname: urlObj.pathname,
18 search: urlObj.search,
19 hash: urlObj.hash
20 };
21
22 if ( urlRec.hostname === 'fake-host' ) {
23 urlRec.host = '';
24 urlRec.hostname = '';
25 urlRec.port = '';
26 }
27
28 return urlRec;
29 }
30
31 describe( 'Page Source', () => {
32 const page = utils.title( 'PageSource_' );
33 const pageWithSpaces = page.replace( '_', ' ' );
34 const variantPage = utils.title( 'PageSourceVariant' );
35 const fallbackVariantPage = 'MediaWiki:Tog-underline/sh-latn';
36
37 // Create a page (or "agepay") for the pig latin variant test.
38 const agepayHash = utils.title( '' ).replace( /\d/g, 'x' ).toLowerCase(); // only lower-case letters.
39 const agepay = 'Page' + agepayHash; // will not exist
40 const atinlayAgepay = 'Age' + agepayHash + 'pay'; // will exist
41
42 const redirectPage = utils.title( 'Redirect ' );
43 const redirectedPage = redirectPage.replace( 'Redirect', 'Redirected' );
44
45 let client;
46 let mindy;
47 const baseEditText = "''Edit 1'' and '''Edit 2'''";
48
49 before( async () => {
50 client = new REST( pathPrefix );
51
52 mindy = await action.mindy();
53 await mindy.edit( page, { text: baseEditText } );
54 await mindy.edit( atinlayAgepay, { text: baseEditText } );
55
56 // Setup page with redirects
57 await mindy.edit( redirectPage, { text: `Original name is ${ redirectPage }` } );
58 const token = await mindy.token();
59 await mindy.action( 'move', {
60 from: redirectPage,
61 to: redirectedPage,
62 token
63 }, true );
64 } );
65
66 describe( 'GET /page/{title}', () => {
67 it( 'Title normalization should return permanent redirect (301)', async () => {
68 const redirectDbKey = utils.dbkey( redirectPage );
69 const { status, text, headers } = await client.get( `/page/${ redirectPage }`, { flavor: 'edit' } );
70 const { host, search, pathname } = parseURL( headers.location );
71 assert.include( search, 'flavor=edit' );
72 assert.deepEqual( host, '' );
73 assert.include( pathname, `/page/${ redirectDbKey }` );
74 assert.deepEqual( status, 301, text );
75 } );
76
77 it( 'When a wiki redirect exists, it should be present in the body response', async () => {
78 const redirectPageDbkey = utils.dbkey( redirectPage );
79 const redirectedPageDbKey = utils.dbkey( redirectedPage );
80 const { status, body: { redirect_target }, text, headers } =
81 await client.get( `/page/${ redirectPageDbkey }` );
82 assert.deepEqual( status, 200, text );
83 assert.match( headers[ 'content-type' ], /^application\/json/ );
84 assert.match( redirect_target, new RegExp( `/page/${ encodeURIComponent( redirectedPageDbKey ) }$` ) );
85 } );
86
87 it( 'Should successfully return page source and metadata for Wikitext page', async () => {
88 const { status, body, text, headers } = await client.get( `/page/${ page }` );
89 assert.deepEqual( status, 200, text );
90 assert.match( headers[ 'content-type' ], /^application\/json/ );
91 assert.match( headers.vary, /\bx-restbase-compat\b/ );
92 assert.containsAllKeys( body, [ 'latest', 'id', 'key', 'license', 'title', 'content_model', 'source' ] );
93 assert.nestedPropertyVal( body, 'content_model', 'wikitext' );
94 assert.nestedPropertyVal( body, 'title', pageWithSpaces );
95 assert.nestedPropertyVal( body, 'key', utils.dbkey( page ) );
96 assert.nestedPropertyVal( body, 'source', baseEditText );
97 } );
98 it( 'Should return 404 error for non-existent page', async () => {
99 const dummyPageTitle = utils.title( 'DummyPage_' );
100 const { status } = await client.get( `/page/${ dummyPageTitle }` );
101 assert.deepEqual( status, 404 );
102 } );
103 it( 'Should return 404 error for invalid titles', async () => {
104 const badTitle = '::X::';
105 const { status } = await client.get( `/page/${ badTitle }` );
106 assert.deepEqual( status, 404 );
107 } );
108 it( 'Should return 404 error for special pages', async () => {
109 const badTitle = 'Special:Blankpage';
110 const { status } = await client.get( `/page/${ badTitle }` );
111 assert.deepEqual( status, 404 );
112 } );
113 it( 'Should have appropriate response headers', async () => {
114 const preEditResponse = await client.get( `/page/${ page }` );
115 const preEditDate = new Date( preEditResponse.body.latest.timestamp );
116 const preEditEtag = preEditResponse.headers.etag;
117
118 await mindy.edit( page, { text: "'''Edit 3'''" } );
119 const postEditResponse = await client.get( `/page/${ page }` );
120 const postEditDate = new Date( postEditResponse.body.latest.timestamp );
121 const postEditHeaders = postEditResponse.headers;
122 const postEditEtag = postEditResponse.headers.etag;
123
124 assert.containsAllKeys( postEditHeaders, [ 'etag' ] );
125 assert.deepEqual( postEditHeaders[ 'last-modified' ], postEditDate.toGMTString() );
126 assert.match( postEditHeaders[ 'cache-control' ], /^max-age=\d/ );
127 assert.strictEqual( isNaN( preEditDate.getTime() ), false );
128 assert.strictEqual( isNaN( postEditDate.getTime() ), false );
129 assert.notEqual( preEditDate, postEditDate );
130 assert.notEqual( preEditEtag, postEditEtag );
131 } );
132 } );
133
134 describe( 'GET /page/{title}/bare', () => {
135 it( 'Title normalization should return permanent redirect (301)', async () => {
136 const { status, text, headers } = await client.get( `/page/${ redirectPage }/bare`, { flavor: 'edit' } );
137 const { search } = parseURL( headers.location );
138 assert.include( search, 'flavor=edit' );
139 assert.deepEqual( status, 301, text );
140 } );
141
142 it( 'When a wiki redirect exists, it should be present in the body response', async () => {
143 const redirectPageDbkey = utils.dbkey( redirectPage );
144 const redirectedPageDbKey = utils.dbkey( redirectedPage );
145 const { status, body: { redirect_target }, text, headers } =
146 await client.get( `/page/${ redirectPageDbkey }/bare` );
147 assert.deepEqual( status, 200, text );
148 assert.match( headers[ 'content-type' ], /^application\/json/ );
149 assert.match( redirect_target, new RegExp( `/page/${ encodeURIComponent( redirectedPageDbKey ) }/bare$` ) );
150 } );
151
152 it( 'Should successfully return page bare', async () => {
153 const { status, body, text, headers } = await client.get( `/page/${ page }/bare` );
154 assert.deepEqual( status, 200, text );
155 assert.match( headers[ 'content-type' ], /^application\/json/ );
156 assert.containsAllKeys( body, [ 'latest', 'id', 'key', 'license', 'title', 'content_model', 'html_url' ] );
157 assert.nestedPropertyVal( body, 'content_model', 'wikitext' );
158 assert.nestedPropertyVal( body, 'title', pageWithSpaces );
159 assert.nestedPropertyVal( body, 'key', utils.dbkey( page ) );
160 assert.match( body.html_url, new RegExp( `/page/${ encodeURIComponent( pageWithSpaces ) }/html$` ) );
161 } );
162 it( 'Should return 404 error for non-existent page, even if a variant exists', async () => {
163 const agepayDbkey = utils.dbkey( agepay );
164 const { status } = await client.get( `/page/${ agepayDbkey }/bare` );
165 assert.deepEqual( status, 404 );
166 } );
167 it( 'Should have appropriate response headers', async () => {
168 const preEditResponse = await client.get( `/page/${ page }/bare` );
169 const preEditDate = new Date( preEditResponse.body.latest.timestamp );
170 const preEditEtag = preEditResponse.headers.etag;
171
172 await mindy.edit( page, { text: "'''Edit 4'''" } );
173 const postEditResponse = await client.get( `/page/${ page }/bare` );
174 const postEditDate = new Date( postEditResponse.body.latest.timestamp );
175 const postEditHeaders = postEditResponse.headers;
176 const postEditEtag = postEditResponse.headers.etag;
177
178 assert.containsAllKeys( postEditHeaders, [ 'etag' ] );
179 assert.deepEqual( postEditHeaders[ 'last-modified' ], postEditDate.toGMTString() );
180 assert.match( postEditHeaders[ 'cache-control' ], /^max-age=\d/ );
181 assert.strictEqual( isNaN( preEditDate.getTime() ), false );
182 assert.strictEqual( isNaN( postEditDate.getTime() ), false );
183 assert.notEqual( preEditDate, postEditDate );
184 assert.notEqual( preEditEtag, postEditEtag );
185 } );
186 } );
187
188 describe( 'GET /page/{title}/bare with x-restbase-compat', () => {
189 it( 'Should successfully return restbase-compatible revision meta-data', async () => {
190 const { status, body, text, headers } = await client
191 .get( `/page/${ page }/bare` )
192 .set( 'x-restbase-compat', 'true' );
193
194 assert.deepEqual( status, 200, text );
195 assert.match( headers[ 'content-type' ], /^application\/json/ );
196 assert.match( headers.vary, /\bx-restbase-compat\b/ );
197 assert.containsAllKeys( body.items[ 0 ], [ 'title', 'page_id', 'rev', 'tid', 'namespace', 'user_id',
198 'user_text', 'timestamp', 'comment', 'tags', 'restrictions', 'page_language', 'redirect' ] );
199
200 assert.deepEqual( body.items[ 0 ].title, utils.dbkey( page ) );
201 assert.isAbove( body.items[ 0 ].page_id, 0 );
202 assert.isAbove( body.items[ 0 ].rev, 0 );
203 } );
204
205 it( 'Should successfully return restbase-compatible errors', async () => {
206 const dummyPageTitle = utils.title( 'DummyPage_' );
207 const { status, body, text, headers } = await client
208 .get( `/page/${ dummyPageTitle }/bare` )
209 .set( 'x-restbase-compat', 'true' );
210
211 assert.deepEqual( status, 404, text );
212 assert.match( headers[ 'content-type' ], /^application\/json/ );
213 assert.containsAllKeys( body, [ 'type', 'title', 'method', 'detail', 'uri' ] );
214 } );
215 } );
216
217 describe( 'GET /page/{title}/html', () => {
218 it( 'Title normalization should return permanent redirect (301)', async () => {
219 const { status, text, headers } = await client.get( `/page/${ redirectPage }/html`, { flavor: 'edit' } );
220 const { search } = parseURL( headers.location );
221 assert.include( search, 'flavor=edit' );
222 assert.deepEqual( status, 301, text );
223 } );
224
225 it( 'Wiki redirects should return temporary redirect (307)', async () => {
226 const redirectPageDbkey = utils.dbkey( redirectPage );
227 const redirectedPageDbkey = utils.dbkey( redirectedPage );
228 const { status, text, headers } = await client.get( `/page/${ redirectPageDbkey }/html`, { flavor: 'edit' } );
229 const { host, pathname, search } = parseURL( headers.location );
230 assert.include( search, 'flavor=edit' );
231 assert.include( pathname, `/page/${ redirectedPageDbkey }` );
232 assert.deepEqual( host, '' );
233 assert.deepEqual( status, 307, text );
234 } );
235
236 it( 'Variant redirects should return temporary redirect (307)', async () => {
237 const agepayDbkey = utils.dbkey( agepay );
238 const atinlayAgepayDbkey = utils.dbkey( atinlayAgepay );
239 const { status, text, headers } = await client.get( `/page/${ agepayDbkey }/html` );
240 assert.deepEqual( status, 307, text );
241 assert.include( headers.location, atinlayAgepayDbkey );
242 } );
243
244 it( 'Bypass wiki redirects with query param redirect=no', async () => {
245 const redirectPageDbkey = utils.dbkey( redirectPage );
246 const { status, text, headers } = await client.get(
247 `/page/${ redirectPageDbkey }/html`,
248 { redirect: 'no' }
249 );
250 assert.deepEqual( status, 200, text );
251 assert.match( headers[ 'content-type' ], /^text\/html/ );
252 } );
253
254 it( 'Bypass variant redirects with query param redirect=no', async () => {
255 const agepayDbkey = utils.dbkey( agepay );
256 const { status, headers } = await client.get(
257 `/page/${ agepayDbkey }/html`,
258 { redirect: 'no' }
259 );
260 assert.deepEqual( status, 404 );
261 // rest-nonexistent-title error object returned instead of HTML
262 assert.match( headers[ 'content-type' ], /^application\/json/ );
263 } );
264
265 it( 'Should successfully return page HTML', async () => {
266 const { status, headers, text } = await client.get( `/page/${ page }/html` );
267 assert.deepEqual( status, 200, text );
268 assert.match( headers[ 'content-type' ], /^text\/html/ );
269 assert.match( text, /<html\b/ );
270 assert.match( text, /Edit \w+<\/b>/ );
271 } );
272 it( 'Should successfully return page HTML for a system message', async () => {
273 const msg = 'MediaWiki:Newpage-desc';
274 const { status, headers, text } = await client.get( `/page/${ msg }/html` );
275 assert.deepEqual( status, 200, text );
276 assert.match( headers[ 'content-type' ], /^text\/html/ );
277 assert.match( text, /<html\b/ );
278 assert.match( text, /Start a new page/ );
279 } );
280 it( 'Should return 404 error for non-existent page', async () => {
281 const dummyPageTitle = utils.title( 'DummyPage_' );
282 const { status } = await client.get( `/page/${ dummyPageTitle }/html` );
283 assert.deepEqual( status, 404 );
284 } );
285 it( 'Should have appropriate response headers', async () => {
286 const preEditResponse = await client.get( `/page/${ page }/html` );
287 const preEditDate = new Date( preEditResponse.headers[ 'last-modified' ] );
288 const preEditEtag = preEditResponse.headers.etag;
289
290 await mindy.edit( page, { text: "'''Edit XYZ'''" } );
291 const postEditResponse = await client.get( `/page/${ page }/html` );
292 const postEditDate = new Date( postEditResponse.headers[ 'last-modified' ] );
293 const postEditHeaders = postEditResponse.headers;
294 const postEditEtag = postEditResponse.headers.etag;
295
296 assert.containsAllKeys( postEditHeaders, [ 'etag', 'cache-control', 'last-modified' ] );
297 assert.match( postEditHeaders[ 'cache-control' ], /^max-age=\d/ );
298 assert.strictEqual( isNaN( preEditDate.getTime() ), false );
299 assert.strictEqual( isNaN( postEditDate.getTime() ), false );
300 assert.notEqual( preEditDate, postEditDate );
301 assert.notEqual( preEditEtag, postEditEtag );
302 assert.match( postEditHeaders.etag, /^".*"$/, 'ETag must be present and not marked weak' );
303 } );
304 it( 'Should perform variant conversion', async () => {
305 await mindy.edit( variantPage, { text: '<p>test language conversion</p>' } );
306 const { headers, text } = await client.get( `/page/${ variantPage }/html`, null, {
307 'accept-language': 'en-x-piglatin'
308 } );
309
310 assert.match( text, /esttay anguagelay onversioncay/ );
311 assert.match( headers[ 'content-type' ], /^text\/html/ );
312 assert.match( headers.vary, /\bAccept-Language\b/i );
313 assert.match( headers[ 'content-language' ], /en-x-piglatin/i );
314 assert.match( headers.etag, /en-x-piglatin/i );
315 } );
316 it( 'Should perform fallback variant conversion', async () => {
317 await mindy.edit( fallbackVariantPage, { text: 'Podvlačenje linkova:' } );
318 const { headers, text } = await client.get( `/page/${ encodeURIComponent( fallbackVariantPage ) }/html`, null, {
319 'accept-language': 'sh-cyrl'
320 } );
321
322 assert.match( text, /Подвлачење линкова:/ );
323 assert.match( headers[ 'content-type' ], /^text\/html/ );
324 assert.match( headers.vary, /\bAccept-Language\b/i );
325 assert.match( headers[ 'content-language' ], /sh-cyrl/i );
326 assert.match( headers.etag, /sh-cyrl/i );
327 } );
328 } );
329
330 describe( 'GET /page/{title}/html with x-restbase-compat', () => {
331 it( 'Should successfully return restbase-compatible errors', async () => {
332 const dummyPageTitle = utils.title( 'DummyPage_' );
333 const { status, body, text, headers } = await client
334 .get( `/page/${ dummyPageTitle }/html` )
335 .set( 'x-restbase-compat', 'true' );
336
337 assert.deepEqual( status, 404, text );
338 assert.match( headers[ 'content-type' ], /^application\/json/ );
339 assert.containsAllKeys( body, [ 'type', 'title', 'method', 'detail', 'uri' ] );
340 } );
341 } );
342
343 describe( 'GET /page/{title}/with_html', () => {
344 it( 'Title normalization should return permanent redirect (301)', async () => {
345 const { status, text, headers } = await client.get( `/page/${ redirectPage }/with_html`, { flavor: 'edit' } );
346 const { search } = parseURL( headers.location );
347 assert.include( search, 'flavor=edit' );
348 assert.deepEqual( status, 301, text );
349 } );
350
351 it( 'Wiki redirects should return temporary redirect (307)', async () => {
352 const redirectPageDbkey = utils.dbkey( redirectPage );
353 const { status, text, headers } = await client.get( `/page/${ redirectPageDbkey }/with_html`, { flavor: 'edit' } );
354 const { search } = parseURL( headers.location );
355 assert.include( search, 'flavor=edit' );
356 assert.deepEqual( status, 307, text );
357 } );
358
359 it( 'Bypass redirects with query param redirect=no', async () => {
360 const redirectPageDbkey = utils.dbkey( redirectPage );
361 const redirectedPageDbKey = utils.dbkey( redirectedPage );
362 const { status, body: { redirect_target }, text, headers } = await client.get(
363 `/page/${ redirectPageDbkey }/with_html`,
364 { redirect: 'no' }
365 );
366 assert.match( redirect_target, new RegExp( `/page/${ encodeURIComponent( redirectedPageDbKey ) }/with_html` ) );
367 assert.deepEqual( status, 200, text );
368 assert.match( headers[ 'content-type' ], /^application\/json/ );
369 } );
370
371 it( 'Should successfully return page HTML and metadata for Wikitext page', async () => {
372 const { status, body, text, headers } = await client.get( `/page/${ page }/with_html` );
373 assert.deepEqual( status, 200, text );
374 assert.match( headers[ 'content-type' ], /^application\/json/ );
375 assert.containsAllKeys( body, [ 'latest', 'id', 'key', 'license', 'title', 'content_model', 'html' ] );
376 assert.nestedPropertyVal( body, 'content_model', 'wikitext' );
377 assert.nestedPropertyVal( body, 'title', pageWithSpaces );
378 assert.nestedPropertyVal( body, 'key', utils.dbkey( page ) );
379 assert.match( body.html, /<html\b/ );
380 assert.match( body.html, /Edit \w+<\/b>/ );
381 } );
382 it( 'Should successfully return page HTML and metadata for a system message', async () => {
383 const msg = 'MediaWiki:Newpage-desc';
384 const { status, body, text, headers } = await client.get( `/page/${ msg }/with_html` );
385 assert.deepEqual( status, 200, text );
386 assert.match( headers[ 'content-type' ], /^application\/json/ );
387 assert.containsAllKeys( body, [ 'latest', 'id', 'key', 'license', 'title', 'content_model', 'html' ] );
388 assert.nestedPropertyVal( body, 'content_model', 'wikitext' );
389 assert.nestedPropertyVal( body, 'title', msg );
390 assert.nestedPropertyVal( body, 'key', utils.dbkey( msg ) );
391 assert.nestedPropertyVal( body, 'id', 0 );
392 assert.nestedPropertyVal( body.latest, 'id', 0 );
393 assert.match( body.html, /<html\b/ );
394 assert.match( body.html, /Start a new page/ );
395 } );
396 it( 'Should return 404 error for non-existent page', async () => {
397 const dummyPageTitle = utils.title( 'DummyPage_' );
398 const { status } = await client.get( `/page/${ dummyPageTitle }/with_html` );
399 assert.deepEqual( status, 404 );
400 } );
401 it( 'Should have appropriate response headers', async () => {
402 const preEditResponse = await client.get( `/page/${ page }/with_html` );
403 const preEditRevDate = new Date( preEditResponse.body.latest.timestamp );
404 const preEditEtag = preEditResponse.headers.etag;
405
406 await mindy.edit( page, { text: "'''Edit ABCD'''" } );
407 const postEditResponse = await client.get( `/page/${ page }/with_html` );
408 const postEditRevDate = new Date( postEditResponse.body.latest.timestamp );
409 const postEditHeaders = postEditResponse.headers;
410 const postEditEtag = postEditResponse.headers.etag;
411
412 assert.containsAllKeys( postEditHeaders, [ 'etag', 'last-modified' ] );
413 const postEditHeaderDate = new Date( postEditHeaders[ 'last-modified' ] );
414
415 // The last-modified date is the render timestamp, which may be newer than the revision
416 assert.strictEqual( postEditRevDate.valueOf() <= postEditHeaderDate.valueOf(), true );
417 assert.match( postEditHeaders[ 'cache-control' ], /^max-age=\d/ );
418 assert.strictEqual( isNaN( preEditRevDate.getTime() ), false );
419 assert.strictEqual( isNaN( postEditRevDate.getTime() ), false );
420 assert.notEqual( preEditRevDate, postEditRevDate );
421 assert.notEqual( preEditEtag, postEditEtag );
422 } );
423 it( 'Should perform variant conversion', async () => {
424 await mindy.edit( variantPage, { text: '<p>test language conversion</p>' } );
425 const { headers, text } = await client.get( `/page/${ variantPage }/html`, null, {
426 'accept-language': 'en-x-piglatin'
427 } );
428
429 assert.match( text, /esttay anguagelay onversioncay/ );
430 assert.match( headers[ 'content-type' ], /^text\/html/ );
431 assert.match( headers.vary, /\bAccept-Language\b/i );
432 assert.match( headers.etag, /en-x-piglatin/i );
433
434 // Since with_html returns JSON, content language is not set
435 // but if its set, we expect it to be set correctly.
436 const contentLanguageHeader = headers[ 'content-language' ];
437 if ( contentLanguageHeader ) {
438 assert.match( headers[ 'content-language' ], /en-x-piglatin/i );
439 }
440 } );
441 it( 'Should perform fallback variant conversion', async () => {
442 await mindy.edit( fallbackVariantPage, { text: 'Podvlačenje linkova:' } );
443 const { headers, text } = await client.get( `/page/${ encodeURIComponent( fallbackVariantPage ) }/html`, null, {
444 'accept-language': 'sh-cyrl'
445 } );
446
447 assert.match( text, /Подвлачење линкова:/ );
448 assert.match( headers[ 'content-type' ], /^text\/html/ );
449 assert.match( headers.vary, /\bAccept-Language\b/i );
450 assert.match( headers.etag, /sh-cyrl/i );
451
452 // Since with_html returns JSON, content language is not set
453 // but if its set, we expect it to be set correctly.
454 const contentLanguageHeader = headers[ 'content-language' ];
455 if ( contentLanguageHeader ) {
456 assert.match( headers[ 'content-language' ], /sh-cyrl/i );
457 }
458 } );
459 } );
460 } );
461
462 // eslint-disable-next-line mocha/no-exports
463 exports.init = function ( pp ) {
464 // Allow testing both legacy and module paths using the same tests
465 pathPrefix = pp;
466 };