diff --git a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/CallbackTest.java b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/CallbackTest.java index 3335d7a..bc92c09 100644 --- a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/CallbackTest.java +++ b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/CallbackTest.java @@ -1,3 +1,22 @@ +/* + * SonarQube :: GitLab Auth Plugin + * Copyright (C) 2016-2017 Talanlabs + * gabriel.allaigre@talanlabs.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package com.talanlabs.sonar.plugins.gitlab.auth; import com.squareup.okhttp.mockwebserver.MockResponse; @@ -18,7 +37,7 @@ public class CallbackTest { public MockWebServer gitlab = new MockWebServer(); @Test - public void testCallback() { + public void testCallbackSuccess() { GitLabConfiguration configuration = Mockito.mock(GitLabConfiguration.class); Mockito.when(configuration.isEnabled()).thenReturn(true); Mockito.when(configuration.allowUsersToSignUp()).thenReturn(true); @@ -36,8 +55,7 @@ public void testCallback() { Mockito.when(callbackContext.getRequest()).thenReturn(httpServletRequest); gitlab.enqueue(new MockResponse().setBody( - "{\n" + " \"access_token\": \"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": 7200,\n" - + " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}")); + "{\n" + " \"access_token\": \"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": 7200,\n" + " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}")); gitlab.enqueue(new MockResponse().setBody("{\"username\":\"toto\", \"name\":\"Toto Toto\",\"email\":\"toto@toto.com\"}")); gitLabIdentityProvider.callback(callbackContext); @@ -51,4 +69,31 @@ public void testCallback() { Assertions.assertThat(argument.getValue().getEmail()).isEqualTo("toto@toto.com"); Mockito.verify(callbackContext).redirectToRequestedPage(); } + + @Test + public void testCallbackFail() { + GitLabConfiguration configuration = Mockito.mock(GitLabConfiguration.class); + Mockito.when(configuration.isEnabled()).thenReturn(true); + Mockito.when(configuration.allowUsersToSignUp()).thenReturn(true); + Mockito.when(configuration.applicationId()).thenReturn("123"); + Mockito.when(configuration.secret()).thenReturn("456"); + Mockito.when(configuration.url()).thenReturn(String.format("http://%s:%d", gitlab.getHostName(), gitlab.getPort())); + GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(configuration); + + OAuth2IdentityProvider.CallbackContext callbackContext = Mockito.mock(OAuth2IdentityProvider.CallbackContext.class); + Mockito.when(callbackContext.getCallbackUrl()).thenReturn("http://server/callback"); + + HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getParameter("code")).thenReturn("789"); + + Mockito.when(callbackContext.getRequest()).thenReturn(httpServletRequest); + + gitlab.enqueue(new MockResponse().setBody( + "{\n" + " \"access_token\": \"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": 7200,\n" + + " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}")); + gitlab.enqueue(new MockResponse().setResponseCode(404).setBody("empty")); + + Assertions.assertThatThrownBy(() -> gitLabIdentityProvider.callback(callbackContext)).isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Fail to authenticate the user. Error code is 404, Body of the response is empty"); + } } diff --git a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabApiTest.java b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabApiTest.java index dd34676..f3a64fc 100644 --- a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabApiTest.java +++ b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabApiTest.java @@ -1,3 +1,22 @@ +/* + * SonarQube :: GitLab Auth Plugin + * Copyright (C) 2016-2017 Talanlabs + * gabriel.allaigre@talanlabs.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package com.talanlabs.sonar.plugins.gitlab.auth; import com.github.scribejava.core.extractors.JsonTokenExtractor; diff --git a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabAuthPluginTest.java b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabAuthPluginTest.java index 9e776dd..b5897f8 100644 --- a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabAuthPluginTest.java +++ b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabAuthPluginTest.java @@ -1,5 +1,5 @@ /* - * SonarQube :: GitLab Plugin + * SonarQube :: GitLab Auth Plugin * Copyright (C) 2016-2017 Talanlabs * gabriel.allaigre@talanlabs.com * diff --git a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabConfigurationTest.java b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabConfigurationTest.java index e768309..5444911 100644 --- a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabConfigurationTest.java +++ b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabConfigurationTest.java @@ -1,5 +1,5 @@ /* - * SonarQube :: GitLab Plugin + * SonarQube :: GitLab Auth Plugin * Copyright (C) 2016-2017 Talanlabs * gabriel.allaigre@talanlabs.com * diff --git a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabIdentityProviderTest.java b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabIdentityProviderTest.java index e3697d3..ee4cda8 100644 --- a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabIdentityProviderTest.java +++ b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabIdentityProviderTest.java @@ -1,3 +1,22 @@ +/* + * SonarQube :: GitLab Auth Plugin + * Copyright (C) 2016-2017 Talanlabs + * gabriel.allaigre@talanlabs.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package com.talanlabs.sonar.plugins.gitlab.auth; import org.assertj.core.api.Assertions; @@ -25,7 +44,7 @@ public void testFields() { } @Test - public void testInit() { + public void testInitSuccess() { GitLabConfiguration configuration = Mockito.mock(GitLabConfiguration.class); Mockito.when(configuration.isEnabled()).thenReturn(true); Mockito.when(configuration.allowUsersToSignUp()).thenReturn(true); @@ -41,4 +60,20 @@ public void testInit() { Mockito.verify(initContext).redirectTo("http://server/oauth/authorize?client_id=123&redirect_uri=http%3A%2F%2Fserver%2Fcallback&response_type=code&scope=read_user"); } + + @Test + public void testInitFail() { + GitLabConfiguration configuration = Mockito.mock(GitLabConfiguration.class); + Mockito.when(configuration.isEnabled()).thenReturn(false); + Mockito.when(configuration.allowUsersToSignUp()).thenReturn(true); + Mockito.when(configuration.applicationId()).thenReturn("123"); + Mockito.when(configuration.secret()).thenReturn("456"); + Mockito.when(configuration.url()).thenReturn("http://server"); + GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(configuration); + + OAuth2IdentityProvider.InitContext initContext = Mockito.mock(OAuth2IdentityProvider.InitContext.class); + Mockito.when(initContext.getCallbackUrl()).thenReturn("http://server/callback"); + + Assertions.assertThatThrownBy(() -> gitLabIdentityProvider.init(initContext)).isInstanceOf(IllegalStateException.class).hasMessageContaining("GitLab Authentication is disabled"); + } } diff --git a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GsonUserTest.java b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GsonUserTest.java index 113ee90..db0b906 100644 --- a/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GsonUserTest.java +++ b/src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GsonUserTest.java @@ -1,3 +1,22 @@ +/* + * SonarQube :: GitLab Auth Plugin + * Copyright (C) 2016-2017 Talanlabs + * gabriel.allaigre@talanlabs.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package com.talanlabs.sonar.plugins.gitlab.auth; import org.assertj.core.api.Assertions;