Skip to content

Commit

Permalink
changing name of variable to be
Browse files Browse the repository at this point in the history
independent of environment
  • Loading branch information
hannah-macdonald1 committed Nov 5, 2024
1 parent 766131d commit e0a0b70
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
MEMO_ENDPOINT=/endpoint/path/here
SKIP_AUTH_GUARD=false
5 changes: 5 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 10 additions & 6 deletions src/common/guards/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('AuthGuard', () => {
useValue: {
get: jest.fn((key: string) => {
const lookup = {
NODE_ENV: 'test',
skipAuthGuard: true,
};
return lookup[key];
}),
Expand All @@ -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,
Expand All @@ -92,7 +97,7 @@ describe('AuthGuard', () => {
useValue: {
get: jest.fn((key: string) => {
const lookup = {
NODE_ENV: 'production',
skipAuthGuard: false,
};
return lookup[key];
}),
Expand All @@ -119,7 +124,6 @@ describe('AuthGuard', () => {
getRequest: () => getMockReq(),
}),
};

const isAuthed = await guard.canActivate(execContext);
expect(authSpy).toHaveBeenCalledTimes(1);
expect(guardSpy).toHaveBeenCalledTimes(1);
Expand Down
6 changes: 3 additions & 3 deletions src/common/guards/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> | Observable<boolean> {
if (this.environment !== 'production') {
if (this.skip) {
// skip for local development
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export default () => ({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
},
skipAuthGuard: process.env.SKIP_AUTH_GUARD === 'true',
});

0 comments on commit e0a0b70

Please sign in to comment.