Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hkamel committed Apr 20, 2016
1 parent 5290371 commit d2bda3a
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.aad.adal4j.UserInfo;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
Expand All @@ -41,7 +40,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.sonar.api.server.ServerSide;
Expand All @@ -53,7 +51,11 @@
import org.sonar.api.utils.log.Loggers;

import static java.lang.String.format;
import static org.almrangers.auth.aad.AadSettings.*;
import static org.almrangers.auth.aad.AadSettings.AUTH_REQUEST_FORMAT;
import static org.almrangers.auth.aad.AadSettings.GROUPS_REQUEST_FORMAT;
import static org.almrangers.auth.aad.AadSettings.LOGIN_STRATEGY_PROVIDER_ID;
import static org.almrangers.auth.aad.AadSettings.LOGIN_STRATEGY_UNIQUE;
import static org.almrangers.auth.aad.AadSettings.SECURE_RESOURCE_URL;

@ServerSide
public class AadIdentityProvider implements OAuth2IdentityProvider {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/almrangers/auth/aad/AadSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.util.Arrays;
import java.util.List;

import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.Settings;
import org.sonar.api.server.ServerSide;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/almrangers/auth/aad/AuthAadPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import java.util.ArrayList;
import java.util.List;

import org.sonar.api.SonarPlugin;

public class AuthAadPlugin extends SonarPlugin {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/almrangers/auth/aad/HttpClientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

package org.almrangers.auth.aad;

import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/almrangers/auth/aad/JSONHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.text.WordUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/almrangers/auth/aad/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@
*/

package org.almrangers.auth.aad;

3 changes: 2 additions & 1 deletion src/main/resources/static/azure.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 71 additions & 44 deletions src/test/org/almrangers/auth/aad/AadIdentityProviderTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
/**
* Azure Active Directory Authentication Plugin for SonarQube
* Copyright (c) 2016 Microsoft Corporation
* All rights reserved.
*
* The MIT License (MIT)
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.almrangers.auth.aad;

import org.junit.Rule;
Expand All @@ -7,58 +33,59 @@
import org.sonar.api.server.authentication.OAuth2IdentityProvider;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class AadIdentityProviderTest {

@Rule
public ExpectedException thrown = ExpectedException.none();
Settings settings = new Settings();
AadSettings aadSettings = new AadSettings(settings);
AadIdentityProvider underTest = new AadIdentityProvider(aadSettings);

@Test
public void check_fields() throws Exception {
assertThat(underTest.getKey()).isEqualTo("aad");
assertThat(underTest.getName()).isEqualTo("Azure AD");
assertThat(underTest.getDisplay().getIconPath()).isEqualTo("/static/authaad/azure.svg");
assertThat(underTest.getDisplay().getBackgroundColor()).isEqualTo("#336699");
}
@Rule
public ExpectedException thrown = ExpectedException.none();
Settings settings = new Settings();
AadSettings aadSettings = new AadSettings(settings);
AadIdentityProvider underTest = new AadIdentityProvider(aadSettings);

@Test
public void init() throws Exception {
setSettings(true);
OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
when(context.generateCsrfState()).thenReturn("state");
when(context.getCallbackUrl()).thenReturn("http://localhost/callback");
@Test
public void check_fields() throws Exception {
assertThat(underTest.getKey()).isEqualTo("aad");
assertThat(underTest.getName()).isEqualTo("Azure AD");
assertThat(underTest.getDisplay().getIconPath()).isEqualTo("/static/authaad/azure.svg");
assertThat(underTest.getDisplay().getBackgroundColor()).isEqualTo("#336699");
}

underTest.init(context);
@Test
public void init() throws Exception {
setSettings(true);
OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
when(context.generateCsrfState()).thenReturn("state");
when(context.getCallbackUrl()).thenReturn("http://localhost/callback");

verify(context).redirectTo("https://login.microsoftonline.com/null/oauth2/authorize?client_id=id&response_type=code&redirect_uri=http://localhost/callback&state=state");
}
underTest.init(context);

@Test
public void is_enabled() throws Exception {
settings.setProperty("sonar.auth.aad.clientId.secured", "id");
settings.setProperty("sonar.auth.aad.clientSecret.secured", "secret");
settings.setProperty("sonar.auth.aad.loginStrategy", AadSettings.LOGIN_STRATEGY_DEFAULT_VALUE);
settings.setProperty("sonar.auth.aad.enabled", true);
assertThat(underTest.isEnabled()).isTrue();
verify(context).redirectTo("https://login.microsoftonline.com/null/oauth2/authorize?client_id=id&response_type=code&redirect_uri=http://localhost/callback&state=state");
}

settings.setProperty("sonar.auth.aad.enabled", false);
assertThat(underTest.isEnabled()).isFalse();
}
@Test
public void is_enabled() throws Exception {
settings.setProperty("sonar.auth.aad.clientId.secured", "id");
settings.setProperty("sonar.auth.aad.clientSecret.secured", "secret");
settings.setProperty("sonar.auth.aad.loginStrategy", AadSettings.LOGIN_STRATEGY_DEFAULT_VALUE);
settings.setProperty("sonar.auth.aad.enabled", true);
assertThat(underTest.isEnabled()).isTrue();

settings.setProperty("sonar.auth.aad.enabled", false);
assertThat(underTest.isEnabled()).isFalse();
}

private void setSettings(boolean enabled) {
if (enabled) {
settings.setProperty("sonar.auth.aad.clientId.secured", "id");
settings.setProperty("sonar.auth.aad.clientSecret.secured", "secret");
settings.setProperty("sonar.auth.aad.loginStrategy", AadSettings.LOGIN_STRATEGY_DEFAULT_VALUE);
settings.setProperty("sonar.auth.aad.enabled", true);
} else {
settings.setProperty("sonar.auth.aad.enabled", false);
}
private void setSettings(boolean enabled) {
if (enabled) {
settings.setProperty("sonar.auth.aad.clientId.secured", "id");
settings.setProperty("sonar.auth.aad.clientSecret.secured", "secret");
settings.setProperty("sonar.auth.aad.loginStrategy", AadSettings.LOGIN_STRATEGY_DEFAULT_VALUE);
settings.setProperty("sonar.auth.aad.enabled", true);
} else {
settings.setProperty("sonar.auth.aad.enabled", false);
}
}

}
}
Loading

0 comments on commit d2bda3a

Please sign in to comment.