From 057ae0d79758cd627d6d128406a0d201b6b7ad9b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 6 Apr 2021 09:09:01 +0200 Subject: [PATCH] Added first selfservice test ref #190 --- src/tests/stats/stats_get.spec.ts | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/tests/stats/stats_get.spec.ts diff --git a/src/tests/stats/stats_get.spec.ts b/src/tests/stats/stats_get.spec.ts new file mode 100644 index 0000000..422fe07 --- /dev/null +++ b/src/tests/stats/stats_get.spec.ts @@ -0,0 +1,46 @@ +import axios from 'axios'; +import { config } from '../../config'; +const base = "http://localhost:" + config.internal_port + +let axios_config_full; +let axios_config_stats; + +beforeAll(async () => { + jest.setTimeout(20000); + const res = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" }); + let access_token = res.data["access_token"]; + axios_config_full = { + headers: { "authorization": "Bearer " + access_token }, + validateStatus: undefined + }; + const res2 = await axios.post(base + '/api/statsclients', { username: "demo", password: "demo" }, axios_config_full); + access_token = res2.data["key"]; + axios_config_stats = { + headers: { "authorization": "Bearer " + access_token }, + validateStatus: undefined + }; +}); + +describe('GET /api/stats/distance w/o auth should return 200', () => { + it('get with invalid token should return 401', async () => { + const res = await axios.get(base + '/api/stats/distance', { + headers: { "authorization": "Bearer 123123123123123123" }, + validateStatus: undefined + }); + expect(res.status).toEqual(401); + expect(res.headers['content-type']).toContain("application/json"); + }); +}); +// --------------- +describe('GET /api/stats should return 200', () => { + it('get w/o auth should return 200', async () => { + const res = await axios.get(base + '/api/stats', { validateStatus: undefined }); + expect(res.status).toEqual(200); + expect(res.headers['content-type']).toContain("application/json"); + }); + it('get w/ auth should return 200', async () => { + const res = await axios.get(base + '/api/stats', axios_config_stats); + expect(res.status).toEqual(200); + expect(res.headers['content-type']).toContain("application/json"); + }); +}); \ No newline at end of file