1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/routes/admin-api/user.test.js

84 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-01-17 09:46:16 +01:00
'use strict';
import { createServices } from '../../services';
import { createTestConfig } from '../../../test/config/test-config';
2018-12-17 09:24:49 +01:00
const test = require('ava');
2018-01-17 09:46:16 +01:00
const supertest = require('supertest');
const { EventEmitter } = require('events');
const store = require('../../../test/fixtures/store');
2018-01-17 09:46:16 +01:00
const getApp = require('../../app');
const User = require('../../user');
const eventBus = new EventEmitter();
const currentUser = new User({ email: 'test@mail.com' });
function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const stores = store.createStores();
stores.userStore.insert(currentUser);
const config = createTestConfig({
preHook: a => {
a.use((req, res, next) => {
req.user = currentUser;
next();
});
2018-01-17 09:46:16 +01:00
},
server: { baseUriPath: base },
});
const services = createServices(stores, config);
const app = getApp(config, stores, services, eventBus);
2018-01-17 09:46:16 +01:00
return {
base,
userStore: stores.userStore,
2018-01-17 09:46:16 +01:00
request: supertest(app),
};
}
test('should return current user', t => {
2018-01-17 09:46:16 +01:00
t.plan(1);
const { request, base } = getSetup();
return request
.get(`${base}/api/admin/user`)
.expect(200)
.expect('Content-Type', /json/)
.expect(res => {
t.is(res.body.user.email, currentUser.email);
2018-01-17 09:46:16 +01:00
});
});
const owaspPassword = 't7GTx&$Y9pcsnxRv6';
test('should allow user to change password', async t => {
t.plan(2);
const { request, base, userStore } = getSetup();
const before = await userStore.get(currentUser);
t.falsy(before.passwordHash);
await request
.post(`${base}/api/admin/user/change-password`)
.send({ password: owaspPassword, confirmPassword: owaspPassword })
.expect(200);
const updated = await userStore.get(currentUser);
t.truthy(updated.passwordHash);
});
2018-01-17 09:46:16 +01:00
test('should deny if password and confirmPassword are not equal', async t => {
2018-01-17 09:46:16 +01:00
t.plan(0);
const { request, base } = getSetup();
return request
.post(`${base}/api/admin/user/change-password`)
.send({ password: owaspPassword, confirmPassword: 'somethingelse' })
.expect(400);
});
2018-01-17 09:46:16 +01:00
test('should deny if password does not fulfill owasp criteria', async t => {
t.plan(0);
const { request, base } = getSetup();
2018-01-17 09:46:16 +01:00
return request
.post(`${base}/api/admin/user/change-password`)
.send({ password: 'hunter123', confirmPassword: 'hunter123' })
.expect(400);
2018-01-17 09:46:16 +01:00
});