|
| 1 | +// swiftlint:disable:this file_name |
| 2 | + |
| 3 | +import AuthenticationServices |
| 4 | +import BitwardenKit |
| 5 | +import BitwardenKitMocks |
| 6 | +import BitwardenSdk |
| 7 | +import TestHelpers |
| 8 | +import XCTest |
| 9 | + |
| 10 | +@testable import BitwardenShared |
| 11 | + |
| 12 | +/// The tests for `DefaultAutofillCredentialService` when the app context is `.appExtension`. |
| 13 | +/// This new file is needed given that the app context is necesary on `DefaultAutofillCredentialService` |
| 14 | +/// initialization to see if the subscription to the `VaultTimeoutService` is necessary. So it's easier to test |
| 15 | +/// having a new class test specifically for it. |
| 16 | +@MainActor |
| 17 | +class AutofillCredentialServiceAppExtensionTests: BitwardenTestCase { |
| 18 | + // MARK: Properties |
| 19 | + |
| 20 | + var appContextHelper: MockAppContextHelper! |
| 21 | + var autofillCredentialServiceDelegate: MockAutofillCredentialServiceDelegate! |
| 22 | + var cipherService: MockCipherService! |
| 23 | + var clientService: MockClientService! |
| 24 | + var credentialIdentityFactory: MockCredentialIdentityFactory! |
| 25 | + var errorReporter: MockErrorReporter! |
| 26 | + var eventService: MockEventService! |
| 27 | + var fido2UserInterfaceHelperDelegate: MockFido2UserInterfaceHelperDelegate! |
| 28 | + var fido2CredentialStore: MockFido2CredentialStore! |
| 29 | + var fido2UserInterfaceHelper: MockFido2UserInterfaceHelper! |
| 30 | + var identityStore: MockCredentialIdentityStore! |
| 31 | + var pasteboardService: MockPasteboardService! |
| 32 | + var stateService: MockStateService! |
| 33 | + var timeProvider: MockTimeProvider! |
| 34 | + var totpService: MockTOTPService! |
| 35 | + var subject: DefaultAutofillCredentialService! |
| 36 | + var vaultTimeoutService: MockVaultTimeoutService! |
| 37 | + |
| 38 | + // MARK: Setup & Teardown |
| 39 | + |
| 40 | + override func setUp() { |
| 41 | + super.setUp() |
| 42 | + |
| 43 | + appContextHelper = MockAppContextHelper() |
| 44 | + appContextHelper.appContext = .appExtension |
| 45 | + |
| 46 | + autofillCredentialServiceDelegate = MockAutofillCredentialServiceDelegate() |
| 47 | + cipherService = MockCipherService() |
| 48 | + clientService = MockClientService() |
| 49 | + credentialIdentityFactory = MockCredentialIdentityFactory() |
| 50 | + errorReporter = MockErrorReporter() |
| 51 | + eventService = MockEventService() |
| 52 | + fido2UserInterfaceHelperDelegate = MockFido2UserInterfaceHelperDelegate() |
| 53 | + fido2CredentialStore = MockFido2CredentialStore() |
| 54 | + fido2UserInterfaceHelper = MockFido2UserInterfaceHelper() |
| 55 | + identityStore = MockCredentialIdentityStore() |
| 56 | + pasteboardService = MockPasteboardService() |
| 57 | + stateService = MockStateService() |
| 58 | + timeProvider = MockTimeProvider(.currentTime) |
| 59 | + totpService = MockTOTPService() |
| 60 | + vaultTimeoutService = MockVaultTimeoutService() |
| 61 | + |
| 62 | + subject = DefaultAutofillCredentialService( |
| 63 | + appContextHelper: appContextHelper, |
| 64 | + cipherService: cipherService, |
| 65 | + clientService: clientService, |
| 66 | + credentialIdentityFactory: credentialIdentityFactory, |
| 67 | + errorReporter: errorReporter, |
| 68 | + eventService: eventService, |
| 69 | + fido2CredentialStore: fido2CredentialStore, |
| 70 | + fido2UserInterfaceHelper: fido2UserInterfaceHelper, |
| 71 | + identityStore: identityStore, |
| 72 | + pasteboardService: pasteboardService, |
| 73 | + stateService: stateService, |
| 74 | + timeProvider: timeProvider, |
| 75 | + totpService: totpService, |
| 76 | + vaultTimeoutService: vaultTimeoutService, |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + override func tearDown() async throws { |
| 81 | + try await super.tearDown() |
| 82 | + |
| 83 | + appContextHelper = nil |
| 84 | + autofillCredentialServiceDelegate = nil |
| 85 | + cipherService = nil |
| 86 | + clientService = nil |
| 87 | + credentialIdentityFactory = nil |
| 88 | + errorReporter = nil |
| 89 | + eventService = nil |
| 90 | + fido2UserInterfaceHelperDelegate = nil |
| 91 | + fido2CredentialStore = nil |
| 92 | + fido2UserInterfaceHelper = nil |
| 93 | + identityStore = nil |
| 94 | + pasteboardService = nil |
| 95 | + stateService = nil |
| 96 | + timeProvider = nil |
| 97 | + totpService = nil |
| 98 | + subject = nil |
| 99 | + vaultTimeoutService = nil |
| 100 | + } |
| 101 | + |
| 102 | + // MARK: Tests |
| 103 | + |
| 104 | + /// `syncIdentities(vaultLockStatus:)` doesn't update the credential identity store with the identities |
| 105 | + /// from the user's vault when the app context is `.appExtension`. |
| 106 | + func test_syncIdentities_appExtensionContext() { // swiftlint:disable:this function_body_length |
| 107 | + prepareDataForIdentitiesReplacement() |
| 108 | + |
| 109 | + vaultTimeoutService.vaultLockStatusSubject.send(VaultLockStatus(isVaultLocked: false, userId: "1")) |
| 110 | + |
| 111 | + XCTAssertFalse(cipherService.fetchAllCiphersCalled) |
| 112 | + XCTAssertFalse(credentialIdentityFactory.createCredentialIdentitiesMocker.called) |
| 113 | + XCTAssertFalse(identityStore.replaceCredentialIdentitiesCalled) |
| 114 | + XCTAssertNil(identityStore.replaceCredentialIdentitiesIdentities) |
| 115 | + } |
| 116 | + |
| 117 | + /// `updateCredentialsInStore()` replaces all identities in the identity Store correctly |
| 118 | + /// for the active user ID. |
| 119 | + func test_updateCredentialsInStore_succeedsActiveUserId() async throws { |
| 120 | + prepareDataForIdentitiesReplacement() |
| 121 | + stateService.activeAccount = .fixture(profile: .fixture(userId: "50")) |
| 122 | + |
| 123 | + await subject.updateCredentialsInStore() |
| 124 | + try await waitForAsync { [weak self] in |
| 125 | + guard let self else { return false } |
| 126 | + return identityStore.replaceCredentialIdentitiesIdentities != nil |
| 127 | + } |
| 128 | + |
| 129 | + XCTAssertEqual( |
| 130 | + identityStore.replaceCredentialIdentitiesIdentities, |
| 131 | + [ |
| 132 | + .password (PasswordCredentialIdentity(id : "1", uri : "bitwarden.com", username : "[email protected]")), |
| 133 | + .password (PasswordCredentialIdentity(id : "3", uri : "example.com", username : "[email protected]")), |
| 134 | + ], |
| 135 | + ) |
| 136 | + XCTAssertEqual(subject.lastSyncedUserId, "50") |
| 137 | + } |
| 138 | + |
| 139 | + /// `updateCredentialsInStore()` logs error when it throws. |
| 140 | + func test_updateCredentialsInStore_logOnThrow() async throws { |
| 141 | + stateService.activeAccount = nil |
| 142 | + |
| 143 | + await subject.updateCredentialsInStore() |
| 144 | + |
| 145 | + XCTAssertNil(identityStore.replaceCredentialIdentitiesIdentities) |
| 146 | + XCTAssertEqual(subject.lastSyncedUserId, nil) |
| 147 | + XCTAssertEqual(errorReporter.errors as? [StateServiceError], [.noActiveAccount]) |
| 148 | + } |
| 149 | + |
| 150 | + // MARK: Private methods |
| 151 | + |
| 152 | + /// Prepares fixture data for identities replacement tests. |
| 153 | + func prepareDataForIdentitiesReplacement() { |
| 154 | + cipherService.fetchAllCiphersResult = .success([ |
| 155 | + .fixture( |
| 156 | + id: "1", |
| 157 | + login: .fixture( |
| 158 | + password: "password123", |
| 159 | + uris: [.fixture(uri: "bitwarden.com")], |
| 160 | + |
| 161 | + ), |
| 162 | + ), |
| 163 | + .fixture(id: "2", type: .identity), |
| 164 | + .fixture( |
| 165 | + id: "3", |
| 166 | + login: .fixture( |
| 167 | + password: "123321", |
| 168 | + uris: [.fixture(uri: "example.com")], |
| 169 | + |
| 170 | + ), |
| 171 | + ), |
| 172 | + .fixture(deletedDate: .now, id: "4", type: .login), |
| 173 | + ]) |
| 174 | + |
| 175 | + credentialIdentityFactory.createCredentialIdentitiesMocker |
| 176 | + .withResult { cipher in |
| 177 | + if cipher.id == "1" { |
| 178 | + [ |
| 179 | + .password( |
| 180 | + PasswordCredentialIdentity( |
| 181 | + id: "1", |
| 182 | + uri: "bitwarden.com", |
| 183 | + |
| 184 | + ), |
| 185 | + ), |
| 186 | + ] |
| 187 | + } else if cipher.id == "3" { |
| 188 | + [ |
| 189 | + .password( |
| 190 | + PasswordCredentialIdentity( |
| 191 | + id: "3", |
| 192 | + uri: "example.com", |
| 193 | + |
| 194 | + ), |
| 195 | + ), |
| 196 | + ] |
| 197 | + } else { |
| 198 | + [] |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments