From 2483c0c3b5db69da93e525851f13e012df04da74 Mon Sep 17 00:00:00 2001 From: Christopher Brind Date: Tue, 27 Jul 2021 07:18:13 +0100 Subject: [PATCH] walk up the domain when querying account (#17) --- .../BrowserServicesKit/SecureVault/SecureVault.swift | 10 +++++++++- .../SecureVault/MockVaultProviders.swift | 4 ++-- .../SecureVault/SecureVaultTests.swift | 9 ++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Sources/BrowserServicesKit/SecureVault/SecureVault.swift b/Sources/BrowserServicesKit/SecureVault/SecureVault.swift index 767b6622c..417b1e8dc 100644 --- a/Sources/BrowserServicesKit/SecureVault/SecureVault.swift +++ b/Sources/BrowserServicesKit/SecureVault/SecureVault.swift @@ -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) } diff --git a/Tests/BrowserServicesKitTests/SecureVault/MockVaultProviders.swift b/Tests/BrowserServicesKitTests/SecureVault/MockVaultProviders.swift index a66610342..902b08f60 100644 --- a/Tests/BrowserServicesKitTests/SecureVault/MockVaultProviders.swift +++ b/Tests/BrowserServicesKitTests/SecureVault/MockVaultProviders.swift @@ -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 @@ -38,7 +38,7 @@ internal class MockDatabaseProvider: SecureVaultDatabaseProvider { } func websiteAccountsForDomain(_ domain: String) throws -> [SecureVaultModels.WebsiteAccount] { - self._forDomain = domain + self._forDomain.append(domain) return _accounts } diff --git a/Tests/BrowserServicesKitTests/SecureVault/SecureVaultTests.swift b/Tests/BrowserServicesKitTests/SecureVault/SecureVaultTests.swift index 8b7030684..d9127e926 100644 --- a/Tests/BrowserServicesKitTests/SecureVault/SecureVaultTests.swift +++ b/Tests/BrowserServicesKitTests/SecureVault/SecureVaultTests.swift @@ -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 {