-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into km/tmp-ownership-2
- Loading branch information
Showing
14 changed files
with
336 additions
and
35 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
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 |
---|---|---|
|
@@ -5,17 +5,48 @@ import { MockProxy, mock } from "jest-mock-extended"; | |
import { BehaviorSubject } from "rxjs"; | ||
|
||
import { EmptyComponent } from "@bitwarden/angular/platform/guard/feature-flag.guard.spec"; | ||
import { Account, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; | ||
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; | ||
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/auth/abstractions/device-trust.service.abstraction"; | ||
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; | ||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; | ||
import { UserId } from "@bitwarden/common/types/guid"; | ||
import { KeyService } from "@bitwarden/key-management"; | ||
|
||
import { unauthGuardFn } from "./unauth.guard"; | ||
|
||
describe("UnauthGuard", () => { | ||
const setup = (authStatus: AuthenticationStatus) => { | ||
const activeUser: Account = { | ||
id: "fake_user_id" as UserId, | ||
email: "[email protected]", | ||
emailVerified: true, | ||
name: "Test User", | ||
}; | ||
|
||
const setup = ( | ||
activeUser: Account | null, | ||
authStatus: AuthenticationStatus | null = null, | ||
tdeEnabled: boolean = false, | ||
everHadUserKey: boolean = false, | ||
) => { | ||
const accountService: MockProxy<AccountService> = mock<AccountService>(); | ||
const authService: MockProxy<AuthService> = mock<AuthService>(); | ||
authService.getAuthStatus.mockResolvedValue(authStatus); | ||
const activeAccountStatusObservable = new BehaviorSubject<AuthenticationStatus>(authStatus); | ||
authService.activeAccountStatus$ = activeAccountStatusObservable; | ||
const keyService: MockProxy<KeyService> = mock<KeyService>(); | ||
const deviceTrustService: MockProxy<DeviceTrustServiceAbstraction> = | ||
mock<DeviceTrustServiceAbstraction>(); | ||
const logService: MockProxy<LogService> = mock<LogService>(); | ||
|
||
accountService.activeAccount$ = new BehaviorSubject<Account | null>(activeUser); | ||
|
||
if (authStatus !== null) { | ||
const activeAccountStatusObservable = new BehaviorSubject<AuthenticationStatus>(authStatus); | ||
authService.authStatusFor$.mockReturnValue(activeAccountStatusObservable); | ||
} | ||
|
||
keyService.everHadUserKey$ = new BehaviorSubject<boolean>(everHadUserKey); | ||
deviceTrustService.supportsDeviceTrustByUserId$.mockReturnValue( | ||
new BehaviorSubject<boolean>(tdeEnabled), | ||
); | ||
|
||
const testBed = TestBed.configureTestingModule({ | ||
imports: [ | ||
|
@@ -30,6 +61,7 @@ describe("UnauthGuard", () => { | |
{ path: "lock", component: EmptyComponent }, | ||
{ path: "testhomepage", component: EmptyComponent }, | ||
{ path: "testlocked", component: EmptyComponent }, | ||
{ path: "login-initiated", component: EmptyComponent }, | ||
{ | ||
path: "testOverrides", | ||
component: EmptyComponent, | ||
|
@@ -39,7 +71,13 @@ describe("UnauthGuard", () => { | |
}, | ||
]), | ||
], | ||
providers: [{ provide: AuthService, useValue: authService }], | ||
providers: [ | ||
{ provide: AccountService, useValue: accountService }, | ||
{ provide: AuthService, useValue: authService }, | ||
{ provide: KeyService, useValue: keyService }, | ||
{ provide: DeviceTrustServiceAbstraction, useValue: deviceTrustService }, | ||
{ provide: LogService, useValue: logService }, | ||
], | ||
}); | ||
|
||
return { | ||
|
@@ -48,40 +86,54 @@ describe("UnauthGuard", () => { | |
}; | ||
|
||
it("should be created", () => { | ||
const { router } = setup(AuthenticationStatus.LoggedOut); | ||
const { router } = setup(null, AuthenticationStatus.LoggedOut); | ||
expect(router).toBeTruthy(); | ||
}); | ||
|
||
it("should redirect to /vault for guarded routes when logged in and unlocked", async () => { | ||
const { router } = setup(AuthenticationStatus.Unlocked); | ||
const { router } = setup(activeUser, AuthenticationStatus.Unlocked); | ||
|
||
await router.navigateByUrl("unauth-guarded-route"); | ||
expect(router.url).toBe("/vault"); | ||
}); | ||
|
||
it("should allow access to guarded routes when account is null", async () => { | ||
const { router } = setup(null); | ||
|
||
await router.navigateByUrl("unauth-guarded-route"); | ||
expect(router.url).toBe("/unauth-guarded-route"); | ||
}); | ||
|
||
it("should allow access to guarded routes when logged out", async () => { | ||
const { router } = setup(AuthenticationStatus.LoggedOut); | ||
const { router } = setup(null, AuthenticationStatus.LoggedOut); | ||
|
||
await router.navigateByUrl("unauth-guarded-route"); | ||
expect(router.url).toBe("/unauth-guarded-route"); | ||
}); | ||
|
||
it("should redirect to /login-initiated when locked, TDE is enabled, and the user hasn't decrypted yet", async () => { | ||
const { router } = setup(activeUser, AuthenticationStatus.Locked, true, false); | ||
|
||
await router.navigateByUrl("unauth-guarded-route"); | ||
expect(router.url).toBe("/login-initiated"); | ||
}); | ||
|
||
it("should redirect to /lock for guarded routes when locked", async () => { | ||
const { router } = setup(AuthenticationStatus.Locked); | ||
const { router } = setup(activeUser, AuthenticationStatus.Locked); | ||
|
||
await router.navigateByUrl("unauth-guarded-route"); | ||
expect(router.url).toBe("/lock"); | ||
}); | ||
|
||
it("should redirect to /testhomepage for guarded routes when testOverrides are provided and the account is unlocked", async () => { | ||
const { router } = setup(AuthenticationStatus.Unlocked); | ||
const { router } = setup(activeUser, AuthenticationStatus.Unlocked); | ||
|
||
await router.navigateByUrl("testOverrides"); | ||
expect(router.url).toBe("/testhomepage"); | ||
}); | ||
|
||
it("should redirect to /testlocked for guarded routes when testOverrides are provided and the account is locked", async () => { | ||
const { router } = setup(AuthenticationStatus.Locked); | ||
const { router } = setup(activeUser, AuthenticationStatus.Locked); | ||
|
||
await router.navigateByUrl("testOverrides"); | ||
expect(router.url).toBe("/testlocked"); | ||
|
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
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
Oops, something went wrong.