|
| 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 propertyExecutor: OSPropertyOperationExecutor! |
| 38 | + |
| 39 | + override init() { |
| 40 | + super.init() |
| 41 | + propertyExecutor = OSPropertyOperationExecutor(newRecordsState: newRecordsState, jwtConfig: jwtConfig) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +final class PropertyExecutorTests: 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 testUpdateTagsSendsWhenProcessed() { |
| 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 tags = ["testUserA" : "true"] |
| 66 | + MockUserRequests.setAddTagsResponse(with: mocks.client, tags: tags) |
| 67 | + mocks.propertyExecutor.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: user.identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "tags", value:tags)) |
| 68 | + |
| 69 | + /* When */ |
| 70 | + mocks.propertyExecutor.processDeltaQueue(inBackground: false) |
| 71 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 72 | + |
| 73 | + /* Then */ |
| 74 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestUpdateProperties.self)) |
| 75 | + } |
| 76 | + |
| 77 | + func testUpdateTags_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 tags = ["testUserA" : "true"] |
| 85 | + MockUserRequests.setAddTagsResponse(with: mocks.client, tags: tags) |
| 86 | + mocks.propertyExecutor.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: user.identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "tags", value:tags)) |
| 87 | + |
| 88 | + /* When */ |
| 89 | + mocks.propertyExecutor.processDeltaQueue(inBackground: false) |
| 90 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 91 | + |
| 92 | + /* Then */ |
| 93 | + XCTAssertFalse(mocks.client.hasExecutedRequestOfType(OSRequestUpdateProperties.self)) |
| 94 | + } |
| 95 | + |
| 96 | + func testUpdateTags_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 tags = ["testUserA" : "true"] |
| 105 | + MockUserRequests.setAddTagsResponse(with: mocks.client, tags: tags) |
| 106 | + mocks.propertyExecutor.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: user.identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "tags", value:tags)) |
| 107 | + |
| 108 | + /* When */ |
| 109 | + mocks.propertyExecutor.processDeltaQueue(inBackground: false) |
| 110 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 111 | + |
| 112 | + /* Then */ |
| 113 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestUpdateProperties.self)) |
| 114 | + } |
| 115 | + |
| 116 | + func testCreateUser_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 | + |
| 125 | + |
| 126 | + |
| 127 | + let tags = ["testUserA" : "true"] |
| 128 | + MockUserRequests.setUnauthorizedUpdatePropertiesFailureResponses(with: mocks.client, tags: tags) |
| 129 | + mocks.propertyExecutor.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: user.identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "tags", value:tags)) |
| 130 | + |
| 131 | + var invalidatedCallbackWasCalled = false |
| 132 | + OneSignalUserManagerImpl.sharedInstance.User.onJwtInvalidated { event in |
| 133 | + XCTAssertTrue(event.message == "token has invalid claims: token is expired") |
| 134 | + invalidatedCallbackWasCalled = true |
| 135 | + } |
| 136 | + |
| 137 | + /* When */ |
| 138 | + mocks.propertyExecutor.processDeltaQueue(inBackground: false) |
| 139 | + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) |
| 140 | + |
| 141 | + /* Then */ |
| 142 | + XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestUpdateProperties.self)) |
| 143 | + XCTAssertTrue(invalidatedCallbackWasCalled) |
| 144 | + } |
| 145 | +} |
0 commit comments