|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2026 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 OneSignalCore |
| 30 | +import OneSignalCoreMocks |
| 31 | +import OneSignalUserMocks |
| 32 | +@testable import OneSignalOSCore |
| 33 | +@testable import OneSignalUser |
| 34 | + |
| 35 | +/** |
| 36 | + Regression coverage for stale UpdateSubscription races after permission grant. |
| 37 | + */ |
| 38 | +final class SubscriptionUpdateRaceTests: XCTestCase { |
| 39 | + |
| 40 | + private let subscriptionId = "test-subscription-id" |
| 41 | + private let pushToken = "test-push-token" |
| 42 | + /// Typical iOS alert+badge+sound permission bitmask after Accept. |
| 43 | + private let subscribedNotificationTypes = 31 |
| 44 | + private let promptedNeverAnswered = Int(ERROR_PUSH_PROMPT_NEVER_ANSWERED) |
| 45 | + |
| 46 | + override func setUpWithError() throws { |
| 47 | + OneSignalCoreMocks.clearUserDefaults() |
| 48 | + OneSignalUserMocks.reset() |
| 49 | + OneSignalIdentifiers.currentAppId = "test-app-id" |
| 50 | + OneSignalLog.setLogLevel(.LL_VERBOSE) |
| 51 | + } |
| 52 | + |
| 53 | + override func tearDownWithError() throws { } |
| 54 | + |
| 55 | + /** |
| 56 | + Parameters are built from the live model at init and refreshed again in prepareForExecution before send |
| 57 | + */ |
| 58 | + func testPrepareForExecutionRefreshesEnabledAndNotificationTypesFromLiveModel() throws { |
| 59 | + let model = makePushSubscriptionModel(notificationTypes: promptedNeverAnswered, subscriptionId: subscriptionId) |
| 60 | + XCTAssertFalse(model.enabled) |
| 61 | + |
| 62 | + let request = OSRequestUpdateSubscription(subscriptionModel: model) |
| 63 | + let atInit = try XCTUnwrap(request.parameters?["subscription"] as? [String: Any]) |
| 64 | + XCTAssertEqual(atInit["notification_types"] as? Int, promptedNeverAnswered) |
| 65 | + XCTAssertEqual(atInit["enabled"] as? Bool, false) |
| 66 | + |
| 67 | + // Permission granted — live model is now subscribed. |
| 68 | + model.notificationTypes = subscribedNotificationTypes |
| 69 | + XCTAssertTrue(model.enabled) |
| 70 | + |
| 71 | + XCTAssertTrue(request.prepareForExecution(newRecordsState: OSNewRecordsState())) |
| 72 | + |
| 73 | + let refreshed = try XCTUnwrap(request.parameters?["subscription"] as? [String: Any]) |
| 74 | + XCTAssertEqual(refreshed["notification_types"] as? Int, subscribedNotificationTypes) |
| 75 | + XCTAssertEqual(refreshed["enabled"] as? Bool, true) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + Regression: a pre-permission UpdateSubscription left pending must not send `-19` |
| 80 | + after permission is granted. Coalesce + live refresh should yield only subscribed payload(s). |
| 81 | + */ |
| 82 | + func testPendingPrePermissionUpdateSendsLiveSubscribedPayloadAfterGrant() throws { |
| 83 | + let client = MockOneSignalClient() |
| 84 | + client.executeInstantaneously = false |
| 85 | + client.fireSuccessForAllRequests = true |
| 86 | + OneSignalCoreImpl.setSharedClient(client) |
| 87 | + |
| 88 | + let executor = OSSubscriptionOperationExecutor(newRecordsState: OSNewRecordsState()) |
| 89 | + // Without a subscriptionId, prepareForExecution keeps the update pending. |
| 90 | + let model = makePushSubscriptionModel(notificationTypes: promptedNeverAnswered, subscriptionId: nil) |
| 91 | + let identityModelId = UUID().uuidString |
| 92 | + |
| 93 | + executor.enqueueDelta(OSDelta( |
| 94 | + name: OS_UPDATE_SUBSCRIPTION_DELTA, |
| 95 | + identityModelId: identityModelId, |
| 96 | + model: model, |
| 97 | + property: "notificationTypes", |
| 98 | + value: promptedNeverAnswered |
| 99 | + )) |
| 100 | + executor.processDeltaQueue(inBackground: false) |
| 101 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.2) |
| 102 | + |
| 103 | + XCTAssertTrue(client.executedRequests.isEmpty, "Update should still be pending without subscriptionId") |
| 104 | + |
| 105 | + // User accepts — live model subscribed — then hydrate subscription id and flush again. |
| 106 | + model.notificationTypes = subscribedNotificationTypes |
| 107 | + model.subscriptionId = subscriptionId |
| 108 | + XCTAssertTrue(model.enabled) |
| 109 | + |
| 110 | + executor.enqueueDelta(OSDelta( |
| 111 | + name: OS_UPDATE_SUBSCRIPTION_DELTA, |
| 112 | + identityModelId: identityModelId, |
| 113 | + model: model, |
| 114 | + property: "notificationTypes", |
| 115 | + value: subscribedNotificationTypes |
| 116 | + )) |
| 117 | + executor.processDeltaQueue(inBackground: false) |
| 118 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 119 | + |
| 120 | + let updateRequests = client.executedRequests.compactMap { $0 as? OSRequestUpdateSubscription } |
| 121 | + XCTAssertFalse(updateRequests.isEmpty, "Expected at least one UpdateSubscription after id hydration") |
| 122 | + |
| 123 | + let payloads = updateRequests.compactMap { $0.parameters?["subscription"] as? [String: Any] } |
| 124 | + let sentMinus19 = payloads.contains { |
| 125 | + ($0["notification_types"] as? Int) == promptedNeverAnswered && ($0["enabled"] as? Bool) == false |
| 126 | + } |
| 127 | + let allSubscribed = payloads.allSatisfy { |
| 128 | + ($0["notification_types"] as? Int) == subscribedNotificationTypes && ($0["enabled"] as? Bool) == true |
| 129 | + } |
| 130 | + |
| 131 | + XCTAssertFalse(sentMinus19, "Must not send stale enabled:false / notification_types:-19 after grant") |
| 132 | + XCTAssertTrue(allSubscribed, "All executed UpdateSubscription payloads should reflect the live subscribed model") |
| 133 | + XCTAssertEqual(updateRequests.count, 1, "Unsent updates for the same subscription should be coalesced") |
| 134 | + } |
| 135 | + |
| 136 | + // MARK: - Helpers |
| 137 | + |
| 138 | + private func makePushSubscriptionModel(notificationTypes: Int, subscriptionId: String?) -> OSSubscriptionModel { |
| 139 | + let model = OSSubscriptionModel( |
| 140 | + type: .push, |
| 141 | + address: pushToken, |
| 142 | + subscriptionId: subscriptionId, |
| 143 | + reachable: notificationTypes > 0, |
| 144 | + isDisabled: false, |
| 145 | + changeNotifier: OSEventProducer() |
| 146 | + ) |
| 147 | + model.notificationTypes = notificationTypes |
| 148 | + return model |
| 149 | + } |
| 150 | +} |
0 commit comments