2021-06-23 22:30:45 +02:00
|
|
|
import got from 'got';
|
|
|
|
import jwt from 'jsonwebtoken'
|
|
|
|
import setConfig from '../test-helpers/config.js';
|
|
|
|
import test from 'tape';
|
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
let adminJWT;
|
|
|
|
let adminJWTString;
|
|
|
|
let user;
|
|
|
|
let userJWT;
|
|
|
|
let userJWTString;
|
|
|
|
const userName = 'test-tomte nöff #18';
|
|
|
|
const password = 'lurpassare7½TUR';
|
2021-06-23 22:30:45 +02:00
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
test('test-cases/01basic.js: Authing with configurated API KEY', async t => {
|
2021-06-23 22:30:45 +02:00
|
|
|
// Wrong API key
|
|
|
|
try {
|
|
|
|
await got.post(`${process.env.AUTH_URL}/auth/api-key`, {
|
|
|
|
json: 'a09ifa908wjf92fowreigaoijfaosidfđ@€£đawef',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
t.fail('Calling /auth/api-key with wrong api-key should result in a 403');
|
|
|
|
} catch (err) {
|
|
|
|
t.equal(err.message, 'Response code 403 (Forbidden)', 'Calling /auth/api-key with wrong api-key should result in a 403')
|
|
|
|
}
|
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
// Successful auth
|
2021-06-23 22:30:45 +02:00
|
|
|
const authRes = await got.post(`${process.env.AUTH_URL}/auth/api-key`, {
|
|
|
|
json: 'hihi',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
t.notEqual(authRes.body.jwt, undefined, 'The body should include a jwt key');
|
|
|
|
t.notEqual(authRes.body.renewalToken, undefined, 'The body should include a renewalToken');
|
2021-06-24 00:42:54 +02:00
|
|
|
adminJWTString = authRes.body.jwt;
|
2021-06-23 22:30:45 +02:00
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
adminJWT = jwt.verify(adminJWTString, process.env.JWT_SHARED_SECRET);
|
2021-06-23 22:30:45 +02:00
|
|
|
t.equal(adminJWT.accountName, 'admin', 'The verified account name should be "admin"');
|
2021-06-24 00:42:54 +02:00
|
|
|
});
|
2021-06-23 22:30:45 +02:00
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
test('test-cases/01basic.js: GETting the admin account, with the token we just obtained', async t => {
|
2021-06-23 22:30:45 +02:00
|
|
|
try {
|
|
|
|
await got(`${process.env.AUTH_URL}/account/${adminJWT.accountId}`);
|
|
|
|
t.fail('Calling /account/{id} without proper auth token should give 403');
|
|
|
|
} catch (err) {
|
|
|
|
t.equal(err.message, 'Response code 403 (Forbidden)', 'Calling /account/{id} without proper auth token should give 403');
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountRes = await got(`${process.env.AUTH_URL}/account/${adminJWT.accountId}`, {
|
2021-06-24 00:42:54 +02:00
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
2021-06-23 22:30:45 +02:00
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
t.equal(adminJWT.accountId, accountRes.body.id, 'The account ids should match');
|
2021-06-24 00:42:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('test-cases/01basic.js: Creating a new account', async t => {
|
|
|
|
const res = await got.post(`${process.env.AUTH_URL}/account`, {
|
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
|
|
|
json: {
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'nördområde',
|
|
|
|
values: ['tåg', 'trädgårdstomtar'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'role',
|
|
|
|
values: ['user'],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: userName,
|
|
|
|
password,
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
user = res.body;
|
|
|
|
|
|
|
|
t.notEqual(user.id, undefined, 'The new account should have an id');
|
|
|
|
t.notEqual(user.apiKey, undefined, 'The new account should have an apiKey');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await got.post(`${process.env.AUTH_URL}/account`, {
|
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
|
|
|
json: {
|
|
|
|
fields: [{name: 'role',values: ['user'],}],
|
|
|
|
name: userName,
|
|
|
|
password,
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
t.fail('Trying to create another account with the same name should fail with a 409');
|
|
|
|
} catch(err) {
|
|
|
|
t.equal(err.message, 'Response code 409 (Conflict)', 'Trying to create another account with the same name should fail with a 409');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test-cases/01basic.js: Auth by username and password', async t => {
|
|
|
|
const authRes = await got.post(`${process.env.AUTH_URL}/auth/password`, {
|
|
|
|
json: {
|
|
|
|
name: userName,
|
|
|
|
password,
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
t.notEqual(authRes.body.jwt, undefined, 'The body should include a jwt key');
|
|
|
|
t.notEqual(authRes.body.renewalToken, undefined, 'The body should include a renewalToken');
|
|
|
|
userJWTString = authRes.body.jwt;
|
|
|
|
|
|
|
|
userJWT = jwt.verify(userJWTString, process.env.JWT_SHARED_SECRET);
|
|
|
|
t.equal(userJWT.accountName, userName, 'The verified account name should match the created user');
|
|
|
|
});
|
|
|
|
|
2022-02-27 17:32:30 +01:00
|
|
|
test('test-cases/01basic.js: Auth by username and wrong password', async t => {
|
|
|
|
try {
|
|
|
|
await got.post(`${process.env.AUTH_URL}/auth/password`, {
|
|
|
|
json: {
|
|
|
|
name: userName,
|
|
|
|
password: 'isWrong',
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
t.fail('Trying to login with wrong password should fail with a 403');
|
|
|
|
} catch(err) {
|
|
|
|
t.equal(err.message, 'Response code 403 (Forbidden)', 'Trying to login with wrong password should fail with a 403');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test-cases/01basic.js: Auth by wrong username', async t => {
|
|
|
|
try {
|
|
|
|
await got.post(`${process.env.AUTH_URL}/auth/password`, {
|
|
|
|
json: {
|
|
|
|
name: 'lapptomte',
|
|
|
|
password: 'isWrong',
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
t.fail('Trying to login with wrong username should fail with a 403');
|
|
|
|
} catch(err) {
|
|
|
|
t.equal(err.message, 'Response code 403 (Forbidden)', 'Trying to login with wrong username should fail with a 403');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-24 01:55:47 +02:00
|
|
|
test('test-cases/01basic.js: PUT /account/{id}/fields', async t => {
|
|
|
|
const res = await got.put(`${process.env.AUTH_URL}/account/${user.id}/fields`, {
|
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
|
|
|
json: [
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
values: ['bar'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'role',
|
|
|
|
values: ['tomte'],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
t.equal(user.id, res.body.id, 'The responded account id should be the same as the old one');
|
|
|
|
t.equal(Object.keys(res.body.fields).length, 2, 'There should only be two fields in total');
|
|
|
|
t.equal(JSON.stringify(res.body.fields.foo), '["bar"]', 'The foo field should have values ["bar"]');
|
|
|
|
t.equal(JSON.stringify(res.body.fields.role), '["tomte"]', 'The role field should have values ["tomte"]');
|
|
|
|
|
|
|
|
// Overload the previous user
|
|
|
|
user.fields = res.body.fields;
|
|
|
|
user.name = res.body.name;
|
|
|
|
});
|
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
test('test-cases/01basic.js: Remove an account', async t => {
|
|
|
|
try {
|
|
|
|
// Random uuid that should not exist in the db. The chance of this existing is... small
|
|
|
|
await got.delete(`${process.env.AUTH_URL}/account/a423e690-74b9-4f37-9976-f5bf75a5ea32`, {
|
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
|
|
|
responseType: 'json',
|
|
|
|
retry: 0,
|
|
|
|
});
|
|
|
|
t.fail('Response status for DELETing an account that does not exist should be 404');
|
|
|
|
} catch (err) {
|
|
|
|
t.equal(err.message, 'Response code 404 (Not Found)', 'Response status for DELETing an account that does not exist should be 404');
|
|
|
|
}
|
2021-06-23 22:30:45 +02:00
|
|
|
|
2021-06-24 00:42:54 +02:00
|
|
|
const delRes = await got.delete(`${process.env.AUTH_URL}/account/${user.id}`, {
|
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
|
|
|
responseType: 'json',
|
|
|
|
retry: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
t.equal(delRes.statusCode, 204, 'Response status for DELETE should be 204');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await got(`${process.env.AUTH_URL}/account/${user.id}`, {
|
|
|
|
headers: { 'Authorization': `bearer ${adminJWTString}`},
|
|
|
|
responseType: 'json',
|
|
|
|
retry: 0,
|
|
|
|
});
|
|
|
|
t.fail('Response status for GETing the account again should be 404');
|
|
|
|
} catch (err) {
|
|
|
|
t.equal(err.message, 'Response code 404 (Not Found)', 'Response status for GETing the account again should be 404');
|
|
|
|
}
|
2021-06-23 22:30:45 +02:00
|
|
|
});
|