use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.
the class ZonkyApiTokenSupplierTest method refreshFailOnToken.
@Test
void refreshFailOnToken() {
final OAuth oAuth = mock(OAuth.class);
when(oAuth.login(eq(SECRETS.getUsername()), eq(SECRETS.getPassword()))).thenAnswer(invocation -> getTokenExpiringIn(Duration.ofSeconds(5)));
when(oAuth.refresh(any())).thenThrow(BadRequestException.class);
final ApiProvider api = mockApi(oAuth);
final Supplier<Optional<ZonkyApiToken>> t = new ZonkyApiTokenSupplier(api, SECRETS, Duration.ZERO);
final Optional<ZonkyApiToken> token = t.get();
assertThat(token).isPresent();
final Optional<ZonkyApiToken> token2 = t.get();
assertThat(token2).isPresent();
assertThat(token2).isNotEqualTo(token);
}
use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.
the class AuthenticatedTest method passwordProper.
@Test
void passwordProper() {
// prepare SUT
final SecretProvider sp = SecretProvider.fallback(UUID.randomUUID().toString(), new char[0]);
final String username = sp.getUsername();
final char[] password = sp.getPassword();
final ZonkyApiToken token = new ZonkyApiToken(UUID.randomUUID().toString(), UUID.randomUUID().toString(), 299);
final OAuth oauth = mock(OAuth.class);
when(oauth.login(eq(username), eq(password))).thenReturn(token);
final Zonky z = mock(Zonky.class);
final ApiProvider api = mockApiProvider(oauth, z);
final Authenticated a = Authenticated.passwordBased(api, sp);
// call SUT
final Function<Zonky, Collection<RawInvestment>> f = mock(Function.class);
final Collection<RawInvestment> expectedResult = Collections.emptyList();
when(f.apply(eq(z))).thenReturn(expectedResult);
final Collection<RawInvestment> result = a.call(f);
assertThat(result).isSameAs(expectedResult);
verify(oauth).login(eq(username), eq(password));
verify(oauth, never()).refresh(any());
verify(z).logout();
}
use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.
the class AuthenticatedTest method passwordLogsOutEvenWhenFailing.
@Test
void passwordLogsOutEvenWhenFailing() {
// prepare SUT
final SecretProvider sp = SecretProvider.fallback(UUID.randomUUID().toString(), new char[0]);
final String username = sp.getUsername();
final char[] password = sp.getPassword();
final ZonkyApiToken token = new ZonkyApiToken(UUID.randomUUID().toString(), UUID.randomUUID().toString(), 299);
final OAuth oauth = mock(OAuth.class);
when(oauth.login(eq(username), eq(password))).thenReturn(token);
final Zonky z = mock(Zonky.class);
final ApiProvider api = mockApiProvider(oauth, z);
final Authenticated a = Authenticated.passwordBased(api, sp);
// call SUT
final Function<Zonky, Collection<RawInvestment>> f = mock(Function.class);
when(f.apply(eq(z))).thenThrow(new IllegalStateException());
assertThatThrownBy(() -> a.call(f)).isInstanceOf(IllegalStateException.class);
verify(z).logout();
}
use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.
the class ZonkyApiTokenSupplierTest method fixesExpiredToken.
@Test
void fixesExpiredToken() {
final OAuth oAuth = mock(OAuth.class);
when(oAuth.login(eq(SECRETS.getUsername()), eq(SECRETS.getPassword()))).thenAnswer(invocation -> getStaleToken());
final ApiProvider api = mockApi(oAuth);
final Supplier<Optional<ZonkyApiToken>> t = new ZonkyApiTokenSupplier(api, SECRETS, Duration.ZERO);
final Optional<ZonkyApiToken> token = t.get();
assertThat(token).isPresent();
final Optional<ZonkyApiToken> token2 = t.get();
assertThat(token2).isPresent();
assertThat(token2).isNotEqualTo(token);
}
use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.
the class ZonkyApiTokenSupplierTest method refreshFailUnknown.
@Test
void refreshFailUnknown() {
final OAuth oAuth = mock(OAuth.class);
when(oAuth.login(eq(SECRETS.getUsername()), eq(SECRETS.getPassword()))).thenAnswer(invocation -> getTokenExpiringIn(Duration.ofSeconds(5)));
when(oAuth.refresh(any())).thenThrow(IllegalStateException.class);
final ApiProvider api = mockApi(oAuth);
final Supplier<Optional<ZonkyApiToken>> t = new ZonkyApiTokenSupplier(api, SECRETS, Duration.ZERO);
final Optional<ZonkyApiToken> token = t.get();
assertThat(token).isPresent();
final Optional<ZonkyApiToken> token2 = t.get();
assertThat(token2).isEmpty();
}
Aggregations