Skip to content

Commit

Permalink
fix(Auth): Device name missing in FetchDevice API (aws-amplify#3508)
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 authored Feb 9, 2024
1 parent f57b88c commit ce67d1c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,40 @@ 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()
guard listDevicesResult.count == 1 else {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit ce67d1c

Please sign in to comment.