From 542f8056307cd95d5774572fe5b4c4dca4a9fb35 Mon Sep 17 00:00:00 2001 From: Ezequiel Ramos Date: Tue, 11 Jun 2024 14:17:54 -0300 Subject: [PATCH] adding return of face events --- package-lock.json | 17 ----------------- src/controllers/events.ts | 16 +++++++++++++--- src/routes/events.ts | 8 +++++--- tests/events.test.ts | 28 ++++++++++++++++++++++++---- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index de1a772..50fc1b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "dotenv": "^16.0.1", "express": "^4.18.1", "morgan": "^1.10.0", "multer": "^1.4.5-lts.1", @@ -2139,17 +2138,6 @@ "node": ">=6.0.0" } }, - "node_modules/dotenv": { - "version": "16.3.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", - "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/motdotla/dotenv?sponsor=1" - } - }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -7126,11 +7114,6 @@ "esutils": "^2.0.2" } }, - "dotenv": { - "version": "16.3.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", - "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==" - }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/src/controllers/events.ts b/src/controllers/events.ts index 157c8ed..e09e85d 100644 --- a/src/controllers/events.ts +++ b/src/controllers/events.ts @@ -72,7 +72,7 @@ interface EventFace { } let eventId = 0; -const events: { [eventId: number]: EventFace | undefined; } = {}; +const events: { [eventId: string]: EventFace | undefined; } = {}; function createEvent({ created_date, camera }: { created_date?: string; camera: number; }) { eventId++; @@ -107,7 +107,7 @@ function createEvent({ created_date, camera }: { created_date?: string; camera: acknowledged_by: 0, acknowledged_reaction: "", cluster_confidence: 0.0, - confidence: 0.0, + confidence: (Math.random() * 0.45) + 0.5, external_detector: true, looks_like_confidence: null, matched_lists: [watchLists[0].id], @@ -168,4 +168,14 @@ function getEvent(eventId: number) { return events[eventId]; } -export { createEvent, getEvent }; +function resetEvents() { + for (const eventId of Object.keys(events)) { + delete events[eventId]; + } +} + +function getEvents() { + return Object.values(events); +} + +export { createEvent, getEvent, resetEvents, getEvents }; diff --git a/src/routes/events.ts b/src/routes/events.ts index 262d766..9424963 100644 --- a/src/routes/events.ts +++ b/src/routes/events.ts @@ -1,7 +1,7 @@ import { Express, Request, Response } from 'express'; import multer from 'multer'; import { validAuthorization } from '../services/route_middlewares'; -import { createEvent, getEvent } from '../controllers/events'; +import { createEvent, getEvent, getEvents } from '../controllers/events'; const upload = multer({ storage: multer.memoryStorage() @@ -111,10 +111,12 @@ function loadEventsRoutes(app: Express) { }); app.get('/events/faces/', validAuthorization, async (req: Request, res: Response) => { + const events = getEvents(); + return res.status(200).json({ "next_page": null, - "count": 0, - "results": [] + "count": events.length, + "results": events }); }); } diff --git a/tests/events.test.ts b/tests/events.test.ts index 8750f84..30895da 100644 --- a/tests/events.test.ts +++ b/tests/events.test.ts @@ -3,10 +3,11 @@ import { agent } from 'supertest'; import { readFile } from 'fs/promises'; import { webService } from '../src/services/web-service'; +import { resetEvents } from '../src/controllers/events'; const request = agent(webService.app); -describe('Cards Route Testing', async () => { +describe('Events Route Testing', async () => { let token = ""; let detectionId = ""; @@ -43,23 +44,29 @@ describe('Cards Route Testing', async () => { .set('Authorization', 'Token ' + token); expect(res.statusCode).equals(200); + expect(res.body.objects.face).has.lengthOf(1); expect(res.body.objects.face[0].id).to.not.be.null; detectionId = res.body.objects.face[0].id; }); it('test get events by detectionId', async () => { + resetEvents(); const res = await request.get(`/events/faces/`) .query({ looks_like: "detection:" + detectionId, }) .set('Authorization', 'Token ' + token); expect(res.statusCode).to.be.equal(200); + + expect(res.body.next_page).to.be.equal(null); + expect(res.body.count).to.be.equal(0); }); it('test create event with timestamp', async () => { + resetEvents(); const file = await readFile(__dirname + "/assets/11296869.jpg"); - const res = await request.post(`/events/faces/add/`) + let res = await request.post(`/events/faces/add/`) .attach("fullframe", file, { filename: 'fullframe.jpg', contentType: 'image/jpeg' @@ -71,7 +78,20 @@ describe('Cards Route Testing', async () => { .set('Authorization', 'Token ' + token); expect(res.statusCode).to.be.equal(200); - expect(res.body.id).to.not.be.null; + expect(res.body.events).has.lengthOf(1); + + const createdEventId = res.body.events[0]; + + res = await request.get(`/events/faces/`) + .query({ + looks_like: "detection:" + detectionId, + }) + .set('Authorization', 'Token ' + token); + expect(res.statusCode).to.be.equal(200); + + expect(res.body.next_page).to.be.equal(null); + expect(res.body.count).to.be.equal(1); + expect(res.body.results[0].id).to.be.equal(createdEventId); }); it('test create event without timestamp', async () => { @@ -88,7 +108,7 @@ describe('Cards Route Testing', async () => { .set('Authorization', 'Token ' + token); expect(res.statusCode).to.be.equal(200); - expect(res.body.id).to.not.be.null; + expect(res.body.events).has.lengthOf(1); }); it('test invalid create event - missing picture', async () => {