refactor(core): Reduce boilterplate code in between tests 🧹, and fix the tests in node.js 20 (no-changelog) (#6654)

refactor(core): Reduce boilterplate code in between tests

also cleaned up some imports, and fixed the tests in node.js 20
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-07-13 10:14:48 +02:00
committed by GitHub
parent 3e07ffa73e
commit b895ba438a
78 changed files with 1197 additions and 1597 deletions

View File

@@ -1,4 +1,3 @@
import type { Application } from 'express';
import type { SuperAgentTest } from 'supertest';
import { Container } from 'typedi';
import { License } from '@/License';
@@ -11,37 +10,27 @@ import type { User } from '@db/entities/User';
import { LOGGED_OUT_RESPONSE_BODY } from './shared/constants';
import { randomValidPassword } from './shared/random';
import * as testDb from './shared/testDb';
import type { AuthAgent } from './shared/types';
import * as utils from './shared/utils';
import * as utils from './shared/utils/';
let app: Application;
let globalOwnerRole: Role;
let globalMemberRole: Role;
let owner: User;
let authAgent: AuthAgent;
let authlessAgent: SuperAgentTest;
let authOwnerAgent: SuperAgentTest;
const ownerPassword = randomValidPassword();
beforeAll(async () => {
app = await utils.initTestServer({ endpointGroups: ['auth'] });
authAgent = utils.createAuthAgent(app);
const testServer = utils.setupTestServer({ endpointGroups: ['auth'] });
beforeAll(async () => {
globalOwnerRole = await testDb.getGlobalOwnerRole();
globalMemberRole = await testDb.getGlobalMemberRole();
});
beforeEach(async () => {
await testDb.truncate(['User']);
authlessAgent = utils.createAgent(app);
config.set('ldap.disabled', true);
await utils.setInstanceOwnerSetUp(true);
});
afterAll(async () => {
await testDb.terminate();
});
describe('POST /login', () => {
beforeEach(async () => {
owner = await testDb.createUser({
@@ -51,7 +40,7 @@ describe('POST /login', () => {
});
test('should log user in', async () => {
const response = await authlessAgent.post('/login').send({
const response = await testServer.authlessAgent.post('/login').send({
email: owner.email,
password: ownerPassword,
});
@@ -91,7 +80,7 @@ describe('POST /login', () => {
jest.spyOn(Container.get(License), 'isWithinUsersLimit').mockReturnValueOnce(false);
const member = await testDb.createUserShell(globalMemberRole);
const response = await authAgent(member).get('/login');
const response = await testServer.authAgentFor(member).get('/login');
expect(response.statusCode).toBe(401);
});
@@ -103,14 +92,14 @@ describe('POST /login', () => {
isOwner: true,
});
const response = await authAgent(ownerUser).get('/login');
const response = await testServer.authAgentFor(ownerUser).get('/login');
expect(response.statusCode).toBe(200);
});
});
describe('GET /login', () => {
test('should return 401 Unauthorized if no cookie', async () => {
const response = await authlessAgent.get('/login');
const response = await testServer.authlessAgent.get('/login');
expect(response.statusCode).toBe(401);
@@ -122,7 +111,7 @@ describe('GET /login', () => {
await testDb.createUserShell(globalOwnerRole);
await utils.setInstanceOwnerSetUp(false);
const response = await authlessAgent.get('/login');
const response = await testServer.authlessAgent.get('/login');
expect(response.statusCode).toBe(200);
@@ -131,9 +120,9 @@ describe('GET /login', () => {
});
test('should return 401 Unauthorized if invalid cookie', async () => {
authlessAgent.jar.setCookie(`${AUTH_COOKIE_NAME}=invalid`);
testServer.authlessAgent.jar.setCookie(`${AUTH_COOKIE_NAME}=invalid`);
const response = await authlessAgent.get('/login');
const response = await testServer.authlessAgent.get('/login');
expect(response.statusCode).toBe(401);
@@ -144,7 +133,7 @@ describe('GET /login', () => {
test('should return logged-in owner shell', async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const response = await authAgent(ownerShell).get('/login');
const response = await testServer.authAgentFor(ownerShell).get('/login');
expect(response.statusCode).toBe(200);
@@ -180,7 +169,7 @@ describe('GET /login', () => {
test('should return logged-in member shell', async () => {
const memberShell = await testDb.createUserShell(globalMemberRole);
const response = await authAgent(memberShell).get('/login');
const response = await testServer.authAgentFor(memberShell).get('/login');
expect(response.statusCode).toBe(200);
@@ -216,7 +205,7 @@ describe('GET /login', () => {
test('should return logged-in owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const response = await authAgent(owner).get('/login');
const response = await testServer.authAgentFor(owner).get('/login');
expect(response.statusCode).toBe(200);
@@ -252,7 +241,7 @@ describe('GET /login', () => {
test('should return logged-in member', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole });
const response = await authAgent(member).get('/login');
const response = await testServer.authAgentFor(member).get('/login');
expect(response.statusCode).toBe(200);
@@ -292,7 +281,7 @@ describe('GET /resolve-signup-token', () => {
password: ownerPassword,
globalRole: globalOwnerRole,
});
authOwnerAgent = authAgent(owner);
authOwnerAgent = testServer.authAgentFor(owner);
});
test('should validate invite token', async () => {
@@ -361,7 +350,7 @@ describe('POST /logout', () => {
test('should log user out', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const response = await authAgent(owner).post('/logout');
const response = await testServer.authAgentFor(owner).post('/logout');
expect(response.statusCode).toBe(200);
expect(response.body).toEqual(LOGGED_OUT_RESPONSE_BODY);