|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2024 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 OneSignalOSCore |
| 31 | +import OneSignalCoreMocks |
| 32 | +import OneSignalOSCoreMocks |
| 33 | +import OneSignalUserMocks |
| 34 | +@testable import OneSignalUser |
| 35 | + |
| 36 | +private class Mocks: OneSignalExecutorMocks { |
| 37 | + var subscriptionExecutor: OSSubscriptionOperationExecutor! |
| 38 | + |
| 39 | + override init() { |
| 40 | + super.init() |
| 41 | + subscriptionExecutor = OSSubscriptionOperationExecutor(newRecordsState: newRecordsState, jwtConfig: jwtConfig) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +final class SubscriptionExecutorTests: XCTestCase { |
| 46 | + |
| 47 | + override func setUpWithError() throws { |
| 48 | + OneSignalCoreMocks.clearUserDefaults() |
| 49 | + OneSignalUserMocks.reset() |
| 50 | + // App ID is set because requests have guards against null App ID |
| 51 | + OneSignalConfigManager.setAppId("test-app-id") |
| 52 | + // Temp. logging to help debug during testing |
| 53 | + OneSignalLog.setLogLevel(.LL_VERBOSE) |
| 54 | + } |
| 55 | + |
| 56 | + override func tearDownWithError() throws { } |
| 57 | + |
| 58 | + func testAddEmailSendsWhenProcessed() { |
| 59 | + /* Setup */ |
| 60 | + let mocks = Mocks() |
| 61 | + mocks.setAuthRequired(false) |
| 62 | + OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true |
| 63 | + |
| 64 | + let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID) |
| 65 | + let email = userA_email |
| 66 | + MockUserRequests.setAddEmailResponse(with: mocks.client, email: email) |
| 67 | + mocks.subscriptionExecutor.enqueueDelta(OSDelta(name: OS_ADD_SUBSCRIPTION_DELTA, identityModelId: user.identityModel.modelId, model: OSSubscriptionModel(type: .email, address: email, subscriptionId: nil, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: OSSubscriptionType.email.rawValue, value:email)) |
| 68 | + |
| 69 | + /* When */ |
| 70 | + mocks.subscriptionExecutor.processDeltaQueue(inBackground: false) |
| 71 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 72 | + |
| 73 | + /* Then */ |
| 74 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestCreateSubscription.self)) |
| 75 | + } |
| 76 | + |
| 77 | + func testAddEmail_IdentityVerificationRequired_butNoToken() { |
| 78 | + /* Setup */ |
| 79 | + let mocks = Mocks() |
| 80 | + mocks.setAuthRequired(true) |
| 81 | + OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true |
| 82 | + |
| 83 | + let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID) |
| 84 | + let email = userA_email |
| 85 | + MockUserRequests.setAddEmailResponse(with: mocks.client, email: email) |
| 86 | + mocks.subscriptionExecutor.enqueueDelta(OSDelta(name: OS_ADD_SUBSCRIPTION_DELTA, identityModelId: user.identityModel.modelId, model: OSSubscriptionModel(type: .email, address: email, subscriptionId: nil, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: OSSubscriptionType.email.rawValue, value:email)) |
| 87 | + |
| 88 | + /* When */ |
| 89 | + mocks.subscriptionExecutor.processDeltaQueue(inBackground: false) |
| 90 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 91 | + |
| 92 | + /* Then */ |
| 93 | + XCTAssertFalse(mocks.client.hasExecutedRequestOfType(OSRequestCreateSubscription.self)) |
| 94 | + } |
| 95 | + |
| 96 | + func testAddEmail_IdentityVerificationRequired_withToken() { |
| 97 | + /* Setup */ |
| 98 | + let mocks = Mocks() |
| 99 | + mocks.setAuthRequired(true) |
| 100 | + OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true |
| 101 | + |
| 102 | + let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID) |
| 103 | + user.identityModel.jwtBearerToken = userA_JwtToken |
| 104 | + let email = userA_email |
| 105 | + MockUserRequests.setAddEmailResponse(with: mocks.client, email: email) |
| 106 | + mocks.subscriptionExecutor.enqueueDelta(OSDelta(name: OS_ADD_SUBSCRIPTION_DELTA, identityModelId: user.identityModel.modelId, model: OSSubscriptionModel(type: .email, address: email, subscriptionId: nil, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: OSSubscriptionType.email.rawValue, value:email)) |
| 107 | + |
| 108 | + /* When */ |
| 109 | + mocks.subscriptionExecutor.processDeltaQueue(inBackground: false) |
| 110 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 111 | + |
| 112 | + /* Then */ |
| 113 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestCreateSubscription.self)) |
| 114 | + } |
| 115 | + |
| 116 | + func testAddEmail_IdentityVerificationRequired_withInvalidToken() { |
| 117 | + /* Setup */ |
| 118 | + let mocks = Mocks() |
| 119 | + mocks.setAuthRequired(true) |
| 120 | + OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true |
| 121 | + |
| 122 | + let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID) |
| 123 | + user.identityModel.jwtBearerToken = userA_JwtToken |
| 124 | + let email = userA_email |
| 125 | + MockUserRequests.setUnauthorizedAddEmailFailureResponse(with: mocks.client, email: email) |
| 126 | + mocks.subscriptionExecutor.enqueueDelta(OSDelta(name: OS_ADD_SUBSCRIPTION_DELTA, identityModelId: user.identityModel.modelId, model: OSSubscriptionModel(type: .email, address: email, subscriptionId: nil, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: OSSubscriptionType.email.rawValue, value:email)) |
| 127 | + |
| 128 | + var invalidatedCallbackWasCalled = false |
| 129 | + OneSignalUserManagerImpl.sharedInstance.User.onJwtInvalidated { event in |
| 130 | + XCTAssertTrue(event.message == "token has invalid claims: token is expired") |
| 131 | + invalidatedCallbackWasCalled = true |
| 132 | + } |
| 133 | + |
| 134 | + /* When */ |
| 135 | + mocks.subscriptionExecutor.processDeltaQueue(inBackground: false) |
| 136 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 137 | + |
| 138 | + /* Then */ |
| 139 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestCreateSubscription.self)) |
| 140 | + XCTAssertTrue(invalidatedCallbackWasCalled) |
| 141 | + } |
| 142 | + |
| 143 | + func testDeleteEmail_IdentityVerificationRequired_withInvalidToken() { |
| 144 | + /* Setup */ |
| 145 | + let mocks = Mocks() |
| 146 | + mocks.setAuthRequired(true) |
| 147 | + OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true |
| 148 | + |
| 149 | + let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID) |
| 150 | + user.identityModel.jwtBearerToken = userA_JwtToken |
| 151 | + let email = userA_email |
| 152 | + MockUserRequests.setUnauthorizedRemoveEmailFailureResponse(with: mocks.client, email: email) |
| 153 | + mocks.subscriptionExecutor.enqueueDelta(OSDelta(name: OS_REMOVE_SUBSCRIPTION_DELTA, identityModelId: user.identityModel.modelId, model: OSSubscriptionModel(type: .email, address: email, subscriptionId: testEmailSubId, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: OSSubscriptionType.email.rawValue, value:email)) |
| 154 | + |
| 155 | + var invalidatedCallbackWasCalled = false |
| 156 | + OneSignalUserManagerImpl.sharedInstance.User.onJwtInvalidated { event in |
| 157 | + XCTAssertTrue(event.message == "token has invalid claims: token is expired") |
| 158 | + invalidatedCallbackWasCalled = true |
| 159 | + } |
| 160 | + |
| 161 | + /* When */ |
| 162 | + mocks.subscriptionExecutor.processDeltaQueue(inBackground: false) |
| 163 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 164 | + |
| 165 | + /* Then */ |
| 166 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestDeleteSubscription.self)) |
| 167 | + XCTAssertTrue(invalidatedCallbackWasCalled) |
| 168 | + } |
| 169 | + |
| 170 | + func testUpdateSubscription_IdentityVerificationRequired_withInvalidToken() { |
| 171 | + /* Setup */ |
| 172 | + let mocks = Mocks() |
| 173 | + mocks.setAuthRequired(true) |
| 174 | + OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true |
| 175 | + |
| 176 | + let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID) |
| 177 | + user.identityModel.jwtBearerToken = userA_JwtToken |
| 178 | + let token = testPushToken |
| 179 | + MockUserRequests.setUnauthorizedUpdateSubscriptionFailureResponse(with: mocks.client, token: token) |
| 180 | + mocks.subscriptionExecutor.enqueueDelta(OSDelta(name: OS_UPDATE_SUBSCRIPTION_DELTA, identityModelId: user.identityModel.modelId, model: OSSubscriptionModel(type: .push, address: token, subscriptionId: testPushSubId, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: "token", value:token)) |
| 181 | + |
| 182 | + var invalidatedCallbackWasCalled = false |
| 183 | + OneSignalUserManagerImpl.sharedInstance.User.onJwtInvalidated { event in |
| 184 | + XCTAssertTrue(event.message == "token has invalid claims: token is expired") |
| 185 | + invalidatedCallbackWasCalled = true |
| 186 | + } |
| 187 | + |
| 188 | + /* When */ |
| 189 | + mocks.subscriptionExecutor.processDeltaQueue(inBackground: false) |
| 190 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 191 | + |
| 192 | + /* Then */ |
| 193 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestUpdateSubscription.self)) |
| 194 | + XCTAssertTrue(invalidatedCallbackWasCalled) |
| 195 | + } |
| 196 | +} |
0 commit comments