103 lines
2.3 KiB
JavaScript
103 lines
2.3 KiB
JavaScript
/*
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
* This file is part of auth-server-helper which is released under MIT.
|
|
* See file 'LICENSE' for full license details.
|
|
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
|
*/
|
|
|
|
// @ts-nocheck
|
|
'use strict';
|
|
|
|
const test = require ('ava');
|
|
const mock_server = require ('../mock_server');
|
|
const client = require ('@sapphirecode/auth-client-helper');
|
|
const consts = require ('@sapphirecode/consts');
|
|
const fetch = require ('node-fetch');
|
|
|
|
let port = 0;
|
|
|
|
test.before (async () => {
|
|
port = await mock_server.start_server ();
|
|
});
|
|
|
|
test ('login', async (t) => {
|
|
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');
|
|
});
|
|
|
|
test ('allow access to excluded paths', async (t) => {
|
|
const resp = await fetch (`http://localhost:${port}/noauthreg`);
|
|
|
|
t.is (resp.status, consts.http.status_ok);
|
|
t.is (await resp.text (), 'foo:undefined');
|
|
});
|
|
|
|
test ('allow access to excluded paths with correct method', async (t) => {
|
|
const resp = await fetch (
|
|
`http://localhost:${port}/noauthobj`,
|
|
{ method: 'POST' }
|
|
);
|
|
|
|
t.is (resp.status, consts.http.status_ok);
|
|
t.is (await resp.text (), 'foo:undefined');
|
|
});
|
|
|
|
test ('reject access to excluded paths with wrong method', async (t) => {
|
|
const resp = await fetch (
|
|
`http://localhost:${port}/noauthobj`
|
|
);
|
|
|
|
t.is (resp.status, consts.http.status_unauthorized);
|
|
});
|
|
|
|
test ('reject invalid user', async (t) => {
|
|
await t.throwsAsync (client.login (
|
|
'foo',
|
|
'foo',
|
|
`http://localhost:${port}`
|
|
));
|
|
});
|
|
|
|
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');
|
|
});
|
|
|
|
test ('reject invalid password', async (t) => {
|
|
await t.throwsAsync (client.login (
|
|
'testuser',
|
|
'bar',
|
|
`http://localhost:${port}`
|
|
));
|
|
});
|