use of com.github.robozonky.common.remote.ApiProvider 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.ApiProvider in project robozonky by RoboZonky.
the class AuthenticatedTest method mockApiProvider.
private static ApiProvider mockApiProvider(final OAuth oauth, final Zonky z) {
final ApiProvider api = mock(ApiProvider.class);
when(api.oauth(any(Function.class))).then(i -> {
final Function f = i.getArgument(0);
return f.apply(oauth);
});
when(api.authenticated(any(ZonkyApiToken.class), any(Function.class))).then(i -> {
final Function f = i.getArgument(1);
return f.apply(z);
});
return api;
}
use of com.github.robozonky.common.remote.ApiProvider 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.ApiProvider 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.ApiProvider 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