From d508850bc23b4b0e9133f63da5d1f33a8c0be541 Mon Sep 17 00:00:00 2001 From: Ollie Date: Tue, 25 Feb 2025 12:10:31 +0000 Subject: [PATCH] REST: Fix flaky mocha tests caused by AuthTest.js Occasionally, mocha tests in the `mediawiki-quibble-apitests-vendor-php74` CI job fail with the following error: ``` { code: 'permission-denied', message: 'Access to resource is denied', context: { denial_reason: 'resource-protected' } } ``` I suspect this is due to tests in `AuthTest.js` protecting an item/property that is also used in other tests. `AuthTest.js` uses the `describeWithTestData()` helper function which in turn uses the `getItemId()` helper function which enable the reuse of a single Item across tests. Adding a `createNewItem` param to `describeWithTestData()` allows `AuthTest.js` to request that a new Item is created for its tests. Change-Id: I0ba157a6bffdf8facc9a99905ae9287e71f8d4e1 --- repo/domains/crud/tests/mocha/api-testing/AuthTest.js | 3 +-- .../crud/tests/mocha/helpers/describeWithTestData.js | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/repo/domains/crud/tests/mocha/api-testing/AuthTest.js b/repo/domains/crud/tests/mocha/api-testing/AuthTest.js index 2e2eace703..e180b2cc17 100644 --- a/repo/domains/crud/tests/mocha/api-testing/AuthTest.js +++ b/repo/domains/crud/tests/mocha/api-testing/AuthTest.js @@ -66,7 +66,6 @@ describeWithTestData( 'Auth', ( itemRequestInputs, propertyRequestInputs, descri } ); describe( 'Authorization', () => { - describeEachRouteWithReset( [ ...editRequests, @@ -159,4 +158,4 @@ describeWithTestData( 'Auth', ( itemRequestInputs, propertyRequestInputs, descri expect( response ).to.have.status( 403 ); } ); } ); -} ); +}, true ); // create a new Item for the Auth tests diff --git a/repo/domains/crud/tests/mocha/helpers/describeWithTestData.js b/repo/domains/crud/tests/mocha/helpers/describeWithTestData.js index 70a83873ef..f1336809de 100644 --- a/repo/domains/crud/tests/mocha/helpers/describeWithTestData.js +++ b/repo/domains/crud/tests/mocha/helpers/describeWithTestData.js @@ -12,6 +12,7 @@ const { } = require( './entityHelper' ); const { getAllowedBadges } = require( './getAllowedBadges' ); const { + newCreateItemRequestBuilder, newPatchItemRequestBuilder, newPatchPropertyRequestBuilder } = require( './RequestBuilderFactory' ); @@ -31,9 +32,10 @@ const { expect } = require( '../../../../../rest-api/tests/mocha/helpers/chaiHel * * @param {string} testName * @param {Function} runAllTests + * @param {boolean} createNewItem * @return {void} */ -function describeWithTestData( testName, runAllTests ) { +function describeWithTestData( testName, runAllTests, createNewItem = false ) { const itemRequestInputs = {}; const propertyRequestInputs = {}; const originalLinkedArticle = utils.title( 'Original-article-linked-to-test-item' ); @@ -86,7 +88,8 @@ function describeWithTestData( testName, runAllTests ) { await createWikiPage( newLinkedArticle, 'sitelink test' ); const statementPropertyId = await getStringPropertyId(); - const itemId = await getItemId(); + const itemId = createNewItem ? + ( await newCreateItemRequestBuilder( {} ).makeRequest() ).body.id : await getItemId(); const item = await resetEntityTestData( itemId, statementPropertyId, originalLinkedArticle ); itemRequestInputs.mainTestSubject = itemId; itemRequestInputs.itemId = itemId; @@ -101,7 +104,6 @@ function describeWithTestData( testName, runAllTests ) { propertyRequestInputs.propertyId = propertyId; propertyRequestInputs.statementId = property.statements[ statementPropertyId ][ 0 ].id; propertyRequestInputs.statementPropertyId = statementPropertyId; - } ); runAllTests( itemRequestInputs, propertyRequestInputs, describeEachRouteWithReset );