diff --git a/.env.example b/.env.example index 9fb523a..97463ce 100644 --- a/.env.example +++ b/.env.example @@ -10,4 +10,5 @@ SUPPORT_NETWORK_ENDPOINT=/endpoint/path/here CASE_ENDPOINT=/endpoint/path/here INCIDENT_ENDPOINT=/endpoint/path/here SR_ENDPOINT=/endpoint/path/here -MEMO_ENDPOINT=/endpoint/path/here \ No newline at end of file +MEMO_ENDPOINT=/endpoint/path/here +SKIP_AUTH_GUARD=false \ No newline at end of file diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml index bb89368..a134fde 100644 --- a/helm/templates/deployment.yaml +++ b/helm/templates/deployment.yaml @@ -77,4 +77,9 @@ spec: secretKeyRef: name: visitz-api key: CLIENT_SECRET + - name: SKIP_AUTH_GUARD + valueFrom: + secretKeyRef: + name: visitz-api + key: SKIP_AUTH_GUARD restartPolicy: Always diff --git a/src/common/guards/auth/auth.guard.spec.ts b/src/common/guards/auth/auth.guard.spec.ts index 1023eb7..1d0cb61 100644 --- a/src/common/guards/auth/auth.guard.spec.ts +++ b/src/common/guards/auth/auth.guard.spec.ts @@ -43,7 +43,7 @@ describe('AuthGuard', () => { useValue: { get: jest.fn((key: string) => { const lookup = { - NODE_ENV: 'test', + skipAuthGuard: true, }; return lookup[key]; }), @@ -69,18 +69,23 @@ describe('AuthGuard', () => { }); describe('canActivate tests', () => { - it('should always return true in non-production environment', async () => { + it('should always return true when skipping', async () => { const authSpy = jest .spyOn(service, 'getRecordAndValidate') .mockResolvedValueOnce(false); const guardSpy = jest.spyOn(AuthGuard.prototype, 'canActivate'); - const isAuthed = await guard.canActivate({} as ExecutionContext); + const execContext = { + switchToHttp: () => ({ + getRequest: () => getMockReq(), + }), + }; + const isAuthed = await guard.canActivate(execContext as ExecutionContext); expect(authSpy).toHaveBeenCalledTimes(0); expect(guardSpy).toHaveBeenCalledTimes(1); expect(isAuthed).toBe(true); }); - it('should return the result of getRecordAndValidate in a production environment', async () => { + it('should return the result of getRecordAndValidate when not skipping', async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ AuthService, @@ -92,7 +97,7 @@ describe('AuthGuard', () => { useValue: { get: jest.fn((key: string) => { const lookup = { - NODE_ENV: 'production', + skipAuthGuard: false, }; return lookup[key]; }), @@ -119,7 +124,6 @@ describe('AuthGuard', () => { getRequest: () => getMockReq(), }), }; - const isAuthed = await guard.canActivate(execContext); expect(authSpy).toHaveBeenCalledTimes(1); expect(guardSpy).toHaveBeenCalledTimes(1); diff --git a/src/common/guards/auth/auth.guard.ts b/src/common/guards/auth/auth.guard.ts index 174b652..b9ec944 100644 --- a/src/common/guards/auth/auth.guard.ts +++ b/src/common/guards/auth/auth.guard.ts @@ -5,18 +5,18 @@ import { AuthService } from './auth.service'; @Injectable() export class AuthGuard implements CanActivate { - environment; + skip; constructor( private readonly authService: AuthService, private readonly configService: ConfigService, ) { - this.environment = this.configService.get('NODE_ENV'); + this.skip = this.configService.get('skipAuthGuard'); } canActivate( context: ExecutionContext, ): boolean | Promise | Observable { - if (this.environment !== 'production') { + if (this.skip) { // skip for local development return true; } diff --git a/src/configuration/configuration.ts b/src/configuration/configuration.ts index 144a0d0..22d6b6e 100644 --- a/src/configuration/configuration.ts +++ b/src/configuration/configuration.ts @@ -27,4 +27,5 @@ export default () => ({ clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, }, + skipAuthGuard: process.env.SKIP_AUTH_GUARD === 'true', });