Skip to content

Commit

Permalink
Added an additional test case for excluded_devices (#135)
Browse files Browse the repository at this point in the history
Added an additional test case for excluded_devices
PubNub SDK 6.0.5 release
  • Loading branch information
jguz-pubnub authored May 18, 2023
1 parent 68e4347 commit 4b2596e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
9 changes: 7 additions & 2 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
---
name: swift
scm: github.com/pubnub/swift
version: "6.0.4"
version: "6.0.5"
schema: 1
changelog:
- date: 2023-05-18
version: 6.0.5
changes:
- type: bug
text: "Uppercasing of the `excluded_devices` field to match the format expected by the REST API."
- date: 2023-03-16
version: 6.0.4
changes:
Expand Down Expand Up @@ -480,7 +485,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: PubNub
location: https://github.com/pubnub/swift/archive/refs/tags/6.0.4.zip
location: https://github.com/pubnub/swift/archive/refs/tags/6.0.5.zip
supported-platforms:
supported-operating-systems:
macOS:
Expand Down
16 changes: 8 additions & 8 deletions PubNub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3256,7 +3256,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pubnub.swift.PubNubUser;
Expand Down Expand Up @@ -3303,7 +3303,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pubnub.swift.PubNubUser;
Expand Down Expand Up @@ -3403,7 +3403,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pubnub.swift.PubNubSpace;
Expand Down Expand Up @@ -3452,7 +3452,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pubnub.swift.PubNubSpace;
Expand Down Expand Up @@ -3565,7 +3565,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pubnub.swift.PubNubMembership;
Expand Down Expand Up @@ -3613,7 +3613,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pubnub.swift.PubNubMembership;
Expand Down Expand Up @@ -4069,7 +4069,7 @@
"$(inherited)",
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down Expand Up @@ -4108,7 +4108,7 @@
"$(inherited)",
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
MARKETING_VERSION = 6.0.4;
MARKETING_VERSION = 6.0.5;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down
2 changes: 1 addition & 1 deletion PubNubSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PubNubSwift'
s.version = '6.0.4'
s.version = '6.0.5'
s.homepage = 'https://github.com/pubnub/swift'
s.documentation_url = 'https://www.pubnub.com/docs/swift-native/pubnub-swift-sdk'
s.authors = { 'PubNub, Inc.' => '[email protected]' }
Expand Down
2 changes: 1 addition & 1 deletion Sources/PubNub/Helpers/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum Constant {

static let pubnubSwiftSDKName: String = "PubNubSwift"

static let pubnubSwiftSDKVersion: String = "6.0.4"
static let pubnubSwiftSDKVersion: String = "6.0.5"

static let appBundleId: String = {
if let info = Bundle.main.infoDictionary,
Expand Down
63 changes: 34 additions & 29 deletions Tests/PubNubTests/Push/PubNubPushTargetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,42 @@ import XCTest

class PubNubPushTargetTests: XCTestCase {
func test_ExcludedDeviceTokensAreUppercased() {
let excludedDevices = [
"fafb34563ce",
"7654eghjkl"
]
let pushMessage = PubNubPushMessage(
apns: PubNubAPNSPayload(
aps: APSPayload(
alert: .object(.init(title: "Message")),
badge: 2,
sound: .string("default")
),
pubnub: [.init(
targets: [.init(topic: "com.pubnub.swift", environment: .production, excludedDevices: excludedDevices)],
collapseID: "SwiftSDK"
)],
payload: "Push Message from PubNub Swift SDK"
)
)
let excludedDevices = ["fafb3456", "7654egh"]
let message = testPushMessage(with: .init(topic: "com.pubnub", environment: .production, excludedDevices: excludedDevices))

let encodedData = try? Constant.jsonEncoder.encode(pushMessage)
let encodedData = try? Constant.jsonEncoder.encode(message)
let encodedStr = String(data: encodedData ?? Data(), encoding: .utf8) ?? ""
let expectedExclDevices = excludedDevices.map { $0.uppercased() }.jsonStringify ?? ""

// Retrieves a value for the "excluded_devices" key:
let regex = try! NSRegularExpression(pattern: "(?<=\"excluded_devices\":)(.*?)](=?)")
let regexMatch = regex.matches(in: encodedStr, range: NSRange(location: 0, length: encodedStr.count))[0]
let startIdx = encodedStr.index(encodedStr.startIndex, offsetBy: regexMatch.range.location)
let endIdx = encodedStr.index(startIdx, offsetBy: regexMatch.range.length - 1)

XCTAssertEqual(
String(encodedStr[startIdx...endIdx]),
excludedDevices.map { $0.uppercased() }.jsonStringify ?? ""
)
XCTAssertEqual(retrieveExcludedDevicesValue(from: encodedStr), expectedExclDevices)
}

func test_ExcludedDeviceTokensAreNotSentIfNotProvided() {
let message = testPushMessage(with: .init(topic: "com.pubnub", environment: .production))
let encodedData = try? Constant.jsonEncoder.encode(message)
let encodedStr = String(data: encodedData ?? Data(), encoding: .utf8) ?? ""

XCTAssertEqual(retrieveExcludedDevicesValue(from: encodedStr), nil)
}
}

fileprivate func testPushMessage(with target: PubNubPushTarget) -> PubNubAPNSPayload {
PubNubAPNSPayload(
aps: APSPayload(alert: .object(.init(title: "Apple Message")), badge: 1, sound: .string("default")),
pubnub: [.init(targets: [target], collapseID: "SwiftSDK")],
payload: "Push Message from PubNub Swift SDK"
)
}

fileprivate func retrieveExcludedDevicesValue(from string: String) -> String? {
let regex = try! NSRegularExpression(pattern: "(?<=\"excluded_devices\":)(.*?)](=?)")
let regexMatch = regex.matches(in: string, range: NSRange(location: 0, length: string.count)).first

if let regexMatch = regexMatch {
let startIdx = string.index(string.startIndex, offsetBy: regexMatch.range.location)
let endIdx = string.index(startIdx, offsetBy: regexMatch.range.length - 1)
return String(string[startIdx...endIdx])
} else {
return nil
}
}

0 comments on commit 4b2596e

Please sign in to comment.