/
user.test.ts
98 lines (77 loc) · 3.04 KB
/
user.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { SDK } from '../../../src';
import { ServerError } from '../../../src/errors';
import Utils from '../../utils';
describe('User Validation Tests', () => {
let sdk: SDK;
beforeEach(() => {
sdk = Utils.SDK();
});
test('should handle empty name and email error', async () => {
expect.assertions(3);
try {
const req = { name: Utils.testConstants.TEST_EMPTY_STRING, email: Utils.testConstants.TEST_EMPTY_STRING };
await sdk.users().create(req);
} catch (error) {
expect(error).toBeInstanceOf(ServerError);
expect((error as ServerError).httpStatusCode).toEqual(400);
expect((error as ServerError).getValidationMessages()).toEqual(['name: cannot be blank']);
}
});
test('should handle successful create', async () => {
expect.assertions(1);
const req = { name: Utils.createRandomTestName(), email: Utils.createRandomTestEmail() };
const sendResponse = await sdk.users().create(req);
expect(sendResponse.httpStatusCode).toEqual(200);
});
test('should handle not found delete', async () => {
expect.assertions(3);
try {
const req = {};
await sdk.users().delete(Utils.testConstants.TEST_USER_ID, req);
} catch (error) {
expect(error).toBeInstanceOf(ServerError);
expect((error as ServerError).httpStatusCode).toEqual(400);
expect((error as ServerError).getValidationMessages()).toEqual(['userID: does not exist']);
}
});
test('should handle successful delete', async () => {
const req = {};
const userId = await Utils.createUser();
const response = await sdk.users().delete(userId, req);
expect(response.httpStatusCode).toEqual(200);
});
test('should handle not found get', async () => {
expect.assertions(2);
try {
await sdk.users().get(Utils.testConstants.TEST_USER_ID);
} catch (error) {
expect(error).toBeInstanceOf(ServerError);
expect((error as ServerError).httpStatusCode).toEqual(404);
}
});
test('should handle successful get', async () => {
expect.assertions(1);
const userId = await Utils.createUser();
const getResponse = await sdk.users().get(userId);
expect(getResponse.httpStatusCode).toEqual(200);
});
test('should handle invalid list sort', async () => {
expect.assertions(3);
try {
await sdk.users().list(Utils.testConstants.TEST_EMPTY_STRING, Utils.testConstants.TEST_EMPTY_STRING, 'foo:bar');
} catch (error) {
expect(error).toBeInstanceOf(ServerError);
expect((error as ServerError).httpStatusCode).toEqual(422);
expect((error as ServerError).getValidationMessages()).toEqual(["sort: Invalid order direction 'bar'"]);
}
});
test('should handle successful list', async () => {
expect.assertions(1);
const userId = await Utils.createUser();
const sendResponse = await sdk
.users()
.list(Utils.testConstants.TEST_EMPTY_STRING, Utils.testConstants.TEST_EMPTY_STRING, 'created:desc');
const found = sendResponse.data.users.some((user) => user.ID === userId);
expect(found).toEqual(true);
});
});