Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when user not found in ExperimentController #1692

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog';
import { MarkExperimentValidatorv5 } from './validators/MarkExperimentValidator.v5';
import { Log } from '../models/Log';
import { ExperimentUserValidator } from './validators/ExperimentUserValidator';
import { UserCheckMiddleware } from '../middlewares/UserCheckMiddleware';

interface IMonitoredDecisionPoint {
id: string;
Expand Down Expand Up @@ -97,6 +98,7 @@ interface IMonitoredDecisionPoint {

@JsonController('/v5/')
@UseBefore(ClientLibMiddleware)
@UseBefore(UserCheckMiddleware)
export class ExperimentClientController {
constructor(
public experimentService: ExperimentService,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<any> {
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') {
KD1712 marked this conversation as resolved.
Show resolved Hide resolved
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/controller
next();
} catch (error) {
console.log(error);
return next(error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<any> {
next();
}
}
1 change: 1 addition & 0 deletions clientlibs/js/src/ApiService/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default class ApiService {
'Session-Id': this.clientSessionId,
'Client-source': IS_BROWSER ? 'browser' : 'node',
URL: url,
'User-Id': this.userId,
},
};

Expand Down
Loading