summaryrefslogtreecommitdiff
path: root/tests/root.spec.js
blob: 968ea5d73753a1dbeb0cfb04203e75fa9800b7dd (plain)
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
import { beforeEach, describe, test } from 'vitest';
import supertest from 'supertest';

import app from '../main.js';

const request = supertest(app);

describe('Root handlers tests', function() {
	describe('When not logged in', function() {
		test('GET /', async function() { await request.get('/').expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/login$/); });
		test('GET /login', async function() { await request.get('/login').expect('Content-Type', /text\/html/).expect(200, /<form action="\/login" method="post">/); });
		test('GET /logout', async function() { await request.get('/logout').expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/$/); });
	});

	describe('With valid credentials', function() {
		beforeEach(async function(ctx) {
			const authResponse = await request.post('/login')
				.send('username=Alice&password=Alice42!')
				.expect('Content-Type', /text\/plain/)
				.expect('set-cookie', /JDMSessionId=/)
				.expect(302, /Redirecting to \/games$/);
			ctx.cookie = authResponse.get('Set-Cookie');
		});
		test('GET /', async function(ctx) { await request.get('/').set('Cookie', ctx.cookie).expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/games$/); });
		test('GET /login', async function(ctx) { await request.get('/login').set('Cookie', ctx.cookie).expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/games$/); });
		describe('logout', function() {
			beforeEach(async function(ctx) {
				await request.get('/logout').set('cookie', ctx.cookie).expect('content-type', /text\/plain/).expect('set-cookie', /JDMSessionId=;/).expect(302, /Redirecting to \/$/);
			});
			describe('all handlers with the now invalid cookie', function() {
				test('GET /', async function(ctx) { await request.get('/').set('Cookie', ctx.cookie).expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/login$/); });
				test('GET /login', async function(ctx) { await request.get('/login').set('Cookie', ctx.cookie).expect('Content-Type', /text\/html/).expect(200, /<form action="\/login" method="post">/); });
				test('GET /logout', async function(ctx) { await request.get('/logout').set('Cookie', ctx.cookie).expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/$/); });
			});
		});
	});

	describe('With invalid credentials', function() {
		test('POST /login', async function() {
			await request.post('/login').send('username=NonExistant&password=Alice42!')
				.expect('Content-Type', /text\/html/)
				.expect(403, /erreur de connexion/);
		});
		test('POST /login', async function() {
			await request.post('/login').send('username=Alice&password=Invalid')
				.expect('Content-Type', /text\/html/)
				.expect(403, /erreur de connexion/);
		});
	});
});