diff --git a/Source/ARTLocalDevice.m b/Source/ARTLocalDevice.m index 2153e3bbe..c346bfbd8 100644 --- a/Source/ARTLocalDevice.m +++ b/Source/ARTLocalDevice.m @@ -55,6 +55,14 @@ - (instancetype)initWithStorage:(id)storage logger:(nullable A return self; } +- (void)generateAndPersistPairOfDeviceIdAndSecret { + self.id = [self.class generateId]; + self.secret = [self.class generateSecret]; + + [_storage setObject:self.id forKey:ARTDeviceIdKey]; + [_storage setSecret:self.secret forDevice:self.id]; +} + + (instancetype)deviceWithStorage:(id)storage logger:(nullable ARTInternalLog *)logger { ARTLocalDevice *device = [[ARTLocalDevice alloc] initWithStorage:storage logger:logger]; device.platform = ARTDevicePlatform; @@ -75,8 +83,13 @@ + (instancetype)deviceWithStorage:(id)storage logger:(nullable NSString *deviceId = [storage objectForKey:ARTDeviceIdKey]; NSString *deviceSecret = deviceId == nil ? nil : [storage secretForDevice:deviceId]; - device.id = deviceId ?: [self.class generateId]; // temporarily ignore RSH8a/b, see https://github.com/ably/ably-cocoa/pull/1847#discussion_r1441954284 thread - device.secret = deviceSecret; + if (deviceId == nil || deviceSecret == nil) { + [device generateAndPersistPairOfDeviceIdAndSecret]; // Should be removed later once spec issue #180 resolved. + } + else { + device.id = deviceId; + device.secret = deviceSecret; + } id identityTokenDetailsInfo = [storage objectForKey:ARTDeviceIdentityTokenKey]; ARTDeviceIdentityTokenDetails *identityTokenDetails = [ARTDeviceIdentityTokenDetails unarchive:identityTokenDetailsInfo withLogger:logger]; @@ -105,25 +118,19 @@ - (void)setupDetailsWithClientId:(NSString *)clientId { NSString *deviceId = self.id; NSString *deviceSecret = self.secret; - if (deviceId == nil || deviceSecret == nil) { // generate both at the same time - deviceId = [self.class generateId]; - deviceSecret = [self.class generateSecret]; - - [_storage setObject:deviceId forKey:ARTDeviceIdKey]; - [_storage setSecret:deviceSecret forDevice:deviceId]; + if (deviceId == nil || deviceSecret == nil) { + [self generateAndPersistPairOfDeviceIdAndSecret]; } - self.id = deviceId; - self.secret = deviceSecret; - self.clientId = clientId; [_storage setObject:clientId forKey:ARTClientIdKey]; } - (void)resetDetails { - self.secret = nil; + // Should be replaced later to resetting device's id/secret once spec issue #180 resolved. + [self generateAndPersistPairOfDeviceIdAndSecret]; + self.clientId = nil; - [_storage setSecret:nil forDevice:self.id]; [_storage setObject:nil forKey:ARTClientIdKey]; [self setAndPersistIdentityTokenDetails:nil]; NSArray *supportedTokenTypes = @[ diff --git a/Test/Tests/PushActivationStateMachineTests.swift b/Test/Tests/PushActivationStateMachineTests.swift index 730da5341..b47be792c 100644 --- a/Test/Tests/PushActivationStateMachineTests.swift +++ b/Test/Tests/PushActivationStateMachineTests.swift @@ -156,7 +156,6 @@ class PushActivationStateMachineTests: XCTestCase { let stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) - XCTAssertNil(rest.device.secret) XCTAssertNil(rest.device.clientId) stateMachine.send(ARTPushActivationEventCalledActivate()) @@ -877,15 +876,19 @@ class PushActivationStateMachineTests: XCTestCase { XCTAssertTrue(resetDetailsCalled) // RSH3g2a - XCTAssertNil(stateMachine.rest.device.secret) XCTAssertNil(stateMachine.rest.device.identityTokenDetails) XCTAssertNil(stateMachine.rest.device.clientId) XCTAssertNil(stateMachine.rest.device.push.recipient["push"]) - XCTAssertNil(storage.object(forKey: ARTDeviceIdKey)) XCTAssertNil(storage.object(forKey: ARTDeviceIdentityTokenKey)) XCTAssertNil(ARTLocalDevice.apnsDeviceToken(ofType: ARTAPNSDeviceDefaultTokenType, from: storage)) XCTAssertNil(ARTLocalDevice.apnsDeviceToken(ofType: ARTAPNSDeviceLocationTokenType, from: storage)) + + // Should be replaced with `nil` checks after issue https://github.com/ably/specification/issues/180 resolved + XCTAssertNotNil(stateMachine.rest.device.id) + XCTAssertNotNil(stateMachine.rest.device.secret) + XCTAssertEqual(storage.keysWritten[ARTDeviceIdKey] as? String, stateMachine.rest.device.id) + XCTAssertEqual(storage.keysWritten[ARTDeviceSecretKey] as? String, stateMachine.rest.device.secret) } // RSH3g3