Skip to content
Snippets Groups Projects
Commit 8f620abd authored by Embruch, Gerd's avatar Embruch, Gerd
Browse files

added tests for auth/requestverification

parent 9d99b2ba
Branches
No related tags found
No related merge requests found
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`user send registration token > given the email format is invalid > should respond with a proper body 1`] = `
{
"message": "Validation errors. Please check the error messages.",
"validationErrors": {
"email": "Invalid email",
},
}
`;
exports[`user send registration token > given the email is unknown > should respond with a proper body 1`] = `
{
"message": "Unknown eMail address",
}
`;
exports[`user send registration token > given the inputs are valid > should respond with a proper body 1`] = `
{
"message": "Check your emails for the verification link.",
}
`;
exports[`user send registration token > the request body is empty > should respond with a proper body 1`] = `
{
"message": "Validation errors. Please check the error messages.",
"validationErrors": {
"email": "Required",
},
}
`;
......@@ -2,62 +2,55 @@
import { vi, beforeAll, beforeEach, describe, expect, expectTypeOf, test, it, afterEach } from 'vitest';
import supertest from "supertest";
import app from "../../app.js";
// ignore expiration of the (self-signed) certificate
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
// set timeout
const BEFORE_ALL_TIMEOUT = 30000; // 30 sec
// set route
const ROUTE = '/users/requestverification';
const ROUTE = '/auth/verification';
// prepare response of each test
let response;
// ############################
// OBJECTS
// ############################
const adminLoginResponse = {
admin: {
id: '6gll42mzqhdg69w',
created: '2024-05-02 13:17:09.684Z',
updated: '2024-05-02 13:17:09.684Z',
avatar: 0,
email: 'demo.admin@local.local'
const mockedVals = vi.hoisted(() => {
return {
foundUser: {
_doc: {
_id: '66a29da2942b3eb',
username: 'snoopy',
name: 'My User',
email: 'user@mail.local',
verified: true,
role: 0,
createdAt: '2024-07 - 25T18: 46: 58.982Z',
updatedAt: '2024-07 - 25T18: 46: 58.982Z',
__v: 0,
fullname: '',
id: '66a29da2942b3ebcaf047f07'
}
},
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjE4NDQ2NDAsImlkIjoiNmdsbDQybXpxaGRnNjl3IiwidHlwZSI6ImFkbWluIn0.QsaIDvhiJ7Vn4_q3TiO0PYcA5P6fMhSXPJQnZAhboJ0'
};
const userFoundResponse = {
avatar: '',
collectionId: '_pb_users_auth_',
collectionName: 'users',
created: '2024-05-06 07:45:18.836Z',
email: 'demo.user@local.local',
emailVisibility: false,
id: 'jr9mt8yvuri3sbd',
name: 'Demo User',
updated: '2024-07-02 13:23:52.155Z',
username: 'duser',
verified: true
};
const userNotFoundResponse = {
code: 404,
message: "The requested resource wasn't found.",
data: {}
validInput: {
email: 'john.doe@local.local'
}
};
});
// ############################
// MOCKS
// ############################
// import PocketBase Service
import * as pbService from '../../utils/pocketbase/handlePocketBase.js';
// mock pbService
vi.mock('../../utils/pocketbase/handlePocketBase.js', async (importOriginal) => {
// import Database Service
import * as dbService from '../../utils/handleDB.js';
// mock dbService
vi.mock('../../utils/handleDB.js', async (importOriginal) => {
return {
...await importOriginal(),
pbAdminLogin: vi.fn(() => adminLoginResponse),
pbGetUser: vi.fn(() => userFoundResponse),
pbRequestVerification: vi.fn(() => 'mocked'),
pbClearAuthStore: vi.fn(() => 'mocked'),
dbConnection: vi.fn(() => 'mocked'),
findOneRecord: vi.fn(() => mockedVals.foundUser),
};
});
// mock mailer
vi.mock('../../utils/handleMailer.js', async (importOriginal) => {
return {
...await importOriginal(),
sendEmail: vi.fn(() => 'mocked')
};
});
......@@ -70,79 +63,69 @@ describe('user send registration token', () => {
beforeAll(async () => {
response = await supertest(app)
.post(ROUTE)
.send({ email: 'valid.email@local.local' });
}, BEFORE_ALL_TIMEOUT);
it('should call required mocks', () => {
expect(pbService.pbAdminLogin()).toEqual(adminLoginResponse);
expect(pbService.pbGetUser()).toEqual(userFoundResponse);
.send(mockedVals.validInput);
});
it('should return a proper status code', () => {
expect(response.status).toBe(200);
expect(response.status).toBe(201);
});
it('should respond with a proper message', () => {
expect(response.body.message).toEqual('If the email **valid.email@local.local** is correct you will receive an eMail with further instructions.');
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
// ############################
describe('given the email is invalid', async () => {
describe('given the email is unknown', async () => {
// set response by running route
beforeAll(async () => {
let error = new Error();
error.name = 'PBError';
error.response = userNotFoundResponse;
error.status = 400;
pbService.pbGetUser.mockImplementation(() => { throw error; });
beforeAll(async ({ expect, task }) => {
dbService.findOneRecord.mockImplementationOnce(() => null);
response = await supertest(app)
.post(ROUTE)
.send({ email: 'nonexistent@local.local' });
}, BEFORE_ALL_TIMEOUT);
it('should force getUser to throw an error', () => {
expect(pbService.pbGetUser).toThrowError();
.send(mockedVals.validInput);
});
it('should return a proper status code', () => {
expect(response.status).toBe(200);
expect(response.status).toBe(404);
});
it('should respond with a proper message', () => {
expect(response.body.message).toEqual('If the email **nonexistent@local.local** is correct you will receive an eMail with further instructions.');
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
// ############################
describe('given the email format is invalid', async () => {
// set response by running route
describe('given the email format is invalid', () => {
beforeAll(async () => {
const input = { ...mockedVals.validInput, email: 'invalid-email-format' };
response = await supertest(app)
.post(ROUTE)
.send({ email: 'invalid-email' });
}, BEFORE_ALL_TIMEOUT);
it('should return a proper status code', () => {
.send(input);
});
it('should return a proper status code status', () => {
expect(response.status).toBe(400);
});
it('should respond with a proper message', () => {
expect(response.body.validationErrors.email).toEqual('Invalid email');
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
// ############################
describe('given the request body is empty', async () => {
// set response by running route
describe('the request body is empty', () => {
beforeAll(async () => {
response = await supertest(app)
.post(ROUTE)
.send();
}, BEFORE_ALL_TIMEOUT);
it('should return a proper status code', () => {
});
it('should return a proper status code status', () => {
expect(response.status).toBe(400);
});
it('should respond with a proper message', () => {
expect(response.body.validationErrors.email).toEqual('Required');
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment