2019-05-13 19:29:00 +00:00
|
|
|
/* @jest-environment node */
|
|
|
|
|
2019-05-14 14:49:57 +00:00
|
|
|
import last from 'lodash/last';
|
2019-08-30 18:26:57 +00:00
|
|
|
import { Request, Response } from 'jest-express';
|
|
|
|
|
|
|
|
import User, { createMock, createInstanceMock } from '../../../models/user';
|
2019-05-13 19:29:00 +00:00
|
|
|
import { createApiKey, removeApiKey } from '../../user.controller/apiKey';
|
|
|
|
|
|
|
|
jest.mock('../../../models/user');
|
|
|
|
|
|
|
|
describe('user.controller', () => {
|
2019-08-30 18:26:57 +00:00
|
|
|
let UserMock;
|
|
|
|
let UserInstanceMock;
|
|
|
|
|
2019-05-13 19:29:00 +00:00
|
|
|
beforeEach(() => {
|
2019-08-30 18:26:57 +00:00
|
|
|
UserMock = createMock();
|
|
|
|
UserInstanceMock = createInstanceMock();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
UserMock.restore();
|
|
|
|
UserInstanceMock.restore();
|
2019-05-13 19:29:00 +00:00
|
|
|
});
|
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
|
2019-05-13 19:29:00 +00:00
|
|
|
describe('createApiKey', () => {
|
|
|
|
it('returns an error if user doesn\'t exist', () => {
|
|
|
|
const request = { user: { id: '1234' } };
|
2019-08-30 18:26:57 +00:00
|
|
|
const response = new Response();
|
|
|
|
|
|
|
|
UserMock
|
|
|
|
.expects('findById')
|
|
|
|
.withArgs('1234')
|
|
|
|
.yields(null, null);
|
2019-05-13 19:29:00 +00:00
|
|
|
|
|
|
|
createApiKey(request, response);
|
|
|
|
|
|
|
|
expect(response.status).toHaveBeenCalledWith(404);
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
|
|
|
error: 'User not found'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an error if label not provided', () => {
|
|
|
|
const request = { user: { id: '1234' }, body: {} };
|
2019-08-30 18:26:57 +00:00
|
|
|
const response = new Response();
|
|
|
|
|
|
|
|
UserMock
|
|
|
|
.expects('findById')
|
|
|
|
.withArgs('1234')
|
|
|
|
.yields(null, new User());
|
2019-05-13 19:29:00 +00:00
|
|
|
|
|
|
|
createApiKey(request, response);
|
|
|
|
|
|
|
|
expect(response.status).toHaveBeenCalledWith(400);
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
|
|
|
error: 'Expected field \'label\' was not present in request body'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns generated API key to the user', (done) => {
|
2019-08-30 18:26:57 +00:00
|
|
|
const request = new Request();
|
|
|
|
request.setBody({ label: 'my key' });
|
|
|
|
request.user = { id: '1234' };
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
const response = new Response();
|
|
|
|
|
|
|
|
const user = new User();
|
|
|
|
|
|
|
|
UserMock
|
|
|
|
.expects('findById')
|
|
|
|
.withArgs('1234')
|
|
|
|
.yields(null, user);
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
UserInstanceMock.expects('save')
|
|
|
|
.yields();
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
function expectations() {
|
2019-05-14 14:49:57 +00:00
|
|
|
const lastKey = last(user.apiKeys);
|
|
|
|
|
|
|
|
expect(lastKey.label).toBe('my key');
|
|
|
|
expect(typeof lastKey.hashedKey).toBe('string');
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
const responseData = response.json.mock.calls[0][0];
|
|
|
|
|
|
|
|
expect(responseData.apiKeys.length).toBe(1);
|
|
|
|
expect(responseData.apiKeys[0]).toMatchObject({
|
|
|
|
label: 'my key',
|
|
|
|
token: lastKey.hashedKey,
|
|
|
|
lastUsedAt: undefined,
|
|
|
|
createdAt: undefined
|
2019-05-13 19:29:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
2019-08-30 18:26:57 +00:00
|
|
|
}
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
const promise = createApiKey(request, response);
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
promise.then(expectations, expectations).catch(expectations);
|
2019-05-13 19:29:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeApiKey', () => {
|
|
|
|
it('returns an error if user doesn\'t exist', () => {
|
|
|
|
const request = { user: { id: '1234' } };
|
2019-08-30 18:26:57 +00:00
|
|
|
const response = new Response();
|
|
|
|
|
|
|
|
UserMock
|
|
|
|
.expects('findById')
|
|
|
|
.withArgs('1234')
|
|
|
|
.yields(null, null);
|
2019-05-13 19:29:00 +00:00
|
|
|
|
|
|
|
removeApiKey(request, response);
|
|
|
|
|
|
|
|
expect(response.status).toHaveBeenCalledWith(404);
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
|
|
|
error: 'User not found'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an error if specified key doesn\'t exist', () => {
|
|
|
|
const request = {
|
|
|
|
user: { id: '1234' },
|
|
|
|
params: { keyId: 'not-a-real-key' }
|
|
|
|
};
|
2019-08-30 18:26:57 +00:00
|
|
|
const response = new Response();
|
|
|
|
const user = new User();
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
UserMock
|
|
|
|
.expects('findById')
|
|
|
|
.withArgs('1234')
|
|
|
|
.yields(null, user);
|
2019-05-13 19:29:00 +00:00
|
|
|
|
|
|
|
removeApiKey(request, response);
|
|
|
|
|
|
|
|
expect(response.status).toHaveBeenCalledWith(404);
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
|
|
|
error: 'Key does not exist for user'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
it('removes key if it exists', () => {
|
|
|
|
const user = new User();
|
|
|
|
user.apiKeys.push({ label: 'first key' }); // id 0
|
|
|
|
user.apiKeys.push({ label: 'second key' }); // id 1
|
|
|
|
|
|
|
|
const firstKeyId = user.apiKeys[0]._id.toString();
|
|
|
|
const secondKeyId = user.apiKeys[1]._id.toString();
|
|
|
|
|
2019-05-13 19:29:00 +00:00
|
|
|
const request = {
|
|
|
|
user: { id: '1234' },
|
2019-08-30 18:26:57 +00:00
|
|
|
params: { keyId: firstKeyId }
|
2019-05-13 19:29:00 +00:00
|
|
|
};
|
2019-08-30 18:26:57 +00:00
|
|
|
const response = new Response();
|
2019-05-13 19:29:00 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
UserMock
|
|
|
|
.expects('findById')
|
|
|
|
.withArgs('1234')
|
|
|
|
.yields(null, user);
|
2019-05-14 14:49:57 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
UserInstanceMock
|
|
|
|
.expects('save')
|
|
|
|
.yields();
|
2019-05-13 19:29:00 +00:00
|
|
|
|
|
|
|
removeApiKey(request, response);
|
|
|
|
|
2019-05-14 14:49:57 +00:00
|
|
|
expect(response.status).toHaveBeenCalledWith(200);
|
2019-08-30 18:26:57 +00:00
|
|
|
|
|
|
|
const responseData = response.json.mock.calls[0][0];
|
|
|
|
|
|
|
|
expect(responseData.apiKeys.length).toBe(1);
|
|
|
|
expect(responseData.apiKeys[0]).toMatchObject({
|
|
|
|
id: secondKeyId,
|
|
|
|
label: 'second key',
|
|
|
|
lastUsedAt: undefined,
|
|
|
|
createdAt: undefined
|
2019-05-13 19:29:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|