103 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-03-06 18:02:08 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
2020-05-17 17:37:41 +02:00
* This file is part of auth-server-helper which is released under MIT.
2020-03-06 18:02:08 +01:00
* See file 'LICENSE' for full license details.
2020-05-17 17:37:41 +02:00
* Created by Timo Hocker <timo@scode.ovh>, May 2020
2020-03-06 18:02:08 +01:00
*/
// @ts-nocheck
'use strict';
const test = require ('ava');
2020-03-07 17:56:41 +01:00
const mock_server = require ('../mock_server');
2020-05-06 08:40:58 +02:00
const client = require ('@sapphirecode/auth-client-helper');
const consts = require ('@sapphirecode/consts');
2020-03-08 14:03:18 +01:00
const fetch = require ('node-fetch');
2020-03-06 18:02:08 +01:00
2020-07-10 16:21:39 +02:00
let port = 0;
2020-03-07 17:56:41 +01:00
test.before (async () => {
2020-07-10 16:21:39 +02:00
port = await mock_server.start_server ();
2020-03-07 17:56:41 +01:00
});
2020-07-10 19:30:53 +02:00
test ('login', async (t) => {
2020-03-07 17:56:41 +01:00
const session = await client.login (
'testuser',
'foo',
2020-07-10 16:21:39 +02:00
`http://localhost:${port}`
2020-03-07 17:56:41 +01:00
);
t.is (typeof session, 'string');
2020-03-08 14:03:18 +01:00
2020-07-10 16:21:39 +02:00
const resp = await fetch (
`http://localhost:${port}`,
{ headers: { session } }
);
2020-03-08 14:03:18 +01:00
2020-03-13 20:08:38 +01:00
t.is (resp.status, consts.http.status_ok);
2020-07-10 15:39:14 +02:00
t.is (await resp.text (), 'foo:69');
2020-03-07 17:56:41 +01:00
});
2020-03-13 20:08:38 +01:00
test ('allow access to excluded paths', async (t) => {
2020-07-10 16:21:39 +02:00
const resp = await fetch (`http://localhost:${port}/noauthreg`);
2020-03-13 20:08:38 +01:00
t.is (resp.status, consts.http.status_ok);
2020-07-10 15:39:14 +02:00
t.is (await resp.text (), 'foo:undefined');
2020-03-13 20:08:38 +01:00
});
test ('allow access to excluded paths with correct method', async (t) => {
const resp = await fetch (
2020-07-10 16:21:39 +02:00
`http://localhost:${port}/noauthobj`,
2020-03-13 20:08:38 +01:00
{ method: 'POST' }
);
t.is (resp.status, consts.http.status_ok);
2020-07-10 15:39:14 +02:00
t.is (await resp.text (), 'foo:undefined');
2020-03-13 20:08:38 +01:00
});
test ('reject access to excluded paths with wrong method', async (t) => {
const resp = await fetch (
2020-07-10 16:21:39 +02:00
`http://localhost:${port}/noauthobj`
2020-03-13 20:08:38 +01:00
);
t.is (resp.status, consts.http.status_unauthorized);
});
2020-03-07 17:56:41 +01:00
test ('reject invalid user', async (t) => {
2020-03-07 18:29:18 +01:00
await t.throwsAsync (client.login (
2020-03-07 17:56:41 +01:00
'foo',
'foo',
2020-07-10 16:21:39 +02:00
`http://localhost:${port}`
2020-03-07 18:29:18 +01:00
));
2020-03-07 17:56:41 +01:00
});
2020-07-12 19:41:59 +02:00
test ('reject and recover', async (t) => {
await t.throwsAsync (client.login (
'testuser',
'bar',
`http://localhost:${port}`
));
const session = await client.login (
'testuser',
'foo',
`http://localhost:${port}`
);
t.is (typeof session, 'string');
const resp = await fetch (
`http://localhost:${port}`,
{ headers: { session } }
);
t.is (resp.status, consts.http.status_ok);
t.is (await resp.text (), 'foo:69');
});
2020-03-07 17:56:41 +01:00
test ('reject invalid password', async (t) => {
2020-03-07 18:29:18 +01:00
await t.throwsAsync (client.login (
2020-03-07 17:56:41 +01:00
'testuser',
'bar',
2020-07-10 16:21:39 +02:00
`http://localhost:${port}`
2020-03-07 18:29:18 +01:00
));
2020-03-06 18:02:08 +01:00
});