Skip to content

Commit

Permalink
enableAdServicesAttributionTokenCollection: added integration test (#…
Browse files Browse the repository at this point in the history
…2551)

Because `AAAttribution.attributionToken()` isn't available on
simulators, we currently had no coverage for this entire part of the
codebase.
By mocking the token (on simulator + debug + integration tests only), we
can verify posting to the server works correctly.

As a follow-up, we can also verify that posting it as part of the
receipt with #2549 works correctly as well.
  • Loading branch information
NachoSoto authored May 26, 2023
1 parent b00bfec commit 599cfda
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Sources/Attribution/AttributionFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class AttributionFetcher {
#if canImport(AdServices)
do {
#if targetEnvironment(simulator)
#if DEBUG
if let mockToken = ProcessInfo.mockAdServicesToken {
Logger.warn(Strings.attribution.adservices_mocking_token(mockToken))
return mockToken
}
#endif

// See https://github.com/RevenueCat/purchases-ios/issues/2121
Logger.appleWarning(Strings.attribution.adservices_token_unavailable_in_simulator)
return nil
Expand Down
9 changes: 7 additions & 2 deletions Sources/FoundationExtensions/ProcessInfo+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

import Foundation

#if DEBUG

enum EnvironmentKey: String {

case XCTestConfigurationFile = "XCTestConfigurationFilePath"
case RCRunningTests = "RCRunningTests"
case RCRunningIntegrationTests = "RCRunningIntegrationTests"
case RCMockAdServicesToken = "RCMockAdServicesToken"

}

Expand All @@ -29,8 +32,6 @@ extension ProcessInfo {

}

#if DEBUG

extension ProcessInfo {

static var isRunningUnitTests: Bool {
Expand All @@ -47,6 +48,10 @@ extension ProcessInfo {
return self[.RCRunningIntegrationTests] == "1"
}

static var mockAdServicesToken: String? {
return self[.RCMockAdServicesToken]?.notEmptyOrWhitespaces
}

}

#endif
4 changes: 4 additions & 0 deletions Sources/Logging/Strings/AttributionStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum AttributionStrings {
case attribute_set_locally(attribute: String)
case missing_advertiser_identifiers
case adservices_not_supported
case adservices_mocking_token(String)
case adservices_token_fetch_failed(error: Error)
case adservices_token_post_failed(error: BackendError)
case adservices_token_post_succeeded
Expand Down Expand Up @@ -125,6 +126,9 @@ extension AttributionStrings: CustomStringConvertible {
return "Tried to fetch AdServices attribution token on device without " +
"AdServices support."

case let .adservices_mocking_token(token):
return "AdServices: mocking token: \(token) for tests"

case .adservices_token_fetch_failed(let error):
return "Fetching AdServices attribution token failed with error: \(error.localizedDescription)"

Expand Down
14 changes: 14 additions & 0 deletions Tests/BackendIntegrationTests/OtherIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ class OtherIntegrationTests: BaseBackendIntegrationTests {
expect(result.entitlementsByProduct["com.revenuecat.intro_test.monthly.1_week_intro"]).to(beEmpty())
}

@available(iOS 14.3, macOS 11.1, macCatalyst 14.3, *)
func testEnableAdServicesAttributionTokenCollection() async throws {
let logger = TestLogHandler()

Purchases.shared.attribution.enableAdServicesAttributionTokenCollection()

try await logger.verifyMessageIsEventuallyLogged(
Strings.attribution.adservices_token_post_succeeded.description,
level: .debug,
timeout: .seconds(3),
pollInterval: .milliseconds(200)
)
}

}
4 changes: 4 additions & 0 deletions Tests/TestPlans/BackendIntegrationTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"key" : "RCRunningTests",
"value" : "1"
},
{
"key" : "RCMockAdServicesToken",
"value" : "G9i5hC8lQJeGOfmS+MFycll"
},
{
"key" : "RCRunningIntegrationTests",
"value" : "1"
Expand Down
4 changes: 4 additions & 0 deletions Tests/TestPlans/CI-BackendIntegration.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"defaultOptions" : {
"codeCoverage" : false,
"environmentVariableEntries" : [
{
"key" : "RCMockAdServicesToken",
"value" : "G9i5hC8lQJeGOfmS+MFycll"
},
{
"key" : "RCRunningTests",
"value" : "1"
Expand Down

0 comments on commit 599cfda

Please sign in to comment.