Search in sources :

Example 1 with AccessTokenRequest

use of com.khmelenko.lab.varis.network.request.AccessTokenRequest in project Varis-Android by dkhmelenko.

the class TestAuthPresenter method testLoginAuthFailed.

@Test
public void testLoginAuthFailed() {
    final String login = "login";
    final String password = "password";
    String auth = EncryptionUtils.generateBasicAuthorization(login, password);
    final String gitHubToken = "gitHubToken";
    Authorization authorization = new Authorization();
    authorization.setToken(gitHubToken);
    authorization.setId(1L);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenReturn(Single.just(authorization));
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(gitHubToken);
    final String errorMsg = "error";
    Exception exception = new Exception(errorMsg);
    when(mTravisRestClient.getApiService().auth(request)).thenReturn(Single.error(exception));
    mAuthPresenter.login(login, password);
    verify(mAuthView).hideProgress();
    verify(mAuthView).showErrorMessage(errorMsg);
}
Also used : Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) HttpException(retrofit2.HttpException) Test(org.junit.Test)

Example 2 with AccessTokenRequest

use of com.khmelenko.lab.varis.network.request.AccessTokenRequest in project Varis-Android by dkhmelenko.

the class TestAuthPresenter method testLoginAuthFailed.

@Test
public void testLoginAuthFailed() {
    final String login = "login";
    final String password = "password";
    String auth = EncryptionUtils.generateBasicAuthorization(login, password);
    final String gitHubToken = "gitHubToken";
    Authorization authorization = new Authorization();
    authorization.setToken(gitHubToken);
    authorization.setId(1L);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenReturn(Single.just(authorization));
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(gitHubToken);
    final String errorMsg = "error";
    Exception exception = new Exception(errorMsg);
    when(mTravisRestClient.getApiService().auth(request)).thenReturn(Single.error(exception));
    mAuthPresenter.login(login, password);
    verify(mAuthView).hideProgress();
    verify(mAuthView).showErrorMessage(errorMsg);
}
Also used : Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) HttpException(retrofit2.HttpException) Test(org.junit.Test)

Example 3 with AccessTokenRequest

use of com.khmelenko.lab.varis.network.request.AccessTokenRequest in project Varis-Android by dkhmelenko.

the class TestAuthPresenter method testTwoFactorAuth.

@Test
public void testTwoFactorAuth() {
    final String login = "login";
    final String password = "password";
    String auth = EncryptionUtils.generateBasicAuthorization(login, password);
    // rules for throwing a request for 2-factor auth
    final String expectedUrl = "https://sample.org";
    Request rawRequest = new Request.Builder().url(expectedUrl).build();
    okhttp3.Response rawResponse = new okhttp3.Response.Builder().request(rawRequest).message("no body").protocol(Protocol.HTTP_1_1).code(401).header(GithubApiService.TWO_FACTOR_HEADER, "required").build();
    Response response = Response.error(ResponseBody.create(null, ""), rawResponse);
    HttpException exception = new HttpException(response);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenReturn(Single.error(exception));
    mAuthPresenter.login(login, password);
    verify(mAuthView).showTwoFactorAuth();
    // rules for handling 2-factor auth continuation
    final String securityCode = "123456";
    final String gitHubToken = "gitHubToken";
    Authorization authorization = new Authorization();
    authorization.setToken(gitHubToken);
    authorization.setId(1L);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), eq(securityCode), any(AuthorizationRequest.class))).thenReturn(Single.just(authorization));
    final String accessToken = "token";
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(gitHubToken);
    AccessToken token = new AccessToken();
    token.setAccessToken(accessToken);
    when(mTravisRestClient.getApiService().auth(request)).thenReturn(Single.just(token));
    when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
    mAuthPresenter.twoFactorAuth(securityCode);
    verify(mAuthView, times(2)).hideProgress();
    verify(mAuthView).finishView();
}
Also used : Response(retrofit2.Response) Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) Request(okhttp3.Request) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) HttpException(retrofit2.HttpException) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) Test(org.junit.Test)

Example 4 with AccessTokenRequest

use of com.khmelenko.lab.varis.network.request.AccessTokenRequest in project Varis-Android by dkhmelenko.

the class AuthPresenter method doAuthorization.

private Single<AccessToken> doAuthorization(Authorization authorization) {
    mAuthorization = authorization;
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(authorization.getToken());
    return mTravisRestClient.getApiService().auth(request);
}
Also used : AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest)

Example 5 with AccessTokenRequest

use of com.khmelenko.lab.varis.network.request.AccessTokenRequest in project Varis-Android by dkhmelenko.

the class AuthPresenter method doAuthorization.

private Single<AccessToken> doAuthorization(Authorization authorization) {
    mAuthorization = authorization;
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(authorization.getToken());
    return mTravisRestClient.getApiService().auth(request);
}
Also used : AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest)

Aggregations

AccessTokenRequest (com.khmelenko.lab.varis.network.request.AccessTokenRequest)8 AuthorizationRequest (com.khmelenko.lab.varis.network.request.AuthorizationRequest)6 Authorization (com.khmelenko.lab.varis.network.response.Authorization)6 Test (org.junit.Test)6 AccessToken (com.khmelenko.lab.varis.network.response.AccessToken)4 HttpException (retrofit2.HttpException)4 Request (okhttp3.Request)2 Response (retrofit2.Response)2