Skip to content

Commit

Permalink
fix: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandamas committed Mar 16, 2023
1 parent 069a70e commit 49b04b1
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 86 deletions.
191 changes: 105 additions & 86 deletions src/controllers/__tests__/authController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,119 @@ import app from "../../app";
import USER from "../../models/User";
import { httpRequest, httpResponse } from "../mock/user.mock";
import GoogleController from "../googleAuthController";
import Tokens from "../../models/token"
import Tokens from "../../models/token";
jest.setTimeout(70000);
describe("Login via google", () => {
afterAll(async () => {
USER.destroy({
where: { email: "[email protected]" },
afterAll(async () => {
USER.destroy({
where: { email: "[email protected]" },
});
});
test("redirect to google and authenticate", async () => {
const data = await GoogleController.googleAuth(
httpRequest("[email protected]"),
httpResponse()
);
console.log(data);
expect(data.body).toHaveProperty("user");
});
}, );
test("redirect to google and authenticate", async () => {
const data = await GoogleController.googleAuth(
httpRequest("[email protected]"),
httpResponse()
);
console.log(data);
expect(data.body).toHaveProperty("user");
});

test("testing register", async () => {
const data: any = await GoogleController.googleAuth(
httpRequest("[email protected]"),
httpResponse()
);
console.log(data);
expect(data.body).toHaveProperty("user");
});
test("testing 500", async () => {
const data: any = await GoogleController.googleAuth(
"helll",
httpResponse()
);
expect(data.body.status).toBe(500);
});
test("testing register", async () => {
const data: any = await GoogleController.googleAuth(
httpRequest("[email protected]"),
httpResponse()
);
console.log(data);
expect(data.body).toHaveProperty("user");
});
test("testing 500", async () => {
const data: any = await GoogleController.googleAuth(
"helll",
httpResponse()
);
expect(data.body.status).toBe(500);
});
});

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

it("should add 5 by 3", () => {
const result = add(5, 3);
expect(result).toEqual(8);
});
it("should add 5 by 3", () => {
const result = add(5, 3);
expect(result).toEqual(8);
});
});
// reset password coontroller tests
describe('reset password', () => {
describe('send link to email', () => {
test('incase of unregistered email', async () => {
const response = await supertest(app)
.post('/resetpassword/link')
.send({ email: '[email protected]' })
expect(response.status).toBe(400)
}, 30000) // timeout 30 seconds
})
test('incase of a registered email', async () => {
const response = await supertest(app)
.post('/resetpassword/link')
.send({ email: '[email protected]' })
expect(response.status).toBe(200)
}, 20000)
test('incase invalid email input', async () => {
const response = await supertest(app)
.post('/resetpassword/link')
.send({ email: 'rukundjoseph' })
expect(response.status).toBe(400)
}, 20000)
describe('add token and change password', () => {
test('incase incorrect token', async () => {
const response = await supertest(app)
.patch('/changepassword/[email protected]/65328dba23')
.send({ newpassword: 'newpassword', confirmpass: 'newpassword' })
expect(response.status).toBe(401)
}, 20000)
test('incase of a unmatching passwords', async () => {
const user: any = await USER.findOne({
where: { email: '[email protected]' },
})
const token: any = await Tokens.findOne({ where: { userId: `${user.id}` } })
const response = await supertest(app)
.patch(`/changepassword/[email protected]/${token.token}`)
.send({ newpassword: 'newpas', confirmpass: 'newpaa' })
expect(response.status).toBe(400)
})
test('incase of a valid token and email', async () => {
const user: any = await USER.findOne({
where: { email: '[email protected]' },
})
const token: any = await Tokens.findOne({ where: { userId: `${user.id}` } })
const response = await supertest(app)
.patch(`/changepassword/[email protected]/${token.token}`)
.send({ newpassword: 'newpas', confirmpass: 'newpas' })
expect(response.status).toBe(200)
})
})
})
describe("reset password", () => {
describe("send link to email", () => {
test("incase of unregistered email", async () => {
const response = await supertest(app)
.post("/resetpassword/link")
.send({ email: "[email protected]" });
expect(response.status).toBe(400);
}, 30000); // timeout 30 seconds
});
test("incase of a registered email", async () => {
const response = await supertest(app)
.post("/resetpassword/link")
.send({ email: "[email protected]" });
expect(response.status).toBe(200);
}, 20000);
test("incase invalid email input", async () => {
const response = await supertest(app)
.post("/resetpassword/link")
.send({ email: "rukundjoseph" });
expect(response.status).toBe(400);
}, 20000);
describe("add token and change password", () => {
test("incase incorrect token", async () => {
const response = await supertest(app)
.patch("/changepassword/[email protected]/65328dba23")
.send({
newpassword: "newpassword",
confirmpass: "newpassword",
});
expect(response.status).toBe(401);
}, 20000);
test("incase of a unmatching passwords", async () => {
const user: any = await USER.findOne({
where: { email: "[email protected]" },
});
const token: any = await Tokens.findOne({
where: { userId: `${user.id}` },
});
const response = await supertest(app)
.patch(
`/changepassword/[email protected]/${token.token}`
)
.send({ newpassword: "newpas", confirmpass: "newpaa" });
expect(response.status).toBe(400);
});
test("incase of a valid token and email", async () => {
const user: any = await USER.findOne({
where: { email: "[email protected]" },
});
const token: any = await Tokens.findOne({
where: { userId: `${user.id}` },
});
const response = await supertest(app)
.patch(
`/changepassword/[email protected]/${token.token}`
)
.send({ newpassword: "newpas", confirmpass: "newpas" });
expect(response.status).toBe(200);
});
});
});

// logout tests
describe("Logout user", () => {
test("success logout", async () => {
const response = await supertest(app).post("/logout");
expect(response.status).toBe(200);
}, 30000); // timeout 30 seconds
});
29 changes: 29 additions & 0 deletions src/controllers/__tests__/product.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import supertest from "supertest";
import createServer from "../../utils/server";
/* eslint-disable @typescript-eslint/no-explicit-any */
const app = createServer();
let token;
beforeAll(async () => {
const res = await supertest(app).post("/login").send({
email: "[email protected]",
password: "adminpass",
});
token = res.body.token;
}, 40000);

describe("Seller Collection", () => {
describe("Seller update product availability", () => {
test("Seller update non-existing product", async () => {
const response = await supertest(app).patch(
"/products/delete/72753"
);
expect(response.status).toBe(404);
}, 60000);
test("unauthorised access", async () => {
const response = await supertest(app).patch(
"/products/delete/72753"
);
expect(response.status).toBe(404);
}, 60000);
});
});

0 comments on commit 49b04b1

Please sign in to comment.