Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save entity process transactional #7627

Draft
wants to merge 18 commits into
base: production
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/api/activitylog/specs/activitylogParser.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-lines */
import { testingEnvironment } from 'api/utils/testingEnvironment';
/* eslint-disable max-statements */

import db from 'api/utils/testing_db';
Expand Down Expand Up @@ -27,11 +28,11 @@ describe('Activitylog Parser', () => {
action: 'MIGRATE',
description: 'Dummy log',
});
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

afterAll(async () => {
await db.disconnect();
await testingEnvironment.tearDown();
});

async function testBeautified(log, expected) {
Expand Down
5 changes: 3 additions & 2 deletions app/api/auth/specs/captchaMiddleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import db from 'api/utils/testing_db';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import { NextFunction } from 'express';
import captchaMiddleware from '../captchaMiddleware';
import { CaptchaModel } from '../CaptchaModel';
Expand Down Expand Up @@ -26,10 +27,10 @@ describe('captchaMiddleware', () => {
captchas: [{ _id: captchaId, text: 'k0n2170' }],
};

await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

afterAll(async () => db.disconnect());
afterAll(async () => testingEnvironment.tearDown());

it('should return an error when there is no captcha in the request', async () => {
const middleWare = captchaMiddleware();
Expand Down
8 changes: 4 additions & 4 deletions app/api/auth/specs/publicAPIMiddleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testingDB } from 'api/utils/testing_db';
import { publicAPIMiddleware } from '../publicAPIMiddleware';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import * as auth from '../index';
import { publicAPIMiddleware } from '../publicAPIMiddleware';

jest.mock('../index', () => ({
captchaAuthorization: jest.fn(),
Expand All @@ -10,7 +10,7 @@ describe('publicAPIMiddleware', () => {
let captchaMock: jest.Mock<any, any>;

const setUpSettings = async (open: boolean) =>
testingDB.clearAllAndLoad({
testingEnvironment.setUp({
settings: [
{
openPublicEndpoint: open,
Expand All @@ -24,7 +24,7 @@ describe('publicAPIMiddleware', () => {
captchaMock.mockReset();
});

afterAll(async () => testingDB.disconnect());
afterAll(async () => testingEnvironment.tearDown());

it('should bypass captcha if enabled on settings and request has corresponding header', async () => {
await setUpSettings(true);
Expand Down
15 changes: 7 additions & 8 deletions app/api/auth/specs/routes.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import express from 'express';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import bodyParser from 'body-parser';
import express from 'express';
import request from 'supertest';

import db from 'api/utils/testing_db';

import users from 'api/users/users';
import svgCaptcha from 'svg-captcha';
import backend from 'fetch-mock';
import svgCaptcha from 'svg-captcha';
import instrumentRoutes from '../../utils/instrumentRoutes';
import { CaptchaModel } from '../CaptchaModel';
import { comparePasswords } from '../encryptPassword';
import authRoutes from '../routes';
import fixtures from './fixtures.js';
import instrumentRoutes from '../../utils/instrumentRoutes';
import { comparePasswords } from '../encryptPassword';

describe('Auth Routes', () => {
let routes;
let app;

beforeEach(async () => {
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
routes = instrumentRoutes(authRoutes);
});

afterAll(async () => {
await db.disconnect();
await testingEnvironment.tearDown();
});

describe('/login', () => {
Expand Down
5 changes: 3 additions & 2 deletions app/api/auth2fa/specs/usersUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @format */
import { testingEnvironment } from 'api/utils/testingEnvironment';

import * as otplib from 'otplib';

Expand All @@ -18,11 +19,11 @@ type Error = { code: number; message: string };

describe('auth2fa userUtils', () => {
beforeEach(async () => {
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

afterAll(async () => {
await db.disconnect();
await testingEnvironment.tearDown();
});

const expectError = async (method: string, _id: any, token: string, err: Error) => {
Expand Down
10 changes: 5 additions & 5 deletions app/api/contact/specs/contact.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* eslint-disable max-nested-callbacks */
import mailer from 'api/utils/mailer';
import db from 'api/utils/testing_db';
import settings from 'api/settings';
import fixtures from './fixtures.js';
import mailer from 'api/utils/mailer';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import contact from '../contact';
import fixtures from './fixtures.js';

describe('contact', () => {
beforeEach(async () => {
jest.spyOn(mailer, 'send').mockImplementation(async () => Promise.resolve());
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

afterAll(done => {
db.disconnect().then(done);
testingEnvironment.tearDown().then(done);
});

describe('sendMessage', () => {
Expand Down
25 changes: 14 additions & 11 deletions app/api/csv/specs/csvLoader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import entities from 'api/entities';
import translations from 'api/i18n';
import { search } from 'api/search';
import settings from 'api/settings';
import db from 'api/utils/testing_db';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import moment from 'moment';
import typeParsers from '../typeParsers';
import fixtures, { template1Id } from './csvLoaderFixtures';
import { mockCsvFileReadStream } from './helpers';
import testingDB from 'api/utils/testing_db';

describe('csvLoader', () => {
const csvFile = path.join(__dirname, '/test.csv');
const loader = new CSVLoader();

beforeAll(async () => {
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

beforeEach(() => {
Expand All @@ -28,7 +29,7 @@ describe('csvLoader', () => {
jest.spyOn(entities, 'save').mockImplementation(async e => e);
});

afterAll(async () => db.disconnect());
afterAll(async () => testingEnvironment.tearDown());

describe('user', () => {
it('should use the passed user', async () => {
Expand All @@ -41,7 +42,7 @@ describe('csvLoader', () => {
let csv;
let readStreamMock;
beforeEach(async () => {
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);

const nonExistent = 'Russian';

Expand Down Expand Up @@ -128,7 +129,7 @@ describe('csvLoader', () => {

beforeAll(async () => {
jest.restoreAllMocks();
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
loader.on('entityLoaded', entity => {
events.push(entity.title);
});
Expand Down Expand Up @@ -219,7 +220,7 @@ describe('csvLoader', () => {
it('should stop processing on the first error', async () => {
const testingLoader = new CSVLoader();

await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
jest.spyOn(entities, 'save').mockImplementation(entity => {
throw new Error(`error-${entity.title}`);
});
Expand All @@ -234,7 +235,7 @@ describe('csvLoader', () => {
it('should throw the error that occurred even if it was not the first row', async () => {
const testingLoader = new CSVLoader();

await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
jest
.spyOn(entities, 'save')
.mockImplementationOnce(({ title }) => Promise.resolve({ title }))
Expand All @@ -257,7 +258,7 @@ describe('csvLoader', () => {
}
return entity;
});
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

it('should emit an error', async () => {
Expand Down Expand Up @@ -317,7 +318,7 @@ describe('csvLoader', () => {
describe('when sharedId is provided', () => {
beforeEach(async () => {
jest.restoreAllMocks();
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

it('should update the entity', async () => {
Expand All @@ -344,7 +345,7 @@ describe('csvLoader', () => {
describe('when the title is not provided', () => {
beforeEach(async () => {
jest.restoreAllMocks();
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
});

describe('title not marked with generated Id option', () => {
Expand Down Expand Up @@ -418,7 +419,9 @@ describe('csvLoader', () => {
dateFormat,
},
];
await db.setupFixturesAndContext(_fixtures);

await testingDB.setupFixturesAndContext(_fixtures);
testingEnvironment.setFakeContext();
};

it('should correctly parse MM/dd/yyyy', async () => {
Expand Down
6 changes: 3 additions & 3 deletions app/api/csv/specs/csvLoaderLanguages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as filesystem from 'api/files/filesystem';
import { uploadsPath } from 'api/files/filesystem';
import { search } from 'api/search';
import settings from 'api/settings';
import db from 'api/utils/testing_db';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import path from 'path';
import { EntitySchema } from 'shared/types/entityType';

Expand All @@ -21,7 +21,7 @@ describe('csvLoader languages', () => {
const loader = new CSVLoader();

beforeAll(async () => {
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
await filesystem.setupTestUploadedPaths('csvLoader');
jest.spyOn(translations, 'updateContext').mockImplementation(async () => 'ok');
jest.spyOn(search, 'indexEntities').mockImplementation(async () => Promise.resolve());
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('csvLoader languages', () => {
]);

await removeTestingZip();
await db.disconnect();
await testingEnvironment.tearDown();
});

it('should import entities in the diferent languages', async () => {
Expand Down
12 changes: 6 additions & 6 deletions app/api/csv/specs/csvLoaderThesauri.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import db from 'api/utils/testing_db';
import thesauri from 'api/thesauri';
import translations from 'api/i18n';
import settings from 'api/settings';
import thesauri from 'api/thesauri';
import { testingEnvironment } from 'api/utils/testingEnvironment';

import { IndexedContextValues } from 'api/i18n/translations';
import { WithId } from 'api/odm';
import { ObjectId } from 'mongodb';
import { ThesaurusSchema } from 'shared/types/thesaurusType';
import { WithId } from 'api/odm';
import { IndexedContextValues } from 'api/i18n/translations';
import { CSVLoader } from '../csvLoader';
import { fixtures, thesauri1Id } from './fixtures';
import { mockCsvFileReadStream } from './helpers';
Expand All @@ -19,13 +19,13 @@ const getTranslation = async (lang: string, id: ObjectId) =>
describe('csvLoader thesauri', () => {
const loader = new CSVLoader();

afterAll(async () => db.disconnect());
afterAll(async () => testingEnvironment.tearDown());

let thesauriId: ObjectId;
let result: WithId<ThesaurusSchema>;
describe('load thesauri', () => {
beforeAll(async () => {
await db.clearAllAndLoad(fixtures);
await testingEnvironment.setUp(fixtures);

await settings.addLanguage({ key: 'es', label: 'spanish' });
await translations.addLanguage('es');
Expand Down
6 changes: 3 additions & 3 deletions app/api/csv/specs/csvLoaderZip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import db from 'api/utils/testing_db';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import { files } from 'api/files/files';
import { search } from 'api/search';
import path from 'path';
Expand All @@ -20,7 +20,7 @@ describe('csvLoader zip file', () => {
beforeAll(async () => {
const zip = path.join(__dirname, '/zipData/test.zip');
const loader = new CSVLoader();
await db.setupFixturesAndContext(fixtures);
await testingEnvironment.setUp(fixtures);
await filesystem.setupTestUploadedPaths('csvLoaderZip');
await createTestingZip(
[
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('csvLoader zip file', () => {
filesystem.attachmentsPath('generatedatt2.doc'),
]);
await removeTestingZip();
await db.disconnect();
await testingEnvironment.tearDown();
});

it('should save files into uploaded_documents', async () => {
Expand Down
6 changes: 3 additions & 3 deletions app/api/csv/typeParsers/specs/multiselect.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @format */
import { testingEnvironment } from 'api/utils/testingEnvironment';

import thesauri from 'api/thesauri';
import db from 'api/utils/testing_db';

import { fixtures, thesauri1Id } from '../../specs/fixtures';
import typeParsers from '../../typeParsers';
Expand All @@ -17,9 +17,9 @@ describe('multiselect', () => {

const templateProp = { name: 'multiselect_prop', content: thesauri1Id };

afterAll(async () => db.disconnect());
afterAll(async () => testingEnvironment.tearDown());
beforeAll(async () => {
await db.clearAllAndLoad(fixtures);
await testingEnvironment.setUp(fixtures);

thesauri1 = await thesauri.getById(thesauri1Id);
});
Expand Down
5 changes: 3 additions & 2 deletions app/api/csv/typeParsers/specs/relationship.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import entities, { model } from 'api/entities';
import { search } from 'api/search';
import db from 'api/utils/testing_db';
import { testingEnvironment } from 'api/utils/testingEnvironment';

import { fixtures, templateToRelateId } from '../../specs/fixtures';
import typeParsers from '../../typeParsers';
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('relationship', () => {
};

beforeAll(async () => {
await db.clearAllAndLoad(fixtures);
await testingEnvironment.setUp(fixtures);

jest.spyOn(search, 'indexEntities').mockImplementation(async () => Promise.resolve());
await prepareExtraFixtures();
Expand All @@ -81,7 +82,7 @@ describe('relationship', () => {
entitiesRelated = await entities.get({ template: templateToRelateId, language: 'en' });
});

afterAll(async () => db.disconnect());
afterAll(async () => testingEnvironment.tearDown());

it('should create entities and return the ids', async () => {
expect(entitiesRelated[0].title).toBe('value1');
Expand Down
Loading
Loading