use jasmine
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2020-09-28 11:27:10 +02:00
parent 055bb84a70
commit c6bd55eb0d
5 changed files with 768 additions and 1259 deletions

12
jasmine.json Normal file
View File

@ -0,0 +1,12 @@
{
"spec_dir": "test",
"spec_files": [
"spec/*.js"
],
"helpers": [
"helpers/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}

View File

@ -1,6 +1,6 @@
{
"name": "@sapphirecode/auth-server-helper",
"version": "1.1.2",
"version": "1.1.3",
"main": "index.js",
"author": {
"name": "Timo Hocker",
@ -18,15 +18,16 @@
"@sapphirecode/eslint-config": "^2.1.4",
"@stryker-mutator/core": "^3.2.3",
"@stryker-mutator/javascript-mutator": "^3.2.3",
"ava": "^3.10.1",
"@types/jasmine": "^3.5.14",
"eslint": "^7.0.0",
"express": "^4.17.1",
"jasmine": "^3.6.1",
"node-fetch": "^2.6.0",
"nyc": "^15.0.1"
},
"scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
"test": "nyc ava",
"test": "nyc jasmine --config=\"jasmine.json\"",
"mutate": "stryker run",
"compile": "tsc --allowJs --declaration --emitDeclarationOnly index.js"
},

View File

@ -1,102 +0,0 @@
/*
* 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}`
));
});

123
test/spec/index.js Normal file
View File

@ -0,0 +1,123 @@
/*
* 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');
});
});

1781
yarn.lock

File diff suppressed because it is too large Load Diff