diff --git a/__tests__/users/__snapshots__/confirmverification.test.js.snap b/__tests__/auth/__snapshots__/confirmverification.test.js.snap similarity index 100% rename from __tests__/users/__snapshots__/confirmverification.test.js.snap rename to __tests__/auth/__snapshots__/confirmverification.test.js.snap diff --git a/__tests__/auth/__snapshots__/requestverification.test.js.snap b/__tests__/auth/__snapshots__/requestverification.test.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..0c2f61c0a39077df12125c7468692134c6fb7830 --- /dev/null +++ b/__tests__/auth/__snapshots__/requestverification.test.js.snap @@ -0,0 +1,31 @@ +// 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", + }, +} +`; diff --git a/__tests__/users/confirmverification.test.js b/__tests__/auth/confirmverification.test.js similarity index 100% rename from __tests__/users/confirmverification.test.js rename to __tests__/auth/confirmverification.test.js diff --git a/__tests__/auth/requestverification.test.js b/__tests__/auth/requestverification.test.js new file mode 100644 index 0000000000000000000000000000000000000000..662f16627df5599adf8ce389ed75160afa7106db --- /dev/null +++ b/__tests__/auth/requestverification.test.js @@ -0,0 +1,132 @@ +// import vitest, supertest & app +import { vi, beforeAll, beforeEach, describe, expect, expectTypeOf, test, it, afterEach } from 'vitest'; +import supertest from "supertest"; +import app from "../../app.js"; +// set route +const ROUTE = '/auth/verification'; +// prepare response of each test +let response; + +// ############################ +// OBJECTS +// ############################ +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' + } + }, + validInput: { + email: 'john.doe@local.local' + } + }; +}); + +// ############################ +// MOCKS +// ############################ +// import Database Service +import * as dbService from '../../utils/handleDB.js'; +// mock dbService +vi.mock('../../utils/handleDB.js', async (importOriginal) => { + return { + ...await importOriginal(), + 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') + }; +}); + +// ############################ +// TESTS +// ############################ +describe('user send registration token', () => { + describe('given the inputs are valid', async () => { + // set response by running route + beforeAll(async () => { + response = await supertest(app) + .post(ROUTE) + .send(mockedVals.validInput); + }); + it('should return a proper status code', () => { + expect(response.status).toBe(201); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + + // ############################ + + describe('given the email is unknown', async () => { + // set response by running route + beforeAll(async ({ expect, task }) => { + dbService.findOneRecord.mockImplementationOnce(() => null); + + response = await supertest(app) + .post(ROUTE) + .send(mockedVals.validInput); + }); + it('should return a proper status code', () => { + expect(response.status).toBe(404); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + + + // ############################ + + describe('given the email format is invalid', () => { + beforeAll(async () => { + const input = { ...mockedVals.validInput, email: 'invalid-email-format' }; + + response = await supertest(app) + .post(ROUTE) + .send(input); + }); + + it('should return a proper status code status', () => { + expect(response.status).toBe(400); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + + // ############################ + + describe('the request body is empty', () => { + beforeAll(async () => { + response = await supertest(app) + .post(ROUTE) + .send(); + }); + + it('should return a proper status code status', () => { + expect(response.status).toBe(400); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + +}); \ No newline at end of file diff --git a/logs/__tests__/users/requestverification.test.js b/logs/__tests__/users/requestverification.test.js deleted file mode 100644 index 5ba3a7dd65d9d41aea1fa8c243979482bd643d2f..0000000000000000000000000000000000000000 --- a/logs/__tests__/users/requestverification.test.js +++ /dev/null @@ -1,149 +0,0 @@ -// import vitest, supertest & app -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'; -// 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' - }, - 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: {} -}; - -// ############################ -// MOCKS -// ############################ -// import PocketBase Service -import * as pbService from '../../utils/pocketbase/handlePocketBase.js'; -// mock pbService -vi.mock('../../utils/pocketbase/handlePocketBase.js', async (importOriginal) => { - return { - ...await importOriginal(), - pbAdminLogin: vi.fn(() => adminLoginResponse), - pbGetUser: vi.fn(() => userFoundResponse), - pbRequestVerification: vi.fn(() => 'mocked'), - pbClearAuthStore: vi.fn(() => 'mocked'), - }; -}); - -// ############################ -// TESTS -// ############################ -describe('user send registration token', () => { - describe('given the inputs are valid', async () => { - // set response by running route - 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); - }); - it('should return a proper status code', () => { - expect(response.status).toBe(200); - }); - 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.'); - }); - }); - - // ############################ - - describe('given the email is invalid', 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; }); - - 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(); - }); - it('should return a proper status code', () => { - expect(response.status).toBe(200); - }); - 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.'); - }); - }); - - - // ############################ - - describe('given the email format is invalid', async () => { - // set response by running route - beforeAll(async () => { - response = await supertest(app) - .post(ROUTE) - .send({ email: 'invalid-email' }); - }, BEFORE_ALL_TIMEOUT); - it('should return a proper status code', () => { - expect(response.status).toBe(400); - }); - it('should respond with a proper message', () => { - expect(response.body.validationErrors.email).toEqual('Invalid email'); - }); - }); - - // ############################ - - describe('given the request body is empty', async () => { - // set response by running route - beforeAll(async () => { - response = await supertest(app) - .post(ROUTE) - .send(); - }, BEFORE_ALL_TIMEOUT); - it('should return a proper status code', () => { - expect(response.status).toBe(400); - }); - it('should respond with a proper message', () => { - expect(response.body.validationErrors.email).toEqual('Required'); - }); - }); - -}); \ No newline at end of file