Skip to content

Commit eb7c4e5

Browse files
authored
[PM-18284] Refactor to use CipherListView and decryptList SDK method (#1596)
1 parent 6c0209b commit eb7c4e5

File tree

64 files changed

+1548
-809
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1548
-809
lines changed

BitwardenShared/Core/Auth/Repositories/TestHelpers/MockAuthRepository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,4 @@ class MockAuthRepository: AuthRepository { // swiftlint:disable:this type_body_l
418418
verifyOtpOpt = otp
419419
try verifyOtpResult.get()
420420
}
421-
}
421+
} // swiftlint:disable:this file_length

BitwardenShared/Core/Vault/Extensions/BitwardenSdk+Vault.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,29 @@ extension BitwardenSdk.Cipher {
342342
}
343343
}
344344

345-
extension BitwardenSdk.CipherListView: @retroactive Identifiable {}
345+
extension BitwardenSdk.CipherListView: @retroactive Identifiable, Fido2UserVerifiableCipherView {}
346346

347-
extension BitwardenSdk.CipherView: @retroactive Identifiable {
347+
extension BitwardenSdk.CipherListViewType {
348+
/// Whether the type is login.
349+
var isLogin: Bool {
350+
switch self {
351+
case .login:
352+
return true
353+
default:
354+
return false
355+
}
356+
}
357+
358+
/// The `LoginListView` if this type is a `.login`, otherwise `nil`.
359+
var loginListView: BitwardenSdk.LoginListView? {
360+
guard case let .login(loginListView) = self else {
361+
return nil
362+
}
363+
return loginListView
364+
}
365+
}
366+
367+
extension BitwardenSdk.CipherView: @retroactive Identifiable, Fido2UserVerifiableCipherView {
348368
/// Initializes a new `CipherView` based on a `Fido2CredentialNewView`
349369
/// - Parameters:
350370
/// - fido2CredentialNewView: The `Fido2CredentialNewView` for the Fido2 creation flow
@@ -520,6 +540,10 @@ extension BitwardenSdk.LoginUri {
520540
}
521541
}
522542

543+
extension BitwardenSdk.LoginListView: CipherDecorativeIconDataView {}
544+
545+
extension BitwardenSdk.LoginView: CipherDecorativeIconDataView {}
546+
523547
extension BitwardenSdk.PasswordHistory {
524548
init(cipherPasswordHistoryModel model: CipherPasswordHistoryModel) {
525549
self.init(

BitwardenShared/Core/Vault/Extensions/BitwardenSdkVaultTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ class BitwardenSdkVaultCipherDetailsResponseModelTests: BitwardenTestCase { // s
7070
}
7171
}
7272

73+
// MARK: - CipherListViewType
74+
75+
class BitwardenSdkCipherListViewTypeTests: BitwardenTestCase {
76+
// MARK: Tests
77+
78+
/// `isLogin` returns whether the type is a login.
79+
func test_isLogin() {
80+
XCTAssertTrue(CipherListViewType.login(.fixture()).isLogin)
81+
XCTAssertFalse(CipherListViewType.card.isLogin)
82+
XCTAssertFalse(CipherListViewType.identity.isLogin)
83+
XCTAssertFalse(CipherListViewType.secureNote.isLogin)
84+
XCTAssertFalse(CipherListViewType.sshKey.isLogin)
85+
}
86+
87+
/// `loginListView` returns the `LoginListView` when the type is `.login`.
88+
func test_loginListView() {
89+
let expectedResult = LoginListView.fixture(fido2Credentials: [.fixture()], hasFido2: true)
90+
XCTAssertEqual(
91+
CipherListViewType.login(expectedResult).loginListView,
92+
expectedResult
93+
)
94+
XCTAssertNil(CipherListViewType.card.loginListView)
95+
XCTAssertNil(CipherListViewType.identity.loginListView)
96+
XCTAssertNil(CipherListViewType.secureNote.loginListView)
97+
XCTAssertNil(CipherListViewType.sshKey.loginListView)
98+
}
99+
}
100+
73101
// MARK: - CipherSSHKeyModel
74102

75103
class BitwardenSdkVaultCipherSSHKeyModelTests: BitwardenTestCase {

BitwardenShared/Core/Vault/Helpers/CipherMatchingHelper.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CipherMatchingHelper {
4949
/// - ciphers: The list of ciphers to filter.
5050
/// - Returns: The list of ciphers that match the URI.
5151
///
52-
func ciphersMatching(uri: String?, ciphers: [CipherView]) async -> [CipherView] {
52+
func ciphersMatching(uri: String?, ciphers: [CipherListView]) async -> [CipherListView] {
5353
guard let uri else { return [] }
5454

5555
let matchURL = URL(string: uri)
@@ -66,7 +66,7 @@ class CipherMatchingHelper {
6666
let defaultMatchType = await (try? stateService.getDefaultUriMatchType()) ?? .domain
6767

6868
let matchingCiphers = ciphers.reduce(
69-
into: (exact: [CipherView], fuzzy: [CipherView])([], [])
69+
into: (exact: [CipherListView], fuzzy: [CipherListView])([], [])
7070
) { result, cipher in
7171
let match = checkForCipherMatch(
7272
cipher: cipher,
@@ -138,15 +138,14 @@ class CipherMatchingHelper {
138138
/// - Returns: The result of the match for the cipher and URI.
139139
///
140140
private func checkForCipherMatch( // swiftlint:disable:this function_parameter_count
141-
cipher: CipherView,
141+
cipher: CipherListView,
142142
defaultMatchType: UriMatchType,
143143
isApp: Bool,
144144
matchUri: String,
145145
matchingDomains: Set<String>,
146146
matchingFuzzyDomains: Set<String>
147147
) -> MatchResult {
148-
guard cipher.type == .login,
149-
let login = cipher.login,
148+
guard let login = cipher.type.loginListView,
150149
let loginUris = login.uris,
151150
cipher.deletedDate == nil else {
152151
return .none

BitwardenShared/Core/Vault/Helpers/CipherMatchingHelperTests.swift

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import XCTest
77
class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this type_body_length
88
// MARK: Properties
99

10-
let ciphers: [CipherView] = [
10+
let ciphers: [CipherListView] = [
1111
.fixture(
1212
login: .fixture(uris: [LoginUriView.fixture(uri: "https://vault.bitwarden.com", match: .exact)]),
1313
name: "Bitwarden (Exact)"
@@ -20,14 +20,18 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
2020
login: .fixture(uris: [LoginUriView.fixture(uri: "https://vault.bitwarden.com", match: .never)]),
2121
name: "Bitwarden (Never)"
2222
),
23-
2423
.fixture(
2524
login: .fixture(uris: [LoginUriView.fixture(uri: "https://example.com", match: .startsWith)]),
2625
name: "Example (Starts With)"
2726
),
28-
29-
.fixture(login: .fixture(), name: "No URIs"),
30-
.fixture(login: .fixture(uris: []), name: "Empty URIs"),
27+
.fixture(
28+
login: .fixture(),
29+
name: "No URIs"
30+
),
31+
.fixture(
32+
login: .fixture(uris: []),
33+
name: "Empty URIs"
34+
),
3135
]
3236

3337
var settingsService: MockSettingsService!
@@ -69,7 +73,7 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
6973
("Yahoo", "http://yahoo.com"),
7074
]
7175
let ciphers = uris.map { name, uri in
72-
CipherView.fixture(
76+
CipherListView.fixture(
7377
login: .fixture(uris: [LoginUriView.fixture(uri: uri, match: .domain)]),
7478
name: name
7579
)
@@ -236,7 +240,7 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
236240
/// `ciphersMatching(uri:ciphers)` returns the list of ciphers that match the URI for the exact
237241
/// match type.
238242
func test_ciphersMatching_exact() async {
239-
let ciphers: [CipherView] = [
243+
let ciphers: [CipherListView] = [
240244
.fixture(
241245
login: .fixture(uris: [LoginUriView.fixture(uri: "https://vault.bitwarden.com", match: .exact)]),
242246
name: "Bitwarden Vault"
@@ -246,7 +250,9 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
246250
name: "Bitwarden"
247251
),
248252
.fixture(
249-
login: .fixture(uris: [LoginUriView.fixture(uri: "https://vault.bitwarden.com/login", match: .exact)]),
253+
login: .fixture(uris: [
254+
LoginUriView.fixture(uri: "https://vault.bitwarden.com/login", match: .exact),
255+
]),
250256
name: "Bitwarden Login"
251257
),
252258
.fixture(
@@ -296,7 +302,7 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
296302
("Sub Domain 500", "https://sub.domain.com:5000"),
297303
]
298304
let ciphers = uris.map { name, uri in
299-
CipherView.fixture(
305+
CipherListView.fixture(
300306
login: .fixture(uris: [LoginUriView.fixture(uri: uri, match: .host)]),
301307
name: name
302308
)
@@ -317,7 +323,7 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
317323
/// `ciphersMatching(uri:ciphers)` returns the list of ciphers that match the URI for the never
318324
/// match type.
319325
func test_ciphersMatching_never() async {
320-
let ciphers: [CipherView] = [
326+
let ciphers: [CipherListView] = [
321327
.fixture(
322328
login: .fixture(uris: [LoginUriView.fixture(uri: "https://vault.bitwarden.com", match: .never)]),
323329
name: "Bitwarden Never"
@@ -345,15 +351,13 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
345351
/// `ciphersMatching(uri:ciphers)` returns the list of ciphers that match the URI for the
346352
/// regular expression match type.
347353
func test_ciphersMatching_regularExpression() async {
348-
let cipher = CipherView.fixture(
349-
login: .fixture(
350-
uris: [
351-
LoginUriView.fixture(
352-
uri: #"^https://[a-z]+\.wikipedia\.org/w/index\.php"#,
353-
match: .regularExpression
354-
),
355-
]
356-
)
354+
let cipher = CipherListView.fixture(
355+
login: .fixture(uris: [
356+
LoginUriView.fixture(
357+
uri: #"^https://[a-z]+\.wikipedia\.org/w/index\.php"#,
358+
match: .regularExpression
359+
),
360+
])
357361
)
358362

359363
var matchingCiphers = await subject.ciphersMatching(
@@ -404,18 +408,18 @@ class CipherMatchingHelperTests: BitwardenTestCase { // swiftlint:disable:this t
404408

405409
// MARK: Private
406410

407-
/// Returns a list of `CipherView`s created with the specified name, URI and match type.
408-
func ciphersForUris(_ nameUris: [(String, String)], matchType: BitwardenSdk.UriMatchType?) -> [CipherView] {
411+
/// Returns a list of `CipherListView`s created with the specified name, URI and match type.
412+
func ciphersForUris(_ nameUris: [(String, String)], matchType: BitwardenSdk.UriMatchType?) -> [CipherListView] {
409413
nameUris.map { name, uri in
410-
CipherView.fixture(
414+
CipherListView.fixture(
411415
login: .fixture(uris: [LoginUriView.fixture(uri: uri, match: matchType)]),
412416
name: name
413417
)
414418
}
415419
}
416420

417421
/// Returns a string containing a description of the matching ciphers.
418-
func dumpMatchingCiphers(_ ciphers: [CipherView]) -> String {
422+
func dumpMatchingCiphers(_ ciphers: [CipherListView]) -> String {
419423
ciphers.map(\.name).joined(separator: "\n")
420424
}
421425
} // swiftlint:disable:this file_length

BitwardenShared/Core/Vault/Models/Domain/Fixtures/CipherListView+Fixtures.swift

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@ extension CipherListView {
88
folderId: String? = nil,
99
collectionIds: [String] = [],
1010
key: String? = nil,
11-
name: String = "Example",
12-
subtitle: String = "[email protected]",
13-
type: BitwardenSdk.CipherListViewType = .login(
14-
LoginListView(
15-
fido2Credentials: nil,
16-
hasFido2: false,
17-
username: nil,
18-
totp: nil,
19-
uris: nil
20-
)
21-
),
22-
favorite: Bool = true,
11+
name: String = "Bitwarden",
12+
subtitle: String = "",
13+
type: BitwardenSdk.CipherListViewType = .login(.fixture()),
14+
favorite: Bool = false,
2315
reprompt: BitwardenSdk.CipherRepromptType = .none,
2416
organizationUseTotp: Bool = false,
25-
edit: Bool = false,
17+
edit: Bool = true,
2618
permissions: BitwardenSdk.CipherPermissions? = nil,
2719
viewPassword: Bool = true,
2820
attachments: UInt32 = 0,
@@ -51,4 +43,46 @@ extension CipherListView {
5143
revisionDate: revisionDate
5244
)
5345
}
46+
47+
static func fixture(
48+
id: String? = "1",
49+
organizationId: String? = nil,
50+
folderId: String? = nil,
51+
collectionIds: [String] = [],
52+
key: String? = nil,
53+
login: LoginListView,
54+
name: String = "Bitwarden",
55+
subtitle: String = "",
56+
favorite: Bool = false,
57+
reprompt: BitwardenSdk.CipherRepromptType = .none,
58+
organizationUseTotp: Bool = false,
59+
edit: Bool = true,
60+
permissions: BitwardenSdk.CipherPermissions? = nil,
61+
viewPassword: Bool = true,
62+
attachments: UInt32 = 0,
63+
creationDate: Date = Date(),
64+
deletedDate: Date? = nil,
65+
revisionDate: Date = Date()
66+
) -> CipherListView {
67+
.init(
68+
id: id,
69+
organizationId: organizationId,
70+
folderId: folderId,
71+
collectionIds: collectionIds,
72+
key: key,
73+
name: name,
74+
subtitle: subtitle,
75+
type: .login(login),
76+
favorite: favorite,
77+
reprompt: reprompt,
78+
organizationUseTotp: organizationUseTotp,
79+
edit: edit,
80+
permissions: permissions,
81+
viewPassword: viewPassword,
82+
attachments: attachments,
83+
creationDate: creationDate,
84+
deletedDate: deletedDate,
85+
revisionDate: revisionDate
86+
)
87+
}
5488
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import BitwardenSdk
2+
3+
extension Fido2CredentialListView {
4+
static func fixture(
5+
credentialId: String = "1",
6+
rpId: String = "myApp.com",
7+
userHandle: String? = nil,
8+
userName: String? = nil,
9+
userDisplayName: String? = nil
10+
) -> Fido2CredentialListView {
11+
.init(
12+
credentialId: credentialId,
13+
rpId: rpId,
14+
userHandle: userHandle,
15+
userName: userName,
16+
userDisplayName: userDisplayName
17+
)
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swiftlint:disable:this file_name
2+
3+
import BitwardenSdk
4+
5+
extension BitwardenSdk.LoginListView {
6+
static func fixture(
7+
fido2Credentials: [Fido2CredentialListView]? = nil,
8+
hasFido2: Bool = false,
9+
username: String? = nil,
10+
totp: EncString? = nil,
11+
uris: [LoginUriView]? = nil
12+
) -> LoginListView {
13+
.init(
14+
fido2Credentials: fido2Credentials,
15+
hasFido2: hasFido2,
16+
username: username,
17+
totp: totp,
18+
uris: uris
19+
)
20+
}
21+
}

0 commit comments

Comments
 (0)