use of com.github.robozonky.api.remote.entities.ZonkyApiToken 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.api.remote.entities.ZonkyApiToken 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.api.remote.entities.ZonkyApiToken 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);
}
use of com.github.robozonky.api.remote.entities.ZonkyApiToken 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.api.remote.entities.ZonkyApiToken in project robozonky by RoboZonky.
the class ZonkySettingsValidator method validateDataPossiblyThrowingException.
@Override
public DataValidator.Status validateDataPossiblyThrowingException(final InstallData installData) {
final String username = Variables.ZONKY_USERNAME.getValue(installData);
final String password = Variables.ZONKY_PASSWORD.getValue(installData);
final ApiProvider p = apiSupplier.get();
return p.oauth((oauth) -> {
try {
LOGGER.info("Logging in.");
final ZonkyApiToken token = oauth.login(username, password.toCharArray());
LOGGER.info("Logging out.");
p.authenticated(token, Zonky::logout);
return DataValidator.Status.OK;
} catch (final ServerErrorException t) {
LOGGER.log(Level.SEVERE, "Failed accessing Zonky.", t);
return DataValidator.Status.ERROR;
} catch (final Exception t) {
LOGGER.log(Level.WARNING, "Failed logging in.", t);
return DataValidator.Status.WARNING;
}
});
}
Aggregations