Skip to content

Commit 739ef8b

Browse files
committed
added test cases for device key included and not, as well as a null result from the API and any generic error catch all
1 parent 97ef445 commit 739ef8b

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/actions/FetchAuthSessionCognitoActionsTest.kt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,105 @@ class FetchAuthSessionCognitoActionsTest {
173173

174174
capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent>()
175175
}
176+
177+
@Test
178+
fun `refreshUserPoolTokensAction handles generic exception`() = runTest {
179+
coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } throws RuntimeException("Network error")
180+
181+
val signedInData = mockSignedInData(
182+
username = "username",
183+
cognitoUserPoolTokens = CognitoUserPoolTokens(
184+
accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o",
185+
idToken = "old_id",
186+
refreshToken = "refresh_token",
187+
expiration = 0
188+
)
189+
)
190+
191+
FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
192+
193+
capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent>()
194+
}
195+
196+
@Test
197+
fun `refreshUserPoolTokensAction handles null authentication result with identity pool`() = runTest {
198+
every { configuration.identityPool } returns mockk {
199+
every { poolId } returns "identity_pool_id"
200+
}
201+
202+
coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } returns GetTokensFromRefreshTokenResponse {
203+
authenticationResult = null
204+
}
205+
206+
val signedInData = mockSignedInData(
207+
username = "username",
208+
cognitoUserPoolTokens = CognitoUserPoolTokens(
209+
accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o",
210+
idToken = "old_id",
211+
refreshToken = "refresh_token",
212+
expiration = 0
213+
)
214+
)
215+
216+
FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
217+
218+
capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent>()
219+
}
220+
221+
@Test
222+
fun `refreshUserPoolTokensAction includes device key when available`() = runTest {
223+
val requestSlot = slot<aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest>()
224+
coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot)) } returns GetTokensFromRefreshTokenResponse {
225+
authenticationResult = AuthenticationResultType {
226+
this.accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
227+
this.idToken = "id_token"
228+
this.refreshToken = "new_refresh_token"
229+
this.expiresIn = 3600
230+
}
231+
}
232+
233+
val signedInData = mockSignedInData(
234+
username = "username",
235+
cognitoUserPoolTokens = CognitoUserPoolTokens(
236+
accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o",
237+
idToken = "old_id",
238+
refreshToken = "refresh_token",
239+
expiration = 0
240+
)
241+
)
242+
243+
FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
244+
245+
requestSlot.captured.deviceKey shouldBe "device_key"
246+
}
247+
248+
@Test
249+
fun `refreshUserPoolTokensAction works without device metadata`() = runTest {
250+
coEvery { credentialStoreClient.loadCredentials(any<CredentialType.Device>()) } returns AmplifyCredential.DeviceData(DeviceMetadata.Empty)
251+
252+
val requestSlot = slot<aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest>()
253+
coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot)) } returns GetTokensFromRefreshTokenResponse {
254+
authenticationResult = AuthenticationResultType {
255+
this.accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
256+
this.idToken = "id_token"
257+
this.refreshToken = "new_refresh_token"
258+
this.expiresIn = 3600
259+
}
260+
}
261+
262+
val signedInData = mockSignedInData(
263+
username = "username",
264+
cognitoUserPoolTokens = CognitoUserPoolTokens(
265+
accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o",
266+
idToken = "old_id",
267+
refreshToken = "refresh_token",
268+
expiration = 0
269+
)
270+
)
271+
272+
FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
273+
274+
requestSlot.captured.deviceKey shouldBe null
275+
capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent>()
276+
}
176277
}

0 commit comments

Comments
 (0)