mirror of
https://github.com/yusufipk/RememberMe.git
synced 2026-09-11 10:56:07 +00:00
created(tests) created tests for person route
This commit is contained in:
@@ -2,6 +2,15 @@ const express = require("express");
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
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
|
mongoose
|
||||||
.connect(`mongodb://localhost:27017/rememberme`, {
|
.connect(`mongodb://localhost:27017/rememberme`, {
|
||||||
useUnifiedTopology: true,
|
useUnifiedTopology: true,
|
||||||
@@ -10,6 +19,7 @@ mongoose
|
|||||||
})
|
})
|
||||||
.then(() => console.log("Connected to MongoDB..."))
|
.then(() => console.log("Connected to MongoDB..."))
|
||||||
.catch((err) => console.log("Could not connect to MongoDB", err));
|
.catch((err) => console.log("Could not connect to MongoDB", err));
|
||||||
|
}
|
||||||
|
|
||||||
const people = require("./routes/person");
|
const people = require("./routes/person");
|
||||||
|
|
||||||
@@ -17,4 +27,8 @@ app.use(express.json());
|
|||||||
app.use("/api/person", people);
|
app.use("/api/person", people);
|
||||||
|
|
||||||
const port = process.env.port || 3002;
|
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;
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ const Person = mongoose.model("Person", personSchema);
|
|||||||
function validatePerson(value) {
|
function validatePerson(value) {
|
||||||
const schema = Joi.object({
|
const schema = Joi.object({
|
||||||
name: Joi.string().min(3).max(50).required(),
|
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({
|
contact: Joi.object({
|
||||||
phone: Joi.string().min(5).max(20),
|
phone: Joi.string().min(5).max(20),
|
||||||
email: Joi.string().email().min(5).max(255),
|
email: Joi.string().email().min(5).max(255),
|
||||||
|
|||||||
+1
-1
@@ -10,6 +10,6 @@
|
|||||||
"supertest": "^6.1.4"
|
"supertest": "^6.1.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest --watchAll --verbose --coverage"
|
"test": "jest --watchAll --verbose --runInBand --coverage"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -18,8 +18,8 @@ router.get("/:id", auth, validateObjectId, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get("/get/:name", auth, async (req, res) => {
|
router.get("/get/:name", auth, async (req, res) => {
|
||||||
const p = new RegExp(req.params.name, "i");
|
const findName = new RegExp(req.params.name, "i");
|
||||||
const person = await Person.find({ name: p });
|
const person = await Person.find({ name: findName });
|
||||||
if (person.length === 0)
|
if (person.length === 0)
|
||||||
return res.status(404).send("Person does not exist!");
|
return res.status(404).send("Person does not exist!");
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
module.exports = function () {
|
module.exports.createToken = function () {
|
||||||
const token = jwt.sign({ isAdmin: true }, process.env.rememberMe_jwtKey, {
|
const token = jwt.sign({ isAdmin: true }, process.env.rememberMe_jwtKey, {
|
||||||
expiresIn: "30d",
|
expiresIn: "30d",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!process.env.NODE_ENV === "test") {
|
||||||
|
console.log(token);
|
||||||
|
}
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user