mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-09 09:51:02 +01:00
52665384cf
* Fix migration logic to create keypair for old account * Rename onSuccessfulLogin to reflect usage * Rewrite loginStrategy spec with jest-mock-ex * Rewrite tests with jest-mock-extended * Assert call order * Fix linting
116 lines
4.6 KiB
TypeScript
116 lines
4.6 KiB
TypeScript
import { mock, MockProxy } from "jest-mock-extended";
|
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
import { AppIdService } from "@bitwarden/common/abstractions/appId.service";
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
|
|
import { KeyConnectorService } from "@bitwarden/common/abstractions/keyConnector.service";
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
import { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
import { TokenService } from "@bitwarden/common/abstractions/token.service";
|
|
import { TwoFactorService } from "@bitwarden/common/abstractions/twoFactor.service";
|
|
import { UserApiLogInStrategy } from "@bitwarden/common/misc/logInStrategies/user-api-login.strategy";
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
import { UserApiLogInCredentials } from "@bitwarden/common/models/domain/log-in-credentials";
|
|
|
|
import { identityTokenResponseFactory } from "./logIn.strategy.spec";
|
|
|
|
describe("UserApiLogInStrategy", () => {
|
|
let cryptoService: MockProxy<CryptoService>;
|
|
let apiService: MockProxy<ApiService>;
|
|
let tokenService: MockProxy<TokenService>;
|
|
let appIdService: MockProxy<AppIdService>;
|
|
let platformUtilsService: MockProxy<PlatformUtilsService>;
|
|
let messagingService: MockProxy<MessagingService>;
|
|
let logService: MockProxy<LogService>;
|
|
let stateService: MockProxy<StateService>;
|
|
let twoFactorService: MockProxy<TwoFactorService>;
|
|
let keyConnectorService: MockProxy<KeyConnectorService>;
|
|
let environmentService: MockProxy<EnvironmentService>;
|
|
|
|
let apiLogInStrategy: UserApiLogInStrategy;
|
|
let credentials: UserApiLogInCredentials;
|
|
|
|
const deviceId = Utils.newGuid();
|
|
const keyConnectorUrl = "KEY_CONNECTOR_URL";
|
|
const apiClientId = "API_CLIENT_ID";
|
|
const apiClientSecret = "API_CLIENT_SECRET";
|
|
|
|
beforeEach(async () => {
|
|
cryptoService = mock<CryptoService>();
|
|
apiService = mock<ApiService>();
|
|
tokenService = mock<TokenService>();
|
|
appIdService = mock<AppIdService>();
|
|
platformUtilsService = mock<PlatformUtilsService>();
|
|
messagingService = mock<MessagingService>();
|
|
logService = mock<LogService>();
|
|
stateService = mock<StateService>();
|
|
twoFactorService = mock<TwoFactorService>();
|
|
keyConnectorService = mock<KeyConnectorService>();
|
|
environmentService = mock<EnvironmentService>();
|
|
|
|
appIdService.getAppId.mockResolvedValue(deviceId);
|
|
tokenService.getTwoFactorToken.mockResolvedValue(null);
|
|
tokenService.decodeToken.mockResolvedValue({});
|
|
|
|
apiLogInStrategy = new UserApiLogInStrategy(
|
|
cryptoService,
|
|
apiService,
|
|
tokenService,
|
|
appIdService,
|
|
platformUtilsService,
|
|
messagingService,
|
|
logService,
|
|
stateService,
|
|
twoFactorService,
|
|
environmentService,
|
|
keyConnectorService
|
|
);
|
|
|
|
credentials = new UserApiLogInCredentials(apiClientId, apiClientSecret);
|
|
});
|
|
|
|
it("sends api key credentials to the server", async () => {
|
|
apiService.postIdentityToken.mockResolvedValue(identityTokenResponseFactory());
|
|
await apiLogInStrategy.logIn(credentials);
|
|
|
|
expect(apiService.postIdentityToken).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
clientId: apiClientId,
|
|
clientSecret: apiClientSecret,
|
|
device: expect.objectContaining({
|
|
identifier: deviceId,
|
|
}),
|
|
twoFactor: expect.objectContaining({
|
|
provider: null,
|
|
token: null,
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("sets the local environment after a successful login", async () => {
|
|
apiService.postIdentityToken.mockResolvedValue(identityTokenResponseFactory());
|
|
|
|
await apiLogInStrategy.logIn(credentials);
|
|
|
|
expect(stateService.setApiKeyClientId).toHaveBeenCalledWith(apiClientId);
|
|
expect(stateService.setApiKeyClientSecret).toHaveBeenCalledWith(apiClientSecret);
|
|
expect(stateService.addAccount).toHaveBeenCalled();
|
|
});
|
|
|
|
it("gets and sets the Key Connector key from environmentUrl", async () => {
|
|
const tokenResponse = identityTokenResponseFactory();
|
|
tokenResponse.apiUseKeyConnector = true;
|
|
|
|
apiService.postIdentityToken.mockResolvedValue(tokenResponse);
|
|
environmentService.getKeyConnectorUrl.mockReturnValue(keyConnectorUrl);
|
|
|
|
await apiLogInStrategy.logIn(credentials);
|
|
|
|
expect(keyConnectorService.getAndSetKey).toHaveBeenCalledWith(keyConnectorUrl);
|
|
});
|
|
});
|