|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2025 OneSignal |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + 1. The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | + |
| 16 | + 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | +with services provided by OneSignal. |
| 18 | + |
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import XCTest |
| 29 | +import OneSignalOSCore |
| 30 | +import OneSignalCoreMocks |
| 31 | +import OneSignalOSCoreMocks |
| 32 | +import OneSignalUserMocks |
| 33 | +@testable import OneSignalUser |
| 34 | + |
| 35 | +/** |
| 36 | + Tests for OSMessagingController's user state observer functionality. |
| 37 | + |
| 38 | + These tests verify that IAM fetching is retried when the OneSignal ID becomes available |
| 39 | + after an initial fetch attempt fails due to missing OneSignal ID during early app startup. |
| 40 | + |
| 41 | + Related to PR: https://github.com/OneSignal/OneSignal-iOS-SDK/pull/1626 |
| 42 | + */ |
| 43 | +final class OSMessagingControllerUserStateTests: XCTestCase { |
| 44 | + |
| 45 | + private let testSubscriptionId = "test-subscription-id-12345" |
| 46 | + private let testOneSignalId = "test-onesignal-id-12345" |
| 47 | + private let testExternalId = "test-external-id-12345" |
| 48 | + private let testAppId = "test-app-id" |
| 49 | + |
| 50 | + override func setUpWithError() throws { |
| 51 | + OneSignalCoreMocks.clearUserDefaults() |
| 52 | + OneSignalUserMocks.reset() |
| 53 | + OSConsistencyManager.shared.reset() |
| 54 | + OSMessagingController.removeInstance() |
| 55 | + |
| 56 | + // Set up basic configuration |
| 57 | + OneSignalConfigManager.setAppId(testAppId) |
| 58 | + OneSignalLog.setLogLevel(.LL_VERBOSE) |
| 59 | + } |
| 60 | + |
| 61 | + override func tearDownWithError() throws { |
| 62 | + OSMessagingController.removeInstance() |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + Test that when getInAppMessagesFromServer is called without a OneSignal ID, it stores the subscription ID for later retry. |
| 67 | + |
| 68 | + Scenario: |
| 69 | + - User calls initialize() and login() early in app startup |
| 70 | + - Push subscription ID is available but OneSignal ID is not yet set |
| 71 | + - IAM fetch should be deferred |
| 72 | + |
| 73 | + Expected: |
| 74 | + - shouldFetchOnUserChangeWithSubscriptionID property is set with the subscription ID |
| 75 | + - No IAM fetch actually occurs |
| 76 | + */ |
| 77 | + func testStoresSubscriptionIDWhenOneSignalIDUnavailable() throws { |
| 78 | + /* Setup */ |
| 79 | + let client = MockOneSignalClient() |
| 80 | + OneSignalCoreImpl.setSharedClient(client) |
| 81 | + |
| 82 | + // Note: nothing has set OneSignal ID |
| 83 | + |
| 84 | + /* Execute */ |
| 85 | + OneSignalInAppMessages.getFromServer(testSubscriptionId) |
| 86 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 87 | + |
| 88 | + /* Verify */ |
| 89 | + // The controller should have stored the subscription ID for retry |
| 90 | + let shouldFetchOnUserChangeWithSubscriptionID = OSMessagingController.sharedInstance().value(forKey: "shouldFetchOnUserChangeWithSubscriptionID") |
| 91 | + XCTAssertEqual(shouldFetchOnUserChangeWithSubscriptionID as! String, testSubscriptionId) |
| 92 | + |
| 93 | + // Verify no IAM request was actually made (since we don't have OneSignal ID) |
| 94 | + XCTAssertTrue(client.hasExecutedRequestOfType(OSRequestGetInAppMessages.self, expectedCount: 0)) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + Test that when user state changes with a valid OneSignal ID and shouldFetchOnUserChangeWithSubscriptionID is set, it retries the fetch. |
| 99 | + |
| 100 | + Scenario: |
| 101 | + - IAM fetch was previously deferred due to missing OneSignal ID |
| 102 | + - User response is returned and OneSignal ID becomes available |
| 103 | + - User state change observer is triggered |
| 104 | + |
| 105 | + Expected: |
| 106 | + - IAM fetch is retried with the stored subscription ID |
| 107 | + - shouldFetchOnUserChangeWithSubscriptionID is cleared |
| 108 | + */ |
| 109 | + func testRetriesFetchWhenUserStateChangesWithValidOneSignalID() throws { |
| 110 | + /* Setup */ |
| 111 | + let client = MockOneSignalClient() |
| 112 | + OneSignalCoreImpl.setSharedClient(client) |
| 113 | + OSMessagingController.start() |
| 114 | + let controller = OSMessagingController.sharedInstance() |
| 115 | + |
| 116 | + // Unblock consistency manager |
| 117 | + ConsistencyManagerTestHelpers.setDefaultRywToken(id: testOneSignalId) |
| 118 | + |
| 119 | + /* Execute */ |
| 120 | + // Setup the anonymous user |
| 121 | + MockUserRequests.setDefaultCreateAnonUserResponses(with: client, onesignalId: testOneSignalId, subscriptionId: testSubscriptionId) |
| 122 | + OneSignalUserManagerImpl.sharedInstance.start() |
| 123 | + |
| 124 | + // Login to a new user, without setting the client response, so onesignal ID is not hydrated |
| 125 | + OneSignalUserManagerImpl.sharedInstance.login(externalId: testExternalId, token: nil) |
| 126 | + |
| 127 | + // First attempt: Try to fetch IAMs without OneSignal ID (should be deferred) |
| 128 | + OneSignalInAppMessages.getFromServer(testSubscriptionId) |
| 129 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 130 | + |
| 131 | + // Verify the subscription ID was stored and no IAM fetch occurred |
| 132 | + XCTAssertEqual(controller.value(forKey: "shouldFetchOnUserChangeWithSubscriptionID") as! String, testSubscriptionId) |
| 133 | + XCTAssertTrue(client.hasExecutedRequestOfType(OSRequestGetInAppMessages.self, expectedCount: 0)) |
| 134 | + |
| 135 | + // Now let the login succeed, receive onesignal ID which fires user state observer |
| 136 | + MockUserRequests.setDefaultIdentifyUserResponses(with: client, externalId: testExternalId) |
| 137 | + OneSignalUserManagerImpl.sharedInstance.userExecutor?.userRequestQueue.first?.sentToClient = false |
| 138 | + OneSignalUserManagerImpl.sharedInstance.userExecutor?.executePendingRequests() |
| 139 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 140 | + |
| 141 | + /* Verify */ |
| 142 | + // The fetch should have been retried now that OneSignal ID is available |
| 143 | + XCTAssertTrue(client.hasExecutedRequestOfType(OSRequestGetInAppMessages.self, expectedCount: 1)) |
| 144 | + |
| 145 | + // The stored subscription ID should be cleared after successful retry |
| 146 | + XCTAssertNil(controller.value(forKey: "shouldFetchOnUserChangeWithSubscriptionID")) |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + Test that when user state changes but shouldFetchOnUserChangeWithSubscriptionID is not set, it does nothing. |
| 151 | + |
| 152 | + Scenario: |
| 153 | + - Normal user state change occurs |
| 154 | + - No deferred fetch was pending |
| 155 | + |
| 156 | + Expected: |
| 157 | + - No retry logic is triggered |
| 158 | + - Normal operation continues |
| 159 | + */ |
| 160 | + func testDoesNothingWhenNoRetryPending() throws { |
| 161 | + /* Setup */ |
| 162 | + let client = MockOneSignalClient() |
| 163 | + OneSignalCoreImpl.setSharedClient(client) |
| 164 | + OneSignalInAppMessages.start() |
| 165 | + let controller = OSMessagingController.sharedInstance() |
| 166 | + |
| 167 | + // Set up user with valid OneSignal ID from the start |
| 168 | + MockUserRequests.setDefaultCreateAnonUserResponses( |
| 169 | + with: client, |
| 170 | + onesignalId: testOneSignalId, |
| 171 | + subscriptionId: testSubscriptionId |
| 172 | + ) |
| 173 | + ConsistencyManagerTestHelpers.setDefaultRywToken(id: testOneSignalId) |
| 174 | + OneSignalUserManagerImpl.sharedInstance.start() |
| 175 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 176 | + |
| 177 | + /* Verify */ |
| 178 | + // IAM is fetched and no retry is pending |
| 179 | + XCTAssertTrue(client.hasExecutedRequestOfType(OSRequestGetInAppMessages.self, expectedCount: 1)) |
| 180 | + XCTAssertNil(controller.value(forKey: "shouldFetchOnUserChangeWithSubscriptionID")) |
| 181 | + |
| 182 | + /* Execute */ |
| 183 | + // Trigger a normal user state change by login |
| 184 | + MockUserRequests.setDefaultIdentifyUserResponses(with: client, externalId: testExternalId) |
| 185 | + OneSignalUserManagerImpl.sharedInstance.login(externalId: testExternalId, token: nil) |
| 186 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 187 | + |
| 188 | + /* Verify */ |
| 189 | + // Does not fetch IAMs again |
| 190 | + XCTAssertTrue(client.hasExecutedRequestOfType(OSRequestGetInAppMessages.self, expectedCount: 1)) |
| 191 | + } |
| 192 | +} |
0 commit comments