Skip to content

Commit 5f5328c

Browse files
committed
#RI-4454 - resolve pr comments
1 parent 810c061 commit 5f5328c

File tree

15 files changed

+74
-87
lines changed

15 files changed

+74
-87
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const API_PARAM_DATABASE_ID = 'dbInstance';
22
export const API_HEADER_DATABASE_INDEX = 'ri-db-index';
3-
export const API_HEADER_WINDOW_ID = 'ri-window-id';
3+
export const API_HEADER_WINDOW_ID = 'x-window-id';
44
export const API_PARAM_CLI_CLIENT_ID = 'uuid';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum CustomErrorCodes {
2+
WindowUnauthorized = 'WindowUnauthorizedException',
3+
}

redisinsight/api/src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './telemetry-events';
1010
export * from './app-events';
1111
export * from './redis-connection';
1212
export * from './recommendations';
13+
export * from './custom-error-codes';

redisinsight/api/src/modules/auth/window-auth/constants/exceptions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { HttpException, HttpStatus } from '@nestjs/common';
2+
import { CustomErrorCodes } from 'src/constants';
23

34
export class WindowUnauthorizedException extends HttpException {
45
constructor(message) {
56
super(
67
{
78
statusCode: HttpStatus.UNAUTHORIZED,
9+
errorCode: CustomErrorCodes.WindowUnauthorized,
810
message,
9-
error: 'Unauthorized',
11+
error: 'Window Unauthorized',
1012
},
1113
HttpStatus.UNAUTHORIZED,
1214
);

redisinsight/api/src/modules/auth/window-auth/middleware/window.auth.middleware.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
import {
2-
Injectable,
3-
Logger,
4-
NestMiddleware,
5-
} from '@nestjs/common';
1+
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
62
import { NextFunction, Request, Response } from 'express';
73
import ERROR_MESSAGES from 'src/constants/error-messages';
84
import { API_HEADER_WINDOW_ID } from 'src/common/constants';
9-
import { WindowAuthManager } from '../window-auth.manager';
5+
import { WindowAuthService } from '../window-auth.service';
106
import { WindowUnauthorizedException } from '../constants/exceptions';
117

128
@Injectable()
139
export class WindowAuthMiddleware implements NestMiddleware {
1410
private logger = new Logger('WindowAuthMiddleware');
1511

16-
constructor(
17-
private windowAuthService: WindowAuthManager,
18-
) {}
12+
constructor(private windowAuthService: WindowAuthService) {}
1913

2014
async use(req: Request, res: Response, next: NextFunction): Promise<any> {
2115
const { windowId } = WindowAuthMiddleware.getWindowIdFromReq(req);
22-
const { isExists: isWindowExists = false } = await this.windowAuthService.getStrategy()?.isWindowExists(windowId)
23-
?? {};
16+
const isWindowExists = (await this.windowAuthService.getStrategy()?.isWindowExists(windowId)) ?? false;
2417

2518
if (!isWindowExists) {
2619
this.throwError(req, ERROR_MESSAGES.UNDEFINED_WINDOW_ID);
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { IWindowAuthStrategy, IWindowAuthStrategyData } from '../window.auth.strategy.interface';
2-
3-
export abstract class AbstractWindowAuthStrategy implements IWindowAuthStrategy {
4-
abstract isWindowExists(data: any): Promise<IWindowAuthStrategyData>;
1+
export abstract class AbstractWindowAuthStrategy {
2+
abstract isWindowExists(data: any): Promise<boolean>;
53
}

redisinsight/api/src/modules/auth/window-auth/window-auth.manager.spec.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

redisinsight/api/src/modules/auth/window-auth/window-auth.manager.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

redisinsight/api/src/modules/auth/window-auth/window-auth.module.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
22
import config from 'src/utils/config';
3-
import { WindowAuthManager } from './window-auth.manager';
3+
import { WindowAuthService } from './window-auth.service';
44
import { WindowAuthMiddleware } from './middleware/window.auth.middleware';
55

66
const SERVER_CONFIG = config.get('server');
77

88
@Module({
9-
providers: [
10-
WindowAuthManager,
11-
],
9+
providers: [WindowAuthService],
1210
})
13-
1411
export class WindowAuthModule implements NestModule {
1512
configure(consumer: MiddlewareConsumer) {
1613
consumer
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { WindowAuthService } from './window-auth.service';
3+
import { AbstractWindowAuthStrategy } from './strategies/abstract.window.auth.strategy';
4+
5+
export class TestAuthStrategy extends AbstractWindowAuthStrategy {
6+
async isWindowExists(): Promise<boolean> {
7+
return false;
8+
}
9+
}
10+
11+
const testStrategy = new TestAuthStrategy();
12+
13+
describe('WindowAuthService', () => {
14+
let windowAuthService: WindowAuthService;
15+
16+
beforeAll(async () => {
17+
const module: TestingModule = await Test.createTestingModule({
18+
providers: [WindowAuthService],
19+
exports: [WindowAuthService],
20+
}).compile();
21+
22+
windowAuthService = module.get<WindowAuthService>(WindowAuthService);
23+
});
24+
it('Should set strategy to window auth manager and get it back', () => {
25+
windowAuthService.setStrategy(testStrategy);
26+
expect(windowAuthService.getStrategy()).toEqual(testStrategy);
27+
});
28+
});

0 commit comments

Comments
 (0)