Debian Codegraph @

tests/api-testing/REST/PageHistory.js

raw symbols SCIP
1 'use strict';
2
3 const { action, assert, REST, utils } = require( 'api-testing' );
4 const supertest = require( 'supertest' );
5
6 describe( 'Page History', () => {
7 const title = utils.title( 'PageHistory_' );
8 const titleToDelete = utils.title( 'PageHistoryDelete_' );
9 const client = new REST();
10
11 let mindy, bot, anon, alice, root;
12 let autoCreateTempUserEnabled = false;
13
14 const edits = {
15 all: [],
16 anon: [],
17 temp: [],
18 bot: [],
19 reverts: []
20 };
21
22 async function setupDeletedPage() {
23 const editOne = await bot.edit( titleToDelete, { text: 'Delete Me 1', summary: 'edit 1' } );
24 const editTwo = await mindy.edit( titleToDelete, { text: 'Counting 1', summary: 'edit 2' } );
25 return { editOne, editTwo };
26 }
27
28 async function assertGetStatus( url, status = 200 ) {
29 const response = await supertest.agent( url ).get( '' );
30 assert.equal( response.status, status, `Status of GET ${ url }` );
31 }
32
33 const addEditInfo = ( editInfo, editBuckets, anonUsername ) => {
34 const obj = {
35 id: editInfo.newrevid,
36 comment: editInfo.param_summary,
37 'user.name': anonUsername || editInfo.param_user,
38 minor: !!editInfo.param_minor
39 };
40
41 edits.all.unshift( obj );
42 editBuckets.forEach( ( editBucket ) => editBucket.unshift( obj ) );
43 };
44
45 before( async () => {
46 // Users
47 bot = await action.robby();
48 anon = action.getAnon();
49 alice = await action.alice();
50 mindy = await action.mindy();
51 root = await action.root();
52 await root.addGroups( mindy.username, [ 'suppress' ] );
53
54 const siteInfoQuery = await anon.action(
55 'query',
56 // fetch flag $wgAutoCreateTempUser['enabled'], and format to
57 // true/false for convenience
58 { meta: 'siteinfo', siprop: 'autocreatetempuser', formatversion: 2 }
59 );
60 autoCreateTempUserEnabled = siteInfoQuery.query.autocreatetempuser.enabled;
61
62 // Create a page and make edits by various users and store edit information
63 addEditInfo( await alice.edit( title, { text: 'Counting 1', summary: 'creating page' } ), [] );
64
65 if ( autoCreateTempUserEnabled ) {
66 const resultAnonEdit = await anon.edit( title, { text: 'Counting 1 2', summary: 'anon edit 1' } );
67 const anonInfo = await anon.meta( 'userinfo', {} );
68 addEditInfo( resultAnonEdit, [ edits.temp ], anonInfo.name );
69
70 // Second edit for new anonymous user (new temp account)
71 anon = action.getAnon();
72 const resultAnonEdit2 = await anon.edit( title, { text: 'Counting 1 2 3', summary: 'anon edit 2' } );
73 const anonInfoEdit2 = await anon.meta( 'userinfo', {} );
74 addEditInfo( resultAnonEdit2, [ edits.temp ], anonInfoEdit2.name );
75 } else {
76 const anonInfo = await anon.meta( 'userinfo', {} );
77 anon.username = anonInfo.name;
78 addEditInfo( await anon.edit( title, { text: 'Counting 1 2', summary: 'anon edit 1' } ), [ edits.anon ] );
79 addEditInfo( await anon.edit( title, { text: 'Counting 1 2 3', summary: 'anon edit 2' } ), [ edits.anon ] );
80 }
81 addEditInfo( await bot.edit( title, { text: 'Counting 1 2 3 4', summary: 'bot edit 1' } ), [ edits.bot ] );
82 addEditInfo( await bot.edit( title, { text: 'Counting 1 2 3 4 5', summary: 'bot edit 2' } ), [ edits.bot ] );
83
84 // Rollback edits by bot
85 const summary = 'revert edits by bot';
86 const { rollback } = await mindy.action( 'rollback', {
87 title,
88 user: bot.username,
89 summary,
90 token: await mindy.token( 'rollback' )
91 }, 'POST' );
92
93 // record info for use by addEditInfo()
94 rollback.newrevid = rollback.revid;
95 rollback.param_summary = summary;
96 rollback.param_user = mindy.username;
97 // Rollbacks are minor by default
98 rollback.param_minor = true;
99
100 // Make sure we have something in cache in MW, so that we can verify later on it's updated
101 await client.get( `/page/${ title }/history/counts/edits` );
102 await utils.sleep();
103
104 addEditInfo( rollback, [ edits.reverts ] );
105
106 // The bot manually reverts the page to bot edit 1
107 addEditInfo(
108 await bot.edit( title, { text: 'Counting 1 2 3 4', summary: 'bot edit 3', minor: true } ),
109 [ edits.bot, edits.reverts ]
110 );
111 addEditInfo( await bot.edit( title, { text: 'Counting 1 2 3 4 555', summary: 'bot edit 4' } ), [ edits.bot ] );
112
113 // Undo last edit
114 addEditInfo( await mindy.edit( title, { undo: edits.all[ 0 ].id } ), [ edits.reverts ] );
115
116 } );
117
118 describe( 'Revision deletion and un-deletion', () => {
119 let deleteEdits;
120 it( 'Should get total number of edits and editors when edits are hidden and shown', async () => {
121 deleteEdits = await setupDeletedPage();
122 const { editOne } = deleteEdits;
123
124 // Populate cache
125 const { body, status, headers } = await client.get(
126 `/page/${ titleToDelete }/history/counts/edits`
127 );
128 assert.equal( status, 200 );
129 assert.match( headers[ 'content-type' ], /^application\/json/ );
130 assert.deepEqual( body, { count: 2, limit: false }, 'Initial edit count of 2 ' );
131
132 // Hack: If edits are not > 1 sec apart the latest timestamp will not not be detected.
133 // Handler uses logging table timestamps to determine last modified time,
134 // which are MW timestamps down to the sec, not ms.
135 await utils.sleep();
136
137 // Hide revision
138 await mindy.action( 'revisiondelete',
139 {
140 type: 'revision',
141 token: await mindy.token(),
142 target: titleToDelete,
143 hide: 'content|comment|user',
144 ids: editOne.newrevid
145 },
146 'POST'
147 );
148
149 const revHideEdits = await client.get( `/page/${ titleToDelete }/history/counts/edits` );
150 assert.equal( revHideEdits.status, 200 );
151 assert.match( revHideEdits.headers[ 'content-type' ], /^application\/json/ );
152 assert.deepEqual( revHideEdits.body, { count: 1, limit: false }, 'Edit count of 1 after hiding a revision' );
153
154 const revHideEditors = await client.get( `/page/${ titleToDelete }/history/counts/editors` );
155 assert.equal( revHideEditors.status, 200 );
156 assert.match( revHideEditors.headers[ 'content-type' ], /^application\/json/ );
157 assert.deepEqual( revHideEditors.body, { count: 1, limit: false }, 'Editor count of 1 after hiding a revision' );
158
159 await utils.sleep();
160
161 // Show revision
162 await mindy.action( 'revisiondelete',
163 {
164 type: 'revision',
165 token: await mindy.token(),
166 target: titleToDelete,
167 show: 'content|comment|user',
168 ids: editOne.newrevid
169 },
170 'POST'
171 );
172
173 const revShowEdits = await client.get( `/page/${ titleToDelete }/history/counts/edits` );
174 assert.equal( revShowEdits.status, 200 );
175 assert.match( revShowEdits.headers[ 'content-type' ], /^application\/json/ );
176 assert.deepEqual( revShowEdits.body, { count: 2, limit: false }, 'Edit count of 2 after un-hiding the hidden revision' );
177
178 const revShowEditors = await client.get( `/page/${ titleToDelete }/history/counts/editors` );
179 assert.equal( revShowEditors.status, 200 );
180 assert.match( revShowEditors.headers[ 'content-type' ], /^application\/json/ );
181 assert.deepEqual( revShowEditors.body, { count: 2, limit: false }, 'Editor count of 2 after un-hiding the hidden revision' );
182 } );
183
184 it( 'Should update last-modified header after revision deletion', async () => {
185 const { headers } = await client.get( `/page/${ titleToDelete }/history/counts/edits` );
186 const { editTwo } = deleteEdits;
187 assert.containsAllKeys( headers, [ 'last-modified' ] );
188 const lastTouchedTS = Date.parse( editTwo.newtimestamp );
189 const headerLastModTS = Date.parse( headers[ 'last-modified' ] );
190 assert.isAbove( headerLastModTS, lastTouchedTS );
191 } );
192 } );
193
194 describe( 'GET /page/{title}/history/counts/edits', () => {
195 it( 'Should get total number of edits', async () => {
196 // we do 2 requests to verify the second value coming from cache is the same
197 for ( let i = 0; i < 2; i++ ) {
198 const res = await client.get( `/page/${ title }/history/counts/edits` );
199 assert.equal( res.status, 200 );
200 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
201 assert.deepEqual( res.body, { count: 9, limit: false } );
202 }
203 } );
204
205 it( 'Should return 400 for invalid parameter', async () => {
206 const res = await client.get( `/page/${ title }/history/counts/editts` );
207 assert.equal( res.status, 400 );
208 } );
209
210 it( 'Should return 404 for title that does not exist', async () => {
211 const title2 = utils.title( 'Random_' );
212 const res = await client.get( `/page/${ title2 }/history/counts/edits` );
213
214 assert.equal( res.status, 404 );
215 } );
216
217 it( 'Should get total number of edits between revisions, normal order', async () => {
218 const fromRev = edits.all[ 1 ].id;
219 const toRev = edits.all[ edits.all.length - 2 ].id;
220 const res = await client.get( `/page/${ title }/history/counts/edits?from=${ fromRev }&to=${ toRev }` );
221
222 assert.strictEqual( res.status, 200 );
223 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
224 assert.deepEqual( res.body, { count: 5, limit: false } );
225 } );
226
227 it( 'Should get total number of edits between revisions, reverse order', async () => {
228 const fromRev = edits.all[ 1 ].id;
229 const toRev = edits.all[ edits.all.length - 2 ].id;
230 const res = await client.get( `/page/${ title }/history/counts/edits?from=${ toRev }&to=${ fromRev }` );
231
232 assert.strictEqual( res.status, 200 );
233 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
234 assert.deepEqual( res.body, { count: 5, limit: false } );
235 } );
236
237 it( 'Should return 404 for deleted page', async () => {
238 await mindy.action( 'delete', { title: titleToDelete, token: await mindy.token() }, 'POST' );
239 const { status: editsStatus } = await client.get( `/page/${ titleToDelete }/history/counts/edits` );
240 assert.equal( editsStatus, 404 );
241 } );
242 } );
243
244 describe( 'GET /page/{title}/history/counts/anonymous', () => {
245 it( 'Should get total number of anonymous edits', async () => {
246 const res = await client.get( `/page/${ title }/history/counts/anonymous` );
247
248 assert.equal( res.status, 200 );
249 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
250 const expectedCount = autoCreateTempUserEnabled ? 0 : 2;
251 assert.deepEqual( res.body, { count: expectedCount, limit: false } );
252 } );
253 } );
254
255 describe( 'GET /page/{title}/history/counts/temporary', () => {
256 it( 'Should get total number of edits by temporary users', async () => {
257 const res = await client.get( `/page/${ title }/history/counts/temporary` );
258
259 assert.equal( res.status, 200 );
260 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
261 const expectedCount = autoCreateTempUserEnabled ? 2 : 0;
262 assert.deepEqual( res.body, { count: expectedCount, limit: false } );
263 } );
264 } );
265
266 describe( 'GET /page/{title}/history/counts/bot', () => {
267 it( 'Should get total number of edits by bots', async () => {
268 const res = await client.get( `/page/${ title }/history/counts/bot` );
269
270 assert.equal( res.status, 200 );
271 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
272 assert.deepEqual( res.body, { count: 4, limit: false } );
273 } );
274 } );
275
276 describe( 'GET /page/{title}/history/counts/reverted', () => {
277 it( 'Should get total number of reverted edits', async () => {
278 const res = await client.get( `/page/${ title }/history/counts/reverted` );
279
280 assert.equal( res.status, 200 );
281 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
282 assert.deepEqual( res.body, { count: 3, limit: false } );
283 } );
284 } );
285
286 describe( 'GET /page/{title}/history/counts/editors', () => {
287 it( 'Should return 404 for deleted page', async () => {
288 const { status: editorsStatus, header: editorsHeader } = await client.get( `/page/${ titleToDelete }/history/counts/editors` );
289 assert.equal( editorsStatus, 404 );
290 assert.match( editorsHeader[ 'content-type' ], /^application\/json/ );
291 } );
292
293 it( 'Should get total number of unique editors', async () => {
294 const res = await client.get( `/page/${ title }/history/counts/editors` );
295
296 assert.equal( res.status, 200 );
297 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
298 const expectedCount = autoCreateTempUserEnabled ? 5 : 4;
299 assert.deepEqual( res.body, { count: expectedCount, limit: false } );
300 } );
301
302 it( 'Should get total number of unique editors between revisions, normal order', async () => {
303 const fromRev = edits.all[ 1 ].id;
304 const toRev = edits.all[ edits.all.length - 2 ].id;
305 const res = await client.get( `/page/${ title }/history/counts/editors?from=${ fromRev }&to=${ toRev }` );
306
307 assert.strictEqual( res.status, 200 );
308 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
309 assert.deepEqual( res.body, { count: 3, limit: false } );
310 } );
311
312 it( 'Should get total number of unique editors between revisions, reverse order', async () => {
313 const fromRev = edits.all[ 1 ].id;
314 const toRev = edits.all[ edits.all.length - 2 ].id;
315 const res = await client.get( `/page/${ title }/history/counts/editors?from=${ toRev }&to=${ fromRev }` );
316
317 assert.strictEqual( res.status, 200 );
318 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
319 assert.deepEqual( res.body, { count: 3, limit: false } );
320 } );
321 } );
322
323 describe( 'GET page/{title}/history?filter={tag}', () => {
324 it( 'Should get all revisions', async () => {
325 const res = await client.get( `/page/${ title }/history` );
326
327 assert.equal( res.status, 200 );
328 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
329 assert.lengthOf( res.body.revisions, edits.all.length );
330 res.body.revisions
331 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, edits.all[ i ] ) );
332 assert.include( res.body.latest, `page/${ title }/history` );
333
334 await assertGetStatus( res.body.latest );
335 } );
336
337 it( 'Should get revisions by anonymous users', async () => {
338 const res = await client.get( `/page/${ title }/history`, { filter: 'anonymous' } );
339
340 assert.equal( res.status, 200 );
341 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
342 assert.lengthOf( res.body.revisions, edits.anon.length );
343 res.body.revisions
344 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, edits.anon[ i ] ) );
345 assert.include( res.body.latest, `page/${ title }/history?filter=anonymous` );
346
347 await assertGetStatus( res.body.latest );
348 } );
349
350 it( 'Should get revisions by bots', async () => {
351 const res = await client.get( `/page/${ title }/history`, { filter: 'bot' } );
352
353 assert.equal( res.status, 200 );
354 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
355 assert.lengthOf( res.body.revisions, edits.bot.length );
356 res.body.revisions
357 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, edits.bot[ i ] ) );
358 assert.include( res.body.latest, `page/${ title }/history?filter=bot` );
359 } );
360
361 it( 'Should get reverted revisions', async () => {
362 const res = await client.get( `/page/${ title }/history`, { filter: 'reverted' } );
363
364 assert.equal( res.status, 200 );
365 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
366 assert.lengthOf( res.body.revisions, edits.reverts.length );
367 res.body.revisions
368 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, edits.reverts[ i ] ) );
369 assert.include( res.body.latest, `page/${ title }/history?filter=reverted` );
370 } );
371
372 it( 'Should return 400 for invalid filter parameter', async () => {
373 const res = await client.get( `/page/${ title }/history`, { filter: 'anon' } );
374
375 assert.equal( res.status, 400 );
376 } );
377
378 it( 'Should return 404 for title that does not exist', async () => {
379 const title2 = utils.title( 'Random_' );
380 const res = await client.get( `/page/${ title2 }/history`, { filter: 'bot' } );
381
382 assert.equal( res.status, 404 );
383 } );
384
385 it( 'Should update cache control headers', async () => {
386 const title2 = utils.title( 'Random_' );
387 const edit1 = await alice.edit( title2, { text: 'Old Content', summary: 'make page' } );
388 const res1a = await client.get( `/page/${ title2 }/history`, { filter: 'bot' } );
389 const res1b = await client.get( `/page/${ title2 }/history`, { filter: 'bot' } );
390
391 assert.equal( res1a.headers[ 'last-modified' ], res1b.headers[ 'last-modified' ] );
392 assert.equal( res1a.headers.etag, res1b.headers.etag );
393
394 const edit2 = await alice.edit( title2, { text: 'New Content', summary: 'poke page' } );
395 const res2 = await client.get( `/page/${ title2 }/history`, { filter: 'bot' } );
396
397 assert.equal( Date.parse( res1a.headers[ 'last-modified' ] ),
398 Date.parse( edit1.newtimestamp ) );
399
400 assert.equal( Date.parse( res2.headers[ 'last-modified' ] ),
401 Date.parse( edit2.newtimestamp ) );
402
403 assert.notEqual( res1a.headers.etag, res2.headers.etag );
404 } );
405 } );
406
407 describe( 'GET /page/{title}/history?{older_than|newer_than={id}}', () => {
408 it( 'Should get revisions newer than specified id for a page', async () => {
409 const { id } = edits.all[ 3 ];
410 const expected = edits.all.slice( 0, 3 );
411 const res = await client.get( `/page/${ title }/history`, { newer_than: id } );
412
413 assert.equal( res.status, 200 );
414 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
415 assert.lengthOf( res.body.revisions, expected.length );
416 res.body.revisions
417 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, expected[ i ] ) );
418 assert.include( res.body.latest, `page/${ title }/history` );
419 assert.include( res.body.older, `page/${ title }/history?older_than=${ expected[ expected.length - 1 ].id }` );
420 } );
421
422 it( 'Should get revisions older than specified id for a page', async () => {
423 const { id } = edits.all[ 3 ];
424 const expected = edits.all.slice( 4 );
425 const res = await client.get( `/page/${ title }/history`, { older_than: id } );
426
427 assert.equal( res.status, 200 );
428 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
429 assert.lengthOf( res.body.revisions, expected.length );
430 res.body.revisions
431 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, expected[ i ] ) );
432 assert.include( res.body.latest, `page/${ title }/history` );
433 assert.include( res.body.newer, `page/${ title }/history?newer_than=${ expected[ 0 ].id }` );
434
435 await assertGetStatus( res.body.newer );
436 } );
437
438 it( 'Should get revisions using both filter and newer_than|older_than parameters', async () => {
439 const { id } = edits.all[ 3 ];
440 const res = await client.get( `/page/${ title }/history`, { newer_than: id, filter: 'bot' } );
441
442 assert.equal( res.status, 200 );
443 assert.match( res.headers[ 'content-type' ], /^application\/json/ );
444 assert.lengthOf( res.body.revisions, 2 );
445 res.body.revisions
446 .forEach( ( rev, i ) => assert.deepNestedInclude( rev, edits.bot[ i ] ) );
447 assert.include( res.body.latest, `page/${ title }/history?filter=bot` );
448 assert.include( res.body.older, `page/${ title }/history?filter=bot&older_than=${ edits.bot[ 1 ].id }` );
449
450 await assertGetStatus( res.body.older );
451 } );
452
453 it( 'Should return 400 for revision id less than 0 ', async () => {
454 const res = await client.get( `/page/${ title }/history`, { newer_than: -1 } );
455
456 assert.equal( res.status, 400 );
457 } );
458
459 it( 'Should return 400 when using both newer_than and older_than', async () => {
460 const id1 = edits.all[ 8 ].id;
461 const id2 = edits.all[ 2 ].id;
462 const res = await client.get( `/page/${ title }/history`, { newer_than: id1, older_than: id2 } );
463
464 assert.equal( res.status, 400 );
465 } );
466
467 it( 'Should return 404 for a revision that does not exist for a specified page', async () => {
468 const anon2 = action.getAnon();
469 const title2 = utils.title( 'AnotherPage' );
470 const edit = await anon2.edit( title2, { text: 'Hello world' } );
471 const res = await client.get( `/page/${ title }/history`, { newer_than: edit.newrevid } );
472
473 assert.equal( res.status, 404 );
474 } );
475 } );
476 } );