Search in sources :

Example 6 with ApiProvider

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();
}
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 7 with ApiProvider

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;
}
Also used : Function(java.util.function.Function) ApiProvider(com.github.robozonky.common.remote.ApiProvider) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken)

Example 8 with ApiProvider

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;
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) OAuth(com.github.robozonky.common.remote.OAuth)

Example 9 with ApiProvider

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

Example 10 with ApiProvider

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

Aggregations

ApiProvider (com.github.robozonky.common.remote.ApiProvider)19 Test (org.junit.jupiter.api.Test)13 OAuth (com.github.robozonky.common.remote.OAuth)12 ZonkyApiToken (com.github.robozonky.api.remote.entities.ZonkyApiToken)11 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 InstallData (com.izforge.izpack.api.data.InstallData)3 DataValidator (com.izforge.izpack.api.installer.DataValidator)3 Collection (java.util.Collection)3 ConfirmationProvider (com.github.robozonky.api.confirmations.ConfirmationProvider)2 RawInvestment (com.github.robozonky.api.remote.entities.RawInvestment)2 RawLoan (com.github.robozonky.api.remote.entities.RawLoan)2 Function (java.util.function.Function)2 ServerErrorException (javax.ws.rs.ServerErrorException)2 Consumer (java.util.function.Consumer)1