fix: Save deserialized credential object to Authentication member - #436
Conversation
| this.credentials = { | ||
| ...credentials, | ||
| expiration: new Date(credentials.expiration as Date) | ||
| }; |
There was a problem hiding this comment.
issue: We need unit tests to cover the new behavior (here and in Authentication.ts).
I think this change introduces a bug. Cover the following cases:
- When credentials are read from storage.
- When credentials are read from storage, and then read from the member variable.
|
I've pushed a patch for EnhancedAuthentication.ts:
|
| ); | ||
| }); | ||
|
|
||
| test('when credentials are read from storage then the member variable stores the expiration as a date object', async () => { |
There was a problem hiding this comment.
nit: This test will pass under certain conditions when it should fail (i.e., false negative).
Specifically, when this.credentials.expiration is undefined, and the expiration property of the value returned by AnonymousStorageCredentialsProvider is a Date object. When this happens, control flow will reach AnonymousCognitoCredentialsProvider, and the test needs a way to distinguish the value returned here from the expected value.
You can fix this by mocking the response of getCredentials, like was done in EnhancedAuthentication.test.ts here.
Details
Currently in our Authentication and EnhancedAuthentication, we save credential object as serialized string values. Then, as we read the serialized string from Storage, we parse the string into a Credential object.
However, in AnonymousStorageCredentialsProvider, we overwrite the local member variable credential with the serialized string instead of the parsed object. As a result, any situation that requires reading of the credential from storage leads to:
TypeError: this.credentials.expiration.getTime is not a function.This PR removes the unnecessary step to overwrite the local member
this.credentialwith the serialized string.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.