From 4f2b4d8ef790a34e00ae6f818f08603f3bd8e6c3 Mon Sep 17 00:00:00 2001 From: Khanjan Dalwadi <17khanjan@gmail.com> Date: Tue, 25 Jun 2024 15:55:09 +0530 Subject: [PATCH 1/3] Created middleware to check for user and give 404 error if not found.Mocking middleware for testcase --- .../controllers/ExperimentClientController.ts | 2 ++ .../api/middlewares/UserCheckMiddleware.ts | 36 +++++++++++++++++++ .../ExperimentClientController.test.ts | 3 ++ .../mocks/UserCheckMiddlewareMock.ts | 7 ++++ clientlibs/js/src/ApiService/ApiService.ts | 1 + 5 files changed, 49 insertions(+) create mode 100644 backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts create mode 100644 backend/packages/Upgrade/test/unit/controllers/mocks/UserCheckMiddlewareMock.ts diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts index 9a2215f379..c3c03c4f0f 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts @@ -24,6 +24,7 @@ import flatten from 'lodash.flatten'; import { CaliperLogEnvelope } from './validators/CaliperLogEnvelope'; import { ExperimentUserValidator } from './validators/ExperimentUserValidator'; import { MetricValidator } from './validators/MetricValidator'; +import { UserCheckMiddleware } from '../middlewares/UserCheckMiddleware'; interface IExperimentAssignment { expId: string; @@ -97,6 +98,7 @@ interface IExperimentAssignment { @JsonController('/') @UseBefore(ClientLibMiddleware) +@UseBefore(UserCheckMiddleware) export class ExperimentClientController { constructor( public experimentService: ExperimentService, diff --git a/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts b/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts new file mode 100644 index 0000000000..94fb440760 --- /dev/null +++ b/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts @@ -0,0 +1,36 @@ +import * as express from 'express'; +import { SettingService } from '../services/SettingService'; +import { SERVER_ERROR } from 'upgrade_types'; +import { AppRequest } from '../../types'; +import { Service } from 'typedi'; +import { ExperimentUserService } from '../services/ExperimentUserService'; + +@Service() +export class UserCheckMiddleware { + constructor(public settingService: SettingService, public experimentUserService: ExperimentUserService) {} + + public async use(req: AppRequest, res: AppRequest, next: express.NextFunction): Promise { + try { + const user_id = req.get('User-Id'); + req.logger.child({ user_id }); + req.logger.debug({ message: 'User Id is:', user_id }); + console.log(user_id); + if (req.url !== '/api/init') { + const experimentUserDoc = await this.experimentUserService.getUserDoc(user_id, req.logger); + + if (!experimentUserDoc) { + const error = new Error(`User not found: ${user_id}`); + (error as any).type = SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED; + (error as any).httpCode = 404; + req.logger.error(error); + return next(error); + } + } + // Continue to the next middleware + next(); + } catch (error) { + console.log(error); + return next(error); + } + } +} diff --git a/backend/packages/Upgrade/test/unit/controllers/ExperimentClientController.test.ts b/backend/packages/Upgrade/test/unit/controllers/ExperimentClientController.test.ts index ed2d9310f2..3c92247071 100644 --- a/backend/packages/Upgrade/test/unit/controllers/ExperimentClientController.test.ts +++ b/backend/packages/Upgrade/test/unit/controllers/ExperimentClientController.test.ts @@ -8,12 +8,14 @@ import { ExperimentUserService } from '../../../src/api/services/ExperimentUserS import { FeatureFlagService } from '../../../src/api/services/FeatureFlagService'; import { MetricService } from '../../../src/api/services/MetricService'; import { ClientLibMiddleware } from '../../../src/api/middlewares/ClientLibMiddleware'; +import { UserCheckMiddleware } from '../../../src/api/middlewares/UserCheckMiddleware'; import ExperimentServiceMock from './mocks/ExperimentServiceMock'; import ExperimentAssignmentServiceMock from './mocks/ExperimentAssignmentServiceMock'; import ExperimentUserServiceMock from './mocks/ExperimentUserServiceMock'; import FeatureFlagServiceMock from './mocks/FeatureFlagServiceMock'; import MetricServiceMock from './mocks/MetricServiceMock'; import ClientLibMiddlewareMock from './mocks/ClientLibMiddlewareMock'; +import MockuserCheckMiddleware from './mocks/UserCheckMiddlewareMock'; import { useContainer as classValidatorUseContainer } from 'class-validator'; import { useContainer as ormUseContainer } from 'typeorm'; @@ -31,6 +33,7 @@ describe('Experiment Client Controller Testing', () => { Container.set(FeatureFlagService, new FeatureFlagServiceMock()); Container.set(MetricService, new MetricServiceMock()); Container.set(ClientLibMiddleware, new ClientLibMiddlewareMock()); + Container.set(UserCheckMiddleware, new MockuserCheckMiddleware()); }); afterAll(() => { diff --git a/backend/packages/Upgrade/test/unit/controllers/mocks/UserCheckMiddlewareMock.ts b/backend/packages/Upgrade/test/unit/controllers/mocks/UserCheckMiddlewareMock.ts new file mode 100644 index 0000000000..3278df6fc9 --- /dev/null +++ b/backend/packages/Upgrade/test/unit/controllers/mocks/UserCheckMiddlewareMock.ts @@ -0,0 +1,7 @@ +import * as express from 'express'; + +export default class MockUserCheckMiddleware { + public async use(req: express.Request, res: express.Response, next: express.NextFunction): Promise { + next(); + } +} diff --git a/clientlibs/js/src/ApiService/ApiService.ts b/clientlibs/js/src/ApiService/ApiService.ts index e738ead39a..48711573d4 100644 --- a/clientlibs/js/src/ApiService/ApiService.ts +++ b/clientlibs/js/src/ApiService/ApiService.ts @@ -91,6 +91,7 @@ export default class ApiService { 'Session-Id': this.clientSessionId, 'Client-source': IS_BROWSER ? 'browser' : 'node', URL: url, + 'User-Id': this.userId, }, }; From 37cd0a1a2f83b24a69fd2c3cced5cd905bdb6f5a Mon Sep 17 00:00:00 2001 From: Khanjan Dalwadi <17khanjan@gmail.com> Date: Mon, 1 Jul 2024 15:24:23 +0530 Subject: [PATCH 2/3] Shifted changes to v5 --- .../Upgrade/src/api/controllers/ExperimentClientController.ts | 2 -- .../src/api/controllers/ExperimentClientController.v5.ts | 2 ++ .../packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts index c3c03c4f0f..9a2215f379 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts @@ -24,7 +24,6 @@ import flatten from 'lodash.flatten'; import { CaliperLogEnvelope } from './validators/CaliperLogEnvelope'; import { ExperimentUserValidator } from './validators/ExperimentUserValidator'; import { MetricValidator } from './validators/MetricValidator'; -import { UserCheckMiddleware } from '../middlewares/UserCheckMiddleware'; interface IExperimentAssignment { expId: string; @@ -98,7 +97,6 @@ interface IExperimentAssignment { @JsonController('/') @UseBefore(ClientLibMiddleware) -@UseBefore(UserCheckMiddleware) export class ExperimentClientController { constructor( public experimentService: ExperimentService, diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts index 9864b24b87..36668fa702 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts @@ -38,6 +38,7 @@ import { MarkExperimentValidatorv5 } from './validators/MarkExperimentValidator. import { Log } from '../models/Log'; import { ExperimentUserValidator } from './validators/ExperimentUserValidator'; import { MetricValidator } from './validators/MetricValidator'; +import { UserCheckMiddleware } from '../middlewares/UserCheckMiddleware'; interface IMonitoredDecisionPoint { id: string; @@ -99,6 +100,7 @@ interface IMonitoredDecisionPoint { @JsonController('/v5/') @UseBefore(ClientLibMiddleware) +@UseBefore(UserCheckMiddleware) export class ExperimentClientController { constructor( public experimentService: ExperimentService, diff --git a/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts b/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts index 94fb440760..1ef8fa282c 100644 --- a/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts +++ b/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts @@ -26,7 +26,7 @@ export class UserCheckMiddleware { return next(error); } } - // Continue to the next middleware + // Continue to the next middleware/controller next(); } catch (error) { console.log(error); From ab60c0b4b5407c9f686335bb943309ff399320b5 Mon Sep 17 00:00:00 2001 From: Khanjan Dalwadi <17khanjan@gmail.com> Date: Mon, 1 Jul 2024 18:50:08 +0530 Subject: [PATCH 3/3] resolved conflict --- .../Upgrade/src/api/controllers/ExperimentClientController.v5.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts index 59266149ba..27a50d2433 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts @@ -36,7 +36,6 @@ import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog'; import { MarkExperimentValidatorv5 } from './validators/MarkExperimentValidator.v5'; import { Log } from '../models/Log'; import { ExperimentUserValidator } from './validators/ExperimentUserValidator'; -import { MetricValidator } from './validators/MetricValidator'; import { UserCheckMiddleware } from '../middlewares/UserCheckMiddleware'; interface IMonitoredDecisionPoint {