Skip to content

Commit 3b9ccc0

Browse files
committed
chore(IntegrationTests): Fixing Analytics and Auth integration tests.
1 parent 13fef07 commit 3b9ccc0

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

AmplifyPlugins/Analytics/AWSPinpointAnalyticsPluginIntegrationTests/AWSPinpointAnalyticsPluginIntegrationTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,40 @@ class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
7070
Amplify.Analytics.identifyUser(userId, withProfile: userProfile)
7171

7272
wait(for: [identifyUserEvent], timeout: TestCommonConstants.networkTimeout)
73+
74+
// Remove userId from the current endpoint
75+
let targetingClient = escapeHatch().targetingClient
76+
let currentProfile = targetingClient.currentEndpointProfile()
77+
currentProfile.user?.userId = ""
78+
targetingClient.update(currentProfile)
79+
}
80+
81+
/// Run this test when the number of endpoints for the userId exceeds the limit.
82+
/// The profile should have permissions to run the "mobiletargeting:DeleteUserEndpoints" action.
83+
func skip_testDeleteEndpointsForUser() throws {
84+
let userId = "userId"
85+
let escapeHatch = escapeHatch()
86+
let applicationId = escapeHatch.configuration.appId
87+
guard let targetingConfiguration = escapeHatch.configuration.targetingServiceConfiguration else {
88+
XCTFail("Targeting configuration is not defined.")
89+
return
90+
}
91+
92+
let deleteEndpointsRequest = AWSPinpointTargetingDeleteUserEndpointsRequest()!
93+
deleteEndpointsRequest.userId = userId
94+
deleteEndpointsRequest.applicationId = applicationId
95+
96+
let deleteExpectation = expectation(description: "Delete endpoints")
97+
let lowLevelClient = lowLevelClient(from: targetingConfiguration)
98+
lowLevelClient.deleteUserEndpoints(deleteEndpointsRequest) { response, error in
99+
guard error == nil else {
100+
XCTFail("Unexpected error when attempting to delete endpoints")
101+
deleteExpectation.fulfill()
102+
return
103+
}
104+
deleteExpectation.fulfill()
105+
}
106+
wait(for: [deleteExpectation], timeout: 1)
73107
}
74108

75109
func testRecordEventsAreFlushed() {
@@ -79,6 +113,7 @@ class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
79113
// TODO: Remove exposing AWSPinpointEvent
80114
guard let pinpointEvents = payload.data as? [AWSPinpointEvent] else {
81115
XCTFail("Missing data")
116+
flushEventsInvoked.fulfill()
82117
return
83118
}
84119
XCTAssertNotNil(pinpointEvents)
@@ -117,4 +152,17 @@ class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
117152
XCTAssertNotNil(awsPinpoint.configuration)
118153
XCTAssertTrue(awsPinpoint.configuration.enableAutoSessionRecording)
119154
}
155+
156+
private func escapeHatch() -> AWSPinpoint {
157+
guard let plugin = try? Amplify.Analytics.getPlugin(for: "awsPinpointAnalyticsPlugin"),
158+
let analyticsPlugin = plugin as? AWSPinpointAnalyticsPlugin else {
159+
fatalError("Unable to retrieve configuration")
160+
}
161+
return analyticsPlugin.getEscapeHatch()
162+
}
163+
164+
private func lowLevelClient(from configuration: AWSServiceConfiguration) -> AWSPinpointTargeting {
165+
AWSPinpointTargeting.register(with: configuration, forKey: "integrationTestsTargetingConfiguration")
166+
return AWSPinpointTargeting.init(forKey: "integrationTestsTargetingConfiguration")
167+
}
120168
}

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/AuthDeviceTests/AuthDeviceOperationTests.swift

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
import XCTest
99
@testable import Amplify
10+
import AmplifyTestCommon
1011
import AWSCognitoAuthPlugin
12+
#if COCOAPODS
13+
import AWSMobileClient
14+
#else
15+
import AWSMobileClientXCF
16+
#endif
1117

1218
class AuthDeviceOperationTests: AWSAuthBaseTest {
1319

@@ -22,20 +28,60 @@ class AuthDeviceOperationTests: AWSAuthBaseTest {
2228
sleep(2)
2329
}
2430

31+
/// Test if forgetDevice returns deviceNotTracked error for a signed out user
32+
///
33+
/// - Given: A test with the user not signed in
34+
/// - When:
35+
/// - I invoke forgetDevice
36+
/// - Then:
37+
/// - I should get a notSignedIn error.
38+
///
39+
func testForgetDeviceWithSignedOutUser() {
40+
let forgetDeviceExpectation = expectation(description: "Received event result from forgetDevice")
41+
_ = Amplify.Auth.forgetDevice { result in
42+
forgetDeviceExpectation.fulfill()
43+
switch result {
44+
case .success:
45+
XCTFail("Forget device with signed out user should not return success")
46+
case .failure(let error):
47+
guard let cognitoError = error.underlyingError as? AWSMobileClientError,
48+
case .notSignedIn = cognitoError else {
49+
XCTFail("Should return notSignedIn")
50+
return
51+
}
52+
}
53+
}
54+
wait(for: [forgetDeviceExpectation], timeout: networkTimeout)
55+
}
56+
2557
/// Test if forgetDevice returns deviceNotTracked error for a unknown device
2658
///
27-
/// - Given: A test with the device not tracked
59+
/// - Given: A test with a device not tracked
2860
/// - When:
2961
/// - I invoke forgetDevice
3062
/// - Then:
3163
/// - I should get a deviceNotTracked error.
3264
///
33-
func testForgetDeviceWithUnknowdevice() {
65+
func testForgetDeviceWithUntrackedDevice() {
66+
let username = "integTest\(UUID().uuidString)"
67+
let password = "P123@\(UUID().uuidString)"
68+
let signInExpectation = expectation(description: "SignIn operation should complete")
69+
AuthSignInHelper.registerAndSignInUser(username: username, password: password,
70+
email: email) { didSucceed, error in
71+
signInExpectation.fulfill()
72+
XCTAssertTrue(didSucceed, "SignIn operation failed - \(String(describing: error))")
73+
}
74+
wait(for: [signInExpectation], timeout: networkTimeout)
75+
76+
let user = Amplify.Auth.getCurrentUser()
77+
XCTAssertNotNil(user)
78+
3479
let forgetDeviceExpectation = expectation(description: "Received event result from forgetDevice")
3580
_ = Amplify.Auth.forgetDevice { result in
81+
forgetDeviceExpectation.fulfill()
3682
switch result {
3783
case .success:
38-
XCTFail("Confirm signUp with non existing user should not return result")
84+
XCTFail("Forget device with untracked device should not return result")
3985
case .failure(let error):
4086
guard let cognitoError = error.underlyingError as? AWSCognitoAuthError,
4187
case .deviceNotTracked = cognitoError else {
@@ -44,7 +90,6 @@ class AuthDeviceOperationTests: AWSAuthBaseTest {
4490
}
4591

4692
}
47-
forgetDeviceExpectation.fulfill()
4893
}
4994
wait(for: [forgetDeviceExpectation], timeout: networkTimeout)
5095
}

0 commit comments

Comments
 (0)