diff --git a/Test/Tests/PushActivationStateMachineTests.swift b/Test/Tests/PushActivationStateMachineTests.swift index 2bb0212cd..69eebd84e 100644 --- a/Test/Tests/PushActivationStateMachineTests.swift +++ b/Test/Tests/PushActivationStateMachineTests.swift @@ -118,14 +118,27 @@ class PushActivationStateMachineTests: XCTestCase { func test__014__Activation_state_machine__State_NotActivated__on_Event_CalledActivate__local_device__should_have_a_generated_id() { beforeEach__Activation_state_machine__State_NotActivated() - - rest.internal.resetDeviceSingleton() + + let options = ARTClientOptions(key: "xxxx:xxxx") + let rest = ARTRest(options: options) + rest.internal.storage = storage + let stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) + + stateMachine.send(ARTPushActivationEventCalledActivate()) + XCTAssertEqual(rest.device.id.count, 36) } func test__015__Activation_state_machine__State_NotActivated__on_Event_CalledActivate__local_device__should_have_a_generated_secret() throws { beforeEach__Activation_state_machine__State_NotActivated() - + + let options = ARTClientOptions(key: "xxxx:xxxx") + let rest = ARTRest(options: options) + rest.internal.storage = storage + let stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) + + stateMachine.send(ARTPushActivationEventCalledActivate()) + let secret = try XCTUnwrap(rest.device.secret, "Device Secret should be available in storage") let data = try XCTUnwrap(Data(base64Encoded: secret), "Device Secret should be encoded with Base64") @@ -140,6 +153,11 @@ class PushActivationStateMachineTests: XCTestCase { options.clientId = "deviceClient" let rest = ARTRest(options: options) rest.internal.storage = storage + + let stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) + + XCTAssertNil(rest.device.clientId) + stateMachine.send(ARTPushActivationEventCalledActivate()) XCTAssertEqual(rest.device.clientId, "deviceClient") } @@ -832,6 +850,8 @@ class PushActivationStateMachineTests: XCTestCase { rest.internal.storage = storage stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) + rest.internal.setupLocalDevice() + XCTAssertEqual(stateMachine.rest.device.clientId, "client1") var deactivatedCallbackCalled = false @@ -840,17 +860,19 @@ class PushActivationStateMachineTests: XCTestCase { } defer { hook.remove() } - var clearIdentityTokenDetailsAndClientIdCalled = false - let hookDevice = stateMachine.rest.device.testSuite_injectIntoMethod(after: NSSelectorFromString("clearIdentityTokenDetailsAndClientId")) { - clearIdentityTokenDetailsAndClientIdCalled = true + var resetDetailsCalled = false + let hookDevice = stateMachine.rest.device.testSuite_injectIntoMethod(after: NSSelectorFromString("resetDetails")) { + resetDetailsCalled = true } defer { hookDevice.remove() } stateMachine.send(ARTPushActivationEventDeregistered()) expect(stateMachine.current).to(beAKindOf(ARTPushActivationStateNotActivated.self)) XCTAssertTrue(deactivatedCallbackCalled) - XCTAssertTrue(clearIdentityTokenDetailsAndClientIdCalled) + XCTAssertTrue(resetDetailsCalled) // RSH3g2a + XCTAssertEqual(stateMachine.rest.device.id, "") + XCTAssertNil(stateMachine.rest.device.secret) XCTAssertNil(stateMachine.rest.device.identityTokenDetails) XCTAssertNil(stateMachine.rest.device.clientId) } @@ -875,7 +897,60 @@ class PushActivationStateMachineTests: XCTestCase { expect(stateMachine.current).to(beAKindOf(ARTPushActivationStateWaitingForDeregistration.self)) XCTAssertTrue(deactivatedCallbackCalled) } + + // RSH8b, RSH3a2b, RSH3g2a + func test__056__Activation_state_machine__should_be_possible_to_activate_and_deactivate_and_then_activate_again_with_different_clientId() { + beforeEach__Activation_state_machine__State_NotActivated() + + let options1 = ARTClientOptions(key: "xxxx:xxxx") + options1.clientId = "client1" + let rest1 = ARTRest(options: options1) + httpExecutor = MockHTTPExecutor() + rest1.internal.httpExecutor = httpExecutor + rest1.internal.storage = storage + + let stateMachineDelegate = StateMachineDelegate() + let stateMachine1 = ARTPushActivationStateMachine(rest: rest1.internal, delegate: stateMachineDelegate, logger: .init(core: MockInternalLogCore())) + + let testDeviceToken = "xxxx-xxxx-xxxx-xxxx-xxxx" + stateMachine1.rest.device.setAndPersistAPNSDeviceToken(testDeviceToken) + defer { stateMachine1.rest.device.setAndPersistAPNSDeviceToken(nil) } + waitUntil(timeout: testTimeout) { done in + let partialDone = AblyTests.splitDone(3, done: done) + stateMachine1.transitions = { event, _, _ in + if event is ARTPushActivationEventCalledActivate { + XCTAssertEqual(rest1.internal.device_nosync.clientId, "client1") + partialDone() + } + if event is ARTPushActivationEventGotPushDeviceDetails { + partialDone() + stateMachine1.send(ARTPushActivationEventCalledDeactivate()) + } + if event is ARTPushActivationEventCalledDeactivate { + partialDone() + } + } + stateMachine1.send(ARTPushActivationEventCalledActivate()) + } + + XCTAssertNil(rest1.device.clientId) // after deactivation, RSH3g2a + + let options2 = ARTClientOptions(key: "xxxx:xxxx") + options2.clientId = "client2" + let rest2 = ARTRest(options: options2) + rest2.internal.storage = storage + rest2.internal.httpExecutor = httpExecutor + + XCTAssertNil(rest2.device.clientId) + + let stateMachine2 = ARTPushActivationStateMachine(rest: rest2.internal, delegate: stateMachineDelegate, logger: .init(core: MockInternalLogCore())) + stateMachine2.send(ARTPushActivationEventCalledActivate()) + + XCTAssertEqual(rest2.device.clientId, "client2") + XCTAssertTrue(rest1.device === rest2.device) + } + // RSH4 func test__005__Activation_state_machine__should_queue_event_that_has_no_transition_defined_for_it() throws { // Start with WaitingForDeregistration state diff --git a/Test/Tests/PushTests.swift b/Test/Tests/PushTests.swift index a98488ab4..3234257e6 100644 --- a/Test/Tests/PushTests.swift +++ b/Test/Tests/PushTests.swift @@ -234,11 +234,16 @@ class PushTests: XCTestCase { let rest = ARTRest(key: "fake:key") rest.internal.storage = storage + + storage.simulateOnNextRead(string: "testId", for: ARTDeviceIdKey) + storage.simulateOnNextRead(string: "testSecret", for: ARTDeviceSecretKey) storage.simulateOnNextRead(string: testToken, for: ARTAPNSDeviceTokenKey) storage.simulateOnNextRead(data: testIdentity.archive(withLogger: nil), for: ARTDeviceIdentityTokenKey) let device = rest.device + XCTAssertEqual(device.id, "testId") + XCTAssertEqual(device.secret, "testSecret") XCTAssertEqual(device.apnsDeviceToken(), testToken) XCTAssertEqual(device.identityTokenDetails?.token, testIdentity.token) } @@ -336,7 +341,7 @@ class PushTests: XCTestCase { storage.simulateOnNextRead(string: testDeviceToken, for: ARTAPNSDeviceTokenKey) storage.simulateOnNextRead(data: testDeviceIdentity.archive(withLogger: nil), for: ARTDeviceIdentityTokenKey) - XCTAssertNil(realtime.device.clientId) + XCTAssertEqual(realtime.device.clientId, testDeviceIdentity.clientId) waitUntil(timeout: testTimeout) { done in stateMachine.transitions = { event, _, _ in