From ce67d1c40e6232135ef84c361f39be5338d27395 Mon Sep 17 00:00:00 2001 From: Harsh <6162866+harsh62@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:01:41 -0500 Subject: [PATCH] fix(Auth): Device name missing in FetchDevice API (#3508) --- .../ListDevicesOutputResponse+Helper.swift | 21 +++++++-------- .../DeviceBehaviorFetchDevicesTests.swift | 27 +++++++++++++++++-- .../DeviceTests/AuthFetchDeviceTests.swift | 10 +++++++ .../DeviceTests/AuthRememberDeviceTests.swift | 10 +++++++ 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/ListDevicesOutputResponse+Helper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/ListDevicesOutputResponse+Helper.swift index cf0cc59d5a..570f5bfb5d 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/ListDevicesOutputResponse+Helper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/ListDevicesOutputResponse+Helper.swift @@ -13,23 +13,20 @@ import AWSPluginsCore extension CognitoIdentityProviderClientTypes.DeviceType { func toAWSAuthDevice() -> AuthDevice { - let id = deviceKey ?? "" - let name = "" var attributes: [String: String] = [:] - if deviceAttributes != nil { - for attr in deviceAttributes! { + if let deviceAttributes { + for attr in deviceAttributes { if let attrName = attr.name, let attrValue = attr.value { attributes[attrName] = attrValue } } } - let device = AWSAuthDevice(id: id, - name: name, - attributes: attributes, - createdDate: deviceCreateDate, - lastAuthenticatedDate: deviceLastAuthenticatedDate, - lastModifiedDate: deviceLastModifiedDate) - - return device + return AWSAuthDevice( + id: deviceKey ?? "", + name: attributes["device_name", default: ""], + attributes: attributes, + createdDate: deviceCreateDate, + lastAuthenticatedDate: deviceLastAuthenticatedDate, + lastModifiedDate: deviceLastModifiedDate) } } diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/DeviceBehaviorTests/DeviceBehaviorFetchDevicesTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/DeviceBehaviorTests/DeviceBehaviorFetchDevicesTests.swift index fb9dea7ad4..b8fc44a77d 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/DeviceBehaviorTests/DeviceBehaviorFetchDevicesTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/DeviceBehaviorTests/DeviceBehaviorFetchDevicesTests.swift @@ -69,10 +69,23 @@ class DeviceBehaviorFetchDevicesTests: BasePluginTest { /// - I should get a successful result with one device fetched /// func testSuccessfulListDevices() async throws { - + let dateToTest = Date() + let deviceName = "test device" + let deviceId = "deviceId" mockIdentityProvider = MockIdentityProvider( mockListDevicesOutput: { _ in - ListDevicesOutput(devices: [CognitoIdentityProviderClientTypes.DeviceType(deviceKey: "id")], paginationToken: nil) + ListDevicesOutput( + devices: [ + CognitoIdentityProviderClientTypes.DeviceType( + deviceAttributes: [ + .init(name: "device_name", value: deviceName) + ], + deviceCreateDate: dateToTest, + deviceKey: deviceId, + deviceLastAuthenticatedDate: dateToTest, + deviceLastModifiedDate: dateToTest + ) + ], paginationToken: nil) } ) let listDevicesResult = try await plugin.fetchDevices() @@ -80,6 +93,16 @@ class DeviceBehaviorFetchDevicesTests: BasePluginTest { XCTFail("Result should have device count of 1") return } + guard let awsAuthDevice = listDevicesResult.first as? AWSAuthDevice else { + XCTFail("Resultant device type should be AWSAuthDevice") + return + } + XCTAssertEqual(awsAuthDevice.name, deviceName) + XCTAssertEqual(awsAuthDevice.id, deviceId) + XCTAssertNotEqual(awsAuthDevice.attributes?.count, 0) + XCTAssertEqual(awsAuthDevice.createdDate, dateToTest) + XCTAssertEqual(awsAuthDevice.lastAuthenticatedDate, dateToTest) + XCTAssertEqual(awsAuthDevice.lastModifiedDate, dateToTest) } /// Test a fetchDevices call with invalid response from service diff --git a/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthFetchDeviceTests.swift b/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthFetchDeviceTests.swift index 35b07c5f80..e893bb5a2b 100644 --- a/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthFetchDeviceTests.swift +++ b/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthFetchDeviceTests.swift @@ -63,5 +63,15 @@ class AuthFetchDeviceTests: AWSAuthBaseTest { let devices = try await Amplify.Auth.fetchDevices() XCTAssertNotNil(devices) XCTAssertEqual(devices.count, 1) + XCTAssertEqual(devices.first?.name.isEmpty, false) + XCTAssertEqual(devices.first?.id.isEmpty, false) + guard let awsDevice = devices.first as? AWSAuthDevice else { + XCTFail("Should be able to cast to AWSAuthDevice") + return + } + XCTAssertEqual(awsDevice.attributes.isEmpty, false) + XCTAssertNotNil(awsDevice.createdDate) + XCTAssertNotNil(awsDevice.lastAuthenticatedDate) + XCTAssertNotNil(awsDevice.lastModifiedDate) } } diff --git a/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthRememberDeviceTests.swift b/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthRememberDeviceTests.swift index d6559c25bc..d54ce32273 100644 --- a/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthRememberDeviceTests.swift +++ b/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/DeviceTests/AuthRememberDeviceTests.swift @@ -60,5 +60,15 @@ class AuthRememberDeviceTests: AWSAuthBaseTest { let devices = try await Amplify.Auth.fetchDevices() XCTAssertNotNil(devices) XCTAssertGreaterThan(devices.count, 0) + XCTAssertEqual(devices.first?.name.isEmpty, false) + XCTAssertEqual(devices.first?.id.isEmpty, false) + guard let awsDevice = devices.first as? AWSAuthDevice else { + XCTFail("Should be able to cast to AWSAuthDevice") + return + } + XCTAssertEqual(awsDevice.attributes.isEmpty, false) + XCTAssertNotNil(awsDevice.createdDate) + XCTAssertNotNil(awsDevice.lastAuthenticatedDate) + XCTAssertNotNil(awsDevice.lastModifiedDate) } }