|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.core.credentials |
| 5 | + |
| 6 | +import com.intellij.openapi.project.Project |
| 7 | +import com.intellij.testFramework.ApplicationExtension |
| 8 | +import org.junit.jupiter.api.BeforeEach |
| 9 | +import org.junit.jupiter.api.Test |
| 10 | +import org.junit.jupiter.api.TestInstance |
| 11 | +import org.mockito.kotlin.mock |
| 12 | +import org.mockito.kotlin.never |
| 13 | +import io.mockk.mockkStatic |
| 14 | +import io.mockk.verify |
| 15 | +import io.mockk.every |
| 16 | +import org.mockito.kotlin.verify |
| 17 | +import org.mockito.kotlin.whenever |
| 18 | +import org.junit.jupiter.api.Assertions.* |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith |
| 20 | +import software.amazon.awssdk.auth.token.credentials.SdkToken |
| 21 | +import software.aws.toolkits.jetbrains.core.credentials.sso.DeviceAuthorizationGrantToken |
| 22 | +import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenAuthState |
| 23 | +import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenProvider |
| 24 | +import software.aws.toolkits.jetbrains.utils.notifyInfo |
| 25 | +import software.aws.toolkits.resources.AwsCoreBundle.message |
| 26 | +import java.net.UnknownHostException |
| 27 | +import java.time.Instant |
| 28 | +import java.time.temporal.ChronoUnit |
| 29 | + |
| 30 | +@ExtendWith(ApplicationExtension::class) |
| 31 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 32 | +class ToolkitAuthManagerTest { |
| 33 | + private lateinit var project: Project |
| 34 | + private lateinit var tokenProvider: BearerTokenProvider |
| 35 | + private var reauthCallCount = 0 |
| 36 | + private var notificationShown = false |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + fun setUp() { |
| 40 | + project = mock() |
| 41 | + tokenProvider = mock() |
| 42 | + reauthCallCount = 0 |
| 43 | + notificationShown = false |
| 44 | + |
| 45 | + // Mock the notifyInfo function |
| 46 | + mockkStatic("software.aws.toolkits.jetbrains.utils.NotificationUtilsKt") |
| 47 | + every { |
| 48 | + notifyInfo(any(), any(), any()) |
| 49 | + } answers { |
| 50 | + notificationShown = true |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `test NEEDS_REFRESH state with network error - first occurrence`() { |
| 57 | + whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH) |
| 58 | + whenever(tokenProvider.resolveToken()).thenThrow(RuntimeException(UnknownHostException("Test network error"))) |
| 59 | + |
| 60 | + val result = maybeReauthProviderIfNeeded( |
| 61 | + project, |
| 62 | + ReauthSource.TOOLKIT, |
| 63 | + tokenProvider |
| 64 | + ) { _ -> reauthCallCount++ } |
| 65 | + |
| 66 | + assertFalse(result) |
| 67 | + assertEquals(0, reauthCallCount) |
| 68 | + assertTrue(notificationShown) |
| 69 | + verify { |
| 70 | + notifyInfo( |
| 71 | + message("general.auth.network.error"), |
| 72 | + message("general.auth.network.error.message"), |
| 73 | + project |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `test NEEDS_REFRESH state with network error - subsequent occurrence`() { |
| 80 | + whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH) |
| 81 | + whenever(tokenProvider.resolveToken()).thenThrow(RuntimeException(UnknownHostException("Test network error"))) |
| 82 | + |
| 83 | + // First call to set the internal flag |
| 84 | + maybeReauthProviderIfNeeded( |
| 85 | + project, |
| 86 | + ReauthSource.TOOLKIT, |
| 87 | + tokenProvider |
| 88 | + ) { _ -> reauthCallCount++ } |
| 89 | + |
| 90 | + // Reset our tracking variable |
| 91 | + notificationShown = false |
| 92 | + |
| 93 | + // Second call - should not show notification |
| 94 | + val result = maybeReauthProviderIfNeeded( |
| 95 | + project, |
| 96 | + ReauthSource.TOOLKIT, |
| 97 | + tokenProvider |
| 98 | + ) { _ -> reauthCallCount++ } |
| 99 | + |
| 100 | + assertFalse(result) |
| 101 | + assertEquals(0, reauthCallCount) |
| 102 | + assertFalse(notificationShown) |
| 103 | + verify(exactly = 1) { |
| 104 | + notifyInfo( |
| 105 | + message("general.auth.network.error"), |
| 106 | + message("general.auth.network.error.message"), |
| 107 | + project |
| 108 | + ) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `test successful refresh clears notification flag`() { |
| 114 | + whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH) |
| 115 | + |
| 116 | + // First trigger a network error |
| 117 | + whenever(tokenProvider.resolveToken()).thenThrow(RuntimeException(UnknownHostException("Test network error"))) |
| 118 | + maybeReauthProviderIfNeeded( |
| 119 | + project, |
| 120 | + ReauthSource.TOOLKIT, |
| 121 | + tokenProvider |
| 122 | + ) { _ -> reauthCallCount++ } |
| 123 | + |
| 124 | + // Now simulate successful refresh |
| 125 | + whenever(tokenProvider.resolveToken()).thenReturn( |
| 126 | + DeviceAuthorizationGrantToken( |
| 127 | + startUrl = "https://example.com", |
| 128 | + region = "us-east-1", |
| 129 | + accessToken = "testAccessToken", |
| 130 | + refreshToken = "testRefreshToken", |
| 131 | + expiresAt = Instant.now().plus(1, ChronoUnit.HOURS), |
| 132 | + ) |
| 133 | + ) |
| 134 | + maybeReauthProviderIfNeeded( |
| 135 | + project, |
| 136 | + ReauthSource.TOOLKIT, |
| 137 | + tokenProvider |
| 138 | + ) { _ -> reauthCallCount++ } |
| 139 | + |
| 140 | + // Reset tracking |
| 141 | + notificationShown = false |
| 142 | + |
| 143 | + // Now trigger another network error - should show notification again |
| 144 | + whenever(tokenProvider.resolveToken()).thenThrow(RuntimeException(UnknownHostException("Test network error"))) |
| 145 | + maybeReauthProviderIfNeeded( |
| 146 | + project, |
| 147 | + ReauthSource.TOOLKIT, |
| 148 | + tokenProvider |
| 149 | + ) { _ -> reauthCallCount++ } |
| 150 | + |
| 151 | + assertTrue(notificationShown) |
| 152 | + verify(exactly = 2) { |
| 153 | + notifyInfo( |
| 154 | + message("general.auth.network.error"), |
| 155 | + message("general.auth.network.error.message"), |
| 156 | + project |
| 157 | + ) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments