Search in sources :

Example 6 with ZonkyApiToken

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);
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Test(org.junit.jupiter.api.Test)

Example 7 with ZonkyApiToken

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;
}
Also used : ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken)

Example 8 with ZonkyApiToken

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();
        }
    });
}
Also used : ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken)

Example 9 with ZonkyApiToken

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();
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) Collection(java.util.Collection) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) OAuth(com.github.robozonky.common.remote.OAuth) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 10 with ZonkyApiToken

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();
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) Collection(java.util.Collection) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) OAuth(com.github.robozonky.common.remote.OAuth) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Aggregations

ZonkyApiToken (com.github.robozonky.api.remote.entities.ZonkyApiToken)14 Test (org.junit.jupiter.api.Test)11 ApiProvider (com.github.robozonky.common.remote.ApiProvider)10 OAuth (com.github.robozonky.common.remote.OAuth)9 Zonky (com.github.robozonky.common.remote.Zonky)5 Optional (java.util.Optional)5 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)3 SecretProvider (com.github.robozonky.common.secrets.SecretProvider)3 Collection (java.util.Collection)3 RawInvestment (com.github.robozonky.api.remote.entities.RawInvestment)2 ZonkyOAuthApi (com.github.robozonky.api.remote.ZonkyOAuthApi)1 InstallData (com.izforge.izpack.api.data.InstallData)1 DataValidator (com.izforge.izpack.api.installer.DataValidator)1 ServerErrorException (javax.ws.rs.ServerErrorException)1 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)1 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)1