Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Nov 22, 2024
1 parent fcbdf72 commit f80bf7b
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 269 deletions.
1 change: 0 additions & 1 deletion Core/WebCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ extension HTTPCookie {

}

@MainActor
public protocol WebsiteDataManaging {

func removeCookies(forDomains domains: [String], fromDataStore: WKWebsiteDataStore) async
Expand Down
8 changes: 8 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@
85C2971A248162CA0063A335 /* DaxOnboardingPadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C29719248162CA0063A335 /* DaxOnboardingPadViewController.swift */; };
85C503F92CEF78C10075DF6F /* DataStoreIdManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503F82CEF78C00075DF6F /* DataStoreIdManager.swift */; };
85C503FB2CF0ADCE0075DF6F /* WebCacheManager+ObservationsDataClearing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FA2CF0ADBD0075DF6F /* WebCacheManager+ObservationsDataClearing.swift */; };
85C503FD2CF0E7B10075DF6F /* MockFireproofing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FC2CF0E7B10075DF6F /* MockFireproofing.swift */; };
85C503FF2CF0F4DC0075DF6F /* MockWebsiteDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FE2CF0F4D60075DF6F /* MockWebsiteDataManager.swift */; };
85C8E61D2B0E47380029A6BD /* BookmarksDatabaseSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C8E61C2B0E47380029A6BD /* BookmarksDatabaseSetup.swift */; };
85C91CA224671F4C00A11132 /* AppDeepLinkSchemes.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17D723B1E8BB374003E8B0E /* AppDeepLinkSchemes.swift */; };
85D2187024BF24DB004373D2 /* FaviconRequestModifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85D2186F24BF24DB004373D2 /* FaviconRequestModifierTests.swift */; };
Expand Down Expand Up @@ -1880,6 +1882,8 @@
85C29719248162CA0063A335 /* DaxOnboardingPadViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DaxOnboardingPadViewController.swift; sourceTree = "<group>"; };
85C503F82CEF78C00075DF6F /* DataStoreIdManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreIdManager.swift; sourceTree = "<group>"; };
85C503FA2CF0ADBD0075DF6F /* WebCacheManager+ObservationsDataClearing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WebCacheManager+ObservationsDataClearing.swift"; sourceTree = "<group>"; };
85C503FC2CF0E7B10075DF6F /* MockFireproofing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockFireproofing.swift; sourceTree = "<group>"; };
85C503FE2CF0F4D60075DF6F /* MockWebsiteDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWebsiteDataManager.swift; sourceTree = "<group>"; };
85C8E61C2B0E47380029A6BD /* BookmarksDatabaseSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksDatabaseSetup.swift; sourceTree = "<group>"; };
85CA53A324B9F2BD00A6288C /* Favicons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Favicons.swift; sourceTree = "<group>"; };
85CA53A924BB376800A6288C /* NotFoundCachingDownloader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = NotFoundCachingDownloader.swift; path = ../Core/NotFoundCachingDownloader.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6080,6 +6084,7 @@
F17669A91E412A17003D3222 /* Mocks */ = {
isa = PBXGroup;
children = (
85C503FE2CF0F4D60075DF6F /* MockWebsiteDataManager.swift */,
8598D2E52CEBAA1B00C45685 /* MockFaviconStore.swift */,
9F4CC51A2C48C0C7006A96EB /* MockTabDelegate.swift */,
C14882E927F20DD000D59F0C /* MockBookmarksCoreDataStorage.swift */,
Expand All @@ -6099,6 +6104,7 @@
9F69331C2C5A191400CD6A5D /* MockTutorialSettings.swift */,
852409302C78030D00CB28FC /* MockUsageSegmentation.swift */,
4B27FBB72C93F53B007E21A7 /* MockPersistentPixel.swift */,
85C503FC2CF0E7B10075DF6F /* MockFireproofing.swift */,
);
name = Mocks;
sourceTree = "<group>";
Expand Down Expand Up @@ -8146,6 +8152,7 @@
316790E52C9352190090B0A2 /* MarketplaceAdPostbackManagerTests.swift in Sources */,
F194FAFB1F14E622009B4DF8 /* UIFontExtensionTests.swift in Sources */,
BBFF18B12C76448100C48D7D /* QuerySubmittedTests.swift in Sources */,
85C503FF2CF0F4DC0075DF6F /* MockWebsiteDataManager.swift in Sources */,
854A8D812C7F4452001D62E5 /* AtbTests.swift in Sources */,
9F23B8092C2BE9B700950875 /* MockURLOpener.swift in Sources */,
9F8007262C5261AF003EDAF4 /* MockPrivacyDataReporter.swift in Sources */,
Expand Down Expand Up @@ -8173,6 +8180,7 @@
C185ED672BD43DA100BAE9DC /* ImportPasswordsStatusHandlerTests.swift in Sources */,
6FF9AD452CE766F700C5A406 /* NewTabPageControllerPixelTests.swift in Sources */,
F198D7981E3A45D90088DA8A /* WKWebViewConfigurationExtensionTests.swift in Sources */,
85C503FD2CF0E7B10075DF6F /* MockFireproofing.swift in Sources */,
6F3529FF2CDCEDFF00A59170 /* OmniBarLoadingStateBearerTests.swift in Sources */,
564DE45E2C45218500D23241 /* OnboardingNavigationDelegateTests.swift in Sources */,
C14E2F7729DE14EA002AC515 /* AutofillInterfaceUsernameTruncatorTests.swift in Sources */,
Expand Down
219 changes: 46 additions & 173 deletions DuckDuckGoTests/CookieStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,186 +22,59 @@ import XCTest
import WebKit

public class CookieStorageTests: XCTestCase {

var storage: MigratableCookieStorage!

// This is updated by the `make` function which preserves any cookies added as part of this test
let fireproofing = UserDefaultsFireproofing.shared

static let userDefaultsSuiteName = "test"

public override func setUp() {
super.setUp()
let defaults = UserDefaults(suiteName: Self.userDefaultsSuiteName)!
defaults.removePersistentDomain(forName: Self.userDefaultsSuiteName)
storage = MigratableCookieStorage(userDefaults: defaults)
storage.isConsumed = true
fireproofing.clearAll()
func testLoadCookiesFromDefaultsAndRemovalWhenMigrationCompletes() {
let defaults = UserDefaults(suiteName: "test")!
defaults.removeSuite(named: "test")

addCookies([
make("example.com", name: "test1", value: "value1"),
make("example.com", name: "test2", value: "value2"),
make("facebook.com", name: "test3", value: "value3"),
], defaults)

let storage = MigratableCookieStorage(userDefaults: defaults)
XCTAssertEqual(storage.cookies.count, 3)

XCTAssertTrue(storage.cookies.contains(where: {
$0.domain == "example.com" &&
$0.name == "test1" &&
$0.value == "value1"
}))

XCTAssertTrue(storage.cookies.contains(where: {
$0.domain == "example.com" &&
$0.name == "test2" &&
$0.value == "value2"
}))

XCTAssertTrue(storage.cookies.contains(where: {
$0.domain == "facebook.com" &&
$0.name == "test3" &&
$0.value == "value3"
}))

// Now remove them all
storage.migrationComplete()

XCTAssertTrue(storage.cookies.isEmpty)
}

func testWhenDomainRemovesAllCookesThenTheyAreClearedFromPersisted() {
fireproofing.addToAllowed(domain: "example.com")

XCTAssertEqual(storage.updateCookies([
make("example.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing), .empty)

XCTAssertEqual(1, storage.cookies.count)

storage.isConsumed = true
storage.updateCookies([], preservingFireproofedDomains: fireproofing)
func addCookies(_ cookies: [HTTPCookie], _ defaults: UserDefaults) {

XCTAssertEqual(0, storage.cookies.count)
var cookieData = [[String: Any?]]()
cookies.forEach { cookie in
var mappedCookie = [String: Any?]()
cookie.properties?.forEach {
mappedCookie[$0.key.rawValue] = $0.value
}
cookieData.append(mappedCookie)
}
defaults.setValue(cookieData, forKey: MigratableCookieStorage.Keys.allowedCookies)

}

func testWhenUpdatedThenDuckDuckGoCookiesAreNotRemoved() {
storage.updateCookies([
make("duckduckgo.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(1, storage.cookies.count)

storage.isConsumed = true
storage.updateCookies([
make("duckduckgo.com", name: "x", value: "1"),
make("test.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(2, storage.cookies.count)

storage.isConsumed = true
storage.updateCookies([
make("usedev1.duckduckgo.com", name: "x", value: "1"),
make("duckduckgo.com", name: "x", value: "1"),
make("test.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(3, storage.cookies.count)

}

func testWhenUpdatedThenCookiesWithFutureExpirationAreNotRemoved() {
storage.updateCookies([
make("test.com", name: "x", value: "1", expires: .distantFuture),
make("example.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(2, storage.cookies.count)
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "test.com" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "example.com" }))

}

func testWhenUpdatingThenExistingExpiredCookiesAreRemoved() {
storage.cookies = [
make("test.com", name: "x", value: "1", expires: Date(timeIntervalSinceNow: -100)),
]
XCTAssertEqual(1, storage.cookies.count)

storage.isConsumed = true
storage.updateCookies([
make("example.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(1, storage.cookies.count)
XCTAssertFalse(storage.cookies.contains(where: { $0.domain == "test.com" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "example.com" }))

}

func testWhenExpiredCookieIsAddedThenItIsNotPersisted() {

storage.updateCookies([
make("example.com", name: "x", value: "1", expires: Date(timeIntervalSinceNow: -100)),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(0, storage.cookies.count)

}

func testWhenUpdatedThenNoLongerFireproofedDomainsAreCleared() {
storage.updateCookies([
make("test.com", name: "x", value: "1"),
make("example.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

fireproofing.remove(domain: "test.com")

storage.isConsumed = true
storage.updateCookies([
make("example.com", name: "x", value: "1"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(1, storage.cookies.count)
XCTAssertFalse(storage.cookies.contains(where: { $0.domain == "test.com" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "example.com" }))
}

func testWhenStorageInitialiedThenItIsEmptyAndIsReadyToBeUpdated() {
XCTAssertEqual(0, storage.cookies.count)
XCTAssertTrue(storage.isConsumed)
}

func testWhenStorageIsUpdatedThenConsumedIsResetToFalse() {
storage.isConsumed = true
XCTAssertTrue(storage.isConsumed)
storage.updateCookies([
make("test.com", name: "x", value: "1")
], preservingFireproofedDomains: fireproofing)
XCTAssertFalse(storage.isConsumed)
}

func testWhenStorageIsReinstanciatedThenUsesStoredData() {
storage.updateCookies([
make("test.com", name: "x", value: "1")
], preservingFireproofedDomains: fireproofing)
storage.isConsumed = true

let otherStorage = MigratableCookieStorage(userDefaults: UserDefaults(suiteName: Self.userDefaultsSuiteName)!)
XCTAssertEqual(1, otherStorage.cookies.count)
XCTAssertTrue(otherStorage.isConsumed)
}

func testWhenStorageIsUpdatedThenUpdatingAddsNewCookies() {
storage.updateCookies([
make("test.com", name: "x", value: "1")
], preservingFireproofedDomains: fireproofing)
XCTAssertEqual(1, storage.cookies.count)
}

func testWhenStorageHasMatchingDOmainThenUpdatingReplacesCookies() {
storage.updateCookies([
make("test.com", name: "x", value: "1")
], preservingFireproofedDomains: fireproofing)

storage.isConsumed = true
storage.updateCookies([
make("test.com", name: "x", value: "2"),
make("test.com", name: "y", value: "3"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(2, storage.cookies.count)
XCTAssertFalse(storage.cookies.contains(where: { $0.domain == "test.com" && $0.name == "x" && $0.value == "1" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "test.com" && $0.name == "x" && $0.value == "2" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "test.com" && $0.name == "y" && $0.value == "3" }))
}

func testWhenStorageUpdatedAndNotConsumedThenNothingHappens() {
storage.updateCookies([
make("test.com", name: "x", value: "1")
], preservingFireproofedDomains: fireproofing)

storage.updateCookies([
make("example.com", name: "y", value: "3"),
], preservingFireproofedDomains: fireproofing)

XCTAssertEqual(1, storage.cookies.count)
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "test.com" && $0.name == "x" && $0.value == "1" }))
}

func make(_ domain: String, name: String, value: String, expires: Date? = nil) -> HTTPCookie {
fireproofing.addToAllowed(domain: domain)
return HTTPCookie(properties: [
.domain: domain,
.name: name,
Expand All @@ -210,5 +83,5 @@ public class CookieStorageTests: XCTestCase {
.expires: expires as Any
])!
}

}
31 changes: 6 additions & 25 deletions DuckDuckGoTests/DataStoreIdManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,26 @@ import TestUtils

class DataStoreIdManagerTests: XCTestCase {

func testWhenFreshlyInstalledThenIdIsAllocated() {
func testWhenFreshlyInstalledThenNoIdIsAllocated() {
let manager = DataStoreIdManager(store: MockKeyValueStore())

XCTAssertNil(manager.currentId)
manager.invalidateCurrentIdAndAllocateNew()
XCTAssertNotNil(manager.currentId)
}

func testWhenIdIsInvalidatedThenNewIsCreated() {

func testWhenIdIsInvalidatedThenNoNewIdIsCreated() {
let manager = DataStoreIdManager(store: MockKeyValueStore())

XCTAssertNil(manager.currentId)
manager.invalidateCurrentIdAndAllocateNew()

let firstID = manager.currentId
XCTAssertNotNil(firstID)

manager.invalidateCurrentIdAndAllocateNew()
let secondID = manager.currentId

XCTAssertNotEqual(firstID, secondID)

manager.invalidateCurrentIdAndAllocateNew()
let thirdID = manager.currentId

XCTAssertNotEqual(firstID, thirdID)
XCTAssertNotEqual(secondID, thirdID)
manager.invalidateCurrentId()
XCTAssertNil(manager.currentId)
}

func testWhenIdAlreadyExistsThenItIsRedFromTheStore() {

let storedUUID = UUID().uuidString
let store = MockKeyValueStore()
store.set(storedUUID, forKey: DataStoreIdManager.Constants.currentWebContainerId.rawValue)

let manager = DataStoreIdManager(store: store)

XCTAssertEqual(manager.currentId?.uuidString, storedUUID)
manager.invalidateCurrentId()
XCTAssertNil(manager.currentId)
}

}
Loading

0 comments on commit f80bf7b

Please sign in to comment.