Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 4x 4x 4x 4x 4x 4x 4x 4x 132x 132x 132x 7x 16x 16x 16x 16x 16x 16x | /* eslint-disable @typescript-eslint/no-explicit-any */
import request from 'supertest';
import app from '../app';
import config from '../config';
import initSocketIoServer from '../socket';
import express from 'express';
import http from 'http';
import {Server} from 'socket.io';
import ioClient from 'socket.io-client';
import db, {syncPromise} from '../db';
import {UserDto} from '../models';
export interface SignUpReturn {
user: UserDto & SignUpReturn['signUpBody'];
csrf: string;
agent: request.SuperAgentTest;
signUpBody: {
username: string;
password: string;
email: string;
};
jwtValue: string;
}
export interface TestContext {
agent: request.SuperAgentTest;
user: UserDto & SignUpReturn['signUpBody'];
csrf: string;
jwtValue: string;
}
const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
/**
* Util class for tests.
*/
export class TestUtils {
/**
* Creates a test user and signs in.
*/
public static async signUp(): Promise<SignUpReturn> {
const agent = request.agent(app);
const signUpBody = {
username: 'test',
email: 'test@mail.com',
password: 'password',
};
let user: any;
// Get csrf token
let csrf = await agent.head('/auth')
.then((response) => {
// Save jwt cookie
return response.header[csrfHeaderName];
});
// Sign up to access api and set new jwt
const jwtValue = await agent
.post('/auth/sign-up')
.set(csrfHeaderName, csrf)
.send(signUpBody)
.expect(201)
.then((response) => {
user = response.body;
return this.extractJwtValue(response.header['set-cookie']);
});
csrf = await agent.head('/auth')
.then((response) => {
// Save jwt cookie
return response.header[csrfHeaderName];
});
return {
user,
csrf,
agent,
signUpBody,
jwtValue,
};
}
/**
* Starts a test instance of the socket.io server.
* Creates an empty express app, a server instance
* and then initialize socket.io.
*/
public static startSocketIo(): Promise<{
port: number,
io: Server,
server: http.Server
}> {
return new Promise((resolve, reject) => {
const app = express();
const server = http.createServer(app);
const io = initSocketIoServer(server);
const port = 9999;
server.listen(port);
server.on('listening', () => resolve({port, io, server}));
server.on('error', (e) => {
reject(e);
});
});
}
/**
* Extract the value of the jwt cookie from a set-cookie header.
* @param setCookie - The string of the set-cookie header
*/
public static extractJwtValue(setCookie: string[]): string {
const jwtCookie = setCookie.find((el) => el.includes('jwt='));
Iif (!jwtCookie) {
throw new Error('Cannot extract jwt cookie');
}
return jwtCookie.split(';')[0].replace('jwt=', '');
}
/**
* Creates a socket with the specified port to the specified namespace.
* @param port - The port to the socket server
* @param nsp - The namespace
* @param jwt - Value for the jwt cookie
*/
public static createSocket(
port: number,
nsp: string,
jwt: string,
): SocketIOClient.Socket {
return ioClient(`http://127.0.0.1:${port}${nsp}`, {
forceNew: true,
reconnectionDelay: 0,
path: '/socket',
transportOptions: {
polling: {
extraHeaders: {
'Cookie': 'jwt=' + jwt,
},
},
},
});
}
/**
* Sets up for an integration test.
*
* **Should be used once for each test**
*
* The setup process is done in 3 steps:
* 1. Sync the database, meaning: reset it
* 2. Sign up a default user
* 3. Prepare a SuperTest agent for use in tests
* @returns An instance of SuperAgent which is logged in.
*/
public static async setupIntegrationTest():
Promise<TestContext> {
await syncPromise;
await db.sync({force: true});
const response = await this.signUp();
// Extract needed fields
const {agent, csrf, user, jwtValue} = response;
// Set csrf header
agent.set(csrfHeaderName, csrf);
return {
agent,
csrf,
user: {
...user,
password: response.signUpBody.password,
},
jwtValue,
};
}
}
|