aboutsummaryrefslogtreecommitdiffstats
path: root/tests/plugins/security-plugin/src/test/java/FakeAuthenticatorTest.java
blob: 37f5fc038adf67bd4aa414bc6288cf66e20bbb78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info 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.internal.MapSettings;
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 MapSettings();
    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");
  }

}