From b3a57228416f515006461aeb7586750a6842167f Mon Sep 17 00:00:00 2001 From: ricardo Date: Mon, 11 Apr 2022 21:17:05 -0400 Subject: [PATCH] :zap: Add POST /users tests --- .../publicApi/users.endpoints.test-api.ts | 84 ++++++++++++++++++- packages/cli/test/integration/shared/utils.ts | 8 +- 2 files changed, 85 insertions(+), 7 deletions(-) diff --git a/packages/cli/test/integration/publicApi/users.endpoints.test-api.ts b/packages/cli/test/integration/publicApi/users.endpoints.test-api.ts index 42cdce9e08..e6ce7b4b3a 100644 --- a/packages/cli/test/integration/publicApi/users.endpoints.test-api.ts +++ b/packages/cli/test/integration/publicApi/users.endpoints.test-api.ts @@ -73,7 +73,7 @@ beforeEach(async () => { config.set('userManagement.disabled', false); config.set('userManagement.isInstanceOwnerSetUp', true); - config.set('userManagement.emails.mode', ''); + config.set('userManagement.emails.mode', 'smtp'); }); afterAll(async () => { @@ -106,6 +106,8 @@ test('GET /users should fail due to invalid API Key', async () => { }); test('GET /users should fail due to member trying to access owner only endpoint', async () => { + config.set('userManagement.isInstanceOwnerSetUp', true); + const member = await testDb.createUser(); const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: member }); @@ -125,7 +127,7 @@ test('GET /users should fail due no instance owner not setup', async () => { const response = await authOwnerAgent.get('/v1/users'); - expect(response.statusCode).toBe(400); + expect(response.statusCode).toBe(500); }); @@ -220,7 +222,7 @@ test('GET /users/:identifier should fail due no instance owner not setup', async const response = await authOwnerAgent.get(`/v1/users/${owner.id}`); - expect(response.statusCode).toBe(400); + expect(response.statusCode).toBe(500); }); @@ -322,6 +324,82 @@ test('GET /users/:id should return a user', async () => { expect(updatedAt).toBeDefined(); }); +test('POST /users should fail due to missing API Key', async () => { + const owner = await Db.collections.User!.findOneOrFail(); + + const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: false, user: owner }); + + await testDb.createUser(); + + const response = await authOwnerAgent.post('/v1/users'); + + expect(response.statusCode).toBe(401); + +}); + +test('POST /users should fail due to invalid API Key', async () => { + const owner = await Db.collections.User!.findOneOrFail(); + + owner.apiKey = null; + + const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: false, user: owner }); + + const response = await authOwnerAgent.post('/v1/users'); + + expect(response.statusCode).toBe(401); +}); + +test('POST /users should fail due to member trying to access owner only endpoint', async () => { + + const member = await testDb.createUser(); + + const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: member }); + + const response = await authOwnerAgent.post('/v1/users').send([]); + + expect(response.statusCode).toBe(403); +}); + +test('POST /users should fail due instance owner not setup', async () => { + + config.set('userManagement.isInstanceOwnerSetUp', false); + + const owner = await Db.collections.User!.findOneOrFail(); + + const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner }); + + const response = await authOwnerAgent.post('/v1/users').send([]); + + expect(response.statusCode).toBe(500); + +}); + +test('POST /users should fail due smtp email not setup', async () => { + + config.set('userManagement.emails.mode', ''); + + const owner = await Db.collections.User!.findOneOrFail(); + + const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner }); + + const response = await authOwnerAgent.post('/v1/users').send([]); + + expect(response.statusCode).toBe(500); + +}); + +test('POST /users should fail due not valid body structure', async () => { + + const owner = await Db.collections.User!.findOneOrFail(); + + const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner }); + + const response = await authOwnerAgent.post('/v1/users').send({}); + + expect(response.statusCode).toBe(400); + +}); + const INITIAL_TEST_USER = { id: uuid(), email: randomEmail(), diff --git a/packages/cli/test/integration/shared/utils.ts b/packages/cli/test/integration/shared/utils.ts index 9b9758757b..f0aa98812b 100644 --- a/packages/cli/test/integration/shared/utils.ts +++ b/packages/cli/test/integration/shared/utils.ts @@ -23,7 +23,7 @@ import { passwordResetNamespace as passwordResetEndpoints } from '../../../src/U import { issueJWT } from '../../../src/UserManagement/auth/jwt'; import { getLogger } from '../../../src/Logger'; import { credentialsController } from '../../../src/api/credentials.api'; -import { publicApiController } from '../../../src/PublicApi/v1/'; +import { publicApi } from '../../../src/PublicApi/'; import type { User } from '../../../src/databases/entities/User'; import { Telemetry } from '../../../src/telemetry'; import type { ApiPath, EndpointGroup, SmtpTestAccount } from './types'; @@ -64,14 +64,14 @@ export function initTestServer({ const [routerEndpoints, functionEndpoints] = classifyEndpointGroups(endpointGroups); if (routerEndpoints.length) { - const map: Record = { + const map: Record = { credentials: credentialsController, - //publicApi: publicApiController, + publicApi, }; for (const group of routerEndpoints) { if (group === 'publicApi') { - testServer.app.use(`/${testServer.publicApiEndpoint}`, map[group]); + testServer.app.use(`/${testServer.publicApiEndpoint}`, ...map[group] as express.Router[]); } else { testServer.app.use(`/${testServer.restEndpoint}/${group}`, map[group]); }