2020-06-02 21:24:59 +00:00
|
|
|
'use strict';
|
2020-05-12 18:28:43 +00:00
|
|
|
const { REST, assert, action, utils, clientFactory } = require( 'api-testing' );
|
2020-06-02 21:24:59 +00:00
|
|
|
|
|
|
|
|
describe( 'GET /me/contributions', () => {
|
|
|
|
|
const basePath = 'rest.php/coredev/v0';
|
|
|
|
|
const anon = new REST( basePath );
|
2020-06-03 19:20:35 +00:00
|
|
|
const limit = 2;
|
2020-05-12 18:28:43 +00:00
|
|
|
const arnoldsRevisions = [];
|
|
|
|
|
const arnoldsEdits = [];
|
2020-05-12 18:28:43 +00:00
|
|
|
const arnoldsTags = [];
|
2020-05-12 18:28:43 +00:00
|
|
|
let arnold;
|
2020-06-09 09:33:22 +00:00
|
|
|
let arnoldAction;
|
|
|
|
|
let samAction;
|
2020-06-03 19:20:35 +00:00
|
|
|
|
|
|
|
|
before( async () => {
|
2020-05-12 18:28:43 +00:00
|
|
|
const bobAction = await action.bob();
|
2020-06-09 09:33:22 +00:00
|
|
|
samAction = await action.user( 'sam', [ 'suppress' ] );
|
|
|
|
|
arnoldAction = await action.user( 'arnold' );
|
2020-05-12 18:28:43 +00:00
|
|
|
arnold = clientFactory.getRESTClient( basePath, arnoldAction );
|
2020-06-03 19:20:35 +00:00
|
|
|
|
|
|
|
|
let page = utils.title( 'UserContribution_' );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
// create a tag
|
|
|
|
|
await action.makeTag( 'api-test' );
|
|
|
|
|
|
2020-06-03 19:20:35 +00:00
|
|
|
// bob makes 1 edit
|
|
|
|
|
await bobAction.edit( page, [ { text: 'Bob revision 1', summary: 'Bob made revision 1' } ] );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
for ( let i = 1; i <= 5; i++ ) {
|
2020-05-12 18:28:43 +00:00
|
|
|
// tag odd edits
|
|
|
|
|
const tags = i % 2 ? 'api-test' : null;
|
|
|
|
|
arnoldsTags[ i ] = tags ? tags.split( '|' ) : [];
|
|
|
|
|
|
|
|
|
|
const revData = await arnoldAction.edit( page, { text: `arnold revision ${i}`, tags } );
|
2020-06-03 19:20:35 +00:00
|
|
|
await utils.sleep();
|
2020-05-12 18:28:43 +00:00
|
|
|
arnoldsRevisions[ revData.newrevid ] = revData;
|
|
|
|
|
arnoldsEdits[ i ] = revData;
|
2020-06-03 19:20:35 +00:00
|
|
|
page = utils.title( 'UserContribution_' );
|
|
|
|
|
}
|
|
|
|
|
} );
|
2020-06-02 21:24:59 +00:00
|
|
|
|
|
|
|
|
it( 'Returns status 401 for anon', async () => {
|
|
|
|
|
const { status, body } = await anon.get( '/me/contributions' );
|
|
|
|
|
assert.equal( status, 401 );
|
|
|
|
|
assert.nestedProperty( body, 'messageTranslations' );
|
|
|
|
|
} );
|
|
|
|
|
|
2020-06-03 19:20:35 +00:00
|
|
|
it( 'Returns status OK', async () => {
|
2020-05-12 18:28:43 +00:00
|
|
|
const response = await arnold.get( '/me/contributions' );
|
2020-06-03 19:20:35 +00:00
|
|
|
assert.equal( response.status, 200 );
|
|
|
|
|
} );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
it( 'Returns a list of arnold\'s edits', async () => {
|
|
|
|
|
const { status, body } = await arnold.get( `/me/contributions?limit=${limit}` );
|
2020-06-03 19:20:35 +00:00
|
|
|
assert.equal( status, 200 );
|
|
|
|
|
|
|
|
|
|
// assert body has property revisions
|
|
|
|
|
assert.property( body, 'revisions' );
|
|
|
|
|
const { revisions } = body;
|
|
|
|
|
|
|
|
|
|
// assert body.revisions is array
|
|
|
|
|
assert.isArray( revisions );
|
|
|
|
|
|
|
|
|
|
// assert body.revisions length is limit
|
|
|
|
|
assert.lengthOf( revisions, limit );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
const lastRevision = arnoldsRevisions[ arnoldsRevisions.length - 1 ];
|
2020-06-03 19:20:35 +00:00
|
|
|
|
|
|
|
|
// assert body.revisions object schema is correct
|
|
|
|
|
assert.hasAllDeepKeys( revisions[ 0 ], [
|
2020-05-12 18:28:43 +00:00
|
|
|
'id', 'comment', 'timestamp', 'size', 'page', 'tags'
|
2020-06-03 19:20:35 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
assert.equal( revisions[ 0 ].page.key, utils.dbkey( lastRevision.title ) );
|
|
|
|
|
assert.equal( revisions[ 0 ].page.title, lastRevision.title );
|
|
|
|
|
assert.equal( revisions[ 0 ].comment, lastRevision.param_summary );
|
|
|
|
|
assert.equal( revisions[ 0 ].timestamp, lastRevision.newtimestamp );
|
|
|
|
|
assert.isOk( Date.parse( revisions[ 0 ].timestamp ) );
|
|
|
|
|
assert.isNotOk( Date.parse( 'xyz' ) );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
assert.isArray( revisions[ 0 ].tags );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
assert.isAbove( Date.parse( revisions[ 0 ].timestamp ),
|
|
|
|
|
Date.parse( revisions[ 1 ].timestamp ) );
|
|
|
|
|
|
2020-06-03 19:20:35 +00:00
|
|
|
// assert body.revisions contains edits only by one user
|
|
|
|
|
revisions.forEach( ( rev ) => {
|
2020-05-12 18:28:43 +00:00
|
|
|
assert.property( arnoldsRevisions, rev.id );
|
2020-06-03 19:20:35 +00:00
|
|
|
} );
|
|
|
|
|
} );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
it( 'Can fetch a chain of segments following the "older" field in the response', async () => {
|
|
|
|
|
// get latest segment
|
|
|
|
|
const { body: latestSegment } = await arnold.get( `/me/contributions?limit=${limit}` );
|
|
|
|
|
assert.property( latestSegment, 'older' );
|
|
|
|
|
assert.property( latestSegment, 'revisions' );
|
|
|
|
|
assert.isArray( latestSegment.revisions );
|
|
|
|
|
assert.lengthOf( latestSegment.revisions, 2 );
|
|
|
|
|
|
|
|
|
|
// assert body.revisions has the correct content
|
|
|
|
|
assert.equal( latestSegment.revisions[ 0 ].id, arnoldsEdits[ 5 ].newrevid );
|
|
|
|
|
assert.equal( latestSegment.revisions[ 1 ].id, arnoldsEdits[ 4 ].newrevid );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
assert.deepEqual( latestSegment.revisions[ 0 ].tags, arnoldsTags[ 5 ] );
|
|
|
|
|
assert.deepEqual( latestSegment.revisions[ 1 ].tags, arnoldsTags[ 4 ] );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
// get older segment, using full url
|
|
|
|
|
const req = clientFactory.getHttpClient( arnold );
|
|
|
|
|
|
|
|
|
|
const { body: olderSegment } = await req.get( latestSegment.older );
|
|
|
|
|
assert.property( olderSegment, 'older' );
|
|
|
|
|
assert.property( olderSegment, 'revisions' );
|
|
|
|
|
assert.isArray( olderSegment.revisions );
|
|
|
|
|
assert.lengthOf( olderSegment.revisions, 2 );
|
|
|
|
|
|
|
|
|
|
// assert body.revisions has the correct content
|
|
|
|
|
assert.equal( olderSegment.revisions[ 0 ].id, arnoldsEdits[ 3 ].newrevid );
|
|
|
|
|
assert.equal( olderSegment.revisions[ 1 ].id, arnoldsEdits[ 2 ].newrevid );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
assert.deepEqual( olderSegment.revisions[ 0 ].tags, arnoldsTags[ 3 ] );
|
|
|
|
|
assert.deepEqual( olderSegment.revisions[ 1 ].tags, arnoldsTags[ 2 ] );
|
|
|
|
|
|
2020-05-12 18:28:43 +00:00
|
|
|
// get the next older segment
|
|
|
|
|
const { body: finalSegment } = await req.get( olderSegment.older );
|
|
|
|
|
assert.propertyVal( finalSegment, 'older', null );
|
|
|
|
|
assert.property( finalSegment, 'revisions' );
|
|
|
|
|
assert.isArray( finalSegment.revisions );
|
|
|
|
|
assert.lengthOf( finalSegment.revisions, 1 );
|
|
|
|
|
|
|
|
|
|
// assert body.revisions has the correct content
|
|
|
|
|
assert.equal( finalSegment.revisions[ 0 ].id, arnoldsEdits[ 1 ].newrevid );
|
2020-05-12 18:28:43 +00:00
|
|
|
|
|
|
|
|
assert.deepEqual( finalSegment.revisions[ 0 ].tags, arnoldsTags[ 1 ] );
|
2020-05-12 18:28:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
it( 'Returns 400 if segment size is out of bounds', async () => {
|
|
|
|
|
const { status: minLimitStatus } = await arnold.get( '/me/contributions?limit=0' );
|
|
|
|
|
assert.equal( minLimitStatus, 400 );
|
|
|
|
|
|
|
|
|
|
const { status: maxLimitStatus } = await arnold.get( '/me/contributions?limit=30' );
|
|
|
|
|
assert.equal( maxLimitStatus, 400 );
|
|
|
|
|
} );
|
2020-06-05 08:14:21 +00:00
|
|
|
|
|
|
|
|
it( 'Can fetch a chain of segments following the "newer" field in the response', async () => {
|
|
|
|
|
const req = clientFactory.getHttpClient( arnold );
|
|
|
|
|
|
|
|
|
|
// get latest segment
|
|
|
|
|
const { body: latestSegment } = await arnold.get( `/me/contributions?limit=${limit}` );
|
|
|
|
|
assert.property( latestSegment, 'newer' );
|
|
|
|
|
|
|
|
|
|
// get next older segment
|
|
|
|
|
const { body: olderSegment } = await req.get( latestSegment.older );
|
|
|
|
|
assert.property( olderSegment, 'newer' );
|
|
|
|
|
|
|
|
|
|
// get the final segment
|
|
|
|
|
const { body: finalSegment } = await req.get( olderSegment.older );
|
|
|
|
|
assert.property( finalSegment, 'newer' );
|
|
|
|
|
|
|
|
|
|
// Follow the chain of "newer" links back to the latest segment
|
|
|
|
|
const { body: olderSegment2 } = await req.get( finalSegment.newer );
|
|
|
|
|
assert.deepEqual( olderSegment, olderSegment2 );
|
|
|
|
|
|
|
|
|
|
const { body: latestSegment2 } = await req.get( olderSegment.newer );
|
|
|
|
|
assert.deepEqual( latestSegment, latestSegment2 );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
it( 'Returns a valid link to the latest segment', async () => {
|
|
|
|
|
const req = clientFactory.getHttpClient( arnold );
|
|
|
|
|
|
|
|
|
|
// get latest segment
|
|
|
|
|
const { body: latestSegment } = await arnold.get( `/me/contributions?limit=${limit}` );
|
|
|
|
|
assert.property( latestSegment, 'latest' );
|
|
|
|
|
|
|
|
|
|
// get next older segment
|
|
|
|
|
const { body: olderSegment } = await req.get( latestSegment.older );
|
|
|
|
|
assert.property( olderSegment, 'latest' );
|
|
|
|
|
|
|
|
|
|
// get the final segment
|
|
|
|
|
const { body: finalSegment } = await req.get( olderSegment.older );
|
|
|
|
|
assert.property( finalSegment, 'latest' );
|
|
|
|
|
|
|
|
|
|
// Follow all the "newer" links
|
|
|
|
|
const { body: latestSegment2 } = await req.get( finalSegment.latest );
|
|
|
|
|
assert.deepEqual( latestSegment, latestSegment2 );
|
|
|
|
|
|
|
|
|
|
const { body: latestSegment3 } = await req.get( olderSegment.latest );
|
|
|
|
|
assert.deepEqual( latestSegment, latestSegment3 );
|
|
|
|
|
|
|
|
|
|
const { body: latestSegment4 } = await req.get( finalSegment.latest );
|
|
|
|
|
assert.deepEqual( latestSegment, latestSegment4 );
|
|
|
|
|
} );
|
|
|
|
|
|
2020-06-09 09:33:22 +00:00
|
|
|
it( 'Does not return suppressed revisions when requesting user does not have appropriate permissions', async () => {
|
|
|
|
|
const { body: preDeleteBody } = await arnold.get( '/me/contributions?limit=10' );
|
|
|
|
|
assert.lengthOf( preDeleteBody.revisions, 5 );
|
|
|
|
|
|
|
|
|
|
const pageToDelete = utils.title( 'UserContribution_' );
|
|
|
|
|
const editToDelete = await arnoldAction.edit( pageToDelete, [ { text: 'Delete me 1' } ] );
|
|
|
|
|
await arnoldAction.edit( pageToDelete, [ { text: 'Delete me 2' } ] );
|
|
|
|
|
|
|
|
|
|
await samAction.action( 'revisiondelete',
|
|
|
|
|
{
|
|
|
|
|
type: 'revision',
|
|
|
|
|
token: await samAction.token(),
|
|
|
|
|
target: editToDelete.title,
|
|
|
|
|
hide: 'content|comment|user',
|
|
|
|
|
ids: editToDelete.newrevid
|
|
|
|
|
},
|
|
|
|
|
'POST'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Users without appropriate permissions cannot see suppressed revisions (even their own)
|
|
|
|
|
const { body: arnoldGetBody } = await arnold.get( '/me/contributions?limit=10' );
|
|
|
|
|
assert.lengthOf( arnoldGetBody.revisions, 6 );
|
|
|
|
|
|
|
|
|
|
await samAction.action( 'revisiondelete',
|
|
|
|
|
{
|
|
|
|
|
type: 'revision',
|
|
|
|
|
token: await samAction.token(),
|
|
|
|
|
target: editToDelete.title,
|
|
|
|
|
show: 'content|comment|user',
|
|
|
|
|
ids: editToDelete.newrevid
|
|
|
|
|
},
|
|
|
|
|
'POST'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Users with appropriate permissions can see suppressed revisions
|
|
|
|
|
const { body: arnoldGetBody2 } = await arnold.get( '/me/contributions?limit=10' );
|
|
|
|
|
assert.lengthOf( arnoldGetBody2.revisions, 7 );
|
|
|
|
|
} );
|
|
|
|
|
|
2020-06-02 21:24:59 +00:00
|
|
|
} );
|