1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-18 02:19:18 +00:00
Files
browser/libs/auth/src/angular/login/default-login-component.service.spec.ts
Todd Martin 077e0f89cc [PM-17751] Store SSO email in state on web client (#13295)
* Moved saving of SSO email outside of browser/desktop code

* Clarified comments.

* Tests

* Refactored login component services to manage state

* Fixed input on login component

* Fixed tests

* Linting

* Moved web setting in state into web override

* updated tests

* Fixed typing.

* Fixed type safety issues.

* Added comments and renamed for clarity.

* Removed method parameters that weren't used

* Added clarifying comments

* Added more comments.

* Removed test that is not necessary on base

* Test cleanup

* More comments.

* Linting

* Fixed test.

* Fixed base URL

* Fixed typechecking.

* Type checking

* Moved setting of email state to default service

* Added comments.

* Consolidated SSO URL formatting

* Updated comment

* Fixed reference.

* Fixed missing parameter.

* Initialized service.

* Added comments

* Added initialization of new service

* Made email optional due to CLI.

* Fixed comment on handleSsoClick.

* Added SSO email persistence to v1 component.

---------

Co-authored-by: Bernd Schoolmann <mail@quexten.com>
2025-02-21 17:09:50 -05:00

89 lines
3.6 KiB
TypeScript

import { mock, MockProxy } from "jest-mock-extended";
import { of } from "rxjs";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { ClientType } from "@bitwarden/common/enums";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import {
EnvironmentService,
Environment,
} from "@bitwarden/common/platform/abstractions/environment.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { DefaultLoginComponentService } from "./default-login-component.service";
jest.mock("@bitwarden/common/platform/abstractions/crypto-function.service");
jest.mock("@bitwarden/common/platform/abstractions/environment.service");
jest.mock("@bitwarden/common/platform/abstractions/platform-utils.service");
jest.mock("@bitwarden/common/auth/abstractions/sso-login.service.abstraction");
jest.mock("@bitwarden/generator-legacy");
describe("DefaultLoginComponentService", () => {
let service: DefaultLoginComponentService;
let cryptoFunctionService: MockProxy<CryptoFunctionService>;
let environmentService: MockProxy<EnvironmentService>;
let platformUtilsService: MockProxy<PlatformUtilsService>;
let ssoLoginService: MockProxy<SsoLoginServiceAbstraction>;
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
beforeEach(() => {
cryptoFunctionService = mock<CryptoFunctionService>();
environmentService = mock<EnvironmentService>();
platformUtilsService = mock<PlatformUtilsService>();
ssoLoginService = mock<SsoLoginServiceAbstraction>();
passwordGenerationService = mock<PasswordGenerationServiceAbstraction>();
environmentService.environment$ = of({
getWebVaultUrl: () => "https://webvault.bitwarden.com",
getRegion: () => "US",
getUrls: () => ({}),
isCloud: () => true,
getApiUrl: () => "https://api.bitwarden.com",
} as Environment);
service = new DefaultLoginComponentService(
cryptoFunctionService,
environmentService,
passwordGenerationService,
platformUtilsService,
ssoLoginService,
);
});
it("creates without error", () => {
expect(service).toBeTruthy();
});
describe("isLoginWithPasskeySupported", () => {
it("returns true when clientType is Web", () => {
service["clientType"] = ClientType.Web;
expect(service.isLoginWithPasskeySupported()).toBe(true);
});
it("returns false when clientType is not Web", () => {
service["clientType"] = ClientType.Desktop;
expect(service.isLoginWithPasskeySupported()).toBe(false);
});
});
describe("redirectToSsoLogin", () => {
it("sets the pre-SSO state", async () => {
const email = "test@bitwarden.com";
const state = "testState";
const codeVerifier = "testCodeVerifier";
const codeChallenge = "testCodeChallenge";
passwordGenerationService.generatePassword.mockResolvedValueOnce(state);
passwordGenerationService.generatePassword.mockResolvedValueOnce(codeVerifier);
jest.spyOn(Utils, "fromBufferToUrlB64").mockReturnValue(codeChallenge);
await service.redirectToSsoLogin(email);
expect(ssoLoginService.setSsoEmail).toHaveBeenCalledWith(email);
expect(ssoLoginService.setSsoState).toHaveBeenCalledWith(state);
expect(ssoLoginService.setCodeVerifier).toHaveBeenCalledWith(codeVerifier);
});
});
});