Skip to content

Commit 0242d6a

Browse files
committed
Feat: Tests productController createProduct
1 parent b7014e2 commit 0242d6a

2 files changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import { makePOSTWithTokenRequests } from "../../utils/FetchFunctionsTests/postFunctionWithToken.js";
2+
import { makePOSTRequests } from "../../utils/FetchFunctionsTests/postFunction.js"
3+
import { PrismaService } from "../../database/prisma/prismaService.js";
4+
import { describe, it, before, after } from "node:test";
5+
import assert from "node:assert";
6+
7+
const prismaService = new PrismaService();
8+
9+
describe("Product Controller (createProduct) Tests", () => {
10+
11+
before(async () => {
12+
await prismaService._prismaService.users.deleteMany();
13+
});
14+
15+
after(async () => {
16+
await prismaService._prismaService.users.deleteMany();
17+
await prismaService._prismaService.$disconnect();
18+
});
19+
20+
it("Should be able to create a new Product", async () => {
21+
22+
const user = {
23+
username: "Margaret Richards",
24+
email: "ekzo@fezihov.mt",
25+
password: "2434"
26+
}
27+
28+
const endPoint = "createUser";
29+
await makePOSTRequests(endPoint, user);
30+
31+
const endPoint2 = "loginUser";
32+
const request = {
33+
email: user.email,
34+
password: user.password
35+
}
36+
37+
const data2 = await makePOSTRequests(endPoint2, request);
38+
39+
const endPoint3 = "createProduct";
40+
const request2 = {
41+
product_name: "Smart TV",
42+
price: 5200.10,
43+
category: "TV",
44+
token: data2.accessToken
45+
}
46+
47+
const data3 = await makePOSTWithTokenRequests(endPoint3, request2);
48+
49+
assert.ok(data3.product.hasOwnProperty("user_id"));
50+
assert.ok(data3.product.hasOwnProperty("product_id"));
51+
assert.equal(data3.product.product_name, "Smart TV");
52+
assert.equal(data3.product.price, 5200.10);
53+
assert.equal(data3.product.category, "TV");
54+
assert.ok(data3.product.hasOwnProperty("createdAt"));
55+
});
56+
57+
it("should not be able to create a product, if user dosen't have or dosen't set a valid (token)", async () => {
58+
59+
const user = {
60+
username: "Claudia Greene",
61+
email: "ozvut@bac.br",
62+
password: "1199"
63+
}
64+
65+
const endPoint1 = "createUser";
66+
await makePOSTRequests(endPoint1, user);
67+
68+
const endPoint2 = "loginUser";
69+
const request = {
70+
email: user.email,
71+
password: user.password
72+
}
73+
74+
await makePOSTRequests(endPoint2, request);
75+
76+
const endPoint3 = "createProduct";
77+
const request2 = {
78+
product_name: "Playstation 5",
79+
price: 5000.50,
80+
category: "Video game",
81+
token: ""
82+
}
83+
84+
const data3 = await makePOSTWithTokenRequests(endPoint3, request2);
85+
86+
assert.strictEqual(data3.message, "jwt must be provided");
87+
});
88+
89+
it("should not be able to create a product, if the (token) is not valid or fake", async () => {
90+
91+
const user = {
92+
username: "Victor Andrews",
93+
email: "dumpeuf@sulucep.ad",
94+
password: "2244"
95+
}
96+
97+
const endPoint1 = "createUser";
98+
await makePOSTRequests(endPoint1, user);
99+
100+
const endPoint2 = "loginUser";
101+
const request = {
102+
email: user.email,
103+
password: user.password
104+
}
105+
106+
await makePOSTRequests(endPoint2, request);
107+
108+
const endPoint3 = "createProduct";
109+
const request2 = {
110+
product_name: "Nootebook",
111+
price: 7500.00,
112+
category: "Computer",
113+
token: "FAKE_TOKEN"
114+
}
115+
116+
const data3 = await makePOSTWithTokenRequests(endPoint3, request2);
117+
118+
assert.rejects(async () => {
119+
throw new TypeError(data3.message);
120+
}, {
121+
message: "jwt malformed"
122+
});
123+
124+
});
125+
126+
it("should not be able to create a product, if (token) is not valid", async () => {
127+
128+
const user = {
129+
username: "Steve Rodgers",
130+
email: "owpu@tovekhij.ar",
131+
password: "8822"
132+
}
133+
134+
const endPoint1 = "createUser";
135+
await makePOSTRequests(endPoint1, user);
136+
137+
const endPoint2 = "loginUser";
138+
const request = {
139+
email: user.email,
140+
password: user.password
141+
}
142+
143+
await makePOSTRequests(endPoint2, request);
144+
145+
const endPoint3 = "createProduct";
146+
const request2 = {
147+
product_name: "DVD",
148+
price: 800.10,
149+
category: "Dvd",
150+
token: process.env.INVALID_TOKEN
151+
}
152+
153+
const data3 = await makePOSTWithTokenRequests(endPoint3, request2);
154+
155+
assert.rejects(async () => {
156+
throw new TypeError(data3.message);
157+
}, {
158+
message: "invalid token"
159+
});
160+
161+
});
162+
163+
it("should not be able to create a product, if the (token) signature is not valid", async () => {
164+
165+
const user = {
166+
username: "Minnie Ortiz",
167+
email: "animec@uf.dk",
168+
password: "6644"
169+
}
170+
171+
const endPoint1 = "createUser";
172+
await makePOSTRequests(endPoint1, user);
173+
174+
const endPoint2 = "loginUser";
175+
const request = {
176+
email: user.email,
177+
password: user.password
178+
}
179+
180+
await makePOSTRequests(endPoint2, request);
181+
182+
const endPoint3 = "createProduct";
183+
const request2 = {
184+
product_name: "Monitor",
185+
price: 1000.00,
186+
category: "Computer Items",
187+
token: process.env.FAKE_TOKEN
188+
}
189+
190+
const data3 = await makePOSTWithTokenRequests(endPoint3, request2);
191+
192+
assert.rejects(async () => {
193+
throw new TypeError(data3.message);
194+
}, {
195+
message: "invalid signature"
196+
});
197+
198+
});
199+
200+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const host = process.env.HOST;
2+
const port = process.env.PORT;
3+
4+
export async function makePOSTWithTokenRequests(endPoint, data) {
5+
6+
const request = await fetch(`http://${host}:${port}/${endPoint}`, {
7+
method: "POST",
8+
body: JSON.stringify(data),
9+
headers: {
10+
Authorization: `Bearer ${data.token}`
11+
}
12+
}).then(async (response) => {
13+
const resp = await new Promise(resolve => resolve(response.json()));
14+
return resp;
15+
});
16+
17+
return request;
18+
};

0 commit comments

Comments
 (0)