aboutsummaryrefslogtreecommitdiffstats
path: root/tests/api-testing
diff options
context:
space:
mode:
authorbpirkle <bpirkle@wikimedia.org>2024-07-30 22:11:31 -0500
committerbpirkle <bpirkle@wikimedia.org>2024-08-07 16:51:09 -0500
commit3f614a0898eb0d5dfdd52aa17baed1ee3c4503d0 (patch)
tree2619e5e0bbd3c8a6f56836ec1410fb203fba3ae4 /tests/api-testing
parent2be4b150e43a0653f04f5db99555bcc57964d9cb (diff)
downloadmediawikicore-3f614a0898eb0d5dfdd52aa17baed1ee3c4503d0.tar.gz
mediawikicore-3f614a0898eb0d5dfdd52aa17baed1ee3c4503d0.zip
Add content.v1 REST module with relevant content endpoints
T366837 added the ability to group REST endpoints into modules. This change now introduces the first use of a REST module. The existing endpoints remain accessible at their original paths, and mocha tests are run against both old and new paths. Bug: T370430 Change-Id: I83a5cdbdd359e4d3e5f70dd3930783616b556738
Diffstat (limited to 'tests/api-testing')
-rw-r--r--tests/api-testing/REST/CreationLegacy.js10
-rw-r--r--tests/api-testing/REST/PageLegacy.js10
-rw-r--r--tests/api-testing/REST/RevisionCompare.js52
-rw-r--r--tests/api-testing/REST/RevisionLegacy.js10
-rw-r--r--tests/api-testing/REST/UpdateLegacy.js10
-rw-r--r--tests/api-testing/REST/content.v1/Creation.js (renamed from tests/api-testing/REST/Creation.js)11
-rw-r--r--tests/api-testing/REST/content.v1/Page.js (renamed from tests/api-testing/REST/Page.js)12
-rw-r--r--tests/api-testing/REST/content.v1/Revision.js (renamed from tests/api-testing/REST/Revision.js)51
-rw-r--r--tests/api-testing/REST/content.v1/Update.js (renamed from tests/api-testing/REST/Update.js)11
9 files changed, 131 insertions, 46 deletions
diff --git a/tests/api-testing/REST/CreationLegacy.js b/tests/api-testing/REST/CreationLegacy.js
new file mode 100644
index 000000000000..19dab5e079c7
--- /dev/null
+++ b/tests/api-testing/REST/CreationLegacy.js
@@ -0,0 +1,10 @@
+'use strict';
+
+describe( 'legacy POST /page', () => {
+ // Cache deletion ensures tests will execute for both legacy and module paths
+ // Doing this twice protects against changes in test execution order
+ const testsFile = __dirname + '/content.v1/Creation.js';
+ delete require.cache[ testsFile ];
+ require( testsFile ).init( 'rest.php/v1' );
+ delete require.cache[ testsFile ];
+} );
diff --git a/tests/api-testing/REST/PageLegacy.js b/tests/api-testing/REST/PageLegacy.js
new file mode 100644
index 000000000000..2b3cdae3f4f1
--- /dev/null
+++ b/tests/api-testing/REST/PageLegacy.js
@@ -0,0 +1,10 @@
+'use strict';
+
+describe( 'legacy Page Source', () => {
+ // Cache deletion ensures tests will execute for both legacy and module paths
+ // Doing this twice protects against changes in test execution order
+ const testsFile = __dirname + '/content.v1/Page.js';
+ delete require.cache[ testsFile ];
+ require( testsFile ).init( 'rest.php/v1' );
+ delete require.cache[ testsFile ];
+} );
diff --git a/tests/api-testing/REST/RevisionCompare.js b/tests/api-testing/REST/RevisionCompare.js
new file mode 100644
index 000000000000..85091e91f23e
--- /dev/null
+++ b/tests/api-testing/REST/RevisionCompare.js
@@ -0,0 +1,52 @@
+'use strict';
+
+const { action, assert, REST, utils } = require( 'api-testing' );
+
+describe( 'Revision Compare', () => {
+ const client = new REST();
+ let mindy;
+
+ before( async () => {
+ mindy = await action.mindy();
+ } );
+
+ describe( 'GET /revision/{from}/compare/{to}', () => {
+ const pageOne = utils.title( 'RevisionCompare_1' );
+ const pageTwo = utils.title( 'RevisionCompare_2' );
+ const nonExistentRevId = 99999;
+ const validRevId = 1;
+ const invalidRevId = '#';
+
+ it( 'should return 400 if revision id is not an integer', async () => {
+ const { status } = await client.get( `/revision/${ validRevId }/compare/${ invalidRevId }` );
+ assert.equal( status, 400 );
+ } );
+
+ it( 'should successfully get diff between 2 valid revisions', async () => {
+ // XXX: this test requires php-wikidiff2 1.10 or later to be installed
+ const { newrevid: revId1 } = await mindy.edit( pageOne, { text: 'Mindy Edit 1' } );
+ const { newrevid: revId2 } = await mindy.edit( pageOne, { text: 'Mindy Edit 2' } );
+ const response = await client.get( `/revision/${ revId1 }/compare/${ revId2 }` );
+ const { status, body, text, headers } = response;
+ assert.strictEqual( status, 200, text );
+ assert.match( headers[ 'content-type' ], /^application\/json/ );
+ assert.nestedPropertyVal( body, 'from.id', revId1 );
+ assert.nestedPropertyVal( body, 'to.id', revId2 );
+ assert.nestedProperty( body, 'diff' );
+ assert.isArray( body.diff );
+ } );
+
+ it( 'should return 404 for revision that does not exist', async () => {
+ const { status } = await client.get( `/revision/${ validRevId }/compare/${ nonExistentRevId }` );
+ assert.strictEqual( status, 404 );
+ } );
+
+ it( 'should return 400 if revision ids belong to different pages', async () => {
+ const { newrevid: pageOneRev } = await mindy.edit( pageOne, { text: 'Page 1 edit' } );
+ const { newrevid: pageTwoRev } = await mindy.edit( pageTwo, { text: 'Page 2 edit' } );
+ const { status } = await client.get( `/revision/${ pageOneRev }/compare/${ pageTwoRev }` );
+ assert.strictEqual( status, 400 );
+ } );
+ } );
+
+} );
diff --git a/tests/api-testing/REST/RevisionLegacy.js b/tests/api-testing/REST/RevisionLegacy.js
new file mode 100644
index 000000000000..0adc7abd48b3
--- /dev/null
+++ b/tests/api-testing/REST/RevisionLegacy.js
@@ -0,0 +1,10 @@
+'use strict';
+
+describe( 'legacy Revision', () => {
+ // Cache deletion ensures tests will execute for both legacy and module paths
+ // Doing this twice protects against changes in test execution order
+ const testsFile = __dirname + '/content.v1/Revision.js';
+ delete require.cache[ testsFile ];
+ require( testsFile ).init( 'rest.php/v1' );
+ delete require.cache[ testsFile ];
+} );
diff --git a/tests/api-testing/REST/UpdateLegacy.js b/tests/api-testing/REST/UpdateLegacy.js
new file mode 100644
index 000000000000..e9e0e5b14920
--- /dev/null
+++ b/tests/api-testing/REST/UpdateLegacy.js
@@ -0,0 +1,10 @@
+'use strict';
+
+describe( 'legacy PUT /page/{title}', () => {
+ // Cache deletion ensures tests will execute for both legacy and module paths
+ // Doing this twice protects against changes in test execution order
+ const testsFile = __dirname + '/content.v1/Update.js';
+ delete require.cache[ testsFile ];
+ require( testsFile ).init( 'rest.php/v1' );
+ delete require.cache[ testsFile ];
+} );
diff --git a/tests/api-testing/REST/Creation.js b/tests/api-testing/REST/content.v1/Creation.js
index b2f23dfaf87e..9d77e35cf352 100644
--- a/tests/api-testing/REST/Creation.js
+++ b/tests/api-testing/REST/content.v1/Creation.js
@@ -3,8 +3,9 @@
const { action, assert, REST, utils } = require( 'api-testing' );
const supertest = require( 'supertest' );
+let pathPrefix = 'rest.php/content.v1';
+
describe( 'POST /page', () => {
- // NOTE: the /page/{title} endpoint (PageSourceHandler) has to go to v1 first!
let client, mindy, anon, anonToken;
beforeEach( async () => {
@@ -12,7 +13,7 @@ describe( 'POST /page', () => {
// In a temp account context, making an anonymous edit generates an account
// so we want to reset state after each edit
mindy = await action.mindy();
- client = new REST();
+ client = new REST( pathPrefix );
anon = await action.getAnon();
anonToken = await anon.token();
} );
@@ -257,3 +258,9 @@ describe( 'POST /page', () => {
} );
} );
+
+// eslint-disable-next-line mocha/no-exports
+exports.init = function ( pp ) {
+ // Allow testing both legacy and module paths using the same tests
+ pathPrefix = pp;
+};
diff --git a/tests/api-testing/REST/Page.js b/tests/api-testing/REST/content.v1/Page.js
index 474a5ed67a4e..bf9f1de2ec4f 100644
--- a/tests/api-testing/REST/Page.js
+++ b/tests/api-testing/REST/content.v1/Page.js
@@ -3,6 +3,8 @@
const { action, assert, REST, utils } = require( 'api-testing' );
const url = require( 'url' );
+let pathPrefix = 'rest.php/content.v1';
+
// Parse a URL-ref, which may or may not contain a protocol and host.
// WHATWG URL currently doesn't support partial URLs, see https://github.com/whatwg/url/issues/531
function parseURL( ref ) {
@@ -40,11 +42,13 @@ describe( 'Page Source', () => {
const redirectPage = utils.title( 'Redirect ' );
const redirectedPage = redirectPage.replace( 'Redirect', 'Redirected' );
- const client = new REST();
+ let client;
let mindy;
const baseEditText = "''Edit 1'' and '''Edit 2'''";
before( async () => {
+ client = new REST( pathPrefix );
+
mindy = await action.mindy();
await mindy.edit( page, { text: baseEditText } );
await mindy.edit( atinlayAgepay, { text: baseEditText } );
@@ -410,3 +414,9 @@ describe( 'Page Source', () => {
} );
} );
} );
+
+// eslint-disable-next-line mocha/no-exports
+exports.init = function ( pp ) {
+ // Allow testing both legacy and module paths using the same tests
+ pathPrefix = pp;
+};
diff --git a/tests/api-testing/REST/Revision.js b/tests/api-testing/REST/content.v1/Revision.js
index 8ef899422785..97cc0ab69664 100644
--- a/tests/api-testing/REST/Revision.js
+++ b/tests/api-testing/REST/content.v1/Revision.js
@@ -2,13 +2,16 @@
const { action, assert, REST, utils } = require( 'api-testing' );
+let pathPrefix = 'rest.php/content.v1';
+
describe( 'Revision', () => {
- const client = new REST();
const page = utils.title( 'Revision' );
+ let client;
let mindy;
let newrevid, pageid, param_summary;
before( async () => {
+ client = new REST( pathPrefix );
mindy = await action.mindy();
const resp = await mindy.edit( page, {
@@ -18,45 +21,6 @@ describe( 'Revision', () => {
( { newrevid, pageid, param_summary } = resp );
} );
- describe( 'GET /revision/{from}/compare/{to}', () => {
- const pageOne = utils.title( 'RevisionCompare_1' );
- const pageTwo = utils.title( 'RevisionCompare_2' );
- const nonExistentRevId = 99999;
- const validRevId = 1;
- const invalidRevId = '#';
-
- it( 'should return 400 if revision id is not an integer', async () => {
- const { status } = await client.get( `/revision/${ validRevId }/compare/${ invalidRevId }` );
- assert.equal( status, 400 );
- } );
-
- it( 'should successfully get diff between 2 valid revisions', async () => {
- // XXX: this test requires php-wikidiff2 1.10 or later to be installed
- const { newrevid: revId1 } = await mindy.edit( pageOne, { text: 'Mindy Edit 1' } );
- const { newrevid: revId2 } = await mindy.edit( pageOne, { text: 'Mindy Edit 2' } );
- const response = await client.get( `/revision/${ revId1 }/compare/${ revId2 }` );
- const { status, body, text, headers } = response;
- assert.strictEqual( status, 200, text );
- assert.match( headers[ 'content-type' ], /^application\/json/ );
- assert.nestedPropertyVal( body, 'from.id', revId1 );
- assert.nestedPropertyVal( body, 'to.id', revId2 );
- assert.nestedProperty( body, 'diff' );
- assert.isArray( body.diff );
- } );
-
- it( 'should return 404 for revision that does not exist', async () => {
- const { status } = await client.get( `/revision/${ validRevId }/compare/${ nonExistentRevId }` );
- assert.strictEqual( status, 404 );
- } );
-
- it( 'should return 400 if revision ids belong to different pages', async () => {
- const { newrevid: pageOneRev } = await mindy.edit( pageOne, { text: 'Page 1 edit' } );
- const { newrevid: pageTwoRev } = await mindy.edit( pageTwo, { text: 'Page 2 edit' } );
- const { status } = await client.get( `/revision/${ pageOneRev }/compare/${ pageTwoRev }` );
- assert.strictEqual( status, 400 );
- } );
- } );
-
describe( 'GET /revision/{id}', () => {
it( 'should successfully get source and metadata for revision', async () => {
const { status, body, text, headers } = await client.get( `/revision/${ newrevid }` );
@@ -186,5 +150,10 @@ describe( 'Revision', () => {
assert.match( headers.etag, /en-x-piglatin/i );
} );
} );
-
} );
+
+// eslint-disable-next-line mocha/no-exports
+exports.init = function ( pp ) {
+ // Allow testing both legacy and module paths using the same tests
+ pathPrefix = pp;
+};
diff --git a/tests/api-testing/REST/Update.js b/tests/api-testing/REST/content.v1/Update.js
index 56f2c04bcba5..20666cdc2bd0 100644
--- a/tests/api-testing/REST/Update.js
+++ b/tests/api-testing/REST/content.v1/Update.js
@@ -2,12 +2,14 @@
const { action, assert, REST, utils } = require( 'api-testing' );
+let pathPrefix = 'rest.php/content.v1';
+
describe( 'PUT /page/{title}', () => {
let client, mindy, mindyToken;
before( async () => {
mindy = await action.mindy();
- client = new REST( 'rest.php/v1', mindy );
+ client = new REST( pathPrefix, mindy );
mindyToken = await mindy.token();
} );
@@ -356,6 +358,11 @@ describe( 'PUT /page/{title}', () => {
assert.match( editHeader[ 'content-type' ], /^application\/json/ );
assert.nestedProperty( editBody, 'messageTranslations' );
} );
-
} );
} );
+
+// eslint-disable-next-line mocha/no-exports
+exports.init = function ( pp ) {
+ // Allow testing both legacy and module paths using the same tests
+ pathPrefix = pp;
+};