From f163269b9ee66517f43229c9d34a8c3ed864c478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20=C4=B0pek?= Date: Sun, 25 Jul 2021 09:39:38 +0300 Subject: [PATCH] created(tests) created tests for person route --- index.js | 32 +++- models/person-model.js | 2 +- package.json | 2 +- routes/person.js | 4 +- tests/integration/auth.test.js | 50 ++++++ tests/integration/person.test.js | 276 +++++++++++++++++++++++++++++++ token.js | 7 +- 7 files changed, 359 insertions(+), 14 deletions(-) create mode 100644 tests/integration/auth.test.js create mode 100644 tests/integration/person.test.js diff --git a/index.js b/index.js index 565be7a..717c178 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,24 @@ const express = require("express"); const mongoose = require("mongoose"); const app = express(); -mongoose - .connect(`mongodb://localhost:27017/rememberme`, { - useUnifiedTopology: true, - useNewUrlParser: true, - useCreateIndex: true, - }) - .then(() => console.log("Connected to MongoDB...")) - .catch((err) => console.log("Could not connect to MongoDB", err)); +if (process.env.NODE_ENV === "test") { + mongoose + .connect("mongodb://localhost:27017/remembermetest", { + useUnifiedTopology: true, + useNewUrlParser: true, + useCreateIndex: true, + }) + .then(() => console.log("Connected to MongoDB test...")); +} else { + mongoose + .connect(`mongodb://localhost:27017/rememberme`, { + useUnifiedTopology: true, + useNewUrlParser: true, + useCreateIndex: true, + }) + .then(() => console.log("Connected to MongoDB...")) + .catch((err) => console.log("Could not connect to MongoDB", err)); +} const people = require("./routes/person"); @@ -17,4 +27,8 @@ app.use(express.json()); app.use("/api/person", people); const port = process.env.port || 3002; -app.listen(port, () => console.log(`Listening on port ${port}...`)); +const server = app.listen(port, () => + console.log(`Listening on port ${port}...`) +); + +module.exports = server; diff --git a/models/person-model.js b/models/person-model.js index 21e042a..9533b5a 100644 --- a/models/person-model.js +++ b/models/person-model.js @@ -114,7 +114,7 @@ const Person = mongoose.model("Person", personSchema); function validatePerson(value) { const schema = Joi.object({ name: Joi.string().min(3).max(50).required(), - place: Joi.string().min(3).max(50).required(true), + place: Joi.string().min(3).max(50).required(), contact: Joi.object({ phone: Joi.string().min(5).max(20), email: Joi.string().email().min(5).max(255), diff --git a/package.json b/package.json index 7db7c13..9a17177 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,6 @@ "supertest": "^6.1.4" }, "scripts": { - "test": "jest --watchAll --verbose --coverage" + "test": "jest --watchAll --verbose --runInBand --coverage" } } diff --git a/routes/person.js b/routes/person.js index 58edd4e..147549e 100644 --- a/routes/person.js +++ b/routes/person.js @@ -18,8 +18,8 @@ router.get("/:id", auth, validateObjectId, async (req, res) => { }); router.get("/get/:name", auth, async (req, res) => { - const p = new RegExp(req.params.name, "i"); - const person = await Person.find({ name: p }); + const findName = new RegExp(req.params.name, "i"); + const person = await Person.find({ name: findName }); if (person.length === 0) return res.status(404).send("Person does not exist!"); diff --git a/tests/integration/auth.test.js b/tests/integration/auth.test.js new file mode 100644 index 0000000..a4b3f09 --- /dev/null +++ b/tests/integration/auth.test.js @@ -0,0 +1,50 @@ +const request = require("supertest"); +const { createToken } = require("../../token"); +const mongoose = require("mongoose"); + +let server; +let token; + +describe("auth middleware", () => { + beforeAll((done) => { + server = require("../../index"); + if (!mongoose.connection.db) { + mongoose.connection.on("connected", done); + } else { + done(); + } + }); + + beforeEach(() => { + token = createToken(); + }); + + afterEach(async () => { + await server.close(); + }); + + const exec = async () => { + return await request(server) + .get("/api/person/") + .set({ "x-auth-token": token }); + }; + + it("should return 401 if no token is provided", async () => { + token = ""; + + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is invalid", async () => { + token = "a"; + + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 200 if token is valid", async () => { + const res = await exec(); + expect(res.status).toBe(200); + }); +}); diff --git a/tests/integration/person.test.js b/tests/integration/person.test.js new file mode 100644 index 0000000..caa8c6e --- /dev/null +++ b/tests/integration/person.test.js @@ -0,0 +1,276 @@ +const request = require("supertest"); +const { createToken } = require("../../token"); +const mongoose = require("mongoose"); +const { Person } = require("../../models/person-model"); + +let server; +let token; +let personId; +let person; + +describe("person route", () => { + beforeAll((done) => { + server = require("../../index"); + if (!mongoose.connection.db) { + mongoose.connection.on("connected", done); + } else { + done(); + } + }); + + beforeEach(async () => { + personId = mongoose.Types.ObjectId(); + person = new Person({ + _id: personId, + name: "Mike", + place: "Taksim", + }); + await person.save(); + + token = createToken(); + }); + + afterEach(async () => { + await server.close(); + await Person.deleteMany({}); + }); + + describe("GET", () => { + describe("GET /api/person", () => { + const exec = async () => { + return await request(server) + .get("/api/person/") + .set({ "x-auth-token": token }); + }; + + it("should return 401 if no token provided", async () => { + token = ""; + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is not valid", async () => { + token = "a"; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return all objects if token is valid", async () => { + const res = await exec(); + expect(res.status).toBe(200); + }); + }); + + describe("GET /api/person/:id", () => { + let id; + const exec = async () => { + return await request(server) + .get(`/api/person/${id}`) + .set({ "x-auth-token": token }); + }; + + it("should return 401 if no token provided", async () => { + token = ""; + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is not valid", async () => { + token = "a"; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 404 if objectId is not valid", async () => { + id = mongoose.Types.ObjectId().toString(); + const res = await exec(); + expect(res.status).toBe(404); + }); + + it("should return 404 if person does not exist", async () => { + id = mongoose.Types.ObjectId().toString(); + const res = await exec(); + expect(res.status).toBe(404); + }); + + it("should return person if id is valid", async () => { + id = personId; + const res = await exec(); + expect(res.status).toBe(200); + expect(res.body).toHaveProperty("_id", personId.toString()); + }); + }); + + describe("GET /api/person/get/:name", () => { + let name; + const exec = async () => { + return await request(server) + .get(`/api/person/get/${name}`) + .set({ "x-auth-token": token }); + }; + + it("should return 401 if no token provided", async () => { + token = ""; + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is not valid", async () => { + token = "a"; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 404 if person does not exist", async () => { + name = "Rigby"; + const res = await exec(); + expect(res.status).toBe(404); + }); + + it("should return 404 if name is invalid", async () => { + name = null; + const res = await exec(); + expect(res.status).toBe(404); + }); + + it("should return the person if name is valid", async () => { + name = "Mike"; + const res = await exec(); + expect(res.status).toBe(200); + expect(res.body[0]).toHaveProperty("name", "Mike"); + }); + + it("case sensitivity", async () => { + name = "mike"; + const res = await exec(); + expect(res.status).toBe(200); + expect(res.body[0]).toHaveProperty("name", "Mike"); + }); + }); + }); + + describe("POST /api/person", () => { + let person; + beforeEach(() => { + person = { + name: "Skips", + place: "Car Maintanance", + }; + }); + + const exec = async () => { + return await request(server) + .post(`/api/person/`) + .set({ "x-auth-token": token }) + .send(person); + }; + + it("should return 401 if no token provided", async () => { + token = ""; + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is not valid", async () => { + token = "a"; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("joi should return 400 if input is invalid", async () => { + person.name = ""; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 200 if input is valid", async () => { + const res = await exec(); + + expect(res.status).toBe(200); + expect(res.body).toHaveProperty("_id"); + }); + }); + describe("PUT /api/person/:id", () => { + let person; + beforeEach(() => { + person = { + name: "Jimmy", + place: "Texas", + }; + }); + + const exec = async () => { + return await request(server) + .put(`/api/person/${personId}`) + .set({ "x-auth-token": token }) + .send(person); + }; + + it("should return 401 if no token provided", async () => { + token = ""; + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is not valid", async () => { + token = "a"; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 404 if objectId is not valid", async () => { + personId = mongoose.Types.ObjectId().toString(); + const res = await exec(); + expect(res.status).toBe(404); + }); + + it("joi should return 400 if input is invalid", async () => { + person.name = ""; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("joi should return 400 if input is invalid", async () => { + person.place = ""; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 200 if input is valid", async () => { + const res = await exec(); + expect(res.status).toBe(200); + expect(res.body).toHaveProperty("ok", 1); + }); + }); + describe("DELETE /api/person/:id", () => { + const exec = async () => { + return await request(server) + .delete(`/api/person/${personId}`) + .set({ "x-auth-token": token }); + }; + + it("should return 401 if no token provided", async () => { + token = ""; + const res = await exec(); + expect(res.status).toBe(401); + }); + + it("should return 400 if token is not valid", async () => { + token = "a"; + const res = await exec(); + expect(res.status).toBe(400); + }); + + it("should return 404 if objectId is not valid", async () => { + personId = mongoose.Types.ObjectId().toString(); + const res = await exec(); + expect(res.status).toBe(404); + }); + + it("should return 200 if id is valid", async () => { + const res = await exec(); + expect(res.status).toBe(200); + expect(res.body).toHaveProperty("deletedCount", 1); + }); + }); +}); diff --git a/token.js b/token.js index f569a78..64f9314 100644 --- a/token.js +++ b/token.js @@ -1,7 +1,12 @@ const jwt = require("jsonwebtoken"); -module.exports = function () { +module.exports.createToken = function () { const token = jwt.sign({ isAdmin: true }, process.env.rememberMe_jwtKey, { expiresIn: "30d", }); + + if (!process.env.NODE_ENV === "test") { + console.log(token); + } + return token; };