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

finished testing route auth/logout

parent bf12b44f
Branches
No related tags found
No related merge requests found
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`user logout > given no cookie was send > should respond with a proper body 1`] = `
{
"message": "See you soon.",
}
`;
exports[`user logout > given refresh token is invalid > should respond with a proper body 1`] = `
{
"message": "jwt malformed",
}
`;
exports[`user logout > given refresh token is valid > should respond with a proper body 1`] = `
{
"message": "jwt malformed",
}
`;
......@@ -2,29 +2,44 @@
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/logout';
const ROUTE = '/auth';
// prepare response of each test
let response;
// ############################
// OBJECTS
// ############################
const mockedVals = vi.hoisted(() => {
return {
foundUser: {
_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,
password: 'StrongPass1!',
// password,
id: '66a29da2942b3ebcaf047f07'
}
};
});
// ############################
// 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(),
pbClearAuthStore: vi.fn(() => 'mocked'),
dbConnection: vi.fn(() => 'mocked'),
findOneRecord: vi.fn(() => mockedVals.foundUser),
findOneAndUpdate: vi.fn(() => mockedVals.foundUser),
};
});
......@@ -32,33 +47,49 @@ vi.mock('../../utils/pocketbase/handlePocketBase.js', async (importOriginal) =>
// TESTS
// ############################
describe('user logout', () => {
describe('given nothing but the JWT was send', () => {
// ############################
describe('given refresh token is valid', () => {
beforeAll(async () => {
response = await supertest(app)
.get(ROUTE)
.set('Authorization', 'Bearer 123valid');
}, BEFORE_ALL_TIMEOUT);
.set('Cookie', 'refreshToken=valid');
});
it('should return a proper status code', () => {
expect(response.status).toBe(200);
expect(response.status).toBe(400);
});
it('should respond with a proper record and token', () => {
expect(response.body.message).toEqual('You have been logged out.');
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
// ############################
describe('given nothing was send', () => {
describe('given refresh token is invalid', () => {
beforeAll(async () => {
response = await supertest(app)
.get(ROUTE);
}, BEFORE_ALL_TIMEOUT);
.get(ROUTE)
.set('Cookie', 'refreshToken=invalid');
});
it('should return a proper status code', () => {
expect(response.status).toBe(200);
expect(response.status).toBe(400);
});
it('should respond with a proper record and token', () => {
expect(response.body.message).toEqual('You have been logged out.');
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
// ############################
describe('given no cookie was send', () => {
beforeAll(async () => {
response = await supertest(app)
.delete(ROUTE);
});
it('should return a proper status code', () => {
expect(response.status).toBe(200);
});
it('should respond with a proper body', () => {
expect(response.body).toMatchSnapshot();
});
});
});
\ No newline at end of file
......@@ -45,7 +45,6 @@ vi.mock('../../utils/handleDB.js', async (importOriginal) => {
findByIdAndUpdate: vi.fn(() => { return { ...mockedVals.foundUser, refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY2MOCKED' }; })
};
});
// verifyRefreshToken;
// ############################
// TESTS
......
......@@ -135,6 +135,23 @@ export const findByIdAndUpdate = async (model, id, data) => {
};
/**
* Find a document by id and update it
*
* @param {mongoose model} model [required] model to search the record in
* @param {object} searchObject [required] search object as filter, i.e. {email: 'a@b.c', name: 'John'}
* @param {object} data [required] data to update the record with
*
* @return {object} the edited document
*/
export const findOneAndUpdate = async (model, searchObject, data) => {
try {
return model.updateOne(searchObject, data);
} catch (error) {
throw error;
}
};
/**
* extend a chat record with a new input/response-pair
*
......
import jwt from 'jsonwebtoken';
import User from "../models/User.js";
import { findByIdAndUpdate, findOneRecord } from './handleDB.js';
import { findByIdAndUpdate, findOneAndUpdate, findOneRecord } from './handleDB.js';
/**
* generate a "oneTime" JWT, containing a given object
......@@ -112,7 +112,7 @@ export const verifyRefreshToken = async (refreshToken) => {
*/
export const deleteRefreshToken = async (refreshToken) => {
try {
const user = await User.updateOne({ refreshToken }, { $unset: { refreshToken: "" } });
const user = await findOneAndUpdate(User, { refreshToken }, { $unset: { refreshToken: "" } });
return;
} catch (error) {
throw error;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment