124 lines
3.0 KiB
JavaScript
124 lines
3.0 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
|
||
|
/* eslint-disable no-undef */
|
||
|
'use strict';
|
||
|
|
||
|
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;
|
||
|
|
||
|
// eslint-disable-next-line max-lines-per-function
|
||
|
describe ('server-helper', () => {
|
||
|
beforeAll (async () => {
|
||
|
port = await mock_server.start_server ();
|
||
|
});
|
||
|
|
||
|
it ('should login', async () => {
|
||
|
const session = await client.login (
|
||
|
'testuser',
|
||
|
'foo',
|
||
|
`http://localhost:${port}`
|
||
|
);
|
||
|
expect (typeof session)
|
||
|
.toEqual ('string');
|
||
|
|
||
|
const resp = await fetch (
|
||
|
`http://localhost:${port}`,
|
||
|
{ headers: { session } }
|
||
|
);
|
||
|
|
||
|
expect (resp.status)
|
||
|
.toEqual (consts.http.status_ok);
|
||
|
expect (await resp.text ())
|
||
|
.toEqual ('foo:69');
|
||
|
});
|
||
|
|
||
|
it ('should allow access to excluded paths', async () => {
|
||
|
const resp = await fetch (`http://localhost:${port}/noauthreg`);
|
||
|
|
||
|
expect (resp.status)
|
||
|
.toEqual (consts.http.status_ok);
|
||
|
expect (await resp.text ())
|
||
|
.toEqual ('foo:undefined');
|
||
|
});
|
||
|
|
||
|
it (
|
||
|
'should allow access to excluded paths with correct method',
|
||
|
async () => {
|
||
|
const resp = await fetch (
|
||
|
`http://localhost:${port}/noauthobj`,
|
||
|
{ method: 'POST' }
|
||
|
);
|
||
|
|
||
|
expect (resp.status)
|
||
|
.toEqual (consts.http.status_ok);
|
||
|
expect (await resp.text ())
|
||
|
.toEqual ('foo:undefined');
|
||
|
}
|
||
|
);
|
||
|
|
||
|
it ('should reject access to excluded paths with wrong method', async () => {
|
||
|
const resp = await fetch (
|
||
|
`http://localhost:${port}/noauthobj`
|
||
|
);
|
||
|
|
||
|
expect (resp.status)
|
||
|
.toEqual (consts.http.status_unauthorized);
|
||
|
});
|
||
|
|
||
|
it ('should reject invalid user', async () => {
|
||
|
await expectAsync (client.login (
|
||
|
'foo',
|
||
|
'foo',
|
||
|
`http://localhost:${port}`
|
||
|
))
|
||
|
.toBeRejectedWithError ('user or password invalid');
|
||
|
});
|
||
|
|
||
|
it ('should reject and recover', async () => {
|
||
|
await expectAsync (client.login (
|
||
|
'testuser',
|
||
|
'bar',
|
||
|
`http://localhost:${port}`
|
||
|
))
|
||
|
.toBeRejectedWithError ('user or password invalid');
|
||
|
|
||
|
const session = await client.login (
|
||
|
'testuser',
|
||
|
'foo',
|
||
|
`http://localhost:${port}`
|
||
|
);
|
||
|
expect (typeof session)
|
||
|
.toEqual ('string');
|
||
|
|
||
|
const resp = await fetch (
|
||
|
`http://localhost:${port}`,
|
||
|
{ headers: { session } }
|
||
|
);
|
||
|
|
||
|
expect (resp.status)
|
||
|
.toEqual (consts.http.status_ok);
|
||
|
expect (await resp.text ())
|
||
|
.toEqual ('foo:69');
|
||
|
});
|
||
|
|
||
|
it ('should reject invalid password', async () => {
|
||
|
await expectAsync (client.login (
|
||
|
'testuser',
|
||
|
'bar',
|
||
|
`http://localhost:${port}`
|
||
|
))
|
||
|
.toBeRejectedWithError ('user or password invalid');
|
||
|
});
|
||
|
});
|
||
|
|