Skip to content

Commit

Permalink
Integration test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehbraheem committed Jun 2, 2020
1 parent f69f86e commit cc3613c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { http } from '@ctt/service-utils';
import { http, test } from '@ctt/service-utils';
import { factory } from 'factory-girl';
import application, {
parsedSingleResponse,
parsedErrorResponse,
} from '../../test/utils/requestHelper';
import application from '../../test/utils/requestHelper';
import config, { configFiles } from '../utils/loadconfig';

import '../../test/factories/{{{scaffold_factory}}}';
import { verify{{{scaffold_entity_capitalise}}}, verifyResponse } from '../../test/helpers/{{{scaffold_entities}}}';

import { ROUTE_NAME } from './routes';

const { response: { CREATED, JSON_TYPE , BAD_REQUEST} } = http;
const {
requestHelpers: { parsedSingleResponse, parsedErrorResponse, bearerToken },
} = test;

config.loadFile(configFiles);

let app = null;
const url = `/${ROUTE_NAME}`
const url = `/${ROUTE_NAME}`;
let headers = null;

describe('Users', () => {
beforeAll(async () => {
app = await application();
await app.start();
headers = bearerToken(config);
});

afterAll(async () => {
Expand All @@ -35,6 +40,7 @@ describe('Users', () => {
method: 'POST',
url,
payload,
headers,
});

expect(response.statusCode).toBe(CREATED.code);
Expand All @@ -55,6 +61,7 @@ describe('Users', () => {
method: 'POST',
url,
payload,
headers,
});

expect(response.statusCode).toBe(BAD_REQUEST.code);
Expand Down
2 changes: 1 addition & 1 deletion scaffold/service/package.json.replace
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"dependencies": {
"@ctt/crud-api": "1.6.27",
"@ctt/redis-client": "0.0.0-beta.5",
"@ctt/service-utils": "1.3.10",
"@ctt/service-utils": "1.3.11",
"convict": "4.3.2",
"dotenv": "6.0.0",
"hapi-auth-jwt2": "8.1.0",
Expand Down
34 changes: 1 addition & 33 deletions scaffold/service/test/utils/requestHelper.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
import { factory } from 'factory-girl';
import { jwt } from '@ctt/service-utils';
import server, { Map } from '../../src/server';
import config, { configFiles } from '../../src/utils/loadconfig';
import server from '../../src/server';
import { Server } from 'hapi';

config.loadFile(configFiles);

export default async (): Promise<Server> => await server();

export const bearerToken = async (): Promise<Map<string>> => {
const user = {
fullname: 'test Test',
email: 'bolatan.ibrahim${n}@craftturf.com',
role: '5eb01d542a78e8e74c3ff56c',
}
const { email, fullname, role } = user;
const token = jwt.generateToken(
{
fullname,
role,
email,
},
config,
);

return { authorization: token };
};

export const parsedSingleResponse = ({ payload }): object | any => JSON.parse(payload)['data'][0];

export const parsedResponse = ({ payload }): object | any => JSON.parse(payload);

export const parsedArrayResponse = ({ payload }): object | any => JSON.parse(payload)['data'];

export const parsedErrorResponse = ({ payload }): object | any => JSON.parse(payload)['error'];

0 comments on commit cc3613c

Please sign in to comment.