Search in sources :

Example 11 with ZonkyApiToken

use of com.github.robozonky.api.remote.entities.ZonkyApiToken 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 12 with ZonkyApiToken

use of com.github.robozonky.api.remote.entities.ZonkyApiToken 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)

Example 13 with ZonkyApiToken

use of com.github.robozonky.api.remote.entities.ZonkyApiToken in project robozonky by RoboZonky.

the class ZonkySettingsValidatorTest method properLogin.

@Test
void properLogin() {
    // mock data
    final ZonkyApiToken token = mock(ZonkyApiToken.class);
    final OAuth oauth = mock(OAuth.class);
    when(oauth.login(any(), any())).thenReturn(token);
    final Zonky zonky = mock(Zonky.class);
    final ApiProvider provider = mockApiProvider(oauth, token, zonky);
    // execute SUT
    final ZonkySettingsValidator validator = new ZonkySettingsValidator(() -> provider);
    final InstallData d = ZonkySettingsValidatorTest.mockInstallData();
    final DataValidator.Status result = validator.validateData(d);
    // test
    assertThat(result).isEqualTo(DataValidator.Status.OK);
    verify(oauth).login(eq(ZonkySettingsValidatorTest.USERNAME), eq(ZonkySettingsValidatorTest.PASSWORD.toCharArray()));
    verify(zonky).logout();
}
Also used : InstallData(com.izforge.izpack.api.data.InstallData) DataValidator(com.izforge.izpack.api.installer.DataValidator) ApiProvider(com.github.robozonky.common.remote.ApiProvider) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) OAuth(com.github.robozonky.common.remote.OAuth) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test)

Example 14 with ZonkyApiToken

use of com.github.robozonky.api.remote.entities.ZonkyApiToken in project robozonky by RoboZonky.

the class OAuthTest method refresh.

@Test
void refresh() {
    final String originalTokenId = UUID.randomUUID().toString();
    final ZonkyApiToken originToken = new ZonkyApiToken(UUID.randomUUID().toString(), originalTokenId, OffsetDateTime.now());
    final ZonkyApiToken resultToken = mock(ZonkyApiToken.class);
    final ZonkyOAuthApi api = mock(ZonkyOAuthApi.class);
    when(api.refresh(eq(originalTokenId), anyString(), anyString())).thenReturn(resultToken);
    final Api<ZonkyOAuthApi> wrapper = new Api<>(api);
    final OAuth oauth = new OAuth(wrapper);
    final ZonkyApiToken returnedToken = oauth.refresh(originToken);
    assertThat(returnedToken).isEqualTo(resultToken);
}
Also used : ZonkyOAuthApi(com.github.robozonky.api.remote.ZonkyOAuthApi) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) ZonkyOAuthApi(com.github.robozonky.api.remote.ZonkyOAuthApi) Test(org.junit.jupiter.api.Test)

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