Skip to content

Commit

Permalink
[tests] add tests of new records / delays after create
Browse files Browse the repository at this point in the history
  • Loading branch information
nan-li committed Aug 16, 2024
1 parent 80d940f commit dee1bcf
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 12 deletions.
16 changes: 16 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
3CEE93542B7C78EC008440BD /* OneSignalUser.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DE69E19B282ED8060090BB3D /* OneSignalUser.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3CEE93572B7C78FD008440BD /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
3CEE93582B7C78FE008440BD /* OneSignalCore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3CF11E3D2C6D6155002856F5 /* UserExecutorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF11E3C2C6D6155002856F5 /* UserExecutorTests.swift */; };
3CF11E402C6E6DE2002856F5 /* MockNewRecordsState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF11E3F2C6E6DE2002856F5 /* MockNewRecordsState.swift */; };
3CF1A5632C669EA40056B3AA /* OSNewRecordsState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF1A5622C669EA40056B3AA /* OSNewRecordsState.swift */; };
3CF8629E28A183F900776CA4 /* OSIdentityModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF8629D28A183F900776CA4 /* OSIdentityModel.swift */; };
3CF862A028A1964F00776CA4 /* OSPropertiesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF8629F28A1964F00776CA4 /* OSPropertiesModel.swift */; };
Expand Down Expand Up @@ -1295,6 +1297,8 @@
3CE92279289FA88B001B1062 /* OSIdentityModelStoreListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModelStoreListener.swift; sourceTree = "<group>"; };
3CEE90A62BFE6ABD00B0FB5B /* OSPropertiesSupportedProperty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSPropertiesSupportedProperty.swift; sourceTree = "<group>"; };
3CEE90A82C000BD500B0FB5B /* OneSignalRequest+UnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OneSignalRequest+UnitTests.swift"; sourceTree = "<group>"; };
3CF11E3C2C6D6155002856F5 /* UserExecutorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserExecutorTests.swift; sourceTree = "<group>"; };
3CF11E3F2C6E6DE2002856F5 /* MockNewRecordsState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNewRecordsState.swift; sourceTree = "<group>"; };
3CF1A5622C669EA40056B3AA /* OSNewRecordsState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSNewRecordsState.swift; sourceTree = "<group>"; };
3CF8629D28A183F900776CA4 /* OSIdentityModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModel.swift; sourceTree = "<group>"; };
3CF8629F28A1964F00776CA4 /* OSPropertiesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSPropertiesModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2073,6 +2077,7 @@
children = (
3C8544B82C5AEFF700F542A9 /* OneSignalOSCoreMocks.h */,
3C8544C22C5AF18B00F542A9 /* OSCoreMocks.swift */,
3CF11E3F2C6E6DE2002856F5 /* MockNewRecordsState.swift */,
);
path = OneSignalOSCoreMocks;
sourceTree = "<group>";
Expand Down Expand Up @@ -2153,6 +2158,7 @@
isa = PBXGroup;
children = (
3CDE664A2BFC2A55006DA114 /* OneSignalUserTests-Bridging-Header.h */,
3CF11E3E2C6D61AC002856F5 /* Executors */,
3CC063ED2B6D7FE8002BB07F /* OneSignalUserTests.swift */,
3CC890342C5BF9A7002CB4CC /* UserConcurrencyTests.swift */,
3C67F7792BEB2B710085A0F0 /* SwitchUserIntegrationTests.swift */,
Expand All @@ -2169,6 +2175,14 @@
path = Support;
sourceTree = "<group>";
};
3CF11E3E2C6D61AC002856F5 /* Executors */ = {
isa = PBXGroup;
children = (
3CF11E3C2C6D6155002856F5 /* UserExecutorTests.swift */,
);
path = Executors;
sourceTree = "<group>";
};
3E2400391D4FFC31008BDE70 /* OneSignalFramework */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4058,6 +4072,7 @@
buildActionMask = 2147483647;
files = (
3C8544C32C5AF18B00F542A9 /* OSCoreMocks.swift in Sources */,
3CF11E402C6E6DE2002856F5 /* MockNewRecordsState.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -4096,6 +4111,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3CF11E3D2C6D6155002856F5 /* UserExecutorTests.swift in Sources */,
3C67F77A2BEB2B710085A0F0 /* SwitchUserIntegrationTests.swift in Sources */,
3CC063EE2B6D7FE8002BB07F /* OneSignalUserTests.swift in Sources */,
3CC890352C5BF9A7002CB4CC /* UserConcurrencyTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ public class MockOneSignalClient: NSObject, IOneSignalClient {
public func execute(_ request: OneSignalRequest, onSuccess successBlock: @escaping OSResultSuccessBlock, onFailure failureBlock: @escaping OSFailureBlock) {
print("🧪 MockOneSignalClient execute called")

lock.withLock {
executedRequests.append(request)
}

if executeInstantaneously {
finishExecutingRequest(request, onSuccess: successBlock, onFailure: failureBlock)
} else {
Expand Down Expand Up @@ -127,6 +123,9 @@ public class MockOneSignalClient: NSObject, IOneSignalClient {
print("🧪 completing HTTP request: \(request)")

// TODO: Check for existence of app_id in the request and fail if not.
lock.withLock {
executedRequests.append(request)
}

self.didCompleteRequest(request)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Modified MIT License
Copyright 2024 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

@testable import OneSignalOSCore

public class MockNewRecordsState: OSNewRecordsState {
public struct MockNewRecord {
let key: String
let overwrite: Bool
}

public var records: [MockNewRecord] = []

override public func add(_ key: String, _ overwrite: Bool = false) {
let record = MockNewRecord(key: key, overwrite: overwrite)
records.append(record)

super.add(key, overwrite)
}

override public func canAccess(_ key: String?) -> Bool {
return super.canAccess(key)
}

public func get(_ key: String?) -> [MockNewRecord] {
return records.filter { $0.key == key }
}

public func contains(_ key: String?) -> Bool {
return get(key).count > 0
}

public func wasOverwritten(_ key: String?) -> Bool {
return records.filter { $0.key == key && $0.overwrite }.count > 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
/**
Creates and sets a blank new SDK user with the provided externalId, if any.
*/
private func setNewInternalUser(externalId: String?, pushSubscriptionModel: OSSubscriptionModel?) -> OSUserInternal {
func setNewInternalUser(externalId: String?, pushSubscriptionModel: OSSubscriptionModel?) -> OSUserInternal {
let aliases: [String: String]?
if let externalIdToUse = externalId {
aliases = [OS_EXTERNAL_ID: externalIdToUse]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OSRequestAddAliases: OneSignalRequest, OSUserRequest {
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
if let onesignalId = identityModel.onesignalId,
newRecordsState.canAccess(onesignalId),
let appId = OneSignalConfigManager.getAppId()
let appId = OneSignalConfigManager.getAppId()
{
self.addJWTHeader(identityModel: identityModel)
self.path = "apps/\(appId)/users/by/\(OS_ONESIGNAL_ID)/\(onesignalId)/identity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OSRequestRemoveAlias: OneSignalRequest, OSUserRequest {
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
if let onesignalId = identityModel.onesignalId,
newRecordsState.canAccess(onesignalId),
let appId = OneSignalConfigManager.getAppId()
let appId = OneSignalConfigManager.getAppId()
{
self.addJWTHeader(identityModel: identityModel)
self.path = "apps/\(appId)/users/by/\(OS_ONESIGNAL_ID)/\(onesignalId)/identity/\(labelToRemove)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MockUserRequests: NSObject {

public static func testDefaultPushSubPayload(id: String) -> [String: Any] {
return [
"id": testPushSubId,
"id": id,
"app_id": "test-app-id",
"type": "iOSPush",
"token": "",
Expand All @@ -43,7 +43,7 @@ public class MockUserRequests: NSObject {

public static func testDefaultFullCreateUserResponse(onesignalId: String, externalId: String?, subscriptionId: String?) -> [String: Any] {
let identity = testIdentityPayload(onesignalId: onesignalId, externalId: externalId)
let subscription = testDefaultPushSubPayload(id: testPushSubId)
let subscription = testDefaultPushSubPayload(id: subscriptionId ?? testPushSubId)
let properties = [
"language": "en",
"timezone_id": "America/Los_Angeles",
Expand Down Expand Up @@ -84,11 +84,10 @@ extension MockUserRequests {
response: anonCreateResponse)
}

public static func setDefaultCreateUserResponses(with client: MockOneSignalClient, externalId: String) {
public static func setDefaultCreateUserResponses(with client: MockOneSignalClient, externalId: String, subscriptionId: String? = nil) {
let osid = getOneSignalId(for: externalId)

let userResponse = MockUserRequests.testIdentityPayload(onesignalId: osid, externalId: externalId)

let userResponse = testDefaultFullCreateUserResponse(onesignalId: osid, externalId: externalId, subscriptionId: subscriptionId)
client.setMockResponseForRequest(
request: "<OSRequestCreateUser with external_id: \(externalId)>",
response: userResponse
Expand Down
Loading

0 comments on commit dee1bcf

Please sign in to comment.