Skip to content

Commit

Permalink
add test 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Allaigre committed Mar 3, 2017
1 parent 9aba821 commit f63317c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 6 deletions.
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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
}
}
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;
Expand Down
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]
*
Expand Down
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]
*
Expand Down
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;
Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
}
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;
Expand Down

0 comments on commit f63317c

Please sign in to comment.