diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-02-03 11:39:29 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-02-04 10:10:10 +0100 |
commit | a153a7058e2890d1730d5c368689a604b6d502b0 (patch) | |
tree | e57d172826d7151d2b0c0bea49acd43c4a1e5238 /sonar-plugin-api | |
parent | 52022312cc34f18744707f25bad36d3dc585b56e (diff) | |
download | sonarqube-a153a7058e2890d1730d5c368689a604b6d502b0.tar.gz sonarqube-a153a7058e2890d1730d5c368689a604b6d502b0.zip |
SONAR-7242 Identity Provider must define a display with icon url abd background color
Diffstat (limited to 'sonar-plugin-api')
3 files changed, 228 insertions, 6 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/Display.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/Display.java new file mode 100644 index 00000000000..74ffc57ad6a --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/Display.java @@ -0,0 +1,107 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT 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 org.sonar.api.server.authentication; + +import javax.annotation.concurrent.Immutable; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.commons.lang.StringUtils.isNotBlank; + +/** + * Display information provided by the Identity Provider to be displayed into the login form. + * + * @since 5.4 + */ +@Immutable +public final class Display { + + private final String iconPath; + private final String backgroundColor; + + private Display(Builder builder) { + this.iconPath = builder.iconPath; + this.backgroundColor = builder.backgroundColor; + } + + /** + * URL path to the provider icon, as deployed at runtime, for example "/static/authgithub/github.svg" (in this + * case "authgithub" is the plugin key. Source file is "src/main/resources/static/github.svg"). + * It can also be an external URL, for example "http://www.mydomain/myincon.png". + * + * Must not be blank. + * <p/> + * The recommended format is SVG with a size of 24x24 pixels. + * Other supported format is PNG, with a size of 40x40 pixels. + */ + public String getIconPath() { + return iconPath; + } + + /** + * Background color for the provider button displayed in the login form. + * It's a Hexadecimal value, for instance #205081. + * <p/> + * If not provided, the default value is black (#000000) + */ + public String getBackgroundColor() { + return backgroundColor; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private String iconPath; + private String backgroundColor = "#000000"; + + private Builder() { + } + + /** + * @see Display#getIconPath() + */ + public Builder setIconPath(String iconPath) { + this.iconPath = iconPath; + return this; + } + + /** + * @see Display#getBackgroundColor() + */ + public Builder setBackgroundColor(String backgroundColor) { + this.backgroundColor = backgroundColor; + return this; + } + + public Display build() { + checkArgument(isNotBlank(iconPath), "Icon path must not be blank"); + validateBackgroundColor(); + return new Display(this); + } + + private void validateBackgroundColor() { + checkArgument(isNotBlank(backgroundColor), "Background color must not be blank"); + checkArgument(backgroundColor.length() == 7 && backgroundColor.startsWith("#"), + "Background color must begin with a sharp followed by 6 characters"); + } + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java index 9944d2c51a7..f1b43ba1b2c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java @@ -47,13 +47,9 @@ public interface IdentityProvider { String getName(); /** - * URL path to the provider icon, as deployed at runtime, for example "/static/authgithub/github.svg" (in this - * case "authgithub" is the plugin key. Source file is "src/main/resources/static/github.svg"). Must not be blank. - * <p/> - * The recommended format is SVG with a size of 24x24 pixels. - * Other supported format is PNG, with a size of 48x48 pixels. + * Display information for the login form */ - String getIconPath(); + Display getDisplay(); /** * Is the provider fully configured and enabled ? If {@code true}, then @@ -67,4 +63,5 @@ public interface IdentityProvider { * registered users can login. */ boolean allowsUsersToSignUp(); + } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java new file mode 100644 index 00000000000..2b756383238 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java @@ -0,0 +1,118 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT 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 org.sonar.api.server.authentication; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DisplayTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void create_display() throws Exception { + Display display = Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("#123456") + .build(); + + assertThat(display.getIconPath()).isEqualTo("/static/authgithub/github.svg"); + assertThat(display.getBackgroundColor()).isEqualTo("#123456"); + } + + @Test + public void create_display_woth_default_background_color() throws Exception { + Display display = Display.builder() + .setIconPath("/static/authgithub/github.svg") + .build(); + + assertThat(display.getBackgroundColor()).isEqualTo("#000000"); + } + + @Test + public void fail_when_icon_path_is_null() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Icon path must not be blank"); + + Display.builder() + .setIconPath(null) + .setBackgroundColor("#123456") + .build(); + } + + @Test + public void fail_when_icon_path_is_blank() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Icon path must not be blank"); + + Display.builder() + .setIconPath("") + .setBackgroundColor("#123456") + .build(); + } + + @Test + public void fail_when_background_color_is_null() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must not be blank"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor(null) + .build(); + } + + @Test + public void fail_when_background_color_is_blank() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must not be blank"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("") + .build(); + } + + @Test + public void fail_when_background_color_has_wrong_size() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must begin with a sharp followed by 6 characters"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("#1234") + .build(); + } + + @Test + public void fail_when_background_color_doesnt_begin_with_sharp() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must begin with a sharp followed by 6 characters"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("*123456") + .build(); + } +} |