From bff9205219ea647ffde0aacf7021f4b433280558 Mon Sep 17 00:00:00 2001 From: Marat Al Date: Sun, 12 Nov 2023 19:39:03 +0100 Subject: [PATCH] This partially fixes issue #1177 (deregistration does not clear device details) and only resets clientId, since it affects push device registration with a different clientId (after deregistration with previous clientId). This only affects clients that do not restart their apps after deregistration, since clientId is loaded into deviceId once per app launch. This commit resets clientId after deregistration. --- Source/ARTLocalDevice.m | 5 +++++ Source/ARTPushActivationState.m | 2 +- .../Ably/ARTLocalDevice+Private.h | 1 + .../PushActivationStateMachineTests.swift | 21 +++++++++++++------ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Source/ARTLocalDevice.m b/Source/ARTLocalDevice.m index 4083ff4d6..873b751c3 100644 --- a/Source/ARTLocalDevice.m +++ b/Source/ARTLocalDevice.m @@ -153,4 +153,9 @@ - (BOOL)isRegistered { return _identityTokenDetails != nil; } +- (void)clearIdentityTokenDetailsAndClientId { + [self setAndPersistIdentityTokenDetails:nil]; + self.clientId = nil; +} + @end diff --git a/Source/ARTPushActivationState.m b/Source/ARTPushActivationState.m index 3810e627c..b8d28649e 100644 --- a/Source/ARTPushActivationState.m +++ b/Source/ARTPushActivationState.m @@ -275,7 +275,7 @@ - (ARTPushActivationState *)transition:(ARTPushActivationEvent *)event { else if ([event isKindOfClass:[ARTPushActivationEventDeregistered class]]) { #if TARGET_OS_IOS ARTLocalDevice *local = self.machine.rest.device_nosync; - [local setAndPersistIdentityTokenDetails:nil]; + [local clearIdentityTokenDetailsAndClientId]; #endif [self.machine callDeactivatedCallback:nil]; return [ARTPushActivationStateNotActivated newWithMachine:self.machine logger:self.logger]; diff --git a/Source/PrivateHeaders/Ably/ARTLocalDevice+Private.h b/Source/PrivateHeaders/Ably/ARTLocalDevice+Private.h index d94bdea8d..5c9518b41 100644 --- a/Source/PrivateHeaders/Ably/ARTLocalDevice+Private.h +++ b/Source/PrivateHeaders/Ably/ARTLocalDevice+Private.h @@ -25,6 +25,7 @@ NSString* ARTAPNSDeviceTokenKeyOfType(NSString * _Nullable tokenType); - (void)setAndPersistAPNSDeviceToken:(nullable NSString *)deviceToken; - (void)setAndPersistIdentityTokenDetails:(nullable ARTDeviceIdentityTokenDetails *)tokenDetails; - (BOOL)isRegistered; +- (void)clearIdentityTokenDetailsAndClientId; + (NSString *)generateId; + (NSString *)generateSecret; diff --git a/Test/Tests/PushActivationStateMachineTests.swift b/Test/Tests/PushActivationStateMachineTests.swift index 105cf240b..6c6c2a924 100644 --- a/Test/Tests/PushActivationStateMachineTests.swift +++ b/Test/Tests/PushActivationStateMachineTests.swift @@ -822,26 +822,35 @@ class PushActivationStateMachineTests: XCTestCase { // RSH3g2 func test__054__Activation_state_machine__State_WaitingForDeregistration__on_Event_Deregistered() { - beforeEach__Activation_state_machine__State_WaitingForDeregistration() - + storage = MockDeviceStorage(startWith: ARTPushActivationStateWaitingForDeregistration(machine: initialStateMachine, logger: .init(core: MockInternalLogCore()))) + + let options = ARTClientOptions(key: "xxxx:xxxx") + options.clientId = "client1" + let rest = ARTRest(options: options) + rest.internal.storage = storage + stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) + + XCTAssertEqual(stateMachine.rest.device.clientId, "client1") + var deactivatedCallbackCalled = false let hook = stateMachine.testSuite_injectIntoMethod(after: NSSelectorFromString("callDeactivatedCallback:")) { deactivatedCallbackCalled = true } defer { hook.remove() } - var setAndPersistIdentityTokenDetailsCalled = false - let hookDevice = stateMachine.rest.device.testSuite_injectIntoMethod(after: NSSelectorFromString("setAndPersistIdentityTokenDetails:")) { - setAndPersistIdentityTokenDetailsCalled = true + var clearIdentityTokenDetailsAndClientIdCalled = false + let hookDevice = stateMachine.rest.device.testSuite_injectIntoMethod(after: NSSelectorFromString("clearIdentityTokenDetailsAndClientId")) { + clearIdentityTokenDetailsAndClientIdCalled = true } defer { hookDevice.remove() } stateMachine.send(ARTPushActivationEventDeregistered()) expect(stateMachine.current).to(beAKindOf(ARTPushActivationStateNotActivated.self)) XCTAssertTrue(deactivatedCallbackCalled) - XCTAssertTrue(setAndPersistIdentityTokenDetailsCalled) + XCTAssertTrue(clearIdentityTokenDetailsAndClientIdCalled) // RSH3g2a XCTAssertNil(stateMachine.rest.device.identityTokenDetails) + XCTAssertNil(stateMachine.rest.device.clientId) } // RSH3g3