p5.js-web-editor/server/controllers/user.controller/__tests__/apiKey.test.js
Andrew Nicolaou 37fcf46972 Public API: Create new project (fixes #1095) (#1106)
* Converts import script to use public API endpoints

The endpoints don't exist yet, but this is a good way to see how
the implementation of the data structures differ.

* Exposes public API endpoint to fetch user's sketches

* Implements public API delete endpoint

* Adds helper to create custom ApplicationError classes

* Adds create project endpoint that understand API's data structure

This transforms the nested tree of file data into a mongoose
Project model

* Returns '201 Created' to match API spec

* Removes 'CustomError' variable assignment as it shows up in test output

* transformFiles will return file validation errors

* Tests API project controller

* Tests toModel()

* Creates default files if no root-level .html file is provided

* Do not auto-generate a slug if it is provided

Fixes a bug where the slug was auto-generated using the sketch name,
even if a slug property had been provided.

* Validates uniqueness of slugs for projects created by the public API

* Adds tests for slug uniqueness

* Configures node's Promise implementation for mongoose (fixes warnings)

* Moves createProject tests to match controller location

* Adds support for code to ApplicationErrors

* deleteProject controller tests

* getProjectsForUser controller tests

- implements tests
- update apiKey tests to use new User mocks

* Ensure error objects have consistent property names

`message` is used as a high-level description of the errors
`detail` is optional and has an plain language explanation of the
individual errors
`errors` is an array of each individual problem from `detail` in a
machine-readable format

* Assert environment variables are provided at script start

* Version public API

* Expect "files" property to always be provided

* Fixes linting error

* Converts import script to use public API endpoints

The endpoints don't exist yet, but this is a good way to see how
the implementation of the data structures differ.

* Exposes public API endpoint to fetch user's sketches

* Implements public API delete endpoint

* Adds helper to create custom ApplicationError classes

* Adds create project endpoint that understand API's data structure

This transforms the nested tree of file data into a mongoose
Project model

* Returns '201 Created' to match API spec

* Removes 'CustomError' variable assignment as it shows up in test output

* transformFiles will return file validation errors

* Tests API project controller

* Tests toModel()

* Creates default files if no root-level .html file is provided

* Do not auto-generate a slug if it is provided

Fixes a bug where the slug was auto-generated using the sketch name,
even if a slug property had been provided.

* Validates uniqueness of slugs for projects created by the public API

* Adds tests for slug uniqueness

* Configures node's Promise implementation for mongoose (fixes warnings)

* Moves createProject tests to match controller location

* deleteProject controller tests

* Adds support for code to ApplicationErrors

* getProjectsForUser controller tests

- implements tests
- update apiKey tests to use new User mocks

* Ensure error objects have consistent property names

`message` is used as a high-level description of the errors
`detail` is optional and has an plain language explanation of the
individual errors
`errors` is an array of each individual problem from `detail` in a
machine-readable format

* Assert environment variables are provided at script start

* Version public API

* Expect "files" property to always be provided

* Fixes linting error

* Checks that authenticated user has permission to create under this namespace

Previously, the project was always created under the authenticated user's
namespace, but this not obvious behaviour.
2019-09-08 16:45:58 +02:00

180 lines
4.6 KiB
JavaScript

/* @jest-environment node */
import last from 'lodash/last';
import { Request, Response } from 'jest-express';
import User, { createMock, createInstanceMock } from '../../../models/user';
import { createApiKey, removeApiKey } from '../../user.controller/apiKey';
jest.mock('../../../models/user');
describe('user.controller', () => {
let UserMock;
let UserInstanceMock;
beforeEach(() => {
UserMock = createMock();
UserInstanceMock = createInstanceMock();
});
afterEach(() => {
UserMock.restore();
UserInstanceMock.restore();
});
describe('createApiKey', () => {
it('returns an error if user doesn\'t exist', () => {
const request = { user: { id: '1234' } };
const response = new Response();
UserMock
.expects('findById')
.withArgs('1234')
.yields(null, null);
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: {} };
const response = new Response();
UserMock
.expects('findById')
.withArgs('1234')
.yields(null, new User());
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) => {
const request = new Request();
request.setBody({ label: 'my key' });
request.user = { id: '1234' };
const response = new Response();
const user = new User();
UserMock
.expects('findById')
.withArgs('1234')
.yields(null, user);
UserInstanceMock.expects('save')
.yields();
function expectations() {
const lastKey = last(user.apiKeys);
expect(lastKey.label).toBe('my key');
expect(typeof lastKey.hashedKey).toBe('string');
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
});
done();
}
const promise = createApiKey(request, response);
promise.then(expectations, expectations).catch(expectations);
});
});
describe('removeApiKey', () => {
it('returns an error if user doesn\'t exist', () => {
const request = { user: { id: '1234' } };
const response = new Response();
UserMock
.expects('findById')
.withArgs('1234')
.yields(null, null);
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' }
};
const response = new Response();
const user = new User();
UserMock
.expects('findById')
.withArgs('1234')
.yields(null, user);
removeApiKey(request, response);
expect(response.status).toHaveBeenCalledWith(404);
expect(response.json).toHaveBeenCalledWith({
error: 'Key does not exist for user'
});
});
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();
const request = {
user: { id: '1234' },
params: { keyId: firstKeyId }
};
const response = new Response();
UserMock
.expects('findById')
.withArgs('1234')
.yields(null, user);
UserInstanceMock
.expects('save')
.yields();
removeApiKey(request, response);
expect(response.status).toHaveBeenCalledWith(200);
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
});
});
});
});