-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new feature toggle and guard to hide get audio file
- Loading branch information
1 parent
4ab85e7
commit 4563b57
Showing
8 changed files
with
127 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
AdminWebsite/AdminWebsite/ClientApp/src/app/security/audio-search.guard.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { fakeAsync, TestBed } from '@angular/core/testing'; | ||
import { AuthGuard } from './auth.guard'; | ||
import { Router } from '@angular/router'; | ||
import { MockOidcSecurityService } from '../testing/mocks/MockOidcSecurityService'; | ||
import { OidcSecurityService } from 'angular-auth-oidc-client'; | ||
import { Logger } from '../services/logger'; | ||
import { AudioSearchGuard } from './audio-search.guard'; | ||
import { LaunchDarklyService } from '../services/launch-darkly.service'; | ||
import { MockLaunchDarklyService } from '../testing/mocks/MockLaunchDarklyService'; | ||
|
||
describe('audiosearchguard', () => { | ||
let audioSearchGuard: AudioSearchGuard; | ||
let launchDarklyService; | ||
const loggerSpy = jasmine.createSpyObj<Logger>('Logger', ['error', 'debug', 'warn']); | ||
const router = { | ||
navigate: jasmine.createSpy('navigate') | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
AuthGuard, | ||
{ provide: LaunchDarklyService, useClass: MockLaunchDarklyService }, | ||
{ provide: Router, useValue: router }, | ||
{ provide: Logger, useValue: loggerSpy } | ||
] | ||
}).compileComponents(); | ||
launchDarklyService = TestBed.inject(LaunchDarklyService); | ||
audioSearchGuard = TestBed.inject(AudioSearchGuard); | ||
}); | ||
|
||
describe('when logged in with successful authentication', () => { | ||
it('canActivate should return true', () => { | ||
launchDarklyService.setAudioSearchFlag(true); | ||
audioSearchGuard.canActivate().subscribe(result => expect(result).toBeTruthy()); | ||
}); | ||
}); | ||
|
||
describe('when login failed with unsuccessful authentication', () => { | ||
it('canActivate should return false', fakeAsync(() => { | ||
launchDarklyService.setAudioSearchFlag(false); | ||
audioSearchGuard.canActivate().subscribe(result => expect(result).toBeFalsy()); | ||
})); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
AdminWebsite/AdminWebsite/ClientApp/src/app/security/audio-search.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, Router } from '@angular/router'; | ||
import { AuthenticatedResult, OidcSecurityService } from 'angular-auth-oidc-client'; | ||
import { Observable, takeUntil } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { PageUrls } from '../shared/page-url.constants'; | ||
import { Logger } from '../services/logger'; | ||
import { FeatureFlags, LaunchDarklyService } from '../services/launch-darkly.service'; | ||
|
||
@Injectable() | ||
export class AudioSearchGuard implements CanActivate { | ||
private readonly loggerPrefix = '[AudioSearchGuard] -'; | ||
constructor(private launchDarklyService: LaunchDarklyService, private router: Router, private logger: Logger) {} | ||
|
||
canActivate(): Observable<boolean> { | ||
return this.launchDarklyService | ||
.getFlag<boolean>(FeatureFlags.audioSearch) | ||
.pipe( | ||
map((result) => { | ||
if (result) { | ||
this.logger.warn(`${this.loggerPrefix} - canActivate isAuthorized: ` + result); | ||
this.router.navigate([`/${PageUrls.Login}`]); | ||
return false; | ||
} | ||
this.logger.debug(`${this.loggerPrefix} - canActivate isAuthorized: ` + result); | ||
return true; | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
AdminWebsite/AdminWebsite/ClientApp/src/app/testing/mocks/MockLaunchDarklyService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { OpenIdConfiguration, LoginResponse, AuthenticatedResult, ConfigAuthenticatedResult } from 'angular-auth-oidc-client'; | ||
import { LDFlagValue } from 'launchdarkly-js-client-sdk'; | ||
|
||
class MockLoginResponse implements LoginResponse { | ||
constructor(isAuthenticated: boolean) { | ||
this.isAuthenticated = isAuthenticated; | ||
} | ||
|
||
isAuthenticated: boolean; | ||
userData: any; | ||
accessToken: string; | ||
idToken: string; | ||
configId: string; | ||
errorMessage?: string; | ||
} | ||
export class MockLaunchDarklyService { | ||
audioSearchFlag: boolean; | ||
|
||
setAudioSearchFlag(flag: boolean) { | ||
this.audioSearchFlag = flag; | ||
} | ||
getFlag<T>(flagKey: string, defaultValue: LDFlagValue = false): T { | ||
return this.audioSearchFlag as T; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters