Search in sources :

Example 1 with ZonkyApiToken

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

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);
}
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 3 with ZonkyApiToken

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);
}
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 4 with ZonkyApiToken

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);
}
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 5 with ZonkyApiToken

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;
        }
    });
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) ServerErrorException(javax.ws.rs.ServerErrorException) ServerErrorException(javax.ws.rs.ServerErrorException) Zonky(com.github.robozonky.common.remote.Zonky)

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