143 lines
3.7 KiB
JavaScript
143 lines
3.7 KiB
JavaScript
|
/* eslint-disable jest/no-test-callback */
|
||
|
import { createBrands, truncate } from "./utils/common";
|
||
|
|
||
|
import models from "../models";
|
||
|
import Brands from "../rules/Brands";
|
||
|
import Cars from "../rules/Cars";
|
||
|
import Middelware from "../index";
|
||
|
|
||
|
describe("getAll", () => {
|
||
|
beforeAll(done => {
|
||
|
createBrands(40, done);
|
||
|
});
|
||
|
afterAll(done => {
|
||
|
truncate(["Brands", "Cars"], done);
|
||
|
});
|
||
|
|
||
|
test("It should return a 401 when guest tries to get all item", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: null,
|
||
|
params: {},
|
||
|
query: {}
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(res).toBeUndefined();
|
||
|
expect(parseFloat(err.errorCode)).toBe(401.1);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return a empty result allowed role get items from empty collection", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "user"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {}
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(err).toBeUndefined();
|
||
|
expect(res).toBeUndefined();
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return all items when allowed role call getAll without filters", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "user"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(err).toBeNull();
|
||
|
expect(res).toHaveProperty("data");
|
||
|
expect(res).toHaveProperty("paging");
|
||
|
expect(res).toHaveProperty("total");
|
||
|
expect(res).toHaveProperty("maxPage");
|
||
|
|
||
|
expect(res.data.length).toBe(40);
|
||
|
expect(res.total).toBe(40);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
expect(res.paging.first).toBeUndefined();
|
||
|
expect(res.paging.prev).toBeUndefined();
|
||
|
expect(res.paging.next).toBeUndefined();
|
||
|
expect(res.paging.last).toBeUndefined();
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return 5 items when allowed role call getAll with limit filter", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "user"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
limit: 5,
|
||
|
page: 3
|
||
|
},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/?page=3&limit=5"
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(err).toBeNull();
|
||
|
expect(res).toHaveProperty("data");
|
||
|
expect(res).toHaveProperty("paging");
|
||
|
expect(res).toHaveProperty("total");
|
||
|
expect(res).toHaveProperty("maxPage");
|
||
|
|
||
|
expect(res.data.length).toBe(5);
|
||
|
expect(res.total).toBe(40);
|
||
|
expect(res.maxPage).toBe(8);
|
||
|
|
||
|
expect(res.data[0]).toHaveProperty("id");
|
||
|
expect(res.data[0]).toHaveProperty("name");
|
||
|
expect(res.data[0]).not.toHaveProperty("createdAt");
|
||
|
expect(res.data[0]).not.toHaveProperty("created");
|
||
|
expect(res.data[0]).not.toHaveProperty("updatedAt");
|
||
|
expect(res.data[0]).not.toHaveProperty("updated");
|
||
|
|
||
|
expect(res.paging.first.href).toBe(
|
||
|
"http://internal.test/v1/?page=1&limit=5"
|
||
|
);
|
||
|
expect(res.paging.prev.href).toBe(
|
||
|
"http://internal.test/v1/?page=2&limit=5"
|
||
|
);
|
||
|
expect(res.paging.next.href).toBe(
|
||
|
"http://internal.test/v1/?page=4&limit=5"
|
||
|
);
|
||
|
expect(res.paging.last.href).toBe(
|
||
|
"http://internal.test/v1/?page=8&limit=5"
|
||
|
);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|