Added Jest for testing both front and backend
This commit is contained in:
10
server/.jest-config
Normal file
10
server/.jest-config
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"verbose": true,
|
||||
"testPathDirs": [
|
||||
"<rootDir>/server"
|
||||
],
|
||||
"setupFiles": [
|
||||
"<rootDir>/__mocks__/console.js",
|
||||
"./server/test/helper.js"
|
||||
]
|
||||
}
|
||||
16
server/api/__snapshots__/user.test.js.snap
Normal file
16
server/api/__snapshots__/user.test.js.snap
Normal file
@@ -0,0 +1,16 @@
|
||||
exports[`test should require authentication 1`] = `
|
||||
Object {
|
||||
"message": "Authentication required"
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`test should return known user 1`] = `
|
||||
Object {
|
||||
"data": Object {
|
||||
"avatarUrl": "http://example.com/avatar.png",
|
||||
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
|
||||
"name": "User 1",
|
||||
"username": "user1"
|
||||
}
|
||||
}
|
||||
`;
|
||||
@@ -10,8 +10,6 @@ export default function auth({ require = true } = {}) {
|
||||
return async function authMiddleware(ctx, next) {
|
||||
let token;
|
||||
|
||||
console.log(ctx.body);
|
||||
|
||||
const authorizationHeader = ctx.request.get('authorization');
|
||||
if (authorizationHeader) {
|
||||
const parts = authorizationHeader.split(' ');
|
||||
|
||||
37
server/api/user.test.js
Normal file
37
server/api/user.test.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import TestServer from 'fetch-test-server';
|
||||
|
||||
import app from '..';
|
||||
import { User } from '../models';
|
||||
|
||||
import { flushdb, seed, sequelize } from '../test/support';
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
|
||||
beforeEach(seed);
|
||||
afterEach(flushdb);
|
||||
afterAll(() => server.close());
|
||||
afterAll(() => sequelize.close());
|
||||
|
||||
it('should return known user', async () => {
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
email: 'user1@example.com',
|
||||
},
|
||||
});
|
||||
|
||||
const res = await server.post('/api/user.info', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should require authentication', async () => {
|
||||
const res = await server.post('/api/user.info');
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import crypto from 'crypto';
|
||||
import {
|
||||
DataTypes,
|
||||
sequelize,
|
||||
encryptedFields
|
||||
encryptedFields,
|
||||
} from '../sequelize';
|
||||
|
||||
import JWT from 'jsonwebtoken';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Sequelize from 'sequelize';
|
||||
import _orderBy from 'lodash.orderby';
|
||||
import _ from 'lodash';
|
||||
import { Document, Atlas, User, Revision } from './models';
|
||||
|
||||
export function presentUser(ctx, user) {
|
||||
@@ -123,7 +123,7 @@ export function presentCollection(ctx, collection, includeRecentDocuments=false)
|
||||
includeCollaborators: true,
|
||||
}));
|
||||
}));
|
||||
data.recentDocuments = _orderBy(recentDocuments, ['updatedAt'], ['desc']);
|
||||
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
|
||||
}
|
||||
|
||||
resolve(data);
|
||||
|
||||
8
server/presenters/__snapshots__/user.test.js.snap
Normal file
8
server/presenters/__snapshots__/user.test.js.snap
Normal file
@@ -0,0 +1,8 @@
|
||||
exports[`test presents a user 1`] = `
|
||||
Object {
|
||||
"avatarUrl": "http://example.com/avatar.png",
|
||||
"id": "123",
|
||||
"name": "Test User",
|
||||
"username": "testuser"
|
||||
}
|
||||
`;
|
||||
15
server/presenters/user.js
Normal file
15
server/presenters/user.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const presentUser = (ctx, user) => {
|
||||
ctx.cache.set(user.id, user);
|
||||
|
||||
return new Promise(async (resolve, _reject) => {
|
||||
const data = {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
username: user.username,
|
||||
avatarUrl: user.slackData.image_192,
|
||||
};
|
||||
resolve(data);
|
||||
});
|
||||
};
|
||||
|
||||
export default presentUser;
|
||||
19
server/presenters/user.test.js
Normal file
19
server/presenters/user.test.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import presentUser from './user';
|
||||
|
||||
import ctx from '../../__mocks__/ctx';
|
||||
|
||||
it('presents a user', async () => {
|
||||
const user = await presentUser(
|
||||
ctx,
|
||||
{
|
||||
id: '123',
|
||||
name: 'Test User',
|
||||
username: 'testuser',
|
||||
slackData: {
|
||||
image_192: 'http://example.com/avatar.png',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(user).toMatchSnapshot();
|
||||
});
|
||||
30
server/test/helper.js
Normal file
30
server/test/helper.js
Normal file
@@ -0,0 +1,30 @@
|
||||
require('localenv');
|
||||
|
||||
// test environment variables
|
||||
process.env.DATABASE_URL = process.env.DATABASE_URL_TEST;
|
||||
process.env.NODE_ENV = 'test';
|
||||
|
||||
const Sequelize = require('sequelize');
|
||||
const sequelize = require('../sequelize').sequelize;
|
||||
const Umzug = require('umzug');
|
||||
|
||||
const queryInterface = sequelize.getQueryInterface();
|
||||
|
||||
function runMigrations() {
|
||||
const umzug = new Umzug({
|
||||
storage: 'sequelize',
|
||||
storageOptions: {
|
||||
sequelize,
|
||||
},
|
||||
migrations: {
|
||||
params: [queryInterface, Sequelize],
|
||||
path: './server/migrations',
|
||||
},
|
||||
});
|
||||
umzug.up()
|
||||
.then(() => {
|
||||
sequelize.close();
|
||||
});
|
||||
}
|
||||
|
||||
runMigrations();
|
||||
29
server/test/support.js
Normal file
29
server/test/support.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { User } from '../models';
|
||||
import { sequelize } from '../sequelize';
|
||||
|
||||
export function flushdb() {
|
||||
const sql = sequelize.getQueryInterface();
|
||||
const tables = Object.keys(sequelize.models).map((model) =>
|
||||
sql.quoteTable(sequelize.models[model].getTableName()));
|
||||
const query = `TRUNCATE ${tables.join(', ')} CASCADE`;
|
||||
|
||||
return sequelize.query(query);
|
||||
}
|
||||
|
||||
const seed = async () => {
|
||||
await User.create({
|
||||
id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61',
|
||||
email: 'user1@example.com',
|
||||
username: 'user1',
|
||||
name: 'User 1',
|
||||
slackId: '123',
|
||||
slackData: {
|
||||
image_192: 'http://example.com/avatar.png',
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export {
|
||||
seed,
|
||||
sequelize,
|
||||
};
|
||||
Reference in New Issue
Block a user