diff options
Diffstat (limited to 'it/it-plugins/security-plugin/src/test/java/FakeAuthenticatorTest.java')
-rw-r--r-- | it/it-plugins/security-plugin/src/test/java/FakeAuthenticatorTest.java | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/it/it-plugins/security-plugin/src/test/java/FakeAuthenticatorTest.java b/it/it-plugins/security-plugin/src/test/java/FakeAuthenticatorTest.java new file mode 100644 index 00000000000..6a0b1884a05 --- /dev/null +++ b/it/it-plugins/security-plugin/src/test/java/FakeAuthenticatorTest.java @@ -0,0 +1,126 @@ +/* + * 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. + */ +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.api.security.UserDetails; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class FakeAuthenticatorTest { + + private Settings settings; + private FakeAuthenticator authenticator; + + @Before + public void setUp() { + settings = new Settings(); + authenticator = new FakeAuthenticator(settings); + authenticator.init(); + } + + @Test + public void shouldNeverTouchAdmin() { + assertThat(authenticator.authenticate("admin", "admin")).isTrue(); + assertThat(authenticator.doGetGroups("admin")).isNull(); + assertThat(authenticator.doGetUserDetails("admin")).isNull(); + } + + @Test + public void shouldAuthenticateFakeUsers() { + settings.setProperty(FakeAuthenticator.DATA_PROPERTY, "evgeny.password=foo"); + + assertThat(authenticator.authenticate("evgeny", "foo")).isTrue(); + assertThat(authenticator.authenticate("evgeny", "bar")).isFalse(); + } + + @Test(expected = RuntimeException.class) + public void shouldNotAuthenticateNotExistingUsers() { + authenticator.authenticate("evgeny", "foo"); + } + + @Test + public void shouldGetUserDetails() { + settings.setProperty(FakeAuthenticator.DATA_PROPERTY, "evgeny.password=foo\n" + + "evgeny.name=Tester Testerovich\n" + + "evgeny.email=evgeny@example.org"); + + UserDetails details = authenticator.doGetUserDetails("evgeny"); + assertThat(details.getName()).isEqualTo("Tester Testerovich"); + assertThat(details.getEmail()).isEqualTo("evgeny@example.org"); + } + + @Test(expected = RuntimeException.class) + public void shouldNotReturnDetailsForNotExistingUsers() { + authenticator.doGetUserDetails("evgeny"); + } + + @Test + public void shouldGetGroups() { + settings.setProperty(FakeAuthenticator.DATA_PROPERTY, "evgeny.password=foo\n" + + "evgeny.groups=sonar-users,sonar-developers"); + + assertThat(authenticator.doGetGroups("evgeny")).containsOnly("sonar-users", "sonar-developers"); + } + + @Test(expected = RuntimeException.class) + public void shouldNotReturnGroupsForNotExistingUsers() { + authenticator.doGetGroups("evgeny"); + } + + @Test + public void shouldParseList() { + assertThat(FakeAuthenticator.parseList(null)).isEmpty(); + assertThat(FakeAuthenticator.parseList("")).isEmpty(); + assertThat(FakeAuthenticator.parseList(",,,")).isEmpty(); + assertThat(FakeAuthenticator.parseList("a,b")).containsOnly("a", "b"); + } + + @Test + public void shouldParseMap() { + Map<String, String> map = FakeAuthenticator.parse(null); + assertThat(map).isEmpty(); + + map = FakeAuthenticator.parse(""); + assertThat(map).isEmpty(); + + map = FakeAuthenticator.parse("foo=bar"); + assertThat(map).hasSize(1); + assertThat(map.get("foo")).isEqualTo("bar"); + + map = FakeAuthenticator.parse("foo=bar\r\nbaz=qux"); + assertThat(map).hasSize(2); + assertThat(map.get("foo")).isEqualTo("bar"); + assertThat(map.get("baz")).isEqualTo("qux"); + + map = FakeAuthenticator.parse("foo=bar\nbaz=qux"); + assertThat(map).hasSize(2); + assertThat(map.get("foo")).isEqualTo("bar"); + assertThat(map.get("baz")).isEqualTo("qux"); + + map = FakeAuthenticator.parse("foo=bar\n\n\nbaz=qux"); + assertThat(map).hasSize(2); + assertThat(map.get("foo")).isEqualTo("bar"); + assertThat(map.get("baz")).isEqualTo("qux"); + } + +} |