add basic tests
This commit is contained in:
parent
22d1c984d5
commit
cfc3d1e9d6
6
index.js
6
index.js
@ -11,7 +11,7 @@
|
||||
'use strict';
|
||||
|
||||
const crypto = require ('@scode/crypto-helper');
|
||||
const http_consts = require ('@scode/consts');
|
||||
const consts = require ('@scode/consts');
|
||||
const fetch = require ('node-fetch');
|
||||
|
||||
/**
|
||||
@ -24,7 +24,7 @@ const fetch = require ('node-fetch');
|
||||
async function login (user, password, host = '') {
|
||||
const salt_str = await fetch (`${host}/auth`, { headers: { user } })
|
||||
.then (async (res) => {
|
||||
if (res.status !== http_consts.status_ok)
|
||||
if (res.status !== consts.http.status_ok)
|
||||
return '';
|
||||
|
||||
const session_id = await res.text ();
|
||||
@ -38,7 +38,7 @@ async function login (user, password, host = '') {
|
||||
|
||||
return fetch (`${host}/auth`, { headers: { user, key } })
|
||||
.then ((res) => {
|
||||
if (res.status !== http_consts.status_ok)
|
||||
if (res.status !== consts.http.status_ok)
|
||||
throw new Error ('user or password invalid');
|
||||
return res.text ();
|
||||
});
|
||||
|
12
package.json
12
package.json
@ -6,16 +6,18 @@
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@scode/eslint-config": "^1.2.26",
|
||||
"eslint": "^6.8.0"
|
||||
"ava": "^3.5.0",
|
||||
"eslint": "^6.8.0",
|
||||
"nyc": "^15.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"test": "echo \"no test\"",
|
||||
"test": "nyc ava",
|
||||
"ci": "yarn && node jenkins.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@scode/consts": "^1.0.1",
|
||||
"@scode/crypto-helper": "^1.0.2",
|
||||
"@scode/consts": "^1.0.3",
|
||||
"@scode/crypto-helper": "^1.1.9",
|
||||
"node-fetch": "^2.6.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
49
test/index.js
Normal file
49
test/index.js
Normal file
@ -0,0 +1,49 @@
|
||||
// @ts-nocheck
|
||||
'use strict';
|
||||
|
||||
const test = require ('ava');
|
||||
const http = require ('http');
|
||||
const client = require ('../index');
|
||||
const consts = require ('@scode/consts');
|
||||
|
||||
|
||||
test.before (() => {
|
||||
http.createServer ((req, res) => {
|
||||
let str = '';
|
||||
if (req.headers.user)
|
||||
str += req.headers.user;
|
||||
if (req.headers.key)
|
||||
str += req.headers.key;
|
||||
res.writeHead (
|
||||
req.headers.user === 'fail'
|
||||
// eslint-disable-next-line max-len
|
||||
|| req.headers.key === '73192367f6dde83a7e3c0bb412dff8e1b7dfbdb5e5010f00057f317b8eab68e8e448528303142c1455dfe72e062bb2e48f07441b38c7b65329ba7e5acbea6126'
|
||||
? consts.http.status_forbidden
|
||||
: consts.http.status_ok
|
||||
);
|
||||
res.end (str);
|
||||
})
|
||||
.listen (3000);
|
||||
});
|
||||
|
||||
test ('send request', async (t) => {
|
||||
const session = await client.login ('foo', 'bar', 'http://localhost:3000');
|
||||
|
||||
t.is (
|
||||
session,
|
||||
// eslint-disable-next-line max-len
|
||||
'foo39082c11afda7a927fe4e23c5ba81b64186c33f7e58adf492be5a5c64ddc9db459e0778d573000a5ebaeeae902b6c8641198406b58bf9c53ce48ecdef73a33d1'
|
||||
);
|
||||
});
|
||||
|
||||
test ('fail salt', (t) => {
|
||||
t.throwsAsync (async () => {
|
||||
await client.login ('fail', 'bar', 'http://localhost:3000');
|
||||
});
|
||||
});
|
||||
|
||||
test ('fail password', (t) => {
|
||||
t.throwsAsync (async () => {
|
||||
await client.login ('foo', 'fail', 'http://localhost:3000');
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user