// hasRecipe
import middleware from "../../../middleware";
const {hasRecipe} = middleware;
import {Recipe} from "../../../database/models";
class MyRecipe {
constructor() {
this.id = 1;
this.title = "Napoli Pizza";
this.description = "The traditional Napoli pizza";
this.timeToCook = "15";
this.ingredients = JSON.stringify([
"250g of pizza dough.",
"80g of tomato pasta.",
"Tomato",
"15g of cheesse",
"Some basil leaves",
"Olive oil"
]);
this.imageUrl = "http://f.i.uol.com.br/folha/saopaulo/images/15259369.jpeg";
this.procedure = JSON.stringify([
"1. Preheat the oven to 250 degrees.",
"2. On a workbench dusted with flour, stretch the dough as finely as possible first with your fingertips, then stirring the dough around the palm of your hand. Increase the dough disc from the center to the edges.",
"3. Place the dough into a lightly greased pizza shape with the help of a brush.",
"4. Spread the previously prepared tomato sauce on the dough disc in a circular motion and place a strand of olive oil.",
"5. Bake for 10 minutes.",
"6. Remove from the oven and spread the buffalo muffin and grated parmesan. Bake for another 5 minutes.",
"7. Remove from the oven and sprinkle the basil leaves and a little more grated parmesan if you want."
]);
}
}
describe("The hasRecipe", () => {
it("Should call next if the recipe is valid", async () => {
await Recipe.destroy({where: {}})
const myRecipe = new MyRecipe();
await Recipe.create(myRecipe);
const req = {
params: {
recipeId: 1
}
};
const res = {}
const next = jest.fn();
await hasRecipe(req, res, next);
expect(next).toHaveBeenCalled();
})
it("Should call sendFailureResponse if recipe is not found.", async () => {
const req = {
params: {
}
};
const res = {
sendFailureResponse: jest.fn()
};
const next = jest.fn();
await hasRecipe(req, res, next);
expect(res.sendFailureResponse).toBeCalledWith(
{ message: 'Recipe not found.' }, 404
);
})
it("Should call a sendFailureResponse if the recipe is not valid ", async () => {
const req = {
params: {
recipeId: "abc"
}
};
const res = {
sendFailureResponse: jest.fn()
};
const next = jest.fn();
await hasRecipe(req, res, next);
expect(res.sendFailureResponse).toBeCalledWith(
{ message: 'Recipe not found.' }, 404
);
})
})
// authorize
import middleware from "../../../middleware";
const {
authorize
} = middleware;
test("Should call next function", () => {
const req = {
currentRecipe: {
userId: 1
},
authUser: {
id: 1
}
};
const res = {
sendFailureResponse() {}
}
const next = jest.fn()
authorize(req, res, next)
expect(next).toHaveBeenCalled();
})
test("Should return a sendFailureResponse if recipe user id and user id are diferent", async () => {
const req = {
currentRecipe: {
userId: 1
},
authUser: {
id: 2
}
}
const res = {
sendFailureResponse: jest.fn()
}
const next = jest.fn()
await authorize(req, res, next)
expect(res.sendFailureResponse).toHaveBeenCalledWith({
message: 'Unauthorized.'
}, 401);
})
// signinUserValidator
import middleware from "../../../middleware";
const {signinUserValidator} = middleware;
test("Should call next function if user is valid", () => {
const req = {
body: {
email: "gabriel.f.umbelino@gmail.com",
password: "123456"
}
}
const res = {
sendFailureResponse() {}
}
const next = jest.fn();
signinUserValidator(req, res, next)
expect(next).toHaveBeenCalled()
})
test("Should scall sendFailureResponse if validator is invalid", () => {
const req = {
body: {}
}
const res = {
sendFailureResponse: jest.fn()
}
const next = () => {}
signinUserValidator(req, res, next)
expect(res.sendFailureResponse).toHaveBeenCalledWith({
errors: ["The password is required.", "The email is required."]
}, 422)
})
import middleware from "../../../middleware";
const {hasRecipe} = middleware;
import {Recipe} from "../../../database/models";
class MyRecipe {
constructor() {
this.id = 1;
this.title = "Napoli Pizza";
this.description = "The traditional Napoli pizza";
this.timeToCook = "15";
this.ingredients = JSON.stringify([
"250g of pizza dough.",
"80g of tomato pasta.",
"Tomato",
"15g of cheesse",
"Some basil leaves",
"Olive oil"
]);
this.imageUrl = "http://f.i.uol.com.br/folha/saopaulo/images/15259369.jpeg";
this.procedure = JSON.stringify([
"1. Preheat the oven to 250 degrees.",
"2. On a workbench dusted with flour, stretch the dough as finely as possible first with your fingertips, then stirring the dough around the palm of your hand. Increase the dough disc from the center to the edges.",
"3. Place the dough into a lightly greased pizza shape with the help of a brush.",
"4. Spread the previously prepared tomato sauce on the dough disc in a circular motion and place a strand of olive oil.",
"5. Bake for 10 minutes.",
"6. Remove from the oven and spread the buffalo muffin and grated parmesan. Bake for another 5 minutes.",
"7. Remove from the oven and sprinkle the basil leaves and a little more grated parmesan if you want."
]);
}
}
describe("The hasRecipe", () => {
it("Should call next if the recipe is valid", async () => {
await Recipe.destroy({where: {}})
const myRecipe = new MyRecipe();
await Recipe.create(myRecipe);
const req = {
params: {
recipeId: 1
}
};
const res = {}
const next = jest.fn();
await hasRecipe(req, res, next);
expect(next).toHaveBeenCalled();
})
it("Should call sendFailureResponse if recipe is not found.", async () => {
const req = {
params: {
}
};
const res = {
sendFailureResponse: jest.fn()
};
const next = jest.fn();
await hasRecipe(req, res, next);
expect(res.sendFailureResponse).toBeCalledWith(
{ message: 'Recipe not found.' }, 404
);
})
it("Should call a sendFailureResponse if the recipe is not valid ", async () => {
const req = {
params: {
recipeId: "abc"
}
};
const res = {
sendFailureResponse: jest.fn()
};
const next = jest.fn();
await hasRecipe(req, res, next);
expect(res.sendFailureResponse).toBeCalledWith(
{ message: 'Recipe not found.' }, 404
);
})
})
// authorize
import middleware from "../../../middleware";
const {
authorize
} = middleware;
test("Should call next function", () => {
const req = {
currentRecipe: {
userId: 1
},
authUser: {
id: 1
}
};
const res = {
sendFailureResponse() {}
}
const next = jest.fn()
authorize(req, res, next)
expect(next).toHaveBeenCalled();
})
test("Should return a sendFailureResponse if recipe user id and user id are diferent", async () => {
const req = {
currentRecipe: {
userId: 1
},
authUser: {
id: 2
}
}
const res = {
sendFailureResponse: jest.fn()
}
const next = jest.fn()
await authorize(req, res, next)
expect(res.sendFailureResponse).toHaveBeenCalledWith({
message: 'Unauthorized.'
}, 401);
})
// signinUserValidator
import middleware from "../../../middleware";
const {signinUserValidator} = middleware;
test("Should call next function if user is valid", () => {
const req = {
body: {
email: "gabriel.f.umbelino@gmail.com",
password: "123456"
}
}
const res = {
sendFailureResponse() {}
}
const next = jest.fn();
signinUserValidator(req, res, next)
expect(next).toHaveBeenCalled()
})
test("Should scall sendFailureResponse if validator is invalid", () => {
const req = {
body: {}
}
const res = {
sendFailureResponse: jest.fn()
}
const next = () => {}
signinUserValidator(req, res, next)
expect(res.sendFailureResponse).toHaveBeenCalledWith({
errors: ["The password is required.", "The email is required."]
}, 422)
})