From 2c38c738101d34543168444409f9922e92fdfb51 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 +- Source/PrivateHeaders/Ably/ARTLocalDevice+Private.h | 1 + Test/Tests/PushActivationStateMachineTests.swift | 13 +++++++++++-- 4 files changed, 18 insertions(+), 3 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..66799d4c7 100644 --- a/Test/Tests/PushActivationStateMachineTests.swift +++ b/Test/Tests/PushActivationStateMachineTests.swift @@ -822,8 +822,16 @@ 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 @@ -841,6 +849,7 @@ class PushActivationStateMachineTests: XCTestCase { XCTAssertTrue(deactivatedCallbackCalled) XCTAssertTrue(setAndPersistIdentityTokenDetailsCalled) // RSH3g2a + XCTAssertNil(stateMachine.rest.device.clientId) XCTAssertNil(stateMachine.rest.device.identityTokenDetails) }