-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.test.js
167 lines (125 loc) · 3.97 KB
/
api.test.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describe, before, after, it } from "node:test";
import assert from "node:assert";
const BASE_URL = "http://localhost:3000";
describe("/login", () => {
/** @type {import("node:http").Server} */
let _server = {};
let _globalToken = "";
before(async () => {
_server = (await import("./api.js")).app;
await new Promise((resolve) => _server.once("listening", resolve));
});
after((done) => _server.close(done));
it("should receive not authorized given wrong user and password", async () => {
const data = {
user: "eduardofernandes",
password: "",
};
// fetch() ou biblioteca externa chamada supertest
const request = await fetch(`${BASE_URL}/login`, {
method: "POST",
body: JSON.stringify(data),
});
assert.strictEqual(request.status, 401);
const response = await request.json();
assert.deepStrictEqual(response, { error: "user invalid" });
});
it("should login successfully given user and password", async () => {
const data = {
user: "eduardofernandes",
password: "123",
};
const request = await fetch(`${BASE_URL}/login`, {
method: "POST",
body: JSON.stringify(data),
});
assert.strictEqual(request.status, 200);
const response = await request.json();
assert.ok(response.token, "JWT token should exist");
_globalToken = response.token;
});
it("should not be allowed to access private data without a token", async () => {
const request = await fetch(`${BASE_URL}/`, {
method: "GET",
headers: {
authorization: "",
},
});
assert.strictEqual(request.status, 400);
const response = await request.json();
assert.deepStrictEqual(response, { error: "invalid token!" });
});
it("should be allowed to access private data with a valid token", async () => {
const request = await fetch(`${BASE_URL}/login`, {
method: "GET",
headers: { authorization: _globalToken },
});
assert.strictEqual(request.status, 200);
const response = await request.json();
assert.deepStrictEqual(response, { result: "Hey, Welcome!" });
});
it("should not create a product without a valid token", async () => {
const input = {
description: "pasta de dente",
price: 101,
};
const request = await fetch(`${BASE_URL}/products`, {
method: "POST",
body: JSON.stringify(input),
headers: {
authorization: "",
},
});
assert.strictEqual(request.status, 400);
const response = await request.json();
assert.deepStrictEqual(response, { error: "invalid token!" });
});
it("should create a premium product", async () => {
const input = {
description: "pasta de dente",
price: 101,
};
const request = await fetch(`${BASE_URL}/products`, {
method: "POST",
body: JSON.stringify(input),
headers: {
authorization: _globalToken,
},
});
assert.strictEqual(request.status, 200);
const data = await request.json();
assert.deepStrictEqual(data.category, "premium");
});
it("should create a regular product", async () => {
const input = {
description: "enxaguante bucal",
price: 77,
};
const request = await fetch(`${BASE_URL}/products`, {
method: "POST",
body: JSON.stringify(input),
headers: {
authorization: _globalToken,
},
});
assert.strictEqual(request.status, 200);
const data = await request.json();
assert.deepStrictEqual(data.category, "regular");
});
it("should create a basic product", async () => {
const input = {
description: "escova de dente",
price: 36,
};
const request = await fetch(`${BASE_URL}/products`, {
method: "POST",
body: JSON.stringify(input),
headers: {
authorization: _globalToken,
},
});
assert.strictEqual(request.status, 200);
const data = await request.json();
assert.deepStrictEqual(data.category, "basic");
});
});