Skip to content

Commit 49b04b1

Browse files
committed
fix: more tests
1 parent 069a70e commit 49b04b1

File tree

2 files changed

+134
-86
lines changed

2 files changed

+134
-86
lines changed

Diff for: src/controllers/__tests__/authController.test.ts

+105-86
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,119 @@ import app from "../../app";
55
import USER from "../../models/User";
66
import { httpRequest, httpResponse } from "../mock/user.mock";
77
import GoogleController from "../googleAuthController";
8-
import Tokens from "../../models/token"
8+
import Tokens from "../../models/token";
99
jest.setTimeout(70000);
1010
describe("Login via google", () => {
11-
afterAll(async () => {
12-
USER.destroy({
13-
where: { email: "[email protected]" },
11+
afterAll(async () => {
12+
USER.destroy({
13+
where: { email: "[email protected]" },
14+
});
15+
});
16+
test("redirect to google and authenticate", async () => {
17+
const data = await GoogleController.googleAuth(
18+
httpRequest("[email protected]"),
19+
httpResponse()
20+
);
21+
console.log(data);
22+
expect(data.body).toHaveProperty("user");
1423
});
15-
}, );
16-
test("redirect to google and authenticate", async () => {
17-
const data = await GoogleController.googleAuth(
18-
httpRequest("[email protected]"),
19-
httpResponse()
20-
);
21-
console.log(data);
22-
expect(data.body).toHaveProperty("user");
23-
});
2424

25-
test("testing register", async () => {
26-
const data: any = await GoogleController.googleAuth(
27-
httpRequest("[email protected]"),
28-
httpResponse()
29-
);
30-
console.log(data);
31-
expect(data.body).toHaveProperty("user");
32-
});
33-
test("testing 500", async () => {
34-
const data: any = await GoogleController.googleAuth(
35-
"helll",
36-
httpResponse()
37-
);
38-
expect(data.body.status).toBe(500);
39-
});
25+
test("testing register", async () => {
26+
const data: any = await GoogleController.googleAuth(
27+
httpRequest("[email protected]"),
28+
httpResponse()
29+
);
30+
console.log(data);
31+
expect(data.body).toHaveProperty("user");
32+
});
33+
test("testing 500", async () => {
34+
const data: any = await GoogleController.googleAuth(
35+
"helll",
36+
httpResponse()
37+
);
38+
expect(data.body.status).toBe(500);
39+
});
4040
});
4141

4242
/* eslint-disable @typescript-eslint/no-explicit-any */
4343
describe("Math functions", () => {
44-
it("should multiply 5 by 3", () => {
45-
const result = multiply(5, 3);
46-
expect(result).toEqual(15);
47-
});
44+
it("should multiply 5 by 3", () => {
45+
const result = multiply(5, 3);
46+
expect(result).toEqual(15);
47+
});
4848

49-
it("should add 5 by 3", () => {
50-
const result = add(5, 3);
51-
expect(result).toEqual(8);
52-
});
49+
it("should add 5 by 3", () => {
50+
const result = add(5, 3);
51+
expect(result).toEqual(8);
52+
});
5353
});
5454
// reset password coontroller tests
55-
describe('reset password', () => {
56-
describe('send link to email', () => {
57-
test('incase of unregistered email', async () => {
58-
const response = await supertest(app)
59-
.post('/resetpassword/link')
60-
.send({ email: '[email protected]' })
61-
expect(response.status).toBe(400)
62-
}, 30000) // timeout 30 seconds
63-
})
64-
test('incase of a registered email', async () => {
65-
const response = await supertest(app)
66-
.post('/resetpassword/link')
67-
.send({ email: '[email protected]' })
68-
expect(response.status).toBe(200)
69-
}, 20000)
70-
test('incase invalid email input', async () => {
71-
const response = await supertest(app)
72-
.post('/resetpassword/link')
73-
.send({ email: 'rukundjoseph' })
74-
expect(response.status).toBe(400)
75-
}, 20000)
76-
describe('add token and change password', () => {
77-
test('incase incorrect token', async () => {
78-
const response = await supertest(app)
79-
.patch('/changepassword/[email protected]/65328dba23')
80-
.send({ newpassword: 'newpassword', confirmpass: 'newpassword' })
81-
expect(response.status).toBe(401)
82-
}, 20000)
83-
test('incase of a unmatching passwords', async () => {
84-
const user: any = await USER.findOne({
85-
where: { email: '[email protected]' },
86-
})
87-
const token: any = await Tokens.findOne({ where: { userId: `${user.id}` } })
88-
const response = await supertest(app)
89-
.patch(`/changepassword/[email protected]/${token.token}`)
90-
.send({ newpassword: 'newpas', confirmpass: 'newpaa' })
91-
expect(response.status).toBe(400)
92-
})
93-
test('incase of a valid token and email', async () => {
94-
const user: any = await USER.findOne({
95-
where: { email: '[email protected]' },
96-
})
97-
const token: any = await Tokens.findOne({ where: { userId: `${user.id}` } })
98-
const response = await supertest(app)
99-
.patch(`/changepassword/[email protected]/${token.token}`)
100-
.send({ newpassword: 'newpas', confirmpass: 'newpas' })
101-
expect(response.status).toBe(200)
102-
})
103-
})
104-
})
55+
describe("reset password", () => {
56+
describe("send link to email", () => {
57+
test("incase of unregistered email", async () => {
58+
const response = await supertest(app)
59+
.post("/resetpassword/link")
60+
.send({ email: "[email protected]" });
61+
expect(response.status).toBe(400);
62+
}, 30000); // timeout 30 seconds
63+
});
64+
test("incase of a registered email", async () => {
65+
const response = await supertest(app)
66+
.post("/resetpassword/link")
67+
.send({ email: "[email protected]" });
68+
expect(response.status).toBe(200);
69+
}, 20000);
70+
test("incase invalid email input", async () => {
71+
const response = await supertest(app)
72+
.post("/resetpassword/link")
73+
.send({ email: "rukundjoseph" });
74+
expect(response.status).toBe(400);
75+
}, 20000);
76+
describe("add token and change password", () => {
77+
test("incase incorrect token", async () => {
78+
const response = await supertest(app)
79+
.patch("/changepassword/[email protected]/65328dba23")
80+
.send({
81+
newpassword: "newpassword",
82+
confirmpass: "newpassword",
83+
});
84+
expect(response.status).toBe(401);
85+
}, 20000);
86+
test("incase of a unmatching passwords", async () => {
87+
const user: any = await USER.findOne({
88+
where: { email: "[email protected]" },
89+
});
90+
const token: any = await Tokens.findOne({
91+
where: { userId: `${user.id}` },
92+
});
93+
const response = await supertest(app)
94+
.patch(
95+
`/changepassword/[email protected]/${token.token}`
96+
)
97+
.send({ newpassword: "newpas", confirmpass: "newpaa" });
98+
expect(response.status).toBe(400);
99+
});
100+
test("incase of a valid token and email", async () => {
101+
const user: any = await USER.findOne({
102+
where: { email: "[email protected]" },
103+
});
104+
const token: any = await Tokens.findOne({
105+
where: { userId: `${user.id}` },
106+
});
107+
const response = await supertest(app)
108+
.patch(
109+
`/changepassword/[email protected]/${token.token}`
110+
)
111+
.send({ newpassword: "newpas", confirmpass: "newpas" });
112+
expect(response.status).toBe(200);
113+
});
114+
});
115+
});
116+
117+
// logout tests
118+
describe("Logout user", () => {
119+
test("success logout", async () => {
120+
const response = await supertest(app).post("/logout");
121+
expect(response.status).toBe(200);
122+
}, 30000); // timeout 30 seconds
123+
});

Diff for: src/controllers/__tests__/product.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import supertest from "supertest";
2+
import createServer from "../../utils/server";
3+
/* eslint-disable @typescript-eslint/no-explicit-any */
4+
const app = createServer();
5+
let token;
6+
beforeAll(async () => {
7+
const res = await supertest(app).post("/login").send({
8+
9+
password: "adminpass",
10+
});
11+
token = res.body.token;
12+
}, 40000);
13+
14+
describe("Seller Collection", () => {
15+
describe("Seller update product availability", () => {
16+
test("Seller update non-existing product", async () => {
17+
const response = await supertest(app).patch(
18+
"/products/delete/72753"
19+
);
20+
expect(response.status).toBe(404);
21+
}, 60000);
22+
test("unauthorised access", async () => {
23+
const response = await supertest(app).patch(
24+
"/products/delete/72753"
25+
);
26+
expect(response.status).toBe(404);
27+
}, 60000);
28+
});
29+
});

0 commit comments

Comments
 (0)