From 3c003a60b207d744a6763fc8605abeadd07d81fc Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 22 Dec 2020 18:26:20 +0100 Subject: [PATCH] Added logut tests ref #45 --- src/tests/auth/auth_logout.spec.ts | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/tests/auth/auth_logout.spec.ts diff --git a/src/tests/auth/auth_logout.spec.ts b/src/tests/auth/auth_logout.spec.ts new file mode 100644 index 0000000..9cf997b --- /dev/null +++ b/src/tests/auth/auth_logout.spec.ts @@ -0,0 +1,49 @@ +import axios from 'axios'; +import { config } from '../../config'; + +const base = "http://localhost:" + config.internal_port + +const axios_config = { + validateStatus: undefined +};; + +beforeAll(async () => { + const res_login = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" }); + await axios.post(base + '/api/users', { + "firstname": "demo_logout", + "middlename": "demo_logout", + "lastname": "demo_logout", + "username": "demo_logout", + "password": "demo_logout" + }, { + headers: { "authorization": "Bearer " + res_login.data["access_token"] }, + validateStatus: undefined + }); +}); + +describe('POST /api/auth/logout valid', () => { + it('valid logout with token in cookie should return 200', async () => { + const res_login = await axios.post(base + '/api/auth/login', { username: "demo_logout", password: "demo_logout" }); + const res = await axios.post(base + '/api/auth/logout', null, { + headers: { "Cookie": res_login.headers["set-cookie"] }, + validateStatus: undefined + }); + expect(res.status).toEqual(200); + }); + it('valid logout with token in body should return 200', async () => { + const res_login = await axios.post(base + '/api/auth/login', { username: "demo_logout", password: "demo_logout" }); + const res = await axios.post(base + '/api/auth/logout', { token: res_login.data["refresh_token"] }, axios_config); + expect(res.status).toEqual(200); + }); +}); +// --------------- +describe('POST /api/auth/logout ivalid', () => { + it('invalid logout without token should return 406', async () => { + const res = await axios.post(base + '/api/auth/logout', null, axios_config); + expect(res.status).toEqual(406); + }); + it('invalid logout with invalid token in body should return 401', async () => { + const res = await axios.post(base + '/api/auth/logout', { token: "1" }, axios_config); + expect(res.status).toEqual(401); + }); +}); \ No newline at end of file