sequelize-middleware/test/createOne.test.js

178 lines
4.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable jest/no-test-callback */
import uuid from "uuid/v4";
import { truncate } from "./utils/common";
import models from "../models";
import Brands from "../rules/Brands";
import Cars from "../rules/Cars";
import Middelware from "../index";
let createdBrand = null;
describe("createOne", () => {
afterAll(done => {
truncate(["Brands", "Cars"], done);
});
test("It should return a 401 when guest tries to create new item", async done => {
const middleware = new Middelware(Brands, models);
const req = {
method: "POST",
user: null,
body: {
name: uuid()
}
};
middleware.createOne(req, (err, res) => {
expect(res).toBeUndefined();
expect(parseFloat(err.errorCode)).toBe(401.1);
done();
});
});
test("It should return a 401 when unauthorized role tries to create new item", async done => {
const middleware = new Middelware(Brands, models);
middleware.createOne(
{
method: "POST",
user: {
role: "user"
},
body: {
name: uuid()
}
},
(err, res) => {
expect(res).toBeUndefined();
expect(parseFloat(err.errorCode)).toBe(401.1);
done();
}
);
});
test("It should return a 406 when authorized role tries to create new item with missing required values", async done => {
const middleware = new Middelware(Brands, models);
middleware.createOne(
{
method: "POST",
user: {
role: "admin"
},
body: {}
},
(err, res) => {
expect(res).toBeUndefined();
expect(parseFloat(err.errorCode)).toBe(406.1);
done();
}
);
});
test("It should return new item when authorized role create new item", async done => {
const middleware = new Middelware(Brands, models);
const name = uuid();
middleware.createOne(
{
method: "POST",
user: {
role: "admin"
},
body: {
name
}
},
(err, res) => {
expect(err).toBeNull();
expect(res).toHaveProperty("id");
expect(res).toHaveProperty("created");
expect(res).toHaveProperty("updated");
expect(res).not.toHaveProperty("createdAt");
expect(res).not.toHaveProperty("updatedAt");
expect(res.name).toBe(name);
createdBrand = res;
done();
}
);
});
test("It should return errorCode 409.1 when authorized role tries to create new item with same unique field", async done => {
const middleware = new Middelware(Brands, models);
middleware.createOne(
{
method: "POST",
user: {
role: "admin"
},
body: {
name: createdBrand.name
}
},
(err, res) => {
expect(res).toBeUndefined();
expect(parseFloat(err.errorCode)).toBe(409.1);
done();
}
);
});
test("It should return errorCode 406.2 when authorized role tries to create new item with bad foreign id", async done => {
const middleware = new Middelware(Cars, models);
const name = uuid();
middleware.createOne(
{
method: "POST",
user: {
role: "admin"
},
body: {
name,
year: 2004,
brandId: createdBrand.id + 2
}
},
(err, res) => {
expect(res).toBeUndefined();
expect(parseFloat(err.errorCode)).toBe(406.2);
done();
}
);
});
test("It should return errorCode 500 when authorized role tries to create new item with too long value", async done => {
const middleware = new Middelware(Cars, models);
middleware.createOne(
{
method: "POST",
user: {
role: "admin"
},
body: {
name:
"ThohWaigohhieHogahthoxohpheeDah0geetai0cieNgu1The2foQueeloochoH9eulizieshuf1nivohkied8jei5oph2Lajem6ohviijai6booTh8ienaic9eipheixa4ki1iek2pheihe7een7nei7epahngaerieghoe3ahbeil3yied0ievee1moh8jeeN5quoh6uiph6HaeZ0Eiyohshafohniewaer7gaegiefi5eiquiequoow5ohtheiw6ZeihieMoM8Ejoh7leiNeavi7uapheiwoophitoi3queiBeVeip5too8cah9Ohpaetaogahw1tei0eibuyaef3aht8aighuma6ahK4huP4cew6ohd0aiSh2umeeng7Hizahtoo6xoocePhu4ahtheex3jaijooph9iexaiqu3Nu0Ebeich6iTe",
year: 2004
}
},
(err, res) => {
expect(res).toBeUndefined();
expect(parseFloat(err.errorCode)).toBe(500.0);
done();
}
);
});
});