437 lines
11 KiB
JavaScript
437 lines
11 KiB
JavaScript
|
/* eslint-disable jest/no-test-callback */
|
||
|
import { createBrands, createBrand, createCar, truncate } from "./utils/common";
|
||
|
|
||
|
import models from "../models";
|
||
|
import Cars from "../rules/Cars";
|
||
|
import Brands from "../rules/Brands";
|
||
|
import Middelware from "../index";
|
||
|
|
||
|
const createdCars = [];
|
||
|
|
||
|
describe("getAll (with inclusions)", () => {
|
||
|
beforeAll(done => {
|
||
|
createBrands(6, err => {
|
||
|
if (err) {
|
||
|
done(err);
|
||
|
} else {
|
||
|
createBrand((errBrand, createdBrand) => {
|
||
|
if (errBrand) {
|
||
|
done(errBrand);
|
||
|
} else {
|
||
|
createCar(createdBrand.id, true, 2004, (errCar, car) => {
|
||
|
if (errCar) {
|
||
|
done(errCar);
|
||
|
}
|
||
|
createdCars.push(car);
|
||
|
createCar(createdBrand.id, false, 2004, (errCar2, car2) => {
|
||
|
if (errCar2) {
|
||
|
done(errCar2);
|
||
|
}
|
||
|
createdCars.push(car2);
|
||
|
createCar(createdBrand.id, true, 2003, (errCar3, car3) => {
|
||
|
if (errCar3) {
|
||
|
done(errCar3);
|
||
|
}
|
||
|
createdCars.push(car3);
|
||
|
createCar(createdBrand.id, true, 1998, (errCar4, car4) => {
|
||
|
if (errCar4) {
|
||
|
done(errCar4);
|
||
|
}
|
||
|
createdCars.push(car4);
|
||
|
createCar(createdBrand.id, false, 1998, (errCar5, car5) => {
|
||
|
if (errCar5) {
|
||
|
done(errCar5);
|
||
|
}
|
||
|
createdCars.push(car5);
|
||
|
createBrands(2, errLastBrand => {
|
||
|
if (errLastBrand) {
|
||
|
done(errLastBrand);
|
||
|
} else {
|
||
|
models.Cars.create({
|
||
|
name: "la renault fuego",
|
||
|
year: 1980,
|
||
|
active: false
|
||
|
})
|
||
|
.then(() => {
|
||
|
done();
|
||
|
})
|
||
|
.catch(done);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
afterAll(done => {
|
||
|
truncate(["Brands", "Cars"], done);
|
||
|
});
|
||
|
|
||
|
test("It should return all items when allowed role call getAll without filters (except inactive)", async done => {
|
||
|
const middleware = new Middelware(Cars, 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(3);
|
||
|
expect(res.total).toBe(3);
|
||
|
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 all items when allowed role call getAll without filters", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
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(6);
|
||
|
expect(res.total).toBe(6);
|
||
|
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 2 items when set complex filters", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
"year.lte": 2005,
|
||
|
"year.gte": 2004
|
||
|
},
|
||
|
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(2);
|
||
|
expect(res.total).toBe(2);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return 1 item when set complex filters", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "user"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
"year.lte": 2005,
|
||
|
"year.gte": 2004
|
||
|
},
|
||
|
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(1);
|
||
|
expect(res.total).toBe(1);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return 1 item when get all brands", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc"
|
||
|
},
|
||
|
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(9);
|
||
|
expect(res.total).toBe(9);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
expect(res.data[6].Cars.length).toBe(5);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return 1 item when get all brands (without cars)", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc"
|
||
|
},
|
||
|
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(9);
|
||
|
expect(res.total).toBe(9);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
expect(res.data[6].Cars).not.toHaveProperty("Cars");
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return 3 items when admin send weirdfilter", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc",
|
||
|
weirdfilter: "letsgo"
|
||
|
},
|
||
|
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(3);
|
||
|
expect(res.total).toBe(3);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return errorCode 406.1 when trying to send unallowed filter", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc",
|
||
|
id: 65
|
||
|
},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(parseFloat(err.errorCode)).toBe(406.1);
|
||
|
expect(res).toBeUndefined();
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return one item when trying to get item with name contains 'renault'", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc",
|
||
|
"name.lk": "renault"
|
||
|
},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(err).toBeNull();
|
||
|
expect(res.data.length).toBe(1);
|
||
|
expect(res.total).toBe(1);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return one item when trying to get item filtered by name", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc",
|
||
|
name: "la renault fuego"
|
||
|
},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(err).toBeNull();
|
||
|
expect(res.data.length).toBe(1);
|
||
|
expect(res.total).toBe(1);
|
||
|
expect(res.maxPage).toBe(1);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return empty data when trying to get item filtered by name", async done => {
|
||
|
const middleware = new Middelware(Cars, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {
|
||
|
sort: "id",
|
||
|
order: "asc",
|
||
|
name: "Clio"
|
||
|
},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getAll(req, (err, res) => {
|
||
|
expect(err).toBeUndefined();
|
||
|
expect(res).toBeUndefined();
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|