Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
* Remove test on Update Subscription with JWT; it does not use User JWT
* Make some changes to existing tests
  • Loading branch information
nan-li committed Sep 26, 2024
1 parent c27842c commit 74e4ef7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ open class OneSignalExecutorMocks: NSObject {
externalId: externalId,
pushSubscriptionModel: OSSubscriptionModel(type: .push, address: "", subscriptionId: testPushSubId, reachable: false, isDisabled: false, changeNotifier: OSEventProducer()))
if let onesignalId = onesignalId {
user.identityModel.addAliases([OS_ONESIGNAL_ID: onesignalId])
// Setting the OSID directly avoids generating a Delta
user.identityModel.aliases[OS_ONESIGNAL_ID] = onesignalId
}
return user
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ final class IdentityExecutorTests: XCTestCase {
let addAliasRequests = mocks.client.executedRequests.filter { request in
request.isKind(of: OSRequestAddAliases.self)
}
// It is 4 because setting user B's OneSignal ID counts as an add alias request
XCTAssertEqual(addAliasRequests.count, 4)
XCTAssertEqual(addAliasRequests.count, 3)
}

func testRemoveAliasRequests_RetryRequests_OnTokenUpdate_ForOnlyUpdatedUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,6 @@ final class SubscriptionExecutorTests: XCTestCase {
XCTAssertTrue(invalidatedCallbackWasCalled)
}

func testUpdateSubscription_IdentityVerificationRequired_withInvalidToken() {
/* Setup */
let mocks = Mocks()
mocks.setAuthRequired(true)
OneSignalUserManagerImpl.sharedInstance.operationRepo.paused = true

let user = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID)
user.identityModel.jwtBearerToken = userA_InvalidJwtToken
let token = testPushToken
MockUserRequests.setUnauthorizedUpdateSubscriptionFailureResponse(with: mocks.client, token: token)
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))

var invalidatedCallbackWasCalled = false
OneSignalUserManagerImpl.sharedInstance.User.onJwtInvalidated { _ in
invalidatedCallbackWasCalled = true
}

/* When */
mocks.subscriptionExecutor.processDeltaQueue(inBackground: false)
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)

/* Then */
XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestUpdateSubscription.self))
XCTAssertTrue(invalidatedCallbackWasCalled)
}

func testCreateSubscriptionRequests_Retry_OnTokenUpdate() {
/* Setup */
let mocks = Mocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,11 @@ final class UserExecutorTests: XCTestCase {

mocks.setAuthRequired(true)

_ = mocks.setUserManagerInternalUser(externalId: userA_EUID)
let userA = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID)
// We need to use the user manager's executor because the onJWTUpdated callback won't fire on the mock executor
let executor = OneSignalUserManagerImpl.sharedInstance.userExecutor!

let userAIdentityModel = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: userA_OSID, OS_EXTERNAL_ID: userA_EUID], changeNotifier: OSEventProducer())
userAIdentityModel.jwtBearerToken = userA_InvalidJwtToken
userA.identityModel.jwtBearerToken = userA_InvalidJwtToken

MockUserRequests.setUnauthorizedFetchUserFailureResponses(with: mocks.client, onesignalId: userA_OSID)
MockUserRequests.setUnauthorizedCreateUserFailureResponses(with: mocks.client, externalId: userA_EUID)
Expand All @@ -364,8 +363,8 @@ final class UserExecutorTests: XCTestCase {
}

/* When */
executor.fetchUser(onesignalId: userA_OSID, identityModel: userAIdentityModel)
executor.createUser(aliasLabel: OS_EXTERNAL_ID, aliasId: userA_EUID, identityModel: userAIdentityModel)
executor.fetchUser(onesignalId: userA_OSID, identityModel: userA.identityModel)
executor.createUser(aliasLabel: OS_EXTERNAL_ID, aliasId: userA_EUID, identityModel: userA.identityModel)
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)

MockUserRequests.setDefaultFetchUserResponseForHydration(with: mocks.client, externalId: userA_EUID)
Expand All @@ -379,24 +378,34 @@ final class UserExecutorTests: XCTestCase {
XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestFetchUser.self))
XCTAssertTrue(mocks.client.hasExecutedRequestOfType(OSRequestCreateUser.self))
XCTAssertTrue(invalidatedCallbackWasCalled)
XCTAssertEqual(mocks.client.networkRequestCount, 4)
/*
Create and Fetch requests that fail
Create and Fetch requests that pass
Follow up Fetch made after the success of the Create request
*/
XCTAssertEqual(mocks.client.networkRequestCount, 5)
}

/**
This test executes a Fetch on userA, and a Create on userB, encountering an unauthorized response for both requests.
The test next updates the JWT token for userA only.
It expects only the Fetch userA request to be sent next.
*/
func testUserRequests_RetryRequests_OnTokenUpdate_ForOnlyUpdatedUser() {
/* Setup */
let mocks = Mocks()

mocks.setAuthRequired(true)

_ = mocks.setUserManagerInternalUser(externalId: userA_EUID)
let userA = mocks.setUserManagerInternalUser(externalId: userA_EUID, onesignalId: userA_OSID)
// We need to use the user manager's executor because the onJWTUpdated callback won't fire on the mock executor
let executor = OneSignalUserManagerImpl.sharedInstance.userExecutor!

let userAIdentityModel = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: userA_OSID, OS_EXTERNAL_ID: userA_EUID], changeNotifier: OSEventProducer())
userAIdentityModel.jwtBearerToken = userA_InvalidJwtToken
userA.identityModel.jwtBearerToken = userA_InvalidJwtToken

let userBIdentityModel = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: userB_OSID, OS_EXTERNAL_ID: userB_EUID], changeNotifier: OSEventProducer())
userBIdentityModel.jwtBearerToken = userA_InvalidJwtToken
OneSignalUserManagerImpl.sharedInstance.addIdentityModelToRepo(userBIdentityModel)

MockUserRequests.setUnauthorizedFetchUserFailureResponses(with: mocks.client, onesignalId: userA_OSID)
MockUserRequests.setUnauthorizedCreateUserFailureResponses(with: mocks.client, externalId: userB_EUID)
Expand All @@ -407,7 +416,7 @@ final class UserExecutorTests: XCTestCase {
}

/* When */
executor.fetchUser(onesignalId: userA_OSID, identityModel: userAIdentityModel)
executor.fetchUser(onesignalId: userA_OSID, identityModel: userA.identityModel)
executor.createUser(aliasLabel: OS_EXTERNAL_ID, aliasId: userB_EUID, identityModel: userBIdentityModel)
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ final class UserConcurrencyTests: XCTestCase {
}

// 4. Run background threads
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 2)

/* Then */
// Previously caused crash: signal SIGABRT - malloc: double free for ptr
Expand Down Expand Up @@ -165,7 +165,7 @@ final class UserConcurrencyTests: XCTestCase {
}

// 4. Run background threads
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 2)

/* Then */
// Previously caused crash: signal SIGABRT - malloc: double free for ptr
Expand Down

0 comments on commit 74e4ef7

Please sign in to comment.