Skip to content

Commit

Permalink
test: remove unneeded mocks for fabric8io#4442 tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Nov 8, 2022
1 parent 31efb15 commit 7964f14
Showing 1 changed file with 14 additions and 11 deletions.
Expand Up @@ -38,7 +38,8 @@
import static io.fabric8.kubernetes.client.Config.KUBERNETES_KUBECONFIG_FILE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

/**
* Ignoring for now - the token refresh should be based upon the java 11 client or the provided client library and not okhttp
Expand Down Expand Up @@ -98,40 +99,42 @@ void shouldAutoconfigureAfter1Minute() throws Exception {
@DisplayName("#4442 token auto refresh should not overwrite existing token when not applicable")
void refreshShouldNotOverwriteExistingToken() throws Exception {
// Given
final Config originalConfig = spy(new ConfigBuilder(Config.empty())
.withOauthToken("existing-token")
.build());
final Config autoConfig = new ConfigBuilder(Config.empty())
.withOauthToken("") // empty token
.build();
final Config config = Mockito.mock(Config.class);
Mockito.when(config.getOauthToken()).thenReturn("existing-token");
Mockito.when(config.refresh()).thenReturn(autoConfig);
when(originalConfig.refresh()).thenReturn(autoConfig);
final TokenRefreshInterceptor tokenRefreshInterceptor = new TokenRefreshInterceptor(
config, null, Instant.now().minusSeconds(61));
originalConfig, null, Instant.now().minusSeconds(61));
// When
final boolean result = tokenRefreshInterceptor
.afterFailure(new StandardHttpRequest.Builder(), new TestHttpResponse<>().withCode(401)).get();
// Then
assertThat(result).isFalse();
Mockito.verify(config, never()).setOauthToken("new-token");
assertThat(originalConfig).hasFieldOrPropertyWithValue("oauthToken", "existing-token");
}

@Test
@DisplayName("#4442 token auto refresh should overwrite existing token when applicable")
void refreshShouldOverwriteExistingToken() throws Exception {
// Given
final Config originalConfig = spy(new ConfigBuilder(Config.empty())
.withOauthToken("existing-token")
.build());
final Config autoConfig = new ConfigBuilder(Config.empty())
.withOauthToken("new-token")
.build();
final Config config = Mockito.mock(Config.class);
Mockito.when(config.getOauthToken()).thenReturn("existing-token");
Mockito.when(config.refresh()).thenReturn(autoConfig);
when(originalConfig.refresh()).thenReturn(autoConfig);
final TokenRefreshInterceptor tokenRefreshInterceptor = new TokenRefreshInterceptor(
config, null, Instant.now().minusSeconds(61));
originalConfig, null, Instant.now().minusSeconds(61));
// When
final boolean result = tokenRefreshInterceptor
.afterFailure(new StandardHttpRequest.Builder(), new TestHttpResponse<>().withCode(401)).get();
// Then
assertThat(result).isTrue();
Mockito.verify(config).setOauthToken("new-token");
assertThat(originalConfig).hasFieldOrPropertyWithValue("oauthToken", "new-token");
}

@Test
Expand Down

0 comments on commit 7964f14

Please sign in to comment.