Skip to content

Commit ee69990

Browse files
committed
tests pass
1 parent b7cef7f commit ee69990

File tree

1 file changed

+58
-40
lines changed

1 file changed

+58
-40
lines changed

plugins/core/jetbrains-community/tst/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManagerTest.kt

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test
2222
import org.junit.jupiter.api.TestInstance
2323
import org.junit.jupiter.api.extension.ExtendWith
2424
import org.mockito.kotlin.doThrow
25+
import org.mockito.kotlin.given
2526
import org.mockito.kotlin.mock
2627
import org.mockito.kotlin.reset
2728
import org.mockito.kotlin.whenever
@@ -61,17 +62,19 @@ class ToolkitAuthManagerTest {
6162
@Test
6263
fun `test NEEDS_REFRESH state with network error - first occurrence`() {
6364
whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH)
64-
doThrow(UnknownHostException("Unable to execute HTTP request"))
65-
.whenever(tokenProvider)
66-
.resolveToken()
67-
68-
val result = maybeReauthProviderIfNeeded(
69-
project,
70-
ReauthSource.TOOLKIT,
71-
tokenProvider
72-
) { _ -> reauthCallCount++ }
65+
given(tokenProvider.resolveToken()).willAnswer {
66+
throw UnknownHostException("Unable to execute HTTP request")
67+
}
7368

74-
assertFalse(result)
69+
try {
70+
maybeReauthProviderIfNeeded(
71+
project,
72+
ReauthSource.TOOLKIT,
73+
tokenProvider
74+
) { _ -> reauthCallCount++ }
75+
} catch (e: UnknownHostException) {
76+
// ignore
77+
}
7578
assertEquals(0, reauthCallCount)
7679
verify(exactly = 1) {
7780
notifyInfo(
@@ -85,25 +88,32 @@ class ToolkitAuthManagerTest {
8588
@Test
8689
fun `test NEEDS_REFRESH state with network error - subsequent occurrence`() {
8790
whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH)
88-
doThrow(UnknownHostException("Unable to execute HTTP request"))
89-
.`when`(tokenProvider)
90-
.resolveToken()
91+
given(tokenProvider.resolveToken()).willAnswer {
92+
throw UnknownHostException("Unable to execute HTTP request")
93+
}
9194

9295
// First call to set the internal flag
93-
maybeReauthProviderIfNeeded(
94-
project,
95-
ReauthSource.TOOLKIT,
96-
tokenProvider
97-
) { _ -> reauthCallCount++ }
96+
try {
97+
maybeReauthProviderIfNeeded(
98+
project,
99+
ReauthSource.TOOLKIT,
100+
tokenProvider
101+
) { _ -> reauthCallCount++ }
102+
} catch (e: UnknownHostException) {
103+
// ignore
104+
}
98105

99106
// Second call - should not show notification
100-
val result = maybeReauthProviderIfNeeded(
101-
project,
102-
ReauthSource.TOOLKIT,
103-
tokenProvider
104-
) { _ -> reauthCallCount++ }
107+
try {
108+
maybeReauthProviderIfNeeded(
109+
project,
110+
ReauthSource.TOOLKIT,
111+
tokenProvider
112+
) { _ -> reauthCallCount++ }
113+
} catch (e: UnknownHostException) {
114+
// ignore
115+
}
105116

106-
assertFalse(result)
107117
assertEquals(0, reauthCallCount)
108118
verify(exactly = 1) {
109119
notifyInfo(
@@ -119,15 +129,19 @@ class ToolkitAuthManagerTest {
119129
whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH)
120130

121131
// First trigger a network error
122-
doThrow(UnknownHostException("Unable to execute HTTP request"))
123-
.`when`(tokenProvider)
124-
.resolveToken()
132+
given(tokenProvider.resolveToken()).willAnswer {
133+
throw UnknownHostException("Unable to execute HTTP request")
134+
}
125135

126-
maybeReauthProviderIfNeeded(
127-
project,
128-
ReauthSource.TOOLKIT,
129-
tokenProvider
130-
) { _ -> reauthCallCount++ }
136+
try {
137+
maybeReauthProviderIfNeeded(
138+
project,
139+
ReauthSource.TOOLKIT,
140+
tokenProvider
141+
) { _ -> reauthCallCount++ }
142+
} catch (e: UnknownHostException) {
143+
// ignore
144+
}
131145

132146
reset(tokenProvider)
133147

@@ -151,14 +165,18 @@ class ToolkitAuthManagerTest {
151165
reset(tokenProvider)
152166
// Now trigger another network error - should show notification again
153167
whenever(tokenProvider.state()).thenReturn(BearerTokenAuthState.NEEDS_REFRESH)
154-
doThrow(UnknownHostException("Unable to execute HTTP request"))
155-
.`when`(tokenProvider)
156-
.resolveToken()
157-
maybeReauthProviderIfNeeded(
158-
project,
159-
ReauthSource.TOOLKIT,
160-
tokenProvider
161-
) { _ -> reauthCallCount++ }
168+
given(tokenProvider.resolveToken()).willAnswer {
169+
throw UnknownHostException("Unable to execute HTTP request")
170+
}
171+
try {
172+
maybeReauthProviderIfNeeded(
173+
project,
174+
ReauthSource.TOOLKIT,
175+
tokenProvider
176+
) { _ -> reauthCallCount++ }
177+
} catch (e: UnknownHostException) {
178+
// ignore
179+
}
162180

163181
verify(exactly = 2) {
164182
notifyInfo(

0 commit comments

Comments
 (0)