use of com.github.robozonky.api.remote.entities.ZonkyApiToken in project robozonky by RoboZonky.
the class AuthenticatedFilterTest method changes400to401.
@Test
void changes400to401() throws IOException {
final int expectedCode = 400;
final ClientRequestContext ctx = mock(ClientRequestContext.class);
final ClientResponseContext ctx2 = mock(ClientResponseContext.class);
final ZonkyApiToken token = new ZonkyApiToken("", "", 299);
when(ctx2.hasEntity()).thenReturn(true);
when(ctx2.getHeaders()).thenReturn(new MultivaluedMapImpl<>());
when(ctx2.getEntityStream()).thenReturn(c(token));
when(ctx2.getStatusInfo()).thenReturn(Response.Status.fromStatusCode(expectedCode));
when(ctx2.getStatus()).thenReturn(expectedCode);
final RoboZonkyFilter filter = new AuthenticatedFilter(token);
filter.filter(ctx, ctx2);
verify(ctx2, times(1)).setStatus(401);
}
use of com.github.robozonky.api.remote.entities.ZonkyApiToken in project robozonky by RoboZonky.
the class ApiProviderTest method mockToken.
private static ZonkyApiToken mockToken() {
final ZonkyApiToken t = mock(ZonkyApiToken.class);
when(t.getAccessToken()).thenReturn(UUID.randomUUID().toString().toCharArray());
return t;
}
use of com.github.robozonky.api.remote.entities.ZonkyApiToken in project robozonky by RoboZonky.
the class PasswordBasedAccess method call.
@Override
public <T> T call(final Function<Zonky, T> op) {
final ZonkyApiToken token = PasswordBasedAccess.trigger(apis, secrets.getUsername(), secrets.getPassword());
return apis.authenticated(token, (zonky) -> {
try {
return op.apply(zonky);
} finally {
// attempt to log out no matter what happens
LOGGER.trace("Logging out.");
zonky.logout();
}
});
}
use of com.github.robozonky.api.remote.entities.ZonkyApiToken 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.api.remote.entities.ZonkyApiToken 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();
}
Aggregations