Skip to content

Commit 3058767

Browse files
Add unit tests for APNSUtil.
1 parent e55c8ec commit 3058767

File tree

5 files changed

+90
-17
lines changed

5 files changed

+90
-17
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
//
3+
// Created by Tapash Majumder on 7/29/18.
4+
// Copyright © 2018 Iterable. All rights reserved.
5+
//
6+
7+
import XCTest
8+
9+
@testable import IterableSDK
10+
11+
class APNSUtilTests: XCTestCase {
12+
13+
override func setUp() {
14+
super.setUp()
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDown() {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
super.tearDown()
21+
}
22+
23+
func testValidProduction() {
24+
let path = Bundle(for: type(of:self)).path(forResource: "prod-1", ofType: "mobileprovision")!
25+
let mobileProvision = IterableAPNSUtil.readMobileProvision(fromPath: path)
26+
let isSandbox = IterableAPNSUtil.isSandboxAPNS(mobileProvision: mobileProvision, isSimulator: false)
27+
XCTAssertFalse(isSandbox)
28+
}
29+
30+
func testDev() {
31+
let path = Bundle(for: type(of:self)).path(forResource: "dev-1", ofType: "mobileprovision")!
32+
let mobileProvision = IterableAPNSUtil.readMobileProvision(fromPath: path)
33+
let isSandbox = IterableAPNSUtil.isSandboxAPNS(mobileProvision: mobileProvision, isSimulator: false)
34+
XCTAssertTrue(isSandbox)
35+
}
36+
37+
func testNoValue() {
38+
XCTAssertTrue(IterableAPNSUtil.isSandboxAPNS(mobileProvision: [:], isSimulator: true))
39+
XCTAssertFalse(IterableAPNSUtil.isSandboxAPNS(mobileProvision: [:], isSimulator: false))
40+
}
41+
}
Binary file not shown.
Binary file not shown.

swift-sdk.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
00B6FACC210E8484007535CF /* APNSUtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00B6FACB210E8484007535CF /* APNSUtilTests.swift */; };
11+
00B6FACE210E88ED007535CF /* prod-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FACD210E874D007535CF /* prod-1.mobileprovision */; };
12+
00B6FAD1210E8D90007535CF /* dev-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */; };
1013
00CB31B621096129004ACDEC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CB31B4210960C4004ACDEC /* TestUtils.swift */; };
1114
0B7C33B37D3191A8DAAD6A87 /* libPods-swift-sdk-objc-tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FD666A015C69498DD477DAEB /* libPods-swift-sdk-objc-tests.a */; };
1215
A5DF819C89A29E7F3A88DC7F /* libPods-swift-sdk-swift-tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EA3B895070C574A2D2D4FC80 /* libPods-swift-sdk-swift-tests.a */; };
@@ -109,6 +112,9 @@
109112
/* End PBXContainerItemProxy section */
110113

111114
/* Begin PBXFileReference section */
115+
00B6FACB210E8484007535CF /* APNSUtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APNSUtilTests.swift; sourceTree = "<group>"; };
116+
00B6FACD210E874D007535CF /* prod-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "prod-1.mobileprovision"; sourceTree = "<group>"; };
117+
00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "dev-1.mobileprovision"; sourceTree = "<group>"; };
112118
00CB31B4210960C4004ACDEC /* TestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
113119
0BDA0B537E9398A298EEBD89 /* Pods-swift-sdk-objc-tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-swift-sdk-objc-tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-swift-sdk-objc-tests/Pods-swift-sdk-objc-tests.debug.xcconfig"; sourceTree = "<group>"; };
114120
6BBDFB5DDE58A33B50FB1494 /* Pods-swift-sdk-objc-tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-swift-sdk-objc-tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-swift-sdk-objc-tests/Pods-swift-sdk-objc-tests.release.xcconfig"; sourceTree = "<group>"; };
@@ -230,6 +236,15 @@
230236
/* End PBXFrameworksBuildPhase section */
231237

232238
/* Begin PBXGroup section */
239+
00B6FACF210E8B10007535CF /* Test Files */ = {
240+
isa = PBXGroup;
241+
children = (
242+
00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */,
243+
00B6FACD210E874D007535CF /* prod-1.mobileprovision */,
244+
);
245+
name = "Test Files";
246+
sourceTree = "<group>";
247+
};
233248
AC0674E720D87D5B00C2806D /* Helper Files */ = {
234249
isa = PBXGroup;
235250
children = (
@@ -359,13 +374,15 @@
359374
AC7B142C20D02CE200877BFE /* swift-sdk-swift-tests */ = {
360375
isa = PBXGroup;
361376
children = (
377+
00B6FACF210E8B10007535CF /* Test Files */,
362378
AC2C668520D3370D00D46CC9 /* Helper Files */,
363379
AC2C668320D3370600D46CC9 /* Mocks.swift */,
364380
AC2C667F20D31B1F00D46CC9 /* IterableNotificationResponseTests.swift */,
365381
AC2C668620D3435700D46CC9 /* IterableActionInterpreterTests.swift */,
366382
AC6FDD8B20F56309005D811E /* IterableInAppNotificationTests.swift */,
367383
ACD6116D21080564003E7F6B /* IterableAPITests.swift */,
368384
00CB31B4210960C4004ACDEC /* TestUtils.swift */,
385+
00B6FACB210E8484007535CF /* APNSUtilTests.swift */,
369386
);
370387
path = "swift-sdk-swift-tests";
371388
sourceTree = "<group>";
@@ -664,6 +681,8 @@
664681
isa = PBXResourcesBuildPhase;
665682
buildActionMask = 2147483647;
666683
files = (
684+
00B6FACE210E88ED007535CF /* prod-1.mobileprovision in Resources */,
685+
00B6FAD1210E8D90007535CF /* dev-1.mobileprovision in Resources */,
667686
);
668687
runOnlyForDeploymentPostprocessing = 0;
669688
};
@@ -776,6 +795,7 @@
776795
files = (
777796
AC2C668720D3435700D46CC9 /* IterableActionInterpreterTests.swift in Sources */,
778797
00CB31B621096129004ACDEC /* TestUtils.swift in Sources */,
798+
00B6FACC210E8484007535CF /* APNSUtilTests.swift in Sources */,
779799
AC6FDD8C20F56309005D811E /* IterableInAppNotificationTests.swift in Sources */,
780800
AC2C668020D31B1F00D46CC9 /* IterableNotificationResponseTests.swift in Sources */,
781801
AC2C668420D3370600D46CC9 /* Mocks.swift in Sources */,

swift-sdk/IterableAPNSUtil.swift

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,34 @@ import Foundation
1010

1111
struct IterableAPNSUtil {
1212
static func isSandboxAPNS() -> Bool {
13+
#if targetEnvironment(simulator)
14+
return isSandboxAPNS(mobileProvision: mobileProvision, isSimulator: true)
15+
#else
16+
return isSandboxAPNS(mobileProvision: mobileProvision, isSimulator: false)
17+
#endif
18+
}
19+
20+
private static var mobileProvision: [AnyHashable : Any] = {
21+
readMobileProvision()
22+
}()
23+
24+
private static func readMobileProvision() -> [AnyHashable : Any] {
25+
guard let provisioningPath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision") else {
26+
print("resource not found")
27+
return [:]
28+
}
29+
30+
return readMobileProvision(fromPath: provisioningPath)
31+
}
32+
33+
static func isSandboxAPNS(mobileProvision: [AnyHashable : Any], isSimulator: Bool) -> Bool {
1334
if mobileProvision.count == 0 {
1435
// mobileprovision file not found; default to production on devices and sandbox on simulator
15-
#if targetEnvironment(simulator)
36+
if isSimulator {
1637
return true
17-
#else
38+
} else {
1839
return false
19-
#endif
40+
}
2041
} else {
2142
if
2243
let entitlements = mobileProvision["Entitlements"] as? [AnyHashable : Any],
@@ -28,22 +49,13 @@ struct IterableAPNSUtil {
2849
return false
2950
}
3051

31-
private static var mobileProvision: [AnyHashable : Any] = {
32-
readMobileProvision()
33-
}()
34-
35-
private static func readMobileProvision() -> [AnyHashable : Any] {
36-
guard let provisioningPath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision") else {
37-
print("resource not found")
38-
return [:]
39-
}
40-
41-
guard let binaryString = try? String(contentsOfFile: provisioningPath, encoding: .ascii) else {
42-
print("couldn't read from file")
52+
static func readMobileProvision(fromPath path: String) -> [AnyHashable : Any] {
53+
guard let asciiString = try? String(contentsOfFile: path, encoding: .ascii) else {
54+
ITBError("Could not read file: \(path)")
4355
return [:]
4456
}
4557

46-
guard let propertyListString = scan(string: binaryString, begin: "<plist", end: "</plist>") else {
58+
guard let propertyListString = scan(string: asciiString, begin: "<plist", end: "</plist>") else {
4759
return [:]
4860
}
4961

@@ -61,7 +73,7 @@ struct IterableAPNSUtil {
6173
return [:]
6274
}
6375
}
64-
76+
6577
private static func scan(string: String, begin:String, end: String) -> String? {
6678
let scanner = Scanner(string: string)
6779
var buffer: NSString?

0 commit comments

Comments
 (0)