diff --git a/.changeset/old-bottles-hide.md b/.changeset/old-bottles-hide.md new file mode 100644 index 00000000..6e6250b0 --- /dev/null +++ b/.changeset/old-bottles-hide.md @@ -0,0 +1,5 @@ +--- +"@openstapps/backend": patch +--- + +Backend unit tests break every year diff --git a/backend/backend/config/default/backend/index.js b/backend/backend/config/default/backend/index.js index 7a5b7279..8588f4d0 100644 --- a/backend/backend/config/default/backend/index.js +++ b/backend/backend/config/default/backend/index.js @@ -17,7 +17,7 @@ export const backend = { hiddenTypes: [SCThingType.DateSeries, SCThingType.Diff, SCThingType.Floor], mappingIgnoredTags: ['minlength', 'pattern', 'see', 'tjs-format'], maxMultiSearchRouteQueries: 5, - maxRequestBodySize: 2 * 10 ** 6, + maxRequestBodySize: 2e6, name: 'Goethe-Universität Frankfurt am Main', namespace: '909a8cbc-8520-456c-b474-ef1525f14209', sortableFields: [ diff --git a/backend/backend/test/routes/bulk-route.spec.ts b/backend/backend/test/routes/bulk-route.spec.ts index 64707752..cddf8262 100644 --- a/backend/backend/test/routes/bulk-route.spec.ts +++ b/backend/backend/test/routes/bulk-route.spec.ts @@ -14,6 +14,7 @@ * along with this program. If not, see . */ import { + SCBook, SCBulkAddRoute, SCBulkDoneRoute, SCBulkRequest, @@ -23,29 +24,30 @@ import { import {expect} from 'chai'; import {bulk, DEFAULT_TEST_TIMEOUT} from '../common.js'; import {testApp} from '../tests-setup.js'; -import {readFile} from 'fs/promises'; import {v4} from 'uuid'; +import bookFile from '@openstapps/core/test/resources/indexable/Book.2.json' assert {type: 'json'}; -const book = JSON.parse( - await readFile('node_modules/@openstapps/core/test/resources/indexable/Book.2.json', 'utf8'), -).instance; +const book = bookFile.instance as SCBook; describe('Bulk routes', async function () { // increase timeout for the suite this.timeout(DEFAULT_TEST_TIMEOUT); - const request: SCBulkRequest = { - expiration: bulk.expiration, - source: bulk.source, - type: bulk.type, - }; - const bulkRoute = new SCBulkRoute(); - const bulkAddRoute = new SCBulkAddRoute(); - const bulkDoneRoute = new SCBulkDoneRoute(); + let request: SCBulkRequest; + let bulkRoute: SCBulkRoute; + let bulkAddRoute: SCBulkAddRoute; + let bulkDoneRoute: SCBulkDoneRoute; - // afterEach(async function() { - // TODO: Delete saved bulks - // }); + before(function () { + request = { + expiration: bulk.expiration, + source: bulk.source, + type: bulk.type, + }; + bulkRoute = new SCBulkRoute(); + bulkAddRoute = new SCBulkAddRoute(); + bulkDoneRoute = new SCBulkDoneRoute(); + }); it('should create bulk', async function () { const {status, body, error} = await testApp diff --git a/backend/backend/test/routes/index-route.spec.ts b/backend/backend/test/routes/index-route.spec.ts index 5f70603e..a2ad6686 100644 --- a/backend/backend/test/routes/index-route.spec.ts +++ b/backend/backend/test/routes/index-route.spec.ts @@ -21,7 +21,12 @@ import {expect} from 'chai'; describe('Index route', async function () { // increase timeout for the suite this.timeout(DEFAULT_TEST_TIMEOUT); - const indexRoute = new SCIndexRoute(); + + let indexRoute: SCIndexRoute; + + before(function () { + indexRoute = new SCIndexRoute(); + }); it('should respond with both app and backend configuration', async function () { const request: SCIndexRequest = {}; diff --git a/backend/backend/test/routes/plugin-register-route.spec.ts b/backend/backend/test/routes/plugin-register-route.spec.ts index 1bf0d76f..ef0b7abd 100644 --- a/backend/backend/test/routes/plugin-register-route.spec.ts +++ b/backend/backend/test/routes/plugin-register-route.spec.ts @@ -30,15 +30,11 @@ import chaiAsPromised from 'chai-as-promised'; import {DEFAULT_TEST_TIMEOUT} from '../common.js'; import {testApp} from '../tests-setup.js'; import {backendConfig} from '../../src/config.js'; -import {readFile} from 'fs/promises'; +import registerRequest from '@openstapps/core/test/resources/PluginRegisterRequest.1.json' assert {type: 'json'}; // for using promises in expectations (to.eventually.be...) use(chaiAsPromised); -const registerRequest = JSON.parse( - await readFile('node_modules/@openstapps/core/test/resources/PluginRegisterRequest.1.json', 'utf8'), -); - // cast it because of "TS2322: Type 'string' is not assignable to type '"add"'" export const registerAddRequest: SCPluginAdd = registerRequest.instance as SCPluginAdd; diff --git a/backend/backend/test/routes/route.spec.ts b/backend/backend/test/routes/route.spec.ts index 35673d3f..e771eff0 100644 --- a/backend/backend/test/routes/route.spec.ts +++ b/backend/backend/test/routes/route.spec.ts @@ -47,8 +47,8 @@ describe('Create route', async function () { const statusCodeSuccess = 222; const bodySuccess = {foo: true}; const sandbox = sinon.createSandbox(); - const validationError = new SCValidationErrorResponse([]); - const internalServerError = new SCInternalServerErrorResponse(); + let validationError: SCValidationErrorResponse; + let internalServerError: SCInternalServerErrorResponse; beforeEach(function () { app = express(); @@ -64,6 +64,9 @@ describe('Create route', async function () { handler = (_request, _app) => { return Promise.resolve(bodySuccess); }; + + validationError = new SCValidationErrorResponse([]); + internalServerError = new SCInternalServerErrorResponse(); }); afterEach(function () { diff --git a/backend/backend/test/routes/search-route.spec.ts b/backend/backend/test/routes/search-route.spec.ts index bd700ed2..2cf97008 100644 --- a/backend/backend/test/routes/search-route.spec.ts +++ b/backend/backend/test/routes/search-route.spec.ts @@ -29,11 +29,19 @@ import {backendConfig} from '../../src/config.js'; describe('Search route', async function () { // increase timeout for the suite this.timeout(DEFAULT_TEST_TIMEOUT); - const searchRoute = new SCSearchRoute(); - const multiSearchRoute = new SCMultiSearchRoute(); - const syntaxError = new SCSyntaxErrorResponse('Foo Message'); - const methodNotAllowedError = new SCMethodNotAllowedErrorResponse(); - const tooManyRequestsError = new SCTooManyRequestsErrorResponse(); + let searchRoute: SCSearchRoute; + let multiSearchRoute: SCMultiSearchRoute; + let syntaxError: SCSyntaxErrorResponse; + let methodNotAllowedError: SCMethodNotAllowedErrorResponse; + let tooManyRequestsError: SCTooManyRequestsErrorResponse; + + before(function () { + searchRoute = new SCSearchRoute(); + multiSearchRoute = new SCMultiSearchRoute(); + syntaxError = new SCSyntaxErrorResponse('Foo Message'); + methodNotAllowedError = new SCMethodNotAllowedErrorResponse(); + tooManyRequestsError = new SCTooManyRequestsErrorResponse(); + }); it('should reject GET, PUT with a valid search query', async function () { // const expectedParams = JSON.parse(JSON.stringify(defaultParams)); diff --git a/backend/backend/test/routes/thing-update-route.spec.ts b/backend/backend/test/routes/thing-update-route.spec.ts index 173bae21..8c70c91c 100644 --- a/backend/backend/test/routes/thing-update-route.spec.ts +++ b/backend/backend/test/routes/thing-update-route.spec.ts @@ -13,23 +13,25 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import {SCThingUpdateRoute} from '@openstapps/core'; +import {SCBook, SCThingUpdateRoute} from '@openstapps/core'; import chaiAsPromised from 'chai-as-promised'; import {bulkStorageMock, DEFAULT_TEST_TIMEOUT} from '../common.js'; import {expect, use} from 'chai'; import {testApp} from '../tests-setup.js'; -import {readFile} from 'fs/promises'; +import bookFile from '@openstapps/core/test/resources/indexable/Book.1.json' assert {type: 'json'}; use(chaiAsPromised); -const book = JSON.parse( - await readFile('node_modules/@openstapps/core/test/resources/indexable/Book.1.json', 'utf8'), -).instance; +const book = bookFile.instance as SCBook; describe('Thing update route', async function () { // increase timeout for the suite this.timeout(DEFAULT_TEST_TIMEOUT); - const thingUpdateRoute = new SCThingUpdateRoute(); + let thingUpdateRoute: SCThingUpdateRoute; + + before(function () { + thingUpdateRoute = new SCThingUpdateRoute(); + }); it('should update a thing', async function () { const thingUpdateRouteurlPath = thingUpdateRoute.urlPath diff --git a/backend/backend/test/storage/elasticsearch/elasticsearch.spec.ts b/backend/backend/test/storage/elasticsearch/elasticsearch.spec.ts index 1ab1f6b1..95efb397 100644 --- a/backend/backend/test/storage/elasticsearch/elasticsearch.spec.ts +++ b/backend/backend/test/storage/elasticsearch/elasticsearch.spec.ts @@ -39,7 +39,6 @@ import {Elasticsearch} from '../../../src/storage/elasticsearch/elasticsearch.js import {bulk, DEFAULT_TEST_TIMEOUT, getTransport, getIndex} from '../../common.js'; import fs from 'fs'; import {backendConfig} from '../../../src/config.js'; -import {readFile} from 'fs/promises'; import { ACTIVE_INDICES_ALIAS, getIndexUID, @@ -50,6 +49,11 @@ import { } from '../../../src/storage/elasticsearch/util/index.js'; import cron from 'node-cron'; import {query} from './query.js'; +import messageFile from '@openstapps/core/test/resources/indexable/Message.1.json' assert {type: 'json'}; +import bookFile from '@openstapps/core/test/resources/indexable/Book.1.json' assert {type: 'json'}; + +const message = messageFile.instance as SCMessage; +const book = bookFile.instance as SCBook; use(chaiAsPromised); @@ -60,13 +64,6 @@ function searchResponse(...hits: SearchHit[]): SearchResponse { return {hits: {hits}, took: 0, timed_out: false, _shards: {total: 1, failed: 0, successful: 1}}; } -const message = JSON.parse( - await readFile('node_modules/@openstapps/core/test/resources/indexable/Message.1.json', 'utf8'), -); -const book = JSON.parse( - await readFile('node_modules/@openstapps/core/test/resources/indexable/Book.1.json', 'utf8'), -); - describe('Elasticsearch', function () { // increase timeout for the suite this.timeout(DEFAULT_TEST_TIMEOUT); @@ -74,8 +71,15 @@ describe('Elasticsearch', function () { before(function () { // eslint-disable-next-line no-console - console.log('before'); sandbox.stub(fs, 'readFileSync').returns('{}'); + sandbox.stub(backendConfig.internal.boostings.default[0], 'fields').value({ + 'academicTerms.acronym': { + 'SS 2023': 1.05, + 'WS 2023/24': 1.1, + 'SoSe 2023': 1.05, + 'WiSe 2023/24': 1.1, + }, + }); }); after(function () { sandbox.restore(); @@ -445,7 +449,7 @@ describe('Elasticsearch', function () { _id: '', _index: '', _score: 0, - _source: message as SCMessage, + _source: message, }; sandbox.stub(es.client, 'search').resolves(searchResponse(foundObject)); @@ -475,7 +479,7 @@ describe('Elasticsearch', function () { const object: SearchHit = { _id: '', _index: oldIndex, - _source: message as SCMessage, + _source: message, }; sandbox.stub(es.client, 'search').resolves(searchResponse(object)); sandbox.stub(es, 'prepareBulkWrite').resolves(index); @@ -489,7 +493,7 @@ describe('Elasticsearch', function () { sandbox.stub(es.client, 'create').resolves({result: 'not_found'} as CreateResponse); await es.init(); - return expect(es.post(message as SCMessage, bulk)).to.rejectedWith('creation'); + return expect(es.post(message, bulk)).to.rejectedWith('creation'); }); it('should create a new object', async function () { @@ -502,7 +506,7 @@ describe('Elasticsearch', function () { }); await es.init(); - await es.post(message as SCMessage, bulk); + await es.post(message, bulk); expect(createStub.called).to.be.true; expect(caughtParameter.document).to.be.eql({ @@ -527,7 +531,7 @@ describe('Elasticsearch', function () { _id: '', _index: getIndex(), _score: 0, - _source: message as SCMessage, + _source: message, }; sandbox.stub(es.client, 'search').resolves(searchResponse()); @@ -541,7 +545,7 @@ describe('Elasticsearch', function () { _id: '', _index: getIndex(), _score: 0, - _source: message as SCMessage, + _source: message, }; sandbox.stub(es.client, 'search').resolves(searchResponse(object)); // @ts-expect-error unused @@ -564,13 +568,13 @@ describe('Elasticsearch', function () { _id: '123', _index: getIndex(), _score: 0, - _source: message as SCMessage, + _source: message, }; const objectBook: SearchHit = { _id: '321', _index: getIndex(), _score: 0, - _source: book as SCBook, + _source: book, }; const fakeEsAggregations = { '@all': {