This commit is contained in:
Alie 2024-01-06 21:24:12 +01:00
parent f2bb15e0ab
commit 7aceafff7b
5 changed files with 120 additions and 71 deletions

View File

@ -51,11 +51,20 @@ class ImageController {
async getAllImages(req: Request, res: Response): Promise<void> {
try {
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`")
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 status = req.query.status
? (req.query.status as Image["status"])
: undefined;
const images = await imageService.findAll(limit, status);
res.json({ images });

View File

@ -11,7 +11,9 @@ class ImageService {
}
async findAll(limit?: number, status?: Image["status"]): Promise<Image[]> {
const typeError = TypeError("if present, `limit` must be a non-negative integer");
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) {

View File

@ -92,17 +92,27 @@ describe("GET /images works properly", async () => {
});
it("should return 200 for a request with valid status param", async () => {
const status = faker.helpers.arrayElement(["consumed", "available", "unavailable"])
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 status = faker.helpers.arrayElement([
"consumed",
"available",
"unavailable",
]);
const res = await request(app).get(`/images?status=${status}`);
expect(res.body.images
expect(
res.body.images
.map((image: Image) => image.status)
.every((imageStatus: string) => imageStatus === status)).toBeTrue();
.every((imageStatus: string) => imageStatus === status)
).toBeTrue();
});
it("should return 400 for a floating point value in limit value", async () => {
@ -146,7 +156,7 @@ describe("GET /images works properly", async () => {
expect(ids1.length).toBe(ids2.length);
ids1.forEach((id: string) => expect(ids2).toContain(id));
})
});
});
describe("POST /images works properly", () => {
@ -371,5 +381,5 @@ describe("PUT /images/<id> works properly", () => {
const res = await request(app).get(`/images/${id}`);
expect(res.body.image).toHaveProperty("status", "consumed");
})
});
});

View File

@ -1,20 +1,37 @@
import authModel from "../src/models/AuthModel";
import imageModel, { Image } from "../src/models/ImageModel";
import { faker } from '@faker-js/faker';
import { faker } from "@faker-js/faker";
export default async function () {
const images = faker.helpers.multiple(() => ({
const images = faker.helpers.multiple(
() => ({
url: faker.helpers.unique(faker.internet.url),
status: faker.helpers.arrayElement(["available", "unavailable", "consumed"]),
tags: faker.helpers.arrayElements([
"2girls", "sleeping", "touhou",
"pokemon", "closed_eyes", "yume_nikki",
"waifu", "computer", "party", "yuru_camp"
], { min: 0, max: 5 }),
}), {count: {min: 5, max:50}});
status: faker.helpers.arrayElement([
"available",
"unavailable",
"consumed",
]),
tags: faker.helpers.arrayElements(
[
"2girls",
"sleeping",
"touhou",
"pokemon",
"closed_eyes",
"yume_nikki",
"waifu",
"computer",
"party",
"yuru_camp",
],
{ min: 0, max: 5 }
),
}),
{ count: { min: 5, max: 50 } }
);
// Wait until all images are inserted
await Promise.allSettled(images.map(image => imageModel.create(image)));
await Promise.allSettled(images.map((image) => imageModel.create(image)));
await authModel.create({
app: "tester",

View File

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