diff --git a/test/deleteOne.test.js b/test/deleteOne.test.js new file mode 100644 index 0000000..9f5d2c5 --- /dev/null +++ b/test/deleteOne.test.js @@ -0,0 +1,133 @@ +/* eslint-disable jest/no-test-callback */ +import { createBrand, truncate } from "./utils/common"; + +import models from "../models"; +import Brands from "../rules/Brands"; +import Middelware from "../index"; + +let brand = {}; + +describe("deleteOne", () => { + beforeAll(done => { + createBrand((err, item) => { + if (err) { + done(err); + } else { + brand = item; + done(); + } + }); + }); + + afterAll(done => { + truncate(["Brands"], done); + }); + + test("It should return 401.1 if guest tries to delete item", async done => { + const middleware = new Middelware(Brands, models); + const req = { + method: "DELETE", + user: null, + params: { + brandId: brand.id + }, + query: {}, + protocol: "http", + get: () => { + return "internal.test/"; + }, + originalUrl: "v1/" + }; + + middleware.deleteOne(req, (err, res) => { + expect(res).toBeUndefined(); + expect(parseFloat(err.errorCode)).toBe(401.1); + + done(); + }); + }); + + test("It should return 401.1 if bad role tries to delete item", async done => { + const middleware = new Middelware(Brands, models); + const req = { + method: "DELETE", + user: { + role: "user" + }, + params: { + brandId: brand.id + }, + query: {}, + protocol: "http", + get: () => { + return "internal.test/"; + }, + originalUrl: "v1/" + }; + + middleware.deleteOne(req, (err, res) => { + expect(res).toBeUndefined(); + expect(parseFloat(err.errorCode)).toBe(401.1); + + done(); + }); + }); + + test("It should return empty item if allowed user delete item", async done => { + const middleware = new Middelware(Brands, models); + const req = { + method: "DELETE", + user: { + role: "admin" + }, + params: { + brandId: brand.id + }, + query: {}, + body: { + name: "TEST" + }, + protocol: "http", + get: () => { + return "internal.test/"; + }, + originalUrl: "v1/" + }; + + middleware.deleteOne(req, (err, res) => { + expect(err).toBeNull(); + expect(res).toEqual({}); + + done(); + }); + }); + + test("It should return 404.0 if allowed user tries to delete not found item", async done => { + const middleware = new Middelware(Brands, models); + const req = { + method: "DELETE", + user: { + role: "admin" + }, + params: { + brandId: brand.id + }, + query: {}, + body: { + name: "TEST" + }, + protocol: "http", + get: () => { + return "internal.test/"; + }, + originalUrl: "v1/" + }; + + middleware.deleteOne(req, (err, res) => { + expect(res).toBeUndefined(); + expect(parseFloat(err.errorCode)).toBe(404.0); + + done(); + }); + }); +});