From 2b1fc35815a2651c3be04d6c2a79f822255b04f3 Mon Sep 17 00:00:00 2001 From: Dave Snabel-Caunt Date: Mon, 12 Aug 2024 19:54:02 +0100 Subject: [PATCH] Simplify adapter tests - Add an async extension to IMASecureSignalsAdapter to make tests easier to follow - Add a test for no identity --- .../TestData/uid2identity.json | 9 -- .../TestExtensions/DataLoader.swift | 21 --- ...ASecureSignalsAdapter+TestExtensions.swift | 20 +++ .../UID2IMASecureSignalsAdapterTests.swift | 131 ++++++------------ 4 files changed, 65 insertions(+), 116 deletions(-) delete mode 100644 Tests/UID2IMAPluginTests/TestData/uid2identity.json delete mode 100644 Tests/UID2IMAPluginTests/TestExtensions/DataLoader.swift create mode 100644 Tests/UID2IMAPluginTests/TestExtensions/IMASecureSignalsAdapter+TestExtensions.swift diff --git a/Tests/UID2IMAPluginTests/TestData/uid2identity.json b/Tests/UID2IMAPluginTests/TestData/uid2identity.json deleted file mode 100644 index 27e9e12..0000000 --- a/Tests/UID2IMAPluginTests/TestData/uid2identity.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "advertising_token": "NewAdvertisingTokenIjb6u6KcMAtd0/4ZIAYkXvFrMdlZVqfb9LNf99B+1ysE/lBzYVt64pxYxjobJMGbh5q/HsKY7KC0Xo5Rb/Vo8HC4dYOoWXyuGUaL7Jmbw4bzh+3pgokelUGyTX19DfArTeIg7n+8cxWQ=", - "refresh_token": "NewRefreshTokenAAAF2c8H5dF8AAAF2c8H5dF8AAAADX393Vw94afoVLL6A+qjdSUEisEKx6t42fLgN+2dmTgUavagz0Q6Kp7ghM989hKhZDyAGjHyuAAwm+CX1cO7DWEtMeNUA9vkWDjcIc8yeDZ+jmBtEaw07x/cxoul6fpv2PQ==", - "identity_expires": 1633643601000, - "refresh_from": 1633643001000, - "refresh_expires": 1636322000000, - "refresh_response_key": "yptCUTBoZm1ffosgCrmuwg==", - "status": "success" -} diff --git a/Tests/UID2IMAPluginTests/TestExtensions/DataLoader.swift b/Tests/UID2IMAPluginTests/TestExtensions/DataLoader.swift deleted file mode 100644 index 63d7669..0000000 --- a/Tests/UID2IMAPluginTests/TestExtensions/DataLoader.swift +++ /dev/null @@ -1,21 +0,0 @@ -// -// DataLoader.swift -// -// -// Created by Brad Leege on 3/21/23. -// - -import Foundation - -final class DataLoader { - - static func load(fileName: String, fileExtension: String, _ inDirectory: String = "TestData") throws -> Data { - guard let bundlePath = Bundle.module.path(forResource: fileName, ofType: fileExtension, inDirectory: inDirectory), - let stringData = try String(contentsOfFile: bundlePath).data(using: .utf8) else { - throw "Could not load data from file." - } - - return stringData - } - -} diff --git a/Tests/UID2IMAPluginTests/TestExtensions/IMASecureSignalsAdapter+TestExtensions.swift b/Tests/UID2IMAPluginTests/TestExtensions/IMASecureSignalsAdapter+TestExtensions.swift new file mode 100644 index 0000000..2395c22 --- /dev/null +++ b/Tests/UID2IMAPluginTests/TestExtensions/IMASecureSignalsAdapter+TestExtensions.swift @@ -0,0 +1,20 @@ +// +// IMASecureSignalsAdapter+TestExtensions.swift +// + +import GoogleInteractiveMediaAds + +/// Adds an async wrapper interface to simplify testing +extension IMASecureSignalsAdapter { + func collectSignals() async throws -> String? { + try await withCheckedThrowingContinuation { continuation in + collectSignals(completion: { signal, error in + guard error == nil else { + continuation.resume(throwing: error!) + return + } + continuation.resume(returning: signal) + }) + } + } +} diff --git a/Tests/UID2IMAPluginTests/UID2IMASecureSignalsAdapterTests.swift b/Tests/UID2IMAPluginTests/UID2IMASecureSignalsAdapterTests.swift index 53d5672..c61bf61 100644 --- a/Tests/UID2IMAPluginTests/UID2IMASecureSignalsAdapterTests.swift +++ b/Tests/UID2IMAPluginTests/UID2IMASecureSignalsAdapterTests.swift @@ -11,112 +11,71 @@ import UID2 import UID2IMAPlugin final class UID2IMASecureSignalsAdapterTests: XCTestCase { - - private let decoder: JSONDecoder = { - let decoder = JSONDecoder() - decoder.keyDecodingStrategy = .convertFromSnakeCase - return decoder - }() - /// 🟩 - IMA Adapter Request Signal Success func testRequestSignalsSuccess() async throws { - - // Sample UID2Identity data - let uid2IdentityData = try DataLoader.load(fileName: "uid2identity", fileExtension: "json") - let uid2IdentityFromFile = try decoder.decode(UID2Identity.self, from: uid2IdentityData) - - // Emulate A UID2Identity With Valid Times - let identityExpires = Date(timeIntervalSinceNow: 60 * 60).millisecondsSince1970 - let refreshFrom = Date(timeIntervalSinceNow: 60 * 40).millisecondsSince1970 - let refreshExpires = Date(timeIntervalSinceNow: 60 * 50).millisecondsSince1970 - - let uid2Identity = UID2Identity(advertisingToken: uid2IdentityFromFile.advertisingToken, - refreshToken: uid2IdentityFromFile.refreshToken, - identityExpires: identityExpires, - refreshFrom: refreshFrom, - refreshExpires: refreshExpires, - refreshResponseKey: uid2IdentityFromFile.refreshResponseKey) - // Seed the sample UID2Identity data in the UID2Manager await UID2Manager.shared.setAutomaticRefreshEnabled(false) - await UID2Manager.shared.setIdentity(uid2Identity) + await UID2Manager.shared.setIdentity( + UID2Identity( + advertisingToken: "uid2-test-token", + refreshToken: "refresh-token", + identityExpires: Date(timeIntervalSinceNow: 60 * 60).millisecondsSince1970, + refreshFrom: Date(timeIntervalSinceNow: 60 * 40).millisecondsSince1970, + refreshExpires: Date(timeIntervalSinceNow: 60 * 50).millisecondsSince1970, + refreshResponseKey: "" + ) + ) + + let signal = try await UID2IMASecureSignalsAdapter().collectSignals() - // Adapter to test - let adapter = UID2IMASecureSignalsAdapter() - - // Wraps Completion Handler Function Call In Async / Await - let signal: String = try await withCheckedThrowingContinuation { continuation in - - adapter.collectSignals(completion: { signal, error in - guard let signal = signal else { - continuation.resume(throwing: "Signal was Nil") - return - } - continuation.resume(returning: signal) - return - }) - - } - // Confirm that Adapter returns expected data - XCTAssertEqual(uid2Identity.advertisingToken, signal) + XCTAssertEqual("uid2-test-token", signal) } - /// 🟥 - IMA Adapter Request Signal No Advertising Token Erro + /// 🟥 - GMA Adapter Request Signal Error No Identity + func testRequestSignalsNoIdentity() async throws { + // Ensure no identity is set + await UID2Manager.shared.resetIdentity() + + let result = await Task { + try await UID2IMASecureSignalsAdapter().collectSignals() + }.result + XCTAssertThrowsError(try result.get()) { error in + let adapterError = error as? AdvertisingTokenNotFoundError + XCTAssertEqual(AdvertisingTokenNotFoundError(), adapterError) + } + } + + /// 🟥 - IMA Adapter Request Signal No Advertising Token Error func testRequestSignalsNoAdvertisingToken() async throws { - - // Sample UID2Identity data - let uid2IdentityData = try DataLoader.load(fileName: "uid2identity", fileExtension: "json") - let uid2IdentityFromFile = try decoder.decode(UID2Identity.self, from: uid2IdentityData) - - // Emulate A UID2Identity With Valid Times - let identityExpires = Date(timeIntervalSinceNow: 60 * 60).millisecondsSince1970 - let refreshFrom = Date(timeIntervalSinceNow: 60 * 40).millisecondsSince1970 - let refreshExpires = Date(timeIntervalSinceNow: 60 * 50).millisecondsSince1970 - - let uid2Identity = UID2Identity(advertisingToken: "", - refreshToken: uid2IdentityFromFile.refreshToken, - identityExpires: identityExpires, - refreshFrom: refreshFrom, - refreshExpires: refreshExpires, - refreshResponseKey: uid2IdentityFromFile.refreshResponseKey) - - // Seed the sample UID2Identity data in the UID2Manager + // Set an identity with an invalid advertisingToken await UID2Manager.shared.setAutomaticRefreshEnabled(false) - await UID2Manager.shared.setIdentity(uid2Identity) + await UID2Manager.shared.setIdentity( + UID2Identity( + advertisingToken: "", + refreshToken: "refresh-token", + identityExpires: Date(timeIntervalSinceNow: 60 * 60).millisecondsSince1970, + refreshFrom: Date(timeIntervalSinceNow: 60 * 40).millisecondsSince1970, + refreshExpires: Date(timeIntervalSinceNow: 60 * 50).millisecondsSince1970, + refreshResponseKey: "" + ) + ) - // Adapter to test - let adapter = UID2IMASecureSignalsAdapter() - - // Wraps Completion Handler Function Call In Async / Await - let _: String = try await withCheckedThrowingContinuation { continuation in - - adapter.collectSignals(completion: { signal, error in - guard let error = error as? AdvertisingTokenNotFoundError else { - continuation.resume(throwing: "Error returned was not expected type.") - return - } - - XCTAssertEqual(AdvertisingTokenNotFoundError(), error) - - continuation.resume(returning: "Successful Test") - - return - }) - + let result = await Task { + try await UID2IMASecureSignalsAdapter().collectSignals() + }.result + XCTAssertThrowsError(try result.get()) { error in + let adapterError = error as? AdvertisingTokenNotFoundError + XCTAssertEqual(AdvertisingTokenNotFoundError(), adapterError) } - } func testAdSDKVersion() async throws { - let adSDKVersion = UID2IMASecureSignalsAdapter.adSDKVersion() let sdkVersion = await UID2Manager.shared.sdkVersion XCTAssertEqual(sdkVersion.major, adSDKVersion.majorVersion) XCTAssertEqual(sdkVersion.minor, adSDKVersion.minorVersion) XCTAssertEqual(sdkVersion.patch, adSDKVersion.patchVersion) - } - }