Search in sources :

Example 1 with OAuthStatus

use of org.eclipse.che.security.oauth.OAuthStatus in project che by eclipse.

the class GitHubAuthenticatorImplTest method onAuthenticatedWhenGenerateKeysIsFailure.

@Test
public void onAuthenticatedWhenGenerateKeysIsFailure() throws Exception {
    String userId = "userId";
    OAuthStatus authStatus = mock(OAuthStatus.class);
    SshKeyUploader keyProvider = mock(SshKeyUploader.class);
    CurrentUser user = mock(CurrentUser.class);
    ProfileDto profile = mock(ProfileDto.class);
    MessageDialog messageDialog = mock(MessageDialog.class);
    when(view.isGenerateKeysSelected()).thenReturn(true);
    when(registry.getUploader(GITHUB_HOST)).thenReturn(keyProvider);
    when(appContext.getCurrentUser()).thenReturn(user);
    when(user.getProfile()).thenReturn(profile);
    when(profile.getUserId()).thenReturn(userId);
    when(dialogFactory.createMessageDialog(anyString(), anyString(), Matchers.<ConfirmCallback>anyObject())).thenReturn(messageDialog);
    gitHubAuthenticator.authenticate(null, getCallBack());
    gitHubAuthenticator.onAuthenticated(authStatus);
    verify(keyProvider).uploadKey(eq(userId), generateKeyCallbackCaptor.capture());
    AsyncCallback<Void> generateKeyCallback = generateKeyCallbackCaptor.getValue();
    generateKeyCallback.onFailure(new Exception(""));
    verify(view).isGenerateKeysSelected();
    verify(registry).getUploader(eq(GITHUB_HOST));
    verify(appContext).getCurrentUser();
    verify(dialogFactory).createMessageDialog(anyString(), anyString(), Matchers.<ConfirmCallback>anyObject());
    verify(messageDialog).show();
    verify(sshServiceClient).getPairs(eq(SshKeyManagerPresenter.VCS_SSH_SERVICE));
}
Also used : SshKeyUploader(org.eclipse.che.plugin.ssh.key.client.SshKeyUploader) CurrentUser(org.eclipse.che.ide.api.app.CurrentUser) Matchers.anyString(org.mockito.Matchers.anyString) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) MessageDialog(org.eclipse.che.ide.api.dialogs.MessageDialog) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) Test(org.junit.Test)

Example 2 with OAuthStatus

use of org.eclipse.che.security.oauth.OAuthStatus in project che by eclipse.

the class GithubImporterPagePresenterTest method onLoadRepoClickedWhenAuthorizeIsSuccessful.

@Test
public void onLoadRepoClickedWhenAuthorizeIsSuccessful() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onFailRequest(promiseError);
            return null;
        }
    }).doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onSuccessRequest(jsArrayMixed);
            return null;
        }
    }).when(presenter).doRequest(any(Promise.class), any(Promise.class), any(Promise.class));
    final Throwable exception = mock(UnauthorizedException.class);
    String userId = "userId";
    CurrentUser user = mock(CurrentUser.class);
    ProfileDto profile = mock(ProfileDto.class);
    doReturn(exception).when(promiseError).getCause();
    when(appContext.getCurrentUser()).thenReturn(user);
    when(user.getProfile()).thenReturn(profile);
    when(profile.getUserId()).thenReturn(userId);
    presenter.onLoadRepoClicked();
    verify(gitHubClientService).getRepositoriesList();
    verify(gitHubClientService).getUserInfo();
    verify(gitHubClientService).getOrganizations();
    verify(gitHubAuthenticator).authenticate(anyString(), asyncCallbackCaptor.capture());
    AsyncCallback<OAuthStatus> asyncCallback = asyncCallbackCaptor.getValue();
    asyncCallback.onSuccess(null);
    verify(view, times(3)).setLoaderVisibility(eq(true));
    verify(view, times(3)).setInputsEnableState(eq(false));
    verify(view, times(3)).setInputsEnableState(eq(true));
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Promise(org.eclipse.che.api.promises.client.Promise) CurrentUser(org.eclipse.che.ide.api.app.CurrentUser) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) Test(org.junit.Test)

Example 3 with OAuthStatus

use of org.eclipse.che.security.oauth.OAuthStatus in project che by eclipse.

the class GithubImporterPagePresenterTest method onLoadRepoClickedWhenAuthorizeIsFailed.

@Test
public void onLoadRepoClickedWhenAuthorizeIsFailed() throws Exception {
    String userId = "userId";
    CurrentUser user = mock(CurrentUser.class);
    ProfileDto profile = mock(ProfileDto.class);
    when(appContext.getCurrentUser()).thenReturn(user);
    when(user.getProfile()).thenReturn(profile);
    when(profile.getUserId()).thenReturn(userId);
    final Throwable exception = mock(UnauthorizedException.class);
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onFailRequest(promiseError);
            return null;
        }
    }).when(presenter).doRequest(any(Promise.class), any(Promise.class), any(Promise.class));
    doReturn(exception).when(promiseError).getCause();
    presenter.onLoadRepoClicked();
    verify(gitHubClientService).getRepositoriesList();
    verify(gitHubClientService).getUserInfo();
    verify(gitHubClientService).getOrganizations();
    verify(gitHubAuthenticator).authenticate(anyString(), asyncCallbackCaptor.capture());
    AsyncCallback<OAuthStatus> asyncCallback = asyncCallbackCaptor.getValue();
    asyncCallback.onFailure(exception);
    verify(view, times(2)).setLoaderVisibility(eq(true));
    verify(view, times(2)).setInputsEnableState(eq(false));
    verify(view, times(2)).setInputsEnableState(eq(true));
    verify(view, never()).setAccountNames((Set<String>) anyObject());
    verify(view, never()).showGithubPanel();
    verify(view, never()).setRepositories(Matchers.<List<ProjectData>>anyObject());
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Promise(org.eclipse.che.api.promises.client.Promise) CurrentUser(org.eclipse.che.ide.api.app.CurrentUser) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) ProjectData(org.eclipse.che.plugin.github.ide.load.ProjectData) Test(org.junit.Test)

Example 4 with OAuthStatus

use of org.eclipse.che.security.oauth.OAuthStatus in project che by eclipse.

the class ProjectImporter method authUserAndRecallImport.

private Promise<Project> authUserAndRecallImport(final String providerName, final String authenticateUrl, final Path path, final SourceStorage sourceStorage, final ProjectNotificationSubscriber subscriber) {
    return createFromAsyncRequest(new RequestCall<Project>() {

        @Override
        public void makeCall(final AsyncCallback<Project> callback) {
            OAuth2Authenticator authenticator = oAuth2AuthenticatorRegistry.getAuthenticator(providerName);
            if (authenticator == null) {
                authenticator = oAuth2AuthenticatorRegistry.getAuthenticator("default");
            }
            authenticator.authenticate(OAuth2AuthenticatorUrlProvider.get(appContext.getMasterEndpoint(), authenticateUrl), new AsyncCallback<OAuthStatus>() {

                @Override
                public void onFailure(Throwable caught) {
                    callback.onFailure(new Exception(caught.getMessage()));
                }

                @Override
                public void onSuccess(OAuthStatus result) {
                    if (!result.equals(OAuthStatus.NOT_PERFORMED)) {
                        doImport(path, sourceStorage).then(new Operation<Project>() {

                            @Override
                            public void apply(Project project) throws OperationException {
                                callback.onSuccess(project);
                            }
                        }).catchError(new Operation<PromiseError>() {

                            @Override
                            public void apply(PromiseError error) throws OperationException {
                                callback.onFailure(error.getCause());
                            }
                        });
                    } else {
                        subscriber.onFailure("Authentication cancelled");
                        callback.onFailure(new IllegalStateException("Authentication cancelled"));
                    }
                }
            });
        }
    });
}
Also used : OAuth2Authenticator(org.eclipse.che.ide.api.oauth.OAuth2Authenticator) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) Operation(org.eclipse.che.api.promises.client.Operation) FunctionException(org.eclipse.che.api.promises.client.FunctionException) OperationException(org.eclipse.che.api.promises.client.OperationException) Project(org.eclipse.che.ide.api.resources.Project) PromiseError(org.eclipse.che.api.promises.client.PromiseError) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 5 with OAuthStatus

use of org.eclipse.che.security.oauth.OAuthStatus in project che by eclipse.

the class GitHubAuthenticatorImplTest method onAuthenticatedWhenGenerateKeysIsSelected.

@Test
public void onAuthenticatedWhenGenerateKeysIsSelected() throws Exception {
    String userId = "userId";
    OAuthStatus authStatus = mock(OAuthStatus.class);
    SshKeyUploader sshKeyUploader = mock(SshKeyUploader.class);
    CurrentUser user = mock(CurrentUser.class);
    ProfileDto profile = mock(ProfileDto.class);
    when(view.isGenerateKeysSelected()).thenReturn(true);
    when(registry.getUploader(GITHUB_HOST)).thenReturn(sshKeyUploader);
    when(appContext.getCurrentUser()).thenReturn(user);
    when(user.getProfile()).thenReturn(profile);
    when(profile.getUserId()).thenReturn(userId);
    gitHubAuthenticator.onAuthenticated(authStatus);
    verify(view).isGenerateKeysSelected();
    verify(registry).getUploader(eq(GITHUB_HOST));
    verify(appContext).getCurrentUser();
    verify(sshKeyUploader).uploadKey(eq(userId), Matchers.<AsyncCallback<Void>>anyObject());
}
Also used : SshKeyUploader(org.eclipse.che.plugin.ssh.key.client.SshKeyUploader) CurrentUser(org.eclipse.che.ide.api.app.CurrentUser) Matchers.anyString(org.mockito.Matchers.anyString) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) Test(org.junit.Test)

Aggregations

OAuthStatus (org.eclipse.che.security.oauth.OAuthStatus)8 ProfileDto (org.eclipse.che.api.user.shared.dto.ProfileDto)7 CurrentUser (org.eclipse.che.ide.api.app.CurrentUser)7 Test (org.junit.Test)7 Matchers.anyString (org.mockito.Matchers.anyString)7 SshKeyUploader (org.eclipse.che.plugin.ssh.key.client.SshKeyUploader)4 Promise (org.eclipse.che.api.promises.client.Promise)2 MessageDialog (org.eclipse.che.ide.api.dialogs.MessageDialog)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 ArrayList (java.util.ArrayList)1 FunctionException (org.eclipse.che.api.promises.client.FunctionException)1 Operation (org.eclipse.che.api.promises.client.Operation)1 OperationException (org.eclipse.che.api.promises.client.OperationException)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 SshPairDto (org.eclipse.che.api.ssh.shared.dto.SshPairDto)1 OAuth2Authenticator (org.eclipse.che.ide.api.oauth.OAuth2Authenticator)1 Project (org.eclipse.che.ide.api.resources.Project)1