v1.0.0 #28

Merged
bizcochito merged 147 commits from develop into main 2024-01-14 19:49:34 +00:00
5 changed files with 134 additions and 59 deletions
Showing only changes of commit f2bb15e0ab - Show all commits

View File

@ -49,14 +49,24 @@ class ImageController {
} }
} }
async getAllImages(_: Request, res: Response): Promise<void> { async getAllImages(req: Request, res: Response): Promise<void> {
try { try {
const images = await imageService.findAll(); if (req.query.status !== undefined && !["consumed", "unavailable", "available"].includes(req.query.status as string)) {
throw TypeError("if present, `status` should have the values `consumed`, `unavailable`, or `available`")
}
const limit = req.query.limit ? Number(req.query.limit) : undefined;
const status = req.query.status ? req.query.status as Image["status"] : undefined;
const images = await imageService.findAll(limit, status);
res.json({ images }); res.json({ images });
} catch (error) { } catch (error) {
if (error instanceof TypeError) {
res.status(400).json({ error });
} else {
res.status(500).json({ error: "Internal Server Error" }); res.status(500).json({ error: "Internal Server Error" });
} }
} }
}
async addImage(req: Request, res: Response): Promise<void> { async addImage(req: Request, res: Response): Promise<void> {
try { try {
@ -76,7 +86,7 @@ class ImageController {
res.status(400).json({ error: error.message }); res.status(400).json({ error: error.message });
} else { } else {
// Return 500 in other case // Return 500 in other case
res.status(500).json({ error: error }); res.status(500).json({ error });
} }
} }
} }

View File

@ -1,9 +1,9 @@
import mongoose, { Document } from "mongoose"; import mongoose, { Document } from "mongoose";
export interface Image extends Document { export interface Image extends Document {
url: String; url: string;
enum: "consumed" | "unavailable" | "available"; status: "consumed" | "unavailable" | "available";
tags?: String[]; tags?: string[];
} }
const ImageSchema = new mongoose.Schema( const ImageSchema = new mongoose.Schema(

View File

@ -9,10 +9,23 @@ class ImageService {
const image = await imageModel.findOne({ _id: id }); const image = await imageModel.findOne({ _id: id });
return image; return image;
} }
async findAll(): Promise<Image[]> {
const allImages = await imageModel.find(); async findAll(limit?: number, status?: Image["status"]): Promise<Image[]> {
const typeError = TypeError("if present, `limit` must be a non-negative integer");
const filter = status !== undefined ? { status } : {};
let query = imageModel.find(filter);
if (limit !== undefined) {
if (Number.isInteger(limit)) {
if (limit > 0) query = query.limit(limit);
else if (limit < 0) throw typeError;
} else {
throw typeError;
}
}
const allImages = await query;
return allImages; return allImages;
} }
async add(image: Image): Promise<Image> { async add(image: Image): Promise<Image> {
const newImage = await imageModel.create(image); const newImage = await imageModel.create(image);
return newImage; return newImage;

View File

@ -4,6 +4,7 @@ import app, { startApp } from "../src/app";
import imageService from "../src/services/ImageService"; import imageService from "../src/services/ImageService";
import populateDatabase from "./populateDatabase"; import populateDatabase from "./populateDatabase";
import { Image } from "../src/models/ImageModel"; import { Image } from "../src/models/ImageModel";
import { faker } from "@faker-js/faker";
const imageServiceOriginal = imageService; const imageServiceOriginal = imageService;
@ -74,7 +75,7 @@ describe("GET /images works properly", async () => {
it("should return 500 for an error on the service", async () => { it("should return 500 for an error on the service", async () => {
mock.module("../src/services/ImageService", () => ({ mock.module("../src/services/ImageService", () => ({
default: { default: {
add: () => { findAll: () => {
throw new Error("This is an expected testing error"); throw new Error("This is an expected testing error");
}, },
}, },
@ -84,6 +85,68 @@ describe("GET /images works properly", async () => {
expect(res.status).toBe(500); expect(res.status).toBe(500);
}); });
it("should return 400 for an invalid status param value", async () => {
const res = await request(app).get("/images?status=foo");
expect(res.statusCode).toBe(400);
});
it("should return 200 for a request with valid status param", async () => {
const status = faker.helpers.arrayElement(["consumed", "available", "unavailable"])
const res = await request(app).get(`/images?status=${status}`);
expect(res.statusCode).toBe(200);
});
it("should only have the requested status in the images", async () => {
const status = faker.helpers.arrayElement(["consumed", "available", "unavailable"])
const res = await request(app).get(`/images?status=${status}`);
expect(res.body.images
.map((image: Image) => image.status)
.every((imageStatus: string) => imageStatus === status)).toBeTrue();
});
it("should return 400 for a floating point value in limit value", async () => {
const res = await request(app).get("/images?limit=2.4");
expect(res.statusCode).toBe(400);
});
it("should return 400 for a negative value in limit value", async () => {
const res = await request(app).get("/images?limit=-1");
expect(res.statusCode).toBe(400);
});
it("should return 400 for a NaN limit value", async () => {
const res = await request(app).get("/images?limit=foo");
expect(res.statusCode).toBe(400);
});
it("should return 200 for a request with valid limit param", async () => {
const limit = faker.number.int({ min: 5, max: 50 });
const res = await request(app).get(`/images?limit=${limit}`);
expect(res.statusCode).toBe(200);
});
it("should return 200 for a request with valid limit param", async () => {
const limit = faker.number.int({ min: 5, max: 50 });
const res = await request(app).get(`/images?limit=${limit}`);
expect(res.body.images.length).toBeLessThanOrEqual(limit);
});
it("should return 200 for a request with both valid params", async () => {
const res = await request(app).get("/images?limit=3&status=available");
expect(res.statusCode).toBe(200);
});
it("should return the same ids using limit=0 and using no limit parameter", async () => {
const resLimit0 = await request(app).get("/images?limit=0");
const resNoLimit = await request(app).get("/images");
const ids1 = resNoLimit.body.images.map((image: Image) => image._id);
const ids2 = resLimit0.body.images.map((image: Image) => image._id);
expect(ids1.length).toBe(ids2.length);
ids1.forEach((id: string) => expect(ids2).toContain(id));
})
}); });
describe("POST /images works properly", () => { describe("POST /images works properly", () => {

View File

@ -1,6 +1,6 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1 # yarn lockfile v1
# bun ./bun.lockb --hash: 738A3B3B61512E75-26d27a9609e246ac-33059F282FFA6348-27fd5707ca8843fa # bun ./bun.lockb --hash: FB1C344BE4E1C170-a59dde1fb7411c09-538A0DE39E28C689-3912799b87d41761
"@ampproject/remapping@^2.2.0": "@ampproject/remapping@^2.2.0":
@ -25,19 +25,19 @@
integrity sha512-uU27kfDRlhfKl+w1U6vp16IuvSLtjAxdArVXPa9BvLkrr7CYIsxH5adpHObeAGY/41+syctUWOZ140a2Rvkgjw== integrity sha512-uU27kfDRlhfKl+w1U6vp16IuvSLtjAxdArVXPa9BvLkrr7CYIsxH5adpHObeAGY/41+syctUWOZ140a2Rvkgjw==
"@babel/core@>=7.0.0-beta.0 <8", "@babel/core@^7.0.0", "@babel/core@^7.0.0-0", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.8.0": "@babel/core@>=7.0.0-beta.0 <8", "@babel/core@^7.0.0", "@babel/core@^7.0.0-0", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.8.0":
version "7.23.7" version "7.23.6"
resolved "https://registry.npmjs.org/@babel/core/-/core-7.23.7.tgz" resolved "https://registry.npmjs.org/@babel/core/-/core-7.23.6.tgz"
integrity sha512-+UpDgowcmqe36d4NwqvKsyPMlOLNGMsfMmQ5WGCu+siCe3t3dfe9njrzGfdN4qq+bcNUt0+Vw6haRxBOycs4dw== integrity sha512-FxpRyGjrMJXh7X3wGLGhNDCRiwpWEF74sKjTLDJSG5Kyvow3QZaG0Adbqzi9ZrVjTWpsX+2cxWXD71NMg93kdw==
dependencies: dependencies:
"@ampproject/remapping" "^2.2.0" "@ampproject/remapping" "^2.2.0"
"@babel/code-frame" "^7.23.5" "@babel/code-frame" "^7.23.5"
"@babel/generator" "^7.23.6" "@babel/generator" "^7.23.6"
"@babel/helper-compilation-targets" "^7.23.6" "@babel/helper-compilation-targets" "^7.23.6"
"@babel/helper-module-transforms" "^7.23.3" "@babel/helper-module-transforms" "^7.23.3"
"@babel/helpers" "^7.23.7" "@babel/helpers" "^7.23.6"
"@babel/parser" "^7.23.6" "@babel/parser" "^7.23.6"
"@babel/template" "^7.22.15" "@babel/template" "^7.22.15"
"@babel/traverse" "^7.23.7" "@babel/traverse" "^7.23.6"
"@babel/types" "^7.23.6" "@babel/types" "^7.23.6"
convert-source-map "^2.0.0" convert-source-map "^2.0.0"
debug "^4.1.0" debug "^4.1.0"
@ -138,13 +138,13 @@
resolved "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.23.5.tgz" resolved "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.23.5.tgz"
integrity sha512-85ttAOMLsr53VgXkTbkx8oA6YTfT4q7/HzXSLEYmjcSTJPMPQtvq1BD79Byep5xMUYbGRzEpDsjUf3dyp54IKw== integrity sha512-85ttAOMLsr53VgXkTbkx8oA6YTfT4q7/HzXSLEYmjcSTJPMPQtvq1BD79Byep5xMUYbGRzEpDsjUf3dyp54IKw==
"@babel/helpers@^7.23.7": "@babel/helpers@^7.23.6":
version "7.23.7" version "7.23.6"
resolved "https://registry.npmjs.org/@babel/helpers/-/helpers-7.23.7.tgz" resolved "https://registry.npmjs.org/@babel/helpers/-/helpers-7.23.6.tgz"
integrity sha512-6AMnjCoC8wjqBzDHkuqpa7jAKwvMo4dC+lr/TFBz+ucfulO1XMpDnwWPGBNwClOKZ8h6xn5N81W/R5OrcKtCbQ== integrity sha512-wCfsbN4nBidDRhpDhvcKlzHWCTlgJYUUdSJfzXb2NuBssDSIjc3xcb+znA7l+zYsFljAcGM0aFkN40cR3lXiGA==
dependencies: dependencies:
"@babel/template" "^7.22.15" "@babel/template" "^7.22.15"
"@babel/traverse" "^7.23.7" "@babel/traverse" "^7.23.6"
"@babel/types" "^7.23.6" "@babel/types" "^7.23.6"
"@babel/highlight@^7.23.4": "@babel/highlight@^7.23.4":
@ -268,10 +268,10 @@
"@babel/parser" "^7.22.15" "@babel/parser" "^7.22.15"
"@babel/types" "^7.22.15" "@babel/types" "^7.22.15"
"@babel/traverse@^7.23.7": "@babel/traverse@^7.23.6":
version "7.23.7" version "7.23.6"
resolved "https://registry.npmjs.org/@babel/traverse/-/traverse-7.23.7.tgz" resolved "https://registry.npmjs.org/@babel/traverse/-/traverse-7.23.6.tgz"
integrity sha512-tY3mM8rH9jM0YHFGyfC0/xf+SB5eKUu7HPj7/k3fpi9dAlsMc5YbQvDi0Sh2QTPXqMhyaAtzAr807TIyfQrmyg== integrity sha512-czastdK1e8YByZqezMPFiZ8ahwVMh/ESl9vPgvgdB9AmFMGP5jfpFax74AQgl5zj4XHzqeYAg2l8PuUeRS1MgQ==
dependencies: dependencies:
"@babel/code-frame" "^7.23.5" "@babel/code-frame" "^7.23.5"
"@babel/generator" "^7.23.6" "@babel/generator" "^7.23.6"
@ -596,9 +596,9 @@
"@babel/types" "^7.0.0" "@babel/types" "^7.0.0"
"@types/babel__traverse@*", "@types/babel__traverse@^7.0.6": "@types/babel__traverse@*", "@types/babel__traverse@^7.0.6":
version "7.20.5" version "7.20.4"
resolved "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.5.tgz" resolved "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.4.tgz"
integrity sha512-WXCyOcRtH37HAUkpXhUduaxdm82b4GSlyTqajXviN4EfiuPgNYR109xMCKvpl6zPIpua0DGlMEDCq+g8EdoheQ== integrity sha512-mSM/iKUk5fDDrEV/e83qY+Cr3I1+Q3qqTuEn++HAWYjEa1+NxZr6CNrcJGf2ZTnq4HoFGC3zaTPZTobCzCFukA==
dependencies: dependencies:
"@babel/types" "^7.20.7" "@babel/types" "^7.20.7"
@ -711,16 +711,16 @@
integrity sha512-iJt33IQnVRkqeqC7PzBHPTC6fDlRNRW8vjrgqtScAhrmMwe8c4Eo7+fUGTa+XdWrpEgpyKWMYmi2dIwMAYRzPw== integrity sha512-iJt33IQnVRkqeqC7PzBHPTC6fDlRNRW8vjrgqtScAhrmMwe8c4Eo7+fUGTa+XdWrpEgpyKWMYmi2dIwMAYRzPw==
"@types/node@*": "@types/node@*":
version "20.10.6" version "20.10.4"
resolved "https://registry.npmjs.org/@types/node/-/node-20.10.6.tgz" resolved "https://registry.npmjs.org/@types/node/-/node-20.10.4.tgz"
integrity sha512-Vac8H+NlRNNlAmDfGUP7b5h/KA+AtWIzuXy0E6OyP8f1tCLYAtPvKRRDJjAPqhpCb0t6U2j7/xqAuLEebW2kiw== integrity sha512-D08YG6rr8X90YB56tSIuBaddy/UXAA9RKJoFvrsnogAum/0pmjkgi4+2nx96A330FmioegBWmEYQ+syqCFaveg==
dependencies: dependencies:
undici-types "~5.26.4" undici-types "~5.26.4"
"@types/qs@*": "@types/qs@*":
version "6.9.11" version "6.9.10"
resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.11.tgz" resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.10.tgz"
integrity sha512-oGk0gmhnEJK4Yyk+oI7EfXsLayXatCWPHary1MtcmbAifkobT9cM9yutG/hZKIseOU0MqbIwQ/u2nn/Gb+ltuQ== integrity sha512-3Gnx08Ns1sEoCrWssEgTSJs/rsT2vhGP+Ja9cnnk9k4ALxinORlQneLXFeFKOTJMOeZUFD1s7w+w2AphTpvzZw==
"@types/range-parser@*": "@types/range-parser@*":
version "1.2.7" version "1.2.7"
@ -759,9 +759,9 @@
"@types/methods" "^1.1.4" "@types/methods" "^1.1.4"
"@types/supertest@^6.0.1": "@types/supertest@^6.0.1":
version "6.0.2" version "6.0.1"
resolved "https://registry.npmjs.org/@types/supertest/-/supertest-6.0.2.tgz" resolved "https://registry.npmjs.org/@types/supertest/-/supertest-6.0.1.tgz"
integrity sha512-137ypx2lk/wTQbW6An6safu9hXmajAifU/s7szAHLN/FeIm5w7yR0Wkl9fdJMRSHwOn4HLAI0DaB2TOORuhPDg== integrity sha512-M1xs8grAWC4RisSEQjyQV0FZzXnL3y796540Q/HCdiPcErwKpcAfvsNQFb4xp+5btSWMOZG1YlDWs2z96pdbcw==
dependencies: dependencies:
"@types/methods" "^1.1.4" "@types/methods" "^1.1.4"
"@types/superagent" "^8.1.0" "@types/superagent" "^8.1.0"
@ -779,13 +779,6 @@
"@types/node" "*" "@types/node" "*"
"@types/webidl-conversions" "*" "@types/webidl-conversions" "*"
"@types/ws@*":
version "8.5.10"
resolved "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz"
integrity sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==
dependencies:
"@types/node" "*"
"@types/yargs@^17.0.8": "@types/yargs@^17.0.8":
version "17.0.32" version "17.0.32"
resolved "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.32.tgz" resolved "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.32.tgz"
@ -867,7 +860,7 @@ asynckit@^0.4.0:
resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz" resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
babel-jest@^29.7.0: babel-jest@^29.0.0, babel-jest@^29.7.0:
version "29.7.0" version "29.7.0"
resolved "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz" resolved "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz"
integrity sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg== integrity sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg==
@ -1005,13 +998,9 @@ buffer-from@^1.0.0:
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ== integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==
bun-types@latest: bun-types@latest:
version "1.0.21" version "1.0.15"
resolved "https://registry.npmjs.org/bun-types/-/bun-types-1.0.21.tgz" resolved "https://registry.npmjs.org/bun-types/-/bun-types-1.0.15.tgz"
integrity sha512-Ugagjf+XZUXDvxDRa3EnhLeMzm2g8ZYFleBF55Ac3GZSzPqdMLAdK9kvZB6M1H4nAFvrEHdV2PHqkzIoNs+3wQ== integrity sha512-XkEvWLV1JIhcVIpf2Lu6FXnZUxRUkQVJmgY+VT7os6Tk5X1nkXx11q4Rtu6txsqpDJZfUeZXblnnD59K+6wsVA==
dependencies:
"@types/node" "*"
"@types/ws" "*"
undici-types "^5.26.4"
bytes@3.1.2: bytes@3.1.2:
version "3.1.2" version "3.1.2"
@ -1043,9 +1032,9 @@ camelcase@^6.2.0:
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
caniuse-lite@^1.0.30001565: caniuse-lite@^1.0.30001565:
version "1.0.30001574" version "1.0.30001571"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001574.tgz" resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001571.tgz"
integrity sha512-BtYEK4r/iHt/txm81KBudCUcTy7t+s9emrIaHqjYurQ10x71zJ5VQ9x1dYPcz/b+pKSp4y/v1xSI67A+LzpNyg== integrity sha512-tYq/6MoXhdezDLFZuCO/TKboTzuQ/xR5cFdgXPfDtM7/kchBO3b4VWghE/OAi/DV7tTdhmLjZiZBZi1fA/GheQ==
chalk@^2.4.2: chalk@^2.4.2:
version "2.4.2" version "2.4.2"
@ -1272,9 +1261,9 @@ ee-first@1.1.1:
integrity sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow== integrity sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==
electron-to-chromium@^1.4.601: electron-to-chromium@^1.4.601:
version "1.4.623" version "1.4.616"
resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.623.tgz" resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.616.tgz"
integrity sha512-lKoz10iCYlP1WtRYdh5MvocQPWVRoI7ysp6qf18bmeBgR8abE6+I2CsfyNKztRDZvhdWc+krKT6wS7Neg8sw3A== integrity sha512-1n7zWYh8eS0L9Uy+GskE0lkBUNK83cXTVJI0pU3mGprFsbfSdAc15VTFbo+A+Bq4pwstmL30AVcEU3Fo463lNg==
emittery@^0.13.1: emittery@^0.13.1:
version "0.13.1" version "0.13.1"
@ -2927,7 +2916,7 @@ type-is@~1.6.18:
resolved "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz" resolved "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw== integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==
undici-types@^5.26.4, undici-types@~5.26.4: undici-types@~5.26.4:
version "5.26.5" version "5.26.5"
resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz" resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==