Skip to content

Commit 3a10988

Browse files
author
hirsch88
committed
finish black box testing for the user resource
1 parent 93b3bdb commit 3a10988

File tree

3 files changed

+127
-15
lines changed

3 files changed

+127
-15
lines changed

test/black-box/lib/ApiResponeTest.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import * as _ from 'lodash';
22

33

44
export class ApiResponeTest {
5-
constructor(private res: any) { }
5+
6+
constructor(private error: any, private res: any) {
7+
}
68

79
getBody<T>(): T {
810
return this.res['body'];
@@ -13,11 +15,19 @@ export class ApiResponeTest {
1315
}
1416

1517
getHeaders<T>(): T {
16-
return this.res['headers'];
18+
if (this.res) {
19+
return this.res['headers'];
20+
} else {
21+
return this.error['response']['headers'];
22+
}
1723
}
1824

1925
expectStatusCode(code: number): ApiResponeTest {
20-
expect(this.res['statusCode']).toBe(code);
26+
if (this.res) {
27+
expect(this.res['statusCode']).toBe(code);
28+
} else {
29+
expect(this.error['statusCode']).toBe(code);
30+
}
2131
return this;
2232
}
2333

@@ -34,4 +44,13 @@ export class ApiResponeTest {
3444
expect(this.getBody()['success']).toBeTruthy();
3545
return this;
3646
}
47+
48+
printResponse(): void {
49+
console.log(this.res);
50+
}
51+
52+
printError(): void {
53+
console.log(this.error);
54+
}
55+
3756
}

test/black-box/lib/api.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,14 @@ export const api = async <T>(method: string, path: string, options: ApiOptions<T
1919
json: true,
2020
body: options.body
2121
};
22-
return new ApiResponeTest(await request(o));
22+
23+
let res = undefined;
24+
let error = null;
25+
try {
26+
res = await request(o);
27+
} catch (e) {
28+
error = e;
29+
}
30+
31+
return new ApiResponeTest(error, res);
2332
};

test/black-box/user.test.ts

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,125 @@ import { DatabaseResetCommand } from '../../src/console/DatabaseResetCommand';
44
let createdId;
55
const userKeys = ['id', 'firstName', 'lastName', 'email', 'updatedAt', 'createdAt'];
66

7+
const testUser = {
8+
firstName: 'Hans',
9+
lastName: 'Muster',
10+
email: 'hans@muster.ch'
11+
};
12+
13+
const testUserUpdated = {
14+
firstName: 'Horst',
15+
lastName: 'Maier',
16+
email: 'horst@maier.ch'
17+
};
18+
719
beforeAll(async () => {
820
await DatabaseResetCommand.run();
921
});
1022

11-
test('POST [/v1/user] - should create a new user', async () => {
23+
test('GET [/v1/user] - should return a empty list', async () => {
24+
const res = await api('GET', '/api/v1/user');
25+
res.expectJson();
26+
res.expectStatusCode(200);
27+
const data = res.getData<any[]>();
28+
expect(data.length).toBe(0);
29+
});
30+
31+
test('POST [/v1/user] - should create a new user', async () => {
1232
const res = await api('POST', '/api/v1/user', {
13-
body: {
14-
firstName: 'Hans',
15-
lastName: 'Muster',
16-
email: 'hans@muster.ch'
17-
}
33+
body: testUser
1834
});
1935
res.expectJson();
2036
res.expectStatusCode(201);
2137
res.expectData(userKeys);
2238
createdId = res.getData()['id'];
2339
});
2440

25-
test('GET [/v1/user] - should return the api info as a json', async () => {
41+
test('POST [/v1/user] - should fail because we want to create a empty user', async () => {
42+
const res = await api('POST', '/api/v1/user', {
43+
body: {}
44+
});
45+
res.expectJson();
46+
res.expectStatusCode(400);
47+
});
48+
49+
test('GET [/v1/user] - should list of users with our new create one', async () => {
2650
const res = await api('GET', '/api/v1/user');
2751
res.expectJson();
2852
res.expectStatusCode(200);
2953
res.expectData(userKeys);
54+
const data = res.getData<any[]>();
55+
expect(data.length).toBe(1);
56+
57+
const user = data[0];
58+
expect(user.firstName).toBe(testUser.firstName);
59+
expect(user.lastName).toBe(testUser.lastName);
60+
expect(user.email).toBe(testUser.email);
3061
});
3162

32-
test('GET [/v1/user/:id] - should return the api info as a json', async () => {
63+
test('GET [/v1/user/:id] - should return one user', async () => {
3364
const res = await api('GET', `/api/v1/user/${createdId}`);
3465
res.expectJson();
3566
res.expectStatusCode(200);
3667
res.expectData(userKeys);
3768

3869
const user: any = res.getData();
39-
expect(user.firstName).toBe('Hans');
40-
expect(user.lastName).toBe('Muster');
41-
expect(user.email).toBe('hans@muster.ch');
70+
expect(user.firstName).toBe(testUser.firstName);
71+
expect(user.lastName).toBe(testUser.lastName);
72+
expect(user.email).toBe(testUser.email);
73+
});
74+
75+
test('PUT [/v1/user/:id] - should update the user', async () => {
76+
const res = await api('PUT', `/api/v1/user/${createdId}`, {
77+
body: testUserUpdated
78+
});
79+
res.expectJson();
80+
res.expectStatusCode(200);
81+
res.expectData(userKeys);
82+
83+
const user: any = res.getData();
84+
expect(user.firstName).toBe(testUserUpdated.firstName);
85+
expect(user.lastName).toBe(testUserUpdated.lastName);
86+
expect(user.email).toBe(testUserUpdated.email);
87+
});
88+
89+
test('PUT [/v1/user/:id] - should fail because we want to update the user with a invalid email', async () => {
90+
const res = await api('PUT', `/api/v1/user/${createdId}`, {
91+
body: {
92+
email: 'abc'
93+
}
94+
});
95+
res.expectJson();
96+
res.expectStatusCode(400);
97+
});
98+
99+
test('DELETE [/v1/user/:id] - should delete the user', async () => {
100+
const res = await api('DELETE', `/api/v1/user/${createdId}`);
101+
res.expectStatusCode(204);
102+
});
103+
104+
/**
105+
* 404 - NotFound Testing
106+
*/
107+
test('GET [/v1/user/:id] - should return with a 404, because we just deleted the user', async () => {
108+
const res = await api('GET', `/api/v1/user/${createdId}`);
109+
res.expectJson();
110+
res.expectStatusCode(404);
111+
});
112+
113+
test('DELETE [/v1/user/:id] - should return with a 404, because we just deleted the user', async () => {
114+
const res = await api('DELETE', `/api/v1/user/${createdId}`);
115+
res.expectJson();
116+
res.expectStatusCode(404);
42117
});
43118

119+
test('PUT [/v1/user/:id] - should return with a 404, because we just deleted the user', async () => {
120+
const res = await api('PUT', `/api/v1/user/${createdId}`);
121+
res.expectJson();
122+
res.expectStatusCode(404);
123+
});
124+
125+
126+
127+
44128

0 commit comments

Comments
 (0)