Search in sources :

Example 11 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider 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 12 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider 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 13 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider 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)

Example 14 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class ZonkyApiTokenSupplierTest method fixesExpiredToken.

@Test
void fixesExpiredToken() {
    final OAuth oAuth = mock(OAuth.class);
    when(oAuth.login(eq(SECRETS.getUsername()), eq(SECRETS.getPassword()))).thenAnswer(invocation -> getStaleToken());
    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 15 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class ZonkyApiTokenSupplierTest method refreshFailUnknown.

@Test
void refreshFailUnknown() {
    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(IllegalStateException.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).isEmpty();
}
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