use of com.khmelenko.lab.varis.network.response.Authorization 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(authorization);
AccessTokenRequest request = new AccessTokenRequest();
request.setGithubToken(gitHubToken);
TaskError taskError = new TaskError(500, "error");
TaskException exception = new TaskException(taskError);
when(mTravisRestClient.getApiService().auth(request)).thenThrow(exception);
mAuthPresenter.login(login, password);
verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
verify(mTaskManager).startAuth(gitHubToken);
verify(mAuthView).hideProgress();
verify(mAuthView).showErrorMessage(taskError.getMessage());
}
use of com.khmelenko.lab.varis.network.response.Authorization in project Varis-Android by dkhmelenko.
the class AuthPresenter method doLogin.
private void doLogin(Single<Authorization> authorizationJob) {
Disposable subscription = authorizationJob.flatMap(this::doAuthorization).doOnSuccess(this::saveAccessToken).doAfterSuccess(accessToken -> cleanUpAfterAuthorization()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((authorization, throwable) -> {
getView().hideProgress();
if (throwable == null) {
getView().finishView();
} else {
HttpException httpException = (HttpException) throwable;
if (isTwoFactorAuthRequired(httpException)) {
mSecurityCodeInput = true;
getView().showTwoFactorAuth();
} else {
getView().showErrorMessage(throwable.getMessage());
}
}
});
mSubscriptions.add(subscription);
}
use of com.khmelenko.lab.varis.network.response.Authorization in project Varis-Android by dkhmelenko.
the class AuthPresenter method cleanUpAfterAuthorization.
private void cleanUpAfterAuthorization() {
// start deletion authorization on Github, because we don't need it anymore
Single<Object> cleanUpJob;
if (!TextUtils.isEmpty(mSecurityCode)) {
cleanUpJob = mGitHubRestClient.getApiService().deleteAuthorization(mBasicAuth, mSecurityCode, String.valueOf(mAuthorization.getId()));
} else {
cleanUpJob = mGitHubRestClient.getApiService().deleteAuthorization(mBasicAuth, String.valueOf(mAuthorization.getId()));
}
Disposable task = cleanUpJob.onErrorReturn(throwable -> new Object()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
mSubscriptions.add(task);
}
use of com.khmelenko.lab.varis.network.response.Authorization 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
Header header = new Header(GithubApiService.TWO_FACTOR_HEADER, "required");
List<Header> headers = new ArrayList<>();
headers.add(header);
Response response = new Response("https://github.com", 401, "twoFactorAuth", headers, null);
TaskError taskError = new TaskError(401, "twoFactorAuth");
taskError.setResponse(response);
TaskException exception = new TaskException(taskError);
when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenThrow(exception);
mAuthPresenter.login(login, password);
verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
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(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(token);
when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
mAuthPresenter.twoFactorAuth(securityCode);
verify(mTaskManager).startAuth(gitHubToken);
verify(mTaskManager).deleteAuthorization(auth, String.valueOf(authorization.getId()), securityCode);
verify(mAuthView, times(2)).hideProgress();
verify(mAuthView).finishView();
}
use of com.khmelenko.lab.varis.network.response.Authorization in project Varis-Android by dkhmelenko.
the class TestAuthPresenter method testLoginSuccess.
@Test
public void testLoginSuccess() {
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(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(token);
when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
mAuthPresenter.login(login, password);
verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
verify(mTaskManager).startAuth(gitHubToken);
verify(mTaskManager).deleteAuthorization(auth, String.valueOf(authorization.getId()));
verify(mAuthView).hideProgress();
verify(mAuthView).finishView();
}
Aggregations