2019-05-13 21:29:00 +02:00
|
|
|
/* @jest-environment node */
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
import last from 'lodash/last';
|
2019-05-13 21:29:00 +02:00
|
|
|
import { createApiKey, removeApiKey } from '../../user.controller/apiKey';
|
|
|
|
|
|
|
|
jest.mock('../../../models/user');
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
/*
|
|
|
|
Create a mock object representing an express Response
|
|
|
|
*/
|
|
|
|
const createResponseMock = function createResponseMock(done) {
|
2019-05-13 21:29:00 +02:00
|
|
|
const json = jest.fn(() => {
|
|
|
|
if (done) { done(); }
|
|
|
|
});
|
2019-05-14 16:49:57 +02:00
|
|
|
|
2019-05-13 21:29:00 +02:00
|
|
|
const status = jest.fn(() => ({ json }));
|
|
|
|
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
json
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
/*
|
|
|
|
Create a mock of the mongoose User model
|
|
|
|
*/
|
|
|
|
const createUserMock = function createUserMock() {
|
|
|
|
const apiKeys = [];
|
|
|
|
let nextId = 0;
|
|
|
|
|
|
|
|
apiKeys.push = ({ label, hashedKey }) => {
|
|
|
|
const id = nextId;
|
|
|
|
nextId += 1;
|
|
|
|
const publicFields = { id, label };
|
|
|
|
const allFields = { ...publicFields, hashedKey };
|
|
|
|
|
2019-05-15 13:07:20 +02:00
|
|
|
Object.defineProperty(allFields, 'toObject', {
|
|
|
|
value: () => publicFields,
|
2019-05-14 16:49:57 +02:00
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
|
|
|
|
return Array.prototype.push.call(apiKeys, allFields);
|
|
|
|
};
|
|
|
|
|
|
|
|
apiKeys.pull = ({ _id }) => {
|
|
|
|
const index = apiKeys.findIndex(({ id }) => id === _id);
|
|
|
|
return apiKeys.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
apiKeys,
|
|
|
|
save: jest.fn(callback => callback())
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-13 21:29:00 +02:00
|
|
|
const User = require('../../../models/user').default;
|
|
|
|
|
|
|
|
describe('user.controller', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
User.__setFindById(null, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createApiKey', () => {
|
|
|
|
it('returns an error if user doesn\'t exist', () => {
|
|
|
|
const request = { user: { id: '1234' } };
|
|
|
|
const response = createResponseMock();
|
|
|
|
|
|
|
|
createApiKey(request, response);
|
|
|
|
|
|
|
|
expect(User.findById.mock.calls[0][0]).toBe('1234');
|
|
|
|
expect(response.status).toHaveBeenCalledWith(404);
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
|
|
|
error: 'User not found'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an error if label not provided', () => {
|
2019-05-14 16:49:57 +02:00
|
|
|
User.__setFindById(undefined, createUserMock());
|
|
|
|
|
2019-05-13 21:29:00 +02:00
|
|
|
const request = { user: { id: '1234' }, body: {} };
|
|
|
|
const response = createResponseMock();
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
let response;
|
|
|
|
|
|
|
|
const request = {
|
|
|
|
user: { id: '1234' },
|
|
|
|
body: { label: 'my key' }
|
|
|
|
};
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
const user = createUserMock();
|
2019-05-13 21:29:00 +02:00
|
|
|
|
|
|
|
const checkExpecations = () => {
|
2019-05-14 16:49:57 +02:00
|
|
|
const lastKey = last(user.apiKeys);
|
|
|
|
|
|
|
|
expect(lastKey.label).toBe('my key');
|
|
|
|
expect(typeof lastKey.hashedKey).toBe('string');
|
2019-05-13 21:29:00 +02:00
|
|
|
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
2019-05-14 16:49:57 +02:00
|
|
|
apiKeys: [
|
|
|
|
{ id: 0, label: 'my key', token: lastKey.hashedKey }
|
|
|
|
]
|
2019-05-13 21:29:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
response = createResponseMock(checkExpecations);
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
User.__setFindById(undefined, user);
|
2019-05-13 21:29:00 +02:00
|
|
|
|
|
|
|
createApiKey(request, response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeApiKey', () => {
|
|
|
|
it('returns an error if user doesn\'t exist', () => {
|
|
|
|
const request = { user: { id: '1234' } };
|
|
|
|
const response = createResponseMock();
|
|
|
|
|
|
|
|
removeApiKey(request, response);
|
|
|
|
|
|
|
|
expect(User.findById.mock.calls[0][0]).toBe('1234');
|
|
|
|
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' }
|
|
|
|
};
|
|
|
|
const response = createResponseMock();
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
const user = createUserMock();
|
|
|
|
User.__setFindById(undefined, user);
|
2019-05-13 21:29:00 +02:00
|
|
|
|
|
|
|
removeApiKey(request, response);
|
|
|
|
|
|
|
|
expect(response.status).toHaveBeenCalledWith(404);
|
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
|
|
|
error: 'Key does not exist for user'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-15 13:07:20 +02:00
|
|
|
it.skip('removes key if it exists', () => {
|
2019-05-13 21:29:00 +02:00
|
|
|
const request = {
|
|
|
|
user: { id: '1234' },
|
2019-05-14 16:49:57 +02:00
|
|
|
params: { keyId: 0 }
|
2019-05-13 21:29:00 +02:00
|
|
|
};
|
|
|
|
const response = createResponseMock();
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
const user = createUserMock();
|
|
|
|
|
|
|
|
user.apiKeys.push({ label: 'first key' }); // id 0
|
|
|
|
user.apiKeys.push({ label: 'second key' }); // id 1
|
|
|
|
|
|
|
|
User.__setFindById(undefined, user);
|
2019-05-13 21:29:00 +02:00
|
|
|
|
|
|
|
removeApiKey(request, response);
|
|
|
|
|
2019-05-14 16:49:57 +02:00
|
|
|
expect(response.status).toHaveBeenCalledWith(200);
|
2019-05-13 21:29:00 +02:00
|
|
|
expect(response.json).toHaveBeenCalledWith({
|
2019-05-14 16:49:57 +02:00
|
|
|
apiKeys: [
|
|
|
|
{ id: 1, label: 'second key' }
|
|
|
|
]
|
2019-05-13 21:29:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|