use of com.github.robozonky.common.remote.OAuth in project robozonky by RoboZonky.
the class ZonkySettingsValidatorTest method warning.
@Test
void warning() {
// mock data
final OAuth oauth = mock(OAuth.class);
when(oauth.login(any(), any())).thenThrow(new IllegalStateException());
final ApiProvider provider = mockApiProvider(oauth);
final InstallData d = ZonkySettingsValidatorTest.mockInstallData();
// execute SUT
final ZonkySettingsValidator validator = new ZonkySettingsValidator(() -> provider);
final DataValidator.Status result = validator.validateData(d);
// test
assertThat(result).isEqualTo(DataValidator.Status.WARNING);
}
use of com.github.robozonky.common.remote.OAuth in project robozonky by RoboZonky.
the class AuthenticatedTest method tokenProper.
@Test
void tokenProper() {
// 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 TokenBasedAccess a = (TokenBasedAccess) Authenticated.tokenBased(api, sp, Duration.ofSeconds(60));
// 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);
assertSoftly(softly -> {
softly.assertThat(result).isSameAs(expectedResult);
softly.assertThat(a.getSecretProvider()).isSameAs(sp);
});
verify(oauth).login(eq(username), eq(password));
verify(oauth, never()).refresh(any());
verify(z, never()).logout();
}
use of com.github.robozonky.common.remote.OAuth in project robozonky by RoboZonky.
the class ZonkyApiTokenSupplierTest method mockApi.
private static ApiProvider mockApi(final OAuth oAuth) {
final ApiProvider api = mock(ApiProvider.class);
when(api.oauth(any())).thenAnswer(invocation -> {
final Function<OAuth, Object> f = invocation.getArgument(0);
return f.apply(oAuth);
});
return api;
}
use of com.github.robozonky.common.remote.OAuth in project robozonky by RoboZonky.
the class ZonkyApiTokenSupplierTest method reusesExistingToken.
@Test
void reusesExistingToken() {
final OAuth oAuth = mock(OAuth.class);
when(oAuth.login(eq(SECRETS.getUsername()), eq(SECRETS.getPassword()))).thenAnswer(invocation -> getTokenExpiringIn(Duration.ofMinutes(5)));
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).isEqualTo(token);
}
use of com.github.robozonky.common.remote.OAuth in project robozonky by RoboZonky.
the class ZonkyApiTokenSupplierTest method refreshesTokenBeforeExpiration.
@Test
void refreshesTokenBeforeExpiration() {
final OAuth oAuth = mock(OAuth.class);
when(oAuth.login(eq(SECRETS.getUsername()), eq(SECRETS.getPassword()))).thenReturn(getTokenExpiringIn(Duration.ofSeconds(5)));
when(oAuth.refresh(any())).thenAnswer(invocation -> getTokenExpiringIn(Duration.ofSeconds(5)));
final ApiProvider api = mockApi(oAuth);
final Supplier<Optional<ZonkyApiToken>> t = new ZonkyApiTokenSupplier(api, SECRETS, Duration.ofSeconds(1));
final Optional<ZonkyApiToken> token = t.get();
assertThat(token).isPresent();
final Optional<ZonkyApiToken> token2 = t.get();
assertThat(token2).isPresent();
assertThat(token2).isNotEqualTo(token);
}
Aggregations