Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Auth Test #17

Merged
merged 4 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cattleia-backend/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const signIn = async (
if (_user) {
const matchPass = await comparePassword(_user.password, password);
if (!matchPass) {
return res.json({
return res.status(401).json({
ok: false,
error: errors.wrongUserOrPassword,
});
Expand Down Expand Up @@ -110,7 +110,7 @@ export const signUp = async (
if (role) {
const foundRole: Role | null = await RoleModel.findOne({ name: role });
if (foundRole) _user.role = foundRole;
else return res.json({ ok: false });
else return res.json({ ok: false, error: errors.invalidRole });
} else {
const roleD: Role | null = await RoleModel.findOne({ name: "user" });
_user.role = roleD!;
Expand Down
4 changes: 4 additions & 0 deletions cattleia-backend/src/libs/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const errors = {
message: "Invalid auth token.",
code: 9090,
},
invalidRole: {
message: "Invalid role.",
code: 8020,
},
userAlreadyTaken: {
message: "Username already taken.",
code: 5342,
Expand Down
158 changes: 158 additions & 0 deletions cattleia-backend/src/test/jest/auth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import request from "supertest";
import mongoose from "mongoose";
import app from "../../app";
import { UserModel } from "../../models";
import { encryptPassword, errors } from "../../libs";

const api = request(app);

const user = {
name: "Testing",
email: "test@email.com",
userName: "test",
password: "1234",
};

const user2 = {
name: "Testing2",
email: "test2@email.com",
userName: "test2",
password: "1234",
};

beforeEach(async () => {
await UserModel.deleteMany({});
const _user = new UserModel({
...user2,
password: await encryptPassword(user2.password),
});
await _user.save();
});

afterAll(() => {
mongoose.connection.close();
});

describe("POST /api/v1/auth/sign-up", () => {
test("should register an user with default role (user).", async () => {
const res = await api.post("/api/v1/auth/sign-up").send(user);
expect(res.status).toBe(200);
expect(res.body.ok).toEqual(true);
expect(res.body.user.id).toBeDefined();
expect(res.body.user.role).toEqual("user");
expect(res.body.user.token).toBeDefined();
expect(res.body.user.account).toBeDefined();
expect(res.body.user.rank).toBeDefined();
expect(res.body.user.email).toEqual(user.email);
expect(res.body.user.name).toEqual(user.name);
expect(res.body.user.userName).toEqual(user.userName);
});

test("should register an user with role = user.", async () => {
const res = await api
.post("/api/v1/auth/sign-up")
.send({ ...user, role: "user" });
expect(res.status).toBe(200);
expect(res.body.ok).toEqual(true);
expect(res.body.user.id).toBeDefined();
expect(res.body.user.role).toEqual("user");
expect(res.body.user.token).toBeDefined();
expect(res.body.user.account).toBeDefined();
expect(res.body.user.rank).toBeDefined();
expect(res.body.user.email).toEqual(user.email);
expect(res.body.user.name).toEqual(user.name);
expect(res.body.user.userName).toEqual(user.userName);
});

test("should register an user with role = admin.", async () => {
const res = await api
.post("/api/v1/auth/sign-up")
.send({ ...user, role: "admin" });
expect(res.status).toBe(200);
expect(res.body.ok).toEqual(true);
expect(res.body.user.id).toBeDefined();
expect(res.body.user.role).toEqual("admin");
expect(res.body.user.token).toBeDefined();
expect(res.body.user.account).toBeDefined();
expect(res.body.user.rank).toBeDefined();
expect(res.body.user.email).toEqual(user.email);
expect(res.body.user.name).toEqual(user.name);
expect(res.body.user.userName).toEqual(user.userName);
});

test("should respond with 'invalid role' error.", async () => {
const res = await api
.post("/api/v1/auth/sign-up")
.send({ ...user, role: "test" });
expect(res.body.ok).toEqual(false);
expect(res.body.error.message).toEqual(errors.invalidRole.message);
expect(res.body.error.code).toEqual(errors.invalidRole.code);
});

test("should respond with 'userName taken' error.", async () => {
const res = await api.post("/api/v1/auth/sign-up").send(user2);
expect(res.body.ok).toEqual(false);
expect(res.body.error.message).toEqual(errors.userAlreadyTaken.message);
expect(res.body.error.code).toEqual(errors.userAlreadyTaken.code);
});
});

describe("POST /api/v1/auth/sign-in", () => {
test("should be able to login.", async () => {
await api.post("/api/v1/auth/sign-up").send(user);

const res = await api
.post("/api/v1/auth/sign-in")
.send({ userName: user.userName, password: user.password });

expect(res.status).toBe(200);
expect(res.body.ok).toEqual(true);
expect(res.body.user.id).toBeDefined();
expect(res.body.user.role).toEqual("user");
expect(res.body.user.token).toBeDefined();
expect(res.body.user.account).toBeDefined();
expect(res.body.user.rank).toBeDefined();
expect(res.body.user.email).toEqual(user.email);
expect(res.body.user.name).toEqual(user.name);
expect(res.body.user.userName).toEqual(user.userName);
});

test("shouldn't be able to login with wrong username.", async () => {
await api.post("/api/v1/auth/sign-up").send(user);

const res = await api
.post("/api/v1/auth/sign-in")
.send({ userName: "wrong", password: user.password });

expect(res.status).toBe(401);
expect(res.body.ok).toEqual(false);
expect(res.body.error.message).toEqual(errors.wrongUserOrPassword.message);
expect(res.body.error.code).toEqual(errors.wrongUserOrPassword.code);
});

test("shouldn't be able to login with wrong password.", async () => {
await api.post("/api/v1/auth/sign-up").send(user);

const res = await api
.post("/api/v1/auth/sign-in")
.send({ userName: user.userName, password: "wrong" });

expect(res.status).toBe(401);
expect(res.body.ok).toEqual(false);
expect(res.body.error.message).toEqual(errors.wrongUserOrPassword.message);
expect(res.body.error.code).toEqual(errors.wrongUserOrPassword.code);
});

test("shouldn't be able to login with wrong password and wrong username.", async () => {
await api.post("/api/v1/auth/sign-up").send(user);

const res = await api
.post("/api/v1/auth/sign-in")
.send({ userName: "wrong1", password: "wrong2" });

expect(res.status).toBe(401);
expect(res.body.ok).toEqual(false);
expect(res.body.error.message).toEqual(errors.wrongUserOrPassword.message);
expect(res.body.error.code).toEqual(errors.wrongUserOrPassword.code);
});
});