Skip to content

Commit

Permalink
walk up the domain when querying account (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy authored Jul 27, 2021
1 parent 0601d78 commit 2483c0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
10 changes: 9 additions & 1 deletion Sources/BrowserServicesKit/SecureVault/SecureVault.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ class DefaultSecureVault: SecureVault {
}

do {
return try self.providers.database.websiteAccountsForDomain(domain)
var parts = domain.components(separatedBy: ".")
while !parts.isEmpty {
let accounts = try self.providers.database.websiteAccountsForDomain(parts.joined(separator: "."))
if !accounts.isEmpty {
return accounts
}
parts.removeFirst()
}
return []
} catch {
throw SecureVaultError.databaseError(cause: error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class MockDatabaseProvider: SecureVaultDatabaseProvider {

// swiftlint:disable identifier_name
var _accounts = [SecureVaultModels.WebsiteAccount]()
var _forDomain: String?
var _forDomain = [String]()
var _credentials: SecureVaultModels.WebsiteCredentials?
var _lastCredentials: SecureVaultModels.WebsiteCredentials?
// swiftlint:enable identifier_name
Expand All @@ -38,7 +38,7 @@ internal class MockDatabaseProvider: SecureVaultDatabaseProvider {
}

func websiteAccountsForDomain(_ domain: String) throws -> [SecureVaultModels.WebsiteAccount] {
self._forDomain = domain
self._forDomain.append(domain)
return _accounts
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ class SecureVaultTests: XCTestCase {
XCTAssertEqual("domain", accounts[0].domain)
XCTAssertEqual("username", accounts[0].username)

XCTAssertEqual("example.com", mockDatabaseProvider._forDomain)
XCTAssertEqual(["example.com"], mockDatabaseProvider._forDomain)
}

func testWhenRetrievingAccountsForDomain_ThenWalkUpDomainToFindAccounts() throws {

mockDatabaseProvider._accounts = []

_ = try testVault.accountsFor(domain: "www.example.com")
XCTAssertEqual(["www.example.com", "example.com", "com"], mockDatabaseProvider._forDomain)
}

func testWhenDeletingCredentialsForAccount_ThenDatabaseCalled() throws {
Expand Down

0 comments on commit 2483c0c

Please sign in to comment.