Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _None._

### Internal Changes

_None._
- Refactor the unit tests for `ExPlat` assignments request. [#256]

## 2.2.0

Expand Down
148 changes: 102 additions & 46 deletions Tests/Tests/ABTesting/ExPlatTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import XCTest
import OHHTTPStubs
import OHHTTPStubsSwift

#if SWIFT_PACKAGE
@testable import AutomatticExperiments
Expand All @@ -16,17 +18,6 @@ class ExPlatTests: XCTestCase {
anonId: nil
)

var tracksService: TracksService!

override func setUp() {
let contextManager = MockTracksContextManager()
self.tracksService = TracksService(contextManager: contextManager)
}

override func tearDown() {
ExPlat.shared = nil
}

// Save the returned experiments variation
//
func testRefresh() {
Expand Down Expand Up @@ -117,54 +108,119 @@ class ExPlatTests: XCTestCase {
XCTAssertEqual(ExPlat.shared?.experimentNames, ["foo", "bar"])
}

// Tests ExPlat anonymous configuration using TracksService.
//
func testTracksServiceAnonymousConfiguration() throws {
#if os(iOS)
/// Tests the right assignments endpoint is called when ExPlat is configured through `TracksService`.
///
func testAssignmentsEndpointWithAnonymousConfiguration() {
// Given
let eventNamePrefix = "wooios"
let anonymousId = "123"
self.tracksService.platform = nil
self.tracksService.eventNamePrefix = eventNamePrefix
let expectation = XCTestExpectation(description: "The ExPlat assignments endpoint should have the right params")
let experiment = "test"
let anonId = "123"
let eventNamePrefix = "wooios_test"
let expectedEndpoint = Self.makeAssignmentsEndpoint(platform: eventNamePrefix, experiment: experiment, anonId: anonId)
stub(condition: isAbsoluteURLString(expectedEndpoint)) { request in
expectation.fulfill()
return .init(data: Data(), statusCode: 200, headers: nil)
}

// When
self.tracksService.switchToAnonymousUser(withAnonymousID: anonymousId)
self.makeSharedExPlat(
platform: nil,
eventNamePrefix: eventNamePrefix,
experiment: experiment,
anonId: anonId
)
ExPlat.shared?.refresh()

// Then
#if os(iOS)
let exPlat = try XCTUnwrap(ExPlat.shared)
XCTAssertEqual(exPlat.platform, eventNamePrefix)
XCTAssertEqual(exPlat.oAuthToken, nil)
XCTAssertEqual(exPlat.anonId, anonymousId)
#else
XCTAssertNil(ExPlat.shared)
#endif
wait(for: [expectation], timeout: 1.0)
}

// Tests ExPlat user authenticated configuration using TracksService.
//
func testTracksServiceUserAuthConfiguration() throws {
/// Tests the right assignments endpoint is called when ExPlat is configured through `TracksService`.
///
func testAssignmentsEndpointWithUserAuthConfiguration() {
// Given
let username = "foobar"
let userID = "123"
let wpComToken = "abc"
let platform = "wpios"
let eventNamePrefix = "jpios"
let skipAliasEventCreation = true
self.tracksService.platform = platform
self.tracksService.eventNamePrefix = eventNamePrefix
let expectation = XCTestExpectation(description: "The ExPlat assignments endpoint should have the right params")
let experiment = "test"
let platform = "wooios_test"
let token = "abc"
let expectedEndpoint = Self.makeAssignmentsEndpoint(platform: platform, experiment: experiment)
stub(condition: isAbsoluteURLString(expectedEndpoint)) { request in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is probably too strict. The query part in the expectedEndpoint probably won't exactly match the requested URL string. I think it's best to compare the path part in the condition, and assert the request.url contains certain query?

Copy link
Contributor

@mokagio mokagio May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XCTAssertEqual(request.allHTTPHeaderFields?["Authorization"], "Bearer \(token)")
expectation.fulfill()
return .init(data: Data(), statusCode: 200, headers: nil)
}

// When
self.tracksService.switchToAuthenticatedUser(withUsername: username, userID: userID, wpComToken: wpComToken, skipAliasEventCreation: skipAliasEventCreation)
self.makeSharedExPlat(
platform: platform,
eventNamePrefix: "jpios",
experiment: experiment,
username: "foobar",
userID: "123",
wpComToken: token
)
ExPlat.shared?.refresh()

// Then
#if os(iOS)
let exPlat = try XCTUnwrap(ExPlat.shared)
XCTAssertEqual(exPlat.platform, platform)
XCTAssertEqual(exPlat.oAuthToken, wpComToken)
XCTAssertEqual(exPlat.anonId, nil)
#else
XCTAssertNil(ExPlat.shared)
wait(for: [expectation], timeout: 1.0)
}
#endif

// MARK: - Helpers

static func makeAssignmentsEndpoint(platform: String, experiment: String, anonId: String? = nil) -> String {
let baseURL = "https://public-api.wordpress.com"
let path = "wpcom/v2/experiments/0.1.0/assignments"
var endpoint = "\(baseURL)/\(path)/\(platform)?_locale=en&experiment_names=\(experiment)"
if let anonId {
endpoint.append("&anon_id=\(anonId)")
}
return endpoint
}

private func makeTracksService(platform: String?, eventNamePrefix: String?) -> TracksService? {
guard let tracksService = TracksService(contextManager: MockTracksContextManager()) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible that the tracksService would be nil?

return nil
}
tracksService.platform = platform
tracksService.eventNamePrefix = eventNamePrefix
return tracksService
}

private func makeSharedExPlat(platform: String?, eventNamePrefix: String?, experiment: String, anonId: String?) {
self.addTeardownBlock {
ExPlat.shared = nil
}
guard let tracksService = makeTracksService(platform: platform, eventNamePrefix: eventNamePrefix) else {
return
}
tracksService.switchToAnonymousUser(withAnonymousID: anonId)
guard let exPlat = ExPlat.shared else {
return
}
exPlat.register(experiments: [experiment])
}

private func makeSharedExPlat(
platform: String?,
eventNamePrefix: String?,
experiment: String,
username: String,
userID: String,
wpComToken: String
) {
self.addTeardownBlock {
ExPlat.shared = nil
}
guard let tracksService = makeTracksService(platform: platform, eventNamePrefix: eventNamePrefix) else {
return
}
tracksService.switchToAuthenticatedUser(withUsername: username, userID: userID, wpComToken: wpComToken, skipAliasEventCreation: true)
guard let exPlat = ExPlat.shared else {
return
}
exPlat.register(experiments: [experiment])
}
}

Expand Down