-
Notifications
You must be signed in to change notification settings - Fork 13
Refactor ExPlat assignments endpoint unit tests #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
salimbraksa
wants to merge
7
commits into
trunk
Choose a base branch
from
task/explat-assignments-endpoint-unit-tests
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21e3e89
Add helper methods to make the shared ExPlat instance
salimbraksa d24b33e
Add unit test to assert the ExPlat anonymous assignments endpoint
salimbraksa 4c28709
Fix Trailing Whitespace Violation
salimbraksa 69c4ff4
Merge branch 'trunk' into task/explat-assignments-endpoint-unit-tests
salimbraksa 96167ba
Update CHANGELOG
salimbraksa 316b948
Add unit tests for ExPlat with user auth configuration
salimbraksa 539c3bc
Remove the recently added unit tests from macOS
salimbraksa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
@@ -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() { | ||
|
|
@@ -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 | ||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's possible that the |
||
| 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]) | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
expectedEndpointprobably won't exactly match the requested URL string. I think it's best to compare the path part in the condition, and assert therequest.urlcontains certain query?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also the matcher's implementation.