|
| 1 | +// |
| 2 | +// |
| 3 | +// Created by Tapash Majumder on 8/15/18. |
| 4 | +// Copyright © 2018 Iterable. All rights reserved. |
| 5 | +// |
| 6 | + |
| 7 | +import XCTest |
| 8 | + |
| 9 | +@testable import IterableSDK |
| 10 | + |
| 11 | +class IterableAutoRegistrationTests: XCTestCase { |
| 12 | + private static let apiKey = "zeeApiKey" |
| 13 | + |
| 14 | + override func setUp() { |
| 15 | + super.setUp() |
| 16 | + |
| 17 | + TestUtils.clearUserDefaults() |
| 18 | + } |
| 19 | + |
| 20 | + override func tearDown() { |
| 21 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 22 | + super.tearDown() |
| 23 | + } |
| 24 | + |
| 25 | + func testCallDisableAndEnable() { |
| 26 | + let expectation1 = expectation(description: "call register device API") |
| 27 | + let expectation2 = expectation(description: "call registerForRemoteNotifications twice") |
| 28 | + expectation2.expectedFulfillmentCount = 2 |
| 29 | + let expectation3 = expectation(description : "call disable on [email protected]") |
| 30 | + |
| 31 | + let networkSession = MockNetworkSession(statusCode: 200) |
| 32 | + let config = IterableConfig() |
| 33 | + config.pushIntegrationName = "my-push-integration" |
| 34 | + config.autoPushRegistration = true |
| 35 | + |
| 36 | + let notificationStateProvider = MockNotificationStateProvider(enabled: true, expectation: expectation2) |
| 37 | + |
| 38 | + IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey, config:config, networkSession: networkSession, notificationStateProvider: notificationStateProvider) |
| 39 | + IterableAPI.email = "[email protected]" |
| 40 | + let token = "zeeToken".data(using: .utf8)! |
| 41 | + networkSession.callback = {(_, _, _) in |
| 42 | + // First call, API call to register endpoint |
| 43 | + expectation1.fulfill() |
| 44 | + TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: ITBConsts.apiEndpoint, path: ENDPOINT_REGISTER_DEVICE_TOKEN, queryParams: [(name: ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)]) |
| 45 | + networkSession.callback = {(_, _, _)in |
| 46 | + // Second call, API call to disable endpoint |
| 47 | + expectation3.fulfill() |
| 48 | + TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: ITBConsts.apiEndpoint, path: ENDPOINT_DISABLE_DEVICE, queryParams: [(name: ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)]) |
| 49 | + let body = networkSession.getRequestBody() as! [String : Any] |
| 50 | + TestUtils.validateElementPresent(withName: ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body) |
| 51 | + TestUtils.validateElementPresent(withName : ITBL_KEY_EMAIL , andValue : "[email protected]", inDictionary : body ) |
| 52 | + } |
| 53 | + |
| 54 | + IterableAPI.email = "[email protected]" |
| 55 | + } |
| 56 | + IterableAPI.register(token: token) |
| 57 | + |
| 58 | + // only wait for small time, supposed to error out |
| 59 | + wait(for: [expectation1, expectation2, expectation3], timeout: testExpectationTimeout) |
| 60 | + } |
| 61 | + |
| 62 | + func testDoNotCallDisableAndEnableWhenSameValue() { |
| 63 | + let expectation1 = expectation(description: "registerForRemoteNotifications") |
| 64 | + |
| 65 | + let networkSession = MockNetworkSession(statusCode: 200) |
| 66 | + let config = IterableConfig() |
| 67 | + config.pushIntegrationName = "my-push-integration" |
| 68 | + config.autoPushRegistration = true |
| 69 | + // notifications are enabled |
| 70 | + let notificationStateProvider = MockNotificationStateProvider(enabled: true, expectation: expectation1) |
| 71 | + |
| 72 | + IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey, config:config, networkSession: networkSession, notificationStateProvider: notificationStateProvider) |
| 73 | + |
| 74 | + IterableAPI.email = email |
| 75 | + let token = "zeeToken".data(using: .utf8)! |
| 76 | + networkSession.callback = {(_, _, _) in |
| 77 | + // first call back will be called on register |
| 78 | + TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: ITBConsts.apiEndpoint, path: ENDPOINT_REGISTER_DEVICE_TOKEN, queryParams: [(name: ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)]) |
| 79 | + networkSession.callback = {(_, _, _)in |
| 80 | + // Second callback should not happen |
| 81 | + XCTFail("Should not call disable") |
| 82 | + } |
| 83 | + // set same value |
| 84 | + IterableAPI.email = email |
| 85 | + } |
| 86 | + IterableAPI.register(token: token) |
| 87 | + |
| 88 | + // only wait for small time, supposed to error out |
| 89 | + wait(for: [expectation1], timeout: testExpectationTimeout) |
| 90 | + } |
| 91 | + |
| 92 | + func testDoNotCallRegisterForRemoteNotificationsWhenNotificationsAreDisabled() { |
| 93 | + let expectation1 = expectation(description: "Call registerToken API endpoint") |
| 94 | + let expectation2 = expectation(description: "Disable API endpoint called") |
| 95 | + let expectation3 = expectation(description: "do not call registerForRemoteNotifications") |
| 96 | + expectation3.isInverted = true |
| 97 | + |
| 98 | + let networkSession = MockNetworkSession(statusCode: 200) |
| 99 | + let config = IterableConfig() |
| 100 | + config.pushIntegrationName = "my-push-integration" |
| 101 | + let notificationStateProvider = MockNotificationStateProvider(enabled: false, expectation: expectation3) // Notifications are disabled |
| 102 | + |
| 103 | + IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey, |
| 104 | + config: config, |
| 105 | + networkSession: networkSession, |
| 106 | + notificationStateProvider: notificationStateProvider) |
| 107 | + IterableAPI.userId = "userId1" |
| 108 | + let token = "zeeToken".data(using: .utf8)! |
| 109 | + networkSession.callback = {(_, _, _) in |
| 110 | + // first call back will be called on register |
| 111 | + expectation1.fulfill() |
| 112 | + TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: ITBConsts.apiEndpoint, path: ENDPOINT_REGISTER_DEVICE_TOKEN, queryParams: [(name: ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)]) |
| 113 | + networkSession.callback = {(_, _, _) in |
| 114 | + // second call back is for disable when we set new user id |
| 115 | + TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: ITBConsts.apiEndpoint, path: ENDPOINT_DISABLE_DEVICE, queryParams: [(name: ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)]) |
| 116 | + let body = networkSession.getRequestBody() as! [String : Any] |
| 117 | + TestUtils.validateElementPresent(withName: ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body) |
| 118 | + TestUtils.validateElementPresent(withName: ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body) |
| 119 | + expectation2.fulfill() |
| 120 | + } |
| 121 | + IterableAPI.userId = "userId2" |
| 122 | + } |
| 123 | + IterableAPI.register(token: token) |
| 124 | + |
| 125 | + // only wait for small time, supposed to error out |
| 126 | + wait(for: [expectation1, expectation2, expectation3], timeout: 1.0) |
| 127 | + } |
| 128 | + |
| 129 | + func testDoNotCallDisableOrEnableWhenAutoPushIsOff() { |
| 130 | + let expectation1 = expectation(description: "do not call register for remote") |
| 131 | + expectation1.isInverted = true |
| 132 | + |
| 133 | + let networkSession = MockNetworkSession(statusCode: 200) |
| 134 | + let config = IterableConfig() |
| 135 | + config.pushIntegrationName = "my-push-integration" |
| 136 | + config.autoPushRegistration = false |
| 137 | + let notificationStateProvider = MockNotificationStateProvider(enabled: true, expectation: expectation1) |
| 138 | + |
| 139 | + IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey, config:config, networkSession: networkSession, notificationStateProvider: notificationStateProvider) |
| 140 | + IterableAPI.email = "[email protected]" |
| 141 | + let token = "zeeToken".data(using: .utf8)! |
| 142 | + networkSession.callback = {(_, _, _) in |
| 143 | + // first call back will be called on register |
| 144 | + TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: ITBConsts.apiEndpoint, path: ENDPOINT_REGISTER_DEVICE_TOKEN, queryParams: [(name: ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)]) |
| 145 | + networkSession.callback = {(_, _, _)in |
| 146 | + // Second callback should not happen |
| 147 | + XCTFail("should not call disable") |
| 148 | + } |
| 149 | + IterableAPI.email = "[email protected]" |
| 150 | + } |
| 151 | + IterableAPI.register(token: token) |
| 152 | + |
| 153 | + // only wait for small time, supposed to error out |
| 154 | + wait(for: [expectation1], timeout: 1.0) |
| 155 | + } |
| 156 | + |
| 157 | + func testAutomaticPushRegistrationOnInit() { |
| 158 | + let expectation1 = expectation(description: "call register for remote") |
| 159 | + |
| 160 | + let networkSession = MockNetworkSession(statusCode: 200) |
| 161 | + let config = IterableConfig() |
| 162 | + config.pushIntegrationName = "my-push-integration" |
| 163 | + config.autoPushRegistration = true |
| 164 | + let notificationStateProvider = MockNotificationStateProvider(enabled: true, expectation: expectation1) |
| 165 | + |
| 166 | + UserDefaults.standard .set("[email protected]", forKey :ITBConsts.UserDefaults .emailKey ) |
| 167 | + IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey, config:config, networkSession: networkSession, notificationStateProvider: notificationStateProvider) |
| 168 | + |
| 169 | + // only wait for small time, supposed to error out |
| 170 | + wait(for: [expectation1], timeout: testExpectationTimeout) |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments