From 0184273f662e8681639a11e74848ccbf9b913992 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Wed, 20 Sep 2023 16:17:22 +0200 Subject: [PATCH 01/13] updated location attribute of Test event for e2e tests --- apps/api-e2e/src/api/api.spec.ts | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 5e798f9e..3965d78b 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -20,15 +20,7 @@ let TEST_EVENT: IEvent ={ EndDate: new Date("2023-06-13T12:34:56.789Z"), Name: "Testing Event", Category: "Testing Category", - Location: { - Latitude: 0, - Longitude: 0, - StreetName: "None", - CityName: "None", - ProvinceName: "None", - CountryName: "None", - ZIPCode: "None" - }, + Location: "Event Location", Manager: new Types.ObjectId(), FloorLayout: null, // Devices: Array(), @@ -39,15 +31,7 @@ const UPDATED_TEST_EVENT: IEvent ={ EndDate: new Date("2023-06-13T12:34:56.789Z"), Name: "New Testing Event", Category: "New Testing Category", - Location: { - Latitude: 1, - Longitude: 1, - StreetName: "New StreetName", - CityName: "New CityName", - ProvinceName: "New ProvinceName", - CountryName: "New CountryName", - ZIPCode: "New ZIPCode" - }, + Location: "Updated Event location" // Devices: Array(), } From 8f3f16d021a1c38a823eef5949be4304d885e5c9 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Wed, 20 Sep 2023 16:22:13 +0200 Subject: [PATCH 02/13] added Second Test event for e2e tests --- apps/api-e2e/src/api/api.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 3965d78b..da58005f 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -26,6 +26,18 @@ let TEST_EVENT: IEvent ={ // Devices: Array(), } +// eslint-disable-next-line prefer-const +let TEST_EVENT2: IEvent ={ + StartDate: new Date("2023-06-10T12:34:56.789Z"), + EndDate: new Date("2023-06-13T12:34:56.789Z"), + Name: "Testing Event 2", + Category: "Testing Category 2", + Location: "Event Location 2", + Manager: new Types.ObjectId(), + FloorLayout: null, + // Devices: Array(), +} + const UPDATED_TEST_EVENT: IEvent ={ StartDate: new Date("2023-06-10T12:34:56.789Z"), EndDate: new Date("2023-06-13T12:34:56.789Z"), @@ -714,6 +726,8 @@ describe('EventController', ()=>{ }) }) + + }) describe('UserController', ()=>{ From ae1b889c0b4eb780588a0e9c1330661eb0bfb1d6 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Wed, 20 Sep 2023 23:10:47 +0200 Subject: [PATCH 03/13] updated cleanup --- apps/api-e2e/src/api/api.spec.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index da58005f..e709623c 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -27,7 +27,7 @@ let TEST_EVENT: IEvent ={ } // eslint-disable-next-line prefer-const -let TEST_EVENT2: IEvent ={ +let TEST_EVENT_2: IEvent ={ StartDate: new Date("2023-06-10T12:34:56.789Z"), EndDate: new Date("2023-06-13T12:34:56.789Z"), Name: "Testing Event 2", @@ -89,10 +89,12 @@ function objectSubset(target: any, obj: any ): boolean{ // eslint-disable-next-line no-prototype-builtins if(target.hasOwnProperty(key) && element.hasOwnProperty(key)){ if( element.key != target.key){ + console.log(key); return false; } }else{ - return false; + console.log(key); + return false; } } @@ -572,6 +574,7 @@ describe('EventController', ()=>{ expect(requesters[0].Requesters[0]).toEqual(viewer[0]._id); //cleanup + delete TEST_EVENT.Requesters; await userRepository.deleteUserById(manager[0]._id); await userRepository.deleteUserById(viewer[0]._id); await eventRepository.deleteEventbyId(event[0]._id); @@ -613,6 +616,7 @@ describe('EventController', ()=>{ expect(event[0].Requesters.length).toEqual(0); //cleanup + delete TEST_EVENT.Requesters; await eventRepository.deleteEventbyId(event[0]._id); await userRepository.deleteUserById(viewer[0]._id); await userRepository.deleteUserById(manager[0]._id); @@ -668,6 +672,8 @@ describe('EventController', ()=>{ expect(viewer[0].Viewing[0]).toEqual(event[0]._id); //cleanup + delete TEST_EVENT.Requesters; + delete TEST_EVENT.Viewers; await eventRepository.deleteEventbyId(event[0]._id); await userRepository.deleteUserById(viewer[0]._id); await userRepository.deleteUserById(manager[0]._id); @@ -719,6 +725,9 @@ describe('EventController', ()=>{ expect(viewer[0].Viewing.length).toEqual(0); //cleanup + delete TEST_EVENT.Requesters; + delete TEST_EVENT.Manager; + delete TEST_EVENT.Viewers; await eventRepository.deleteEventbyId(event[0]._id); await userRepository.deleteUserById(viewer[0]._id); await userRepository.deleteUserById(manager[0]._id); @@ -726,8 +735,6 @@ describe('EventController', ()=>{ }) }) - - }) describe('UserController', ()=>{ From 4b47c3044adad5b06908e558d6b6b1ecad27bcf9 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Wed, 20 Sep 2023 23:11:24 +0200 Subject: [PATCH 04/13] added e2e test for deleteEvent endpoint --- apps/api-e2e/src/api/api.spec.ts | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index e709623c..992c0d16 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -450,6 +450,58 @@ describe('EventController', ()=>{ }) }) + describe('deleteEvent', ()=>{ + it('Should delete an event object', async ()=>{ + //create event manager and event + await userRepository.createUser(TEST_USER_1); + const manager = await userRepository.getUser(process.env['TEST_USER_EMAIL_1']); + TEST_EVENT.Manager = manager[0]._id; + TEST_EVENT_2.Manager = manager[0]._id; + + //create events + await eventRepository.createEvent(TEST_EVENT); + await eventRepository.createEvent(TEST_EVENT_2); + const event1 = await eventRepository.getEventByName(TEST_EVENT.Name); + const event2 = await eventRepository.getEventByName(TEST_EVENT_2.Name); + + const response = await request(httpServer).post('/event/deleteEvent').send({ + eventId: event2[0]._id + }); + + //event1 should still be present and only event in db + expect(response.status).toBe(201); + let events = await eventRepository.getAllEvents(); + + while(events.length != 1){ + SLEEP(500); + events = await eventRepository.getAllEvents(); + } + + if(events && events.length ==1){ + + const temp: IEvent = { + StartDate: events[0].StartDate, + EndDate: events[0].EndDate, + Name: events[0].Name, + Category: events[0].Category, + Location: events[0].Location, + Manager: events[0].Manager, + FloorLayout: null, + } + // eslint-disable-next-line no-prototype-builtins + const res = objectSubset(TEST_EVENT,[temp]); + expect(res).toBe(true); + }else{ //intentionally fail + expect(true).toBe(false); + } + + //cleanup + await eventRepository.deleteEventbyId(event1[0]._id); + await eventRepository.deleteEventbyId(event2[0]._id); + await userRepository.deleteUserById(manager[0]._id); + }) + }) + describe('updateEventDetails', ()=>{ it('Should update an events details', async ()=>{ await eventRepository.createEvent(TEST_EVENT); From 020b3c0e70418d22a2bd773942b05d8303c792c8 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 13:38:56 +0200 Subject: [PATCH 05/13] added e2e test for getAllActiveEvents endpoint --- apps/api-e2e/src/api/api.spec.ts | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 992c0d16..0703d829 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -787,6 +787,45 @@ describe('EventController', ()=>{ }) }) + describe('getActiveEvents', ()=>{ + it('Should return an array of events', async ()=>{ + //create event manager and event + await userRepository.createUser(TEST_USER_1); + const manager = await userRepository.getUser(process.env['TEST_USER_EMAIL_1']); + TEST_EVENT.Manager = manager[0]._id; + TEST_EVENT_2.Manager = manager[0]._id; + + //set details to current time + TEST_EVENT.StartDate = new Date(); + TEST_EVENT.EndDate = new Date(); + TEST_EVENT.EndDate.setHours(TEST_EVENT.EndDate.getHours() + 2); + + //create events + await eventRepository.createEvent(TEST_EVENT); + await eventRepository.createEvent(TEST_EVENT_2); + const event1 = await eventRepository.getEventByName(TEST_EVENT.Name); + const event2= await eventRepository.getEventByName(TEST_EVENT_2.Name); + + let events = await eventRepository.getAllEvents(); + while(events.length != 2){ + SLEEP(500); + events = await eventRepository.getAllEvents(); + } + + const response = await request(httpServer).get('/event/getAllActiveEvents'); + + //should only contain event1 + expect(response.status).toBe(200); + const res = objectSubset(TEST_EVENT,response.body.events); + expect(res).toBe(true); + + //cleanup + await userRepository.deleteUserById(manager[0]._id); + await eventRepository.deleteEventbyId(event1[0]._id); + await eventRepository.deleteEventbyId(event2[0]._id); + }) + }) + }) describe('UserController', ()=>{ From 0bf5c79bd7f06815c738258c3d02c03a77ba6354 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 13:39:50 +0200 Subject: [PATCH 06/13] updated objectSubset helper method --- apps/api-e2e/src/api/api.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 0703d829..93d659b6 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -89,11 +89,9 @@ function objectSubset(target: any, obj: any ): boolean{ // eslint-disable-next-line no-prototype-builtins if(target.hasOwnProperty(key) && element.hasOwnProperty(key)){ if( element.key != target.key){ - console.log(key); return false; } }else{ - console.log(key); return false; } } From 1f82f4574748be00bcc321bf36d806bce8d5e623 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 13:49:24 +0200 Subject: [PATCH 07/13] added event image for e2e tests --- apps/api-e2e/src/api/api.spec.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 93d659b6..2cf5c731 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -7,7 +7,7 @@ import { CsrfGuard, JwtGuard, RbacGuard } from '@event-participation-trends/api/ import { EventRepository } from '@event-participation-trends/api/event/data-access'; import { UserRepository } from '@event-participation-trends/api/user/data-access'; import { GlobalRepository } from '@event-participation-trends/api/global/data-access'; -import { ICreateEventRequest, IEvent, IFloorLayout, IPosition, IViewEvent, Position } from '@event-participation-trends/api/event/util'; +import { ICreateEventRequest, IEvent, IFloorLayout, IImageUploadRequest, IPosition, IViewEvent, Position } from '@event-participation-trends/api/event/util'; import { IUser, Role } from '@event-participation-trends/api/user/util'; import { ICreateGlobalRequest, IGlobal } from '@event-participation-trends/api/global/util'; import { promisify } from 'util'; @@ -77,6 +77,14 @@ const TEST_GLOBAL: IGlobal ={ }] } +const EVENT_IMAGE: IImageUploadRequest ={ + eventId: "", + imgBase64: "data:image/jpeg;base64,BASE64_STRING", + imageObj: "{x_cord:10,y_cord:20}", + imageScale: 5, + imageType: "jpeg" +} + const SLEEP = promisify(setTimeout); //helper functions From 41f0da49e6a8cb879fdbcdc45c4e3aab32ab8f61 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 15:22:37 +0200 Subject: [PATCH 08/13] added e2e test for uploadFloorlayoutImage endpoint --- apps/api-e2e/src/api/api.spec.ts | 63 +++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 2cf5c731..19514330 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -95,7 +95,7 @@ function objectSubset(target: any, obj: any ): boolean{ // eslint-disable-next-line no-prototype-builtins if (target.hasOwnProperty(key)){ // eslint-disable-next-line no-prototype-builtins - if(target.hasOwnProperty(key) && element.hasOwnProperty(key)){ + if(key in target && key in element){ if( element.key != target.key){ return false; } @@ -820,7 +820,6 @@ describe('EventController', ()=>{ const response = await request(httpServer).get('/event/getAllActiveEvents'); - //should only contain event1 expect(response.status).toBe(200); const res = objectSubset(TEST_EVENT,response.body.events); expect(res).toBe(true); @@ -832,6 +831,66 @@ describe('EventController', ()=>{ }) }) + describe('uploadFloorlayoutImage', ()=>{ + it('Should upload the given image', async ()=>{ + //create event manager and event + await userRepository.createUser(TEST_USER_1); + const manager = await userRepository.getUser(process.env['TEST_USER_EMAIL_1']); + TEST_EVENT.Manager = manager[0]._id; + + //create event + await eventRepository.createEvent(TEST_EVENT); + let event = await eventRepository.getEventByName(TEST_EVENT.Name); + + while(event.length != 1){ + SLEEP(500); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + } + + EVENT_IMAGE.eventId = event[0]._id; + + const response = await request(httpServer).post('/event/uploadFloorlayoutImage').send( + EVENT_IMAGE + ); + expect(response.body.status).toBe("success"); + + //should create an image + let eventImg = await eventRepository.findImageByEventId(event[0]._id); + + while(event.length != 1){ + SLEEP(500); + eventImg = await eventRepository.findImageByEventId(event[0]._id); + } + + const temp: IImageUploadRequest = { + eventId: event[0]._id, + imgBase64: eventImg[0].imageBase64, + imageObj: eventImg[0].imageObj, + imageScale: eventImg[0].imageScale, + imageType: eventImg[0].imageType, + } + + const res = objectSubset(EVENT_IMAGE,[temp]); + expect(res).toBe(true); + + //should add imageid to the event's FloorLayoutImgs array + while(event[0].FloorLayoutImgs.length != 1){ + SLEEP(500); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + } + + if(event[0].FloorLayoutImgs[0].equals(eventImg[0]._id)){ + expect(true).toBe(true); + }else{ + expect(false).toBe(true); + } + + //cleanup + await userRepository.deleteUserById(manager[0]._id); + await eventRepository.deleteEventbyId(event[0]._id); + await eventRepository.removeImage(eventImg[0]._id); + }) + }) }) describe('UserController', ()=>{ From 352b36919419de967d0c1d2cddbce9e45598fc30 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 15:57:21 +0200 Subject: [PATCH 09/13] added e2e test for getFloorLayoutImage endpoint --- apps/api-e2e/src/api/api.spec.ts | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 19514330..9dec1c37 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -12,6 +12,8 @@ import { IUser, Role } from '@event-participation-trends/api/user/util'; import { ICreateGlobalRequest, IGlobal } from '@event-participation-trends/api/global/util'; import { promisify } from 'util'; import { UpdateEventDetails } from '@event-participation-trends/api/event/feature'; +import { Image } from "@event-participation-trends/api/event/feature"; + //constants // eslint-disable-next-line prefer-const @@ -891,6 +893,60 @@ describe('EventController', ()=>{ await eventRepository.removeImage(eventImg[0]._id); }) }) + + describe('getFloorLayoutImage', ()=>{ + it('Should return the given image', async ()=>{ + //create event manager and event + await userRepository.createUser(TEST_USER_1); + const manager = await userRepository.getUser(process.env['TEST_USER_EMAIL_1']); + TEST_EVENT.Manager = manager[0]._id; + + //create event + await eventRepository.createEvent(TEST_EVENT); + let event = await eventRepository.getEventByName(TEST_EVENT.Name); + + while(event.length != 1){ + SLEEP(500); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + } + + EVENT_IMAGE.eventId = event[0]._id; + + await eventRepository.uploadImage(new Image( + EVENT_IMAGE.eventId, + EVENT_IMAGE.imgBase64, + EVENT_IMAGE.imageScale, + EVENT_IMAGE.imageType, + EVENT_IMAGE.imageObj + )); + + let eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + while(eventImg.length != 1){ + SLEEP(500); + eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + } + + const response = await request(httpServer).get(`/event/getFloorLayoutImage?eventId=${event[0]._id}`); + + const temp: IImageUploadRequest = { + eventId: response.body.images[0]._id, + imgBase64: response.body.images[0].imageBase64, + imageObj: response.body.images[0].imageObj, + imageScale: response.body.images[0].imageScale, + imageType: response.body.images[0].imageType, + } + + expect(response.status).toBe(200); + const res = objectSubset(EVENT_IMAGE,[temp]); + expect(res).toBe(true); + + //cleanup + await userRepository.deleteUserById(manager[0]._id); + await eventRepository.deleteEventbyId(event[0]._id); + await eventRepository.removeImage(eventImg[0]._id); + }) + }) + }) describe('UserController', ()=>{ From 27b8d282bb41e2baad036f5a9a21ee8cc2b3323c Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 15:59:13 +0200 Subject: [PATCH 10/13] added updated event image for e2e tests --- apps/api-e2e/src/api/api.spec.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 9dec1c37..9a6f4d41 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -87,6 +87,14 @@ const EVENT_IMAGE: IImageUploadRequest ={ imageType: "jpeg" } +const UPDATED_EVENT_IMAGE: IImageUploadRequest ={ + eventId: "", + imgBase64: "data:image/png;base64,UPDATED_BASE64_STRING", + imageObj: "{x_cord:15,y_cord:25}", + imageScale: 10, + imageType: "png" +} + const SLEEP = promisify(setTimeout); //helper functions From ae74e5272cb47cea71ac1d1d160d3bedeb074e39 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 16:40:09 +0200 Subject: [PATCH 11/13] added a e2e test for the updateEventFloorlayoutImage endpoint --- apps/api-e2e/src/api/api.spec.ts | 79 +++++++++++++++++++++++++++++++- package.json | 2 +- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 9a6f4d41..6d6bad98 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -7,7 +7,7 @@ import { CsrfGuard, JwtGuard, RbacGuard } from '@event-participation-trends/api/ import { EventRepository } from '@event-participation-trends/api/event/data-access'; import { UserRepository } from '@event-participation-trends/api/user/data-access'; import { GlobalRepository } from '@event-participation-trends/api/global/data-access'; -import { ICreateEventRequest, IEvent, IFloorLayout, IImageUploadRequest, IPosition, IViewEvent, Position } from '@event-participation-trends/api/event/util'; +import { ICreateEventRequest, IEvent, IFloorLayout, IImageUploadRequest, IPosition, IUpdateEventFloorLayoutImgRequest, IViewEvent, Position } from '@event-participation-trends/api/event/util'; import { IUser, Role } from '@event-participation-trends/api/user/util'; import { ICreateGlobalRequest, IGlobal } from '@event-participation-trends/api/global/util'; import { promisify } from 'util'; @@ -87,8 +87,10 @@ const EVENT_IMAGE: IImageUploadRequest ={ imageType: "jpeg" } -const UPDATED_EVENT_IMAGE: IImageUploadRequest ={ +const UPDATED_EVENT_IMAGE: IUpdateEventFloorLayoutImgRequest ={ eventId: "", + imageId: "", + managerEmail: "", imgBase64: "data:image/png;base64,UPDATED_BASE64_STRING", imageObj: "{x_cord:15,y_cord:25}", imageScale: 10, @@ -955,6 +957,79 @@ describe('EventController', ()=>{ }) }) + describe('updateEventFloorlayoutImage', ()=>{ + it('Should update the given image', async ()=>{ + //create event manager and event + await userRepository.createUser(TEST_USER_1); + const manager = await userRepository.getUser(process.env['TEST_USER_EMAIL_1']); + TEST_EVENT.Manager = manager[0]._id; + + //create event + await eventRepository.createEvent(TEST_EVENT); + + let event = await eventRepository.getEventByName(TEST_EVENT.Name); + while(event.length != 1){ + SLEEP(500); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + } + + EVENT_IMAGE.eventId = event[0]._id; + + await eventRepository.uploadImage(new Image( + EVENT_IMAGE.eventId, + EVENT_IMAGE.imgBase64, + EVENT_IMAGE.imageScale, + EVENT_IMAGE.imageType, + EVENT_IMAGE.imageObj + )); + + let eventImg = await eventRepository.findImageByEventId(event[0]._id); + while(eventImg.length != 1){ + SLEEP(500); + eventImg = await eventRepository.findImageByEventId(event[0]._id); + } + + UPDATED_EVENT_IMAGE.eventId = event[0]._id; + UPDATED_EVENT_IMAGE.imageId = eventImg[0]._id; + UPDATED_EVENT_IMAGE.managerEmail = process.env['TEST_USER_EMAIL_1']; + + await eventRepository.addImageToEvent(event[0]._id,eventImg[0]._id); + + const response = await request(httpServer).post('/event/updateEventFloorlayoutImage').send( + UPDATED_EVENT_IMAGE + ); + + eventImg = await eventRepository.findImageByEventId(event[0]._id); + //imageType is last to update ref: event handler + console.log(eventImg[0].imageType); + console.log(UPDATED_EVENT_IMAGE.imageType); + while(eventImg[0].imageType != UPDATED_EVENT_IMAGE.imageType ){ + SLEEP(500); + eventImg = await eventRepository.findImageByEventId(event[0]._id); + } + + expect(response.body.status).toBe("success"); + + const temp: IUpdateEventFloorLayoutImgRequest = { + eventId: event[0]._id, + imageId: eventImg[0]._id, + managerEmail: process.env['TEST_USER_EMAIL_1'], + imgBase64: eventImg[0].imageBase64, + imageObj: eventImg[0].imageObj, + imageScale: eventImg[0].imageScale, + imageType: eventImg[0].imageType, + } + + const res = objectSubset(UPDATED_EVENT_IMAGE,[temp]); + expect(res).toBe(true); + + //cleanup + await userRepository.deleteUserById(manager[0]._id); + await eventRepository.deleteEventbyId(event[0]._id); + await eventRepository.removeImage(eventImg[0]._id); + }) + }) + }) describe('UserController', ()=>{ diff --git a/package.json b/package.json index 0ebe85e6..88a79b2e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "compile": "cd rust-lib && wasm-pack build --target nodejs && cd .. && yarn install --force", "start:dev": "yarn concurrently \"yarn nx serve app\" \"yarn nx serve api\"", "test": "jest --testTimeout=10000", - "test:integration": "jest --testTimeout=10000 --detectOpenHandles apps/api-e2e/src/api/api.spec.ts" + "test:integration": "jest --testTimeout=15000 --detectOpenHandles apps/api-e2e/src/api/api.spec.ts" }, "private": true, "dependencies": { From 11f8c5bfba3a51a326ec68bd84ee291ef312e1cb Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 21:28:48 +0200 Subject: [PATCH 12/13] added e2e test for removeFloorlayoutImage endpoint --- apps/api-e2e/src/api/api.spec.ts | 93 ++++++++++++++++++- .../src/events/add-viewing-event.handler.ts | 2 +- package.json | 2 +- 3 files changed, 93 insertions(+), 4 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index 6d6bad98..e8c47e04 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -97,6 +97,27 @@ const UPDATED_EVENT_IMAGE: IUpdateEventFloorLayoutImgRequest ={ imageType: "png" } +const ITETATION_LIMIT = 21; + +/* 20 iterations of the loop will take 10 seconds + * since SLEEP(500) used in surounding context + */ +function SRATIC_CATCH_ITERATION_EXCEEDED(reset: boolean): any{ + let count =0; + + if(reset) + count = 0; + + return ()=>{ + ++count; + if(count > ITETATION_LIMIT) + throw new Error(`Loop iteration limit of ${ITETATION_LIMIT} iterations exceeded`); + } + +} + +const CATCH_ITERATION_EXCEEDED = SRATIC_CATCH_ITERATION_EXCEEDED(true); + const SLEEP = promisify(setTimeout); //helper functions @@ -1001,8 +1022,6 @@ describe('EventController', ()=>{ eventImg = await eventRepository.findImageByEventId(event[0]._id); //imageType is last to update ref: event handler - console.log(eventImg[0].imageType); - console.log(UPDATED_EVENT_IMAGE.imageType); while(eventImg[0].imageType != UPDATED_EVENT_IMAGE.imageType ){ SLEEP(500); eventImg = await eventRepository.findImageByEventId(event[0]._id); @@ -1030,6 +1049,76 @@ describe('EventController', ()=>{ }) }) + describe('removeFloorlayoutImage', ()=>{ + it('Should remove the given image', async ()=>{ + //create event manager and event + await userRepository.createUser(TEST_USER_1); + const manager = await userRepository.getUser(process.env['TEST_USER_EMAIL_1']); + TEST_EVENT.Manager = manager[0]._id; + + //create event + await eventRepository.createEvent(TEST_EVENT); + let event = await eventRepository.getEventByName(TEST_EVENT.Name); + + CATCH_ITERATION_EXCEEDED(true); + while(event.length != 1){ + SLEEP(500); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); + } + + EVENT_IMAGE.eventId = event[0]._id; + + await eventRepository.uploadImage(new Image( + EVENT_IMAGE.eventId, + EVENT_IMAGE.imgBase64, + EVENT_IMAGE.imageScale, + EVENT_IMAGE.imageType, + EVENT_IMAGE.imageObj + )); + + let eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(true); + while(eventImg.length != 1){ // + SLEEP(500); + eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(false); + } + + //delete the image + const response = await request(httpServer).post('/event/removeFloorlayoutImage').send({ + eventId: event[0]._id, + imageId: eventImg[0]._id + }); + expect(response.body.status).toBe("success"); + + //endure that it is deleted + eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(true); + while(eventImg.length != 0){ + SLEEP(500); + eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(false); + } + + //ensure it is removed from the FloorLayoutImgs array + CATCH_ITERATION_EXCEEDED(true); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + while(event[0].FloorLayoutImgs.length != 0){ + SLEEP(500); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); + } + + //if it reaches this point it will have passed + expect(true).toBe(true); + + //cleanups + await userRepository.deleteUserById(manager[0]._id); + await eventRepository.deleteEventbyId(event[0]._id); + }) + }) + }) describe('UserController', ()=>{ diff --git a/libs/api/user/feature/src/events/add-viewing-event.handler.ts b/libs/api/user/feature/src/events/add-viewing-event.handler.ts index 805d54e9..3f486c42 100644 --- a/libs/api/user/feature/src/events/add-viewing-event.handler.ts +++ b/libs/api/user/feature/src/events/add-viewing-event.handler.ts @@ -12,7 +12,7 @@ export class AddViewingEventEventHandler implements IEventHandler event.request.eventId; await this.repository.addViewingEvent(userIDObj,eventIDObj); diff --git a/package.json b/package.json index 88a79b2e..0ebe85e6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "compile": "cd rust-lib && wasm-pack build --target nodejs && cd .. && yarn install --force", "start:dev": "yarn concurrently \"yarn nx serve app\" \"yarn nx serve api\"", "test": "jest --testTimeout=10000", - "test:integration": "jest --testTimeout=15000 --detectOpenHandles apps/api-e2e/src/api/api.spec.ts" + "test:integration": "jest --testTimeout=10000 --detectOpenHandles apps/api-e2e/src/api/api.spec.ts" }, "private": true, "dependencies": { From bbab4c3d78c18838827e3e55b50df80784e13e74 Mon Sep 17 00:00:00 2001 From: LucaLoubser Date: Thu, 21 Sep 2023 21:43:11 +0200 Subject: [PATCH 13/13] added error checking to limit all loop iterations --- apps/api-e2e/src/api/api.spec.ts | 56 ++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/apps/api-e2e/src/api/api.spec.ts b/apps/api-e2e/src/api/api.spec.ts index e8c47e04..98365fd4 100644 --- a/apps/api-e2e/src/api/api.spec.ts +++ b/apps/api-e2e/src/api/api.spec.ts @@ -105,10 +105,10 @@ const ITETATION_LIMIT = 21; function SRATIC_CATCH_ITERATION_EXCEEDED(reset: boolean): any{ let count =0; - if(reset) - count = 0; - return ()=>{ + if(reset) + count = 0; + ++count; if(count > ITETATION_LIMIT) throw new Error(`Loop iteration limit of ${ITETATION_LIMIT} iterations exceeded`); @@ -204,9 +204,11 @@ describe('GlobalController', ()=>{ //due to delayed persistance wait let global = await globalRepository.getGlobal(); + CATCH_ITERATION_EXCEEDED(true); while(global.length == 0){ - global = await globalRepository.getGlobal(); await SLEEP(500); + global = await globalRepository.getGlobal(); + CATCH_ITERATION_EXCEEDED(false); } const res = objectSubset(expectedRequest.sensorIdToMacs[0],[global[0].SensorIdToMacs[0]]); @@ -464,9 +466,11 @@ describe('EventController', ()=>{ //due to delayed persistance wait let event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event.length == 0){ - event = await eventRepository.getEventByName(TEST_EVENT.Name); await SLEEP(50); + event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } //due to async have to unwrap @@ -511,9 +515,11 @@ describe('EventController', ()=>{ expect(response.status).toBe(201); let events = await eventRepository.getAllEvents(); + CATCH_ITERATION_EXCEEDED(true); while(events.length != 1){ SLEEP(500); events = await eventRepository.getAllEvents(); + CATCH_ITERATION_EXCEEDED(false); } if(events && events.length ==1){ @@ -555,9 +561,11 @@ describe('EventController', ()=>{ expect(response.body.status).toBe("success"); event = await eventRepository.getEventByName(UPDATED_TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event.length == 0){ - event = await eventRepository.getEventByName(UPDATED_TEST_EVENT.Name); SLEEP(50); + event = await eventRepository.getEventByName(UPDATED_TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } const temp: IEvent = { @@ -596,9 +604,11 @@ describe('EventController', ()=>{ event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); if(event[0].FloorLayout != "New FloorLayout"){ - await SLEEP(1000); event = await eventRepository.getEventByName(TEST_EVENT.Name); + await SLEEP(1000); + CATCH_ITERATION_EXCEEDED(false); } expect(event[0].FloorLayout).toBe("New FloorLayout"); @@ -656,9 +666,11 @@ describe('EventController', ()=>{ expect(response.body.status).toBe("success"); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); if(event[0].Requesters.length == 0){ SLEEP(1000); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } const requesters = await eventRepository.getRequesters(event[0]._id); @@ -699,9 +711,11 @@ describe('EventController', ()=>{ expect(response.body.status).toBe("success"); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event[0].Requesters.length != 0){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } expect(event[0].Requesters.length).toEqual(0); @@ -741,23 +755,29 @@ describe('EventController', ()=>{ //due to delayed persistance must wait event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event[0].Requesters.length != 0){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } expect(event[0].Requesters.length).toEqual(0); + CATCH_ITERATION_EXCEEDED(true); while(event[0].Viewers.length != 2){ SLEEP(1000); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } expect(event[0].Viewers[1]).toEqual(viewer[0]._id); + CATCH_ITERATION_EXCEEDED(true); while(viewer[0].Viewing.length == 0){ SLEEP(1000); viewer = await userRepository.getUser(TEST_USER_2.Email); + CATCH_ITERATION_EXCEEDED(false); } expect(viewer[0].Viewing[0]).toEqual(event[0]._id); @@ -799,18 +819,22 @@ describe('EventController', ()=>{ //due to delayed persistance must wait event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event[0].Viewers.length != 1){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } expect(event[0].Viewers.length).toEqual(1); expect(event[0].Viewers[0]).toEqual(manager[0]._id); viewer = await userRepository.getUser(TEST_USER_2.Email); + CATCH_ITERATION_EXCEEDED(true); while(viewer[0].Viewing.length != 0){ SLEEP(500); viewer = await userRepository.getUser(TEST_USER_2.Email); + CATCH_ITERATION_EXCEEDED(false); } expect(viewer[0].Viewing.length).toEqual(0); @@ -846,9 +870,11 @@ describe('EventController', ()=>{ const event2= await eventRepository.getEventByName(TEST_EVENT_2.Name); let events = await eventRepository.getAllEvents(); + CATCH_ITERATION_EXCEEDED(true); while(events.length != 2){ SLEEP(500); events = await eventRepository.getAllEvents(); + CATCH_ITERATION_EXCEEDED(false); } const response = await request(httpServer).get('/event/getAllActiveEvents'); @@ -875,9 +901,11 @@ describe('EventController', ()=>{ await eventRepository.createEvent(TEST_EVENT); let event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event.length != 1){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } EVENT_IMAGE.eventId = event[0]._id; @@ -890,9 +918,11 @@ describe('EventController', ()=>{ //should create an image let eventImg = await eventRepository.findImageByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(true); while(event.length != 1){ SLEEP(500); eventImg = await eventRepository.findImageByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(false); } const temp: IImageUploadRequest = { @@ -907,9 +937,11 @@ describe('EventController', ()=>{ expect(res).toBe(true); //should add imageid to the event's FloorLayoutImgs array + CATCH_ITERATION_EXCEEDED(true); while(event[0].FloorLayoutImgs.length != 1){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } if(event[0].FloorLayoutImgs[0].equals(eventImg[0]._id)){ @@ -936,9 +968,11 @@ describe('EventController', ()=>{ await eventRepository.createEvent(TEST_EVENT); let event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event.length != 1){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } EVENT_IMAGE.eventId = event[0]._id; @@ -952,9 +986,11 @@ describe('EventController', ()=>{ )); let eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(true); while(eventImg.length != 1){ SLEEP(500); eventImg = await eventRepository.findImagesIdByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(false); } const response = await request(httpServer).get(`/event/getFloorLayoutImage?eventId=${event[0]._id}`); @@ -989,9 +1025,11 @@ describe('EventController', ()=>{ await eventRepository.createEvent(TEST_EVENT); let event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(true); while(event.length != 1){ SLEEP(500); event = await eventRepository.getEventByName(TEST_EVENT.Name); + CATCH_ITERATION_EXCEEDED(false); } EVENT_IMAGE.eventId = event[0]._id; @@ -1005,9 +1043,11 @@ describe('EventController', ()=>{ )); let eventImg = await eventRepository.findImageByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(true); while(eventImg.length != 1){ SLEEP(500); eventImg = await eventRepository.findImageByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(false); } UPDATED_EVENT_IMAGE.eventId = event[0]._id; @@ -1022,9 +1062,11 @@ describe('EventController', ()=>{ eventImg = await eventRepository.findImageByEventId(event[0]._id); //imageType is last to update ref: event handler + CATCH_ITERATION_EXCEEDED(true); while(eventImg[0].imageType != UPDATED_EVENT_IMAGE.imageType ){ SLEEP(500); eventImg = await eventRepository.findImageByEventId(event[0]._id); + CATCH_ITERATION_EXCEEDED(false); } expect(response.body.status).toBe("success");