Skip to content

Commit

Permalink
Simplify adapter tests
Browse files Browse the repository at this point in the history
- Add an async extension to IMASecureSignalsAdapter to make tests easier to follow
- Add a test for no identity
  • Loading branch information
dcaunt committed Aug 12, 2024
1 parent ee4dca5 commit 2b1fc35
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 116 deletions.
9 changes: 0 additions & 9 deletions Tests/UID2IMAPluginTests/TestData/uid2identity.json

This file was deleted.

21 changes: 0 additions & 21 deletions Tests/UID2IMAPluginTests/TestExtensions/DataLoader.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
})
}
}
}
131 changes: 45 additions & 86 deletions Tests/UID2IMAPluginTests/UID2IMASecureSignalsAdapterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?, Error> {
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<String?, Error> {
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)

}

}

0 comments on commit 2b1fc35

Please sign in to comment.