-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabriel Allaigre
committed
Mar 3, 2017
1 parent
9aba821
commit f63317c
Showing
6 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
/* | ||
* SonarQube :: GitLab Auth Plugin | ||
* Copyright (C) 2016-2017 Talanlabs | ||
* [email protected] | ||
* | ||
* 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\":\"[email protected]\"}")); | ||
|
||
gitLabIdentityProvider.callback(callbackContext); | ||
|
@@ -51,4 +69,31 @@ public void testCallback() { | |
Assertions.assertThat(argument.getValue().getEmail()).isEqualTo("[email protected]"); | ||
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"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
/* | ||
* SonarQube :: GitLab Auth Plugin | ||
* Copyright (C) 2016-2017 Talanlabs | ||
* [email protected] | ||
* | ||
* 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; | ||
|
2 changes: 1 addition & 1 deletion
2
src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabAuthPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* SonarQube :: GitLab Plugin | ||
* SonarQube :: GitLab Auth Plugin | ||
* Copyright (C) 2016-2017 Talanlabs | ||
* [email protected] | ||
* | ||
|
2 changes: 1 addition & 1 deletion
2
src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GitLabConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* SonarQube :: GitLab Plugin | ||
* SonarQube :: GitLab Auth Plugin | ||
* Copyright (C) 2016-2017 Talanlabs | ||
* [email protected] | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
/* | ||
* SonarQube :: GitLab Auth Plugin | ||
* Copyright (C) 2016-2017 Talanlabs | ||
* [email protected] | ||
* | ||
* 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"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/com/talanlabs/sonar/plugins/gitlab/auth/GsonUserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
/* | ||
* SonarQube :: GitLab Auth Plugin | ||
* Copyright (C) 2016-2017 Talanlabs | ||
* [email protected] | ||
* | ||
* 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; | ||
|