Skip to content

Commit dae4290

Browse files
author
hirsch88
committed
improve black-box testing
1 parent 476d9aa commit dae4290

File tree

10 files changed

+203
-794
lines changed

10 files changed

+203
-794
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@types/faker": "^4.1.0",
5757
"@types/helmet": "0.0.35",
5858
"@types/jest": "^19.2.3",
59+
"@types/jsonwebtoken": "^7.2.0",
5960
"@types/knex": "0.0.50",
6061
"@types/lodash": "^4.14.64",
6162
"@types/morgan": "^1.7.32",
@@ -87,6 +88,7 @@
8788
"inversify": "^4.1.0",
8889
"inversify-express-utils": "^3.5.1",
8990
"jest": "^20.0.3",
91+
"jsonwebtoken": "^7.4.1",
9092
"knex": "^0.12.0",
9193
"lodash": "^4.17.4",
9294
"morgan": "^1.7.0",

src/api/models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bookshelf } from '../../core/Bookshelf';
1+
import { Bookshelf } from '../../core/Database';
22
import { Tables } from '../../constants/Tables';
33

44
/**

src/api/requests/UserCreateRequest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ export class UserCreateRequest extends RequestBody {
2222
@IsEmail()
2323
email: string;
2424

25+
picture: string;
26+
27+
@IsNotEmpty()
28+
auth0UserId: string;
29+
2530
}
2631

src/api/requests/UserUpdateRequest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export class UserUpdateRequest extends RequestBody {
2525
@IsEmail()
2626
email: string;
2727

28+
picture: string;
29+
30+
@IsNotEmpty()
31+
auth0UserId: string;
32+
2833
setFirstName(value: string): void {
2934
this.update('firstName', value);
3035
}

src/core/Bookshelf.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import * as knex from 'knex';
2+
import * as bookshelf from 'bookshelf';
23
import { Environment } from './Environment';
34

45

5-
export const Knex: knex = knex({
6+
export const Knex = (): knex => knex({
67
client: Environment.get<string>('DB_CLIENT'),
78
connection: Environment.get<string>('DB_CONNECTION'),
89
pool: {
910
min: Environment.get<number>('DB_POOL_MIN'),
1011
max: Environment.get<number>('DB_POOL_MAX')
1112
}
12-
// migrations: {
13-
// tableName: Environment.get<string>('DB_MIGRATION_TABLE')
14-
// }
1513
});
14+
15+
export const Bookshelf: bookshelf = bookshelf(<any>Knex());
16+
Bookshelf.plugin(['bookshelf-camelcase']);

test/black-box/lib/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ApiResponeTest } from './ApiResponeTest';
55

66

77
export interface ApiOptions<T> {
8+
token?: string;
89
body?: T;
910
headers?: any;
1011
}
@@ -20,6 +21,11 @@ export const api = async <T>(method: string, path: string, options: ApiOptions<T
2021
body: options.body
2122
};
2223

24+
if (options.token) {
25+
o.headers = {};
26+
o.headers['authorization'] = `Bearer ${options.token}`;
27+
}
28+
2329
let res = undefined;
2430
let error = null;
2531
try {

test/black-box/lib/auth.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as jwt from 'jsonwebtoken';
2+
import { Knex } from '../../../src/core/Database';
3+
import { Tables } from '../../../src/constants/Tables';
4+
5+
6+
export const USER = {
7+
firstName: 'Bruce',
8+
lastName: 'Wayne',
9+
email: 'bw@enterprice.com',
10+
auth0UserId: 'batman'
11+
};
12+
13+
export const createAdminUser = async () => {
14+
const knex = Knex();
15+
const user = await knex(Tables.Users).insert({
16+
first_name: USER.firstName,
17+
last_name: USER.lastName,
18+
email: USER.email,
19+
auth_0_user_id: USER.auth0UserId
20+
});
21+
await knex.destroy();
22+
return user;
23+
};
24+
25+
export const getToken = (auth0UserId?: string) => {
26+
return jwt.sign({
27+
user_id: `auth0|${auth0UserId || USER.auth0UserId}`
28+
}, 'auth0-mock');
29+
};

test/black-box/user.test.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
import { api } from './lib/api';
22
import { DatabaseResetCommand } from '../../src/console/DatabaseResetCommand';
3+
import { createAdminUser, getToken } from './lib/auth';
34

4-
let createdId;
5-
const userKeys = ['id', 'firstName', 'lastName', 'email', 'updatedAt', 'createdAt'];
65

7-
const testUser = {
8-
firstName: 'Hans',
9-
lastName: 'Muster',
10-
email: 'hans@muster.ch'
11-
};
6+
describe('User', () => {
127

13-
const testUserUpdated = {
14-
firstName: 'Horst',
15-
lastName: 'Maier',
16-
email: 'horst@maier.ch'
17-
};
8+
const userKeys = ['id', 'firstName', 'lastName', 'email', 'picture', 'auth0UserId', 'updatedAt', 'createdAt'];
189

10+
const testUser = {
11+
firstName: 'Hans',
12+
lastName: 'Muster',
13+
email: 'hans@muster.ch',
14+
auth0UserId: '1234'
15+
};
1916

20-
describe('User', () => {
17+
const testUserUpdated = {
18+
firstName: 'Horst',
19+
lastName: 'Maier',
20+
email: 'horst@maier.ch'
21+
};
2122

23+
let token;
24+
let auth;
25+
let createdId;
2226
beforeAll(async () => {
2327
await DatabaseResetCommand.run();
24-
});
25-
26-
test('GET /v1/user Should return a empty list', async () => {
27-
const res = await api('GET', '/api/v1/user');
28-
res.expectJson();
29-
res.expectStatusCode(200);
30-
const data = res.getData<any[]>();
31-
expect(data.length).toBe(0);
28+
await createAdminUser();
29+
token = getToken();
30+
auth = {
31+
token: token
32+
};
3233
});
3334

3435
test('POST /v1/user Should create a new user', async () => {
3536
const res = await api('POST', '/api/v1/user', {
37+
token: token,
3638
body: testUser
3739
});
3840
res.expectJson();
@@ -43,28 +45,29 @@ describe('User', () => {
4345

4446
test('POST /v1/user Should fail because we want to create a empty user', async () => {
4547
const res = await api('POST', '/api/v1/user', {
48+
token: token,
4649
body: {}
4750
});
4851
res.expectJson();
4952
res.expectStatusCode(400);
5053
});
5154

5255
test('GET /v1/user Should list of users with our new create one', async () => {
53-
const res = await api('GET', '/api/v1/user');
56+
const res = await api('GET', '/api/v1/user', auth);
5457
res.expectJson();
5558
res.expectStatusCode(200);
5659
res.expectData(userKeys);
5760
const data = res.getData<any[]>();
58-
expect(data.length).toBe(1);
61+
expect(data.length).toBe(2);
5962

60-
const user = data[0];
63+
const user = data[1];
6164
expect(user.firstName).toBe(testUser.firstName);
6265
expect(user.lastName).toBe(testUser.lastName);
6366
expect(user.email).toBe(testUser.email);
6467
});
6568

6669
test('GET /v1/user/:id Should return one user', async () => {
67-
const res = await api('GET', `/api/v1/user/${createdId}`);
70+
const res = await api('GET', `/api/v1/user/${createdId}`, auth);
6871
res.expectJson();
6972
res.expectStatusCode(200);
7073
res.expectData(userKeys);
@@ -77,6 +80,7 @@ describe('User', () => {
7780

7881
test('PUT /v1/user/:id Should update the user', async () => {
7982
const res = await api('PUT', `/api/v1/user/${createdId}`, {
83+
token: token,
8084
body: testUserUpdated
8185
});
8286
res.expectJson();
@@ -91,6 +95,7 @@ describe('User', () => {
9195

9296
test('PUT /v1/user/:id Should fail because we want to update the user with a invalid email', async () => {
9397
const res = await api('PUT', `/api/v1/user/${createdId}`, {
98+
token: token,
9499
body: {
95100
email: 'abc'
96101
}
@@ -100,27 +105,27 @@ describe('User', () => {
100105
});
101106

102107
test('DELETE /v1/user/:id Should delete the user', async () => {
103-
const res = await api('DELETE', `/api/v1/user/${createdId}`);
108+
const res = await api('DELETE', `/api/v1/user/${createdId}`, auth);
104109
res.expectStatusCode(204);
105110
});
106111

107112
/**
108113
* 404 - NotFound Testing
109114
*/
110115
test('GET /v1/user/:id Should return with a 404, because we just deleted the user', async () => {
111-
const res = await api('GET', `/api/v1/user/${createdId}`);
116+
const res = await api('GET', `/api/v1/user/${createdId}`, auth);
112117
res.expectJson();
113118
res.expectStatusCode(404);
114119
});
115120

116121
test('DELETE /v1/user/:id Should return with a 404, because we just deleted the user', async () => {
117-
const res = await api('DELETE', `/api/v1/user/${createdId}`);
122+
const res = await api('DELETE', `/api/v1/user/${createdId}`, auth);
118123
res.expectJson();
119124
res.expectStatusCode(404);
120125
});
121126

122127
test('PUT /v1/user/:id Should return with a 404, because we just deleted the user', async () => {
123-
const res = await api('PUT', `/api/v1/user/${createdId}`);
128+
const res = await api('PUT', `/api/v1/user/${createdId}`, auth);
124129
res.expectJson();
125130
res.expectStatusCode(404);
126131
});

0 commit comments

Comments
 (0)