Add POST /users tests

This commit is contained in:
ricardo 2022-04-11 21:17:05 -04:00
parent b47056ca4e
commit b3a5722841
2 changed files with 85 additions and 7 deletions

View file

@ -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(),

View file

@ -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<string, express.Router> = {
const map: Record<string, express.Router | express.Router[]> = {
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]);
}