aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java
blob: 3139bb6b192a2cda2a16f7c3b3f97f97c3d70c99 (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
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2012 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar 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.
 *
 * Sonar 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 Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.server.startup;

import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.sonar.api.config.Settings;
import org.sonar.server.platform.PersistentSettings;

import java.util.Map;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class SetDefaultProjectPermissionsTest {
  @Test
  public void set_default_permissions_if_none() {
    PersistentSettings persistentSettings = mock(PersistentSettings.class);
    Settings settings = new Settings();
    when(persistentSettings.getSettings()).thenReturn(settings);

    new SetDefaultProjectPermissions(persistentSettings).start();

    verify(persistentSettings).saveProperties(argThat(new ArgumentMatcher<Map<String, String>>() {
      @Override
      public boolean matches(Object o) {
        Map<String, String> map = (Map<String, String>) o;
        return map.size()>1 && map.get("sonar.role.admin.TRK.defaultGroups").equals("sonar-administrators");
      }
    }));
  }

  @Test
  public void do_not_set_default_permissions_if_exist() {
    PersistentSettings persistentSettings = mock(PersistentSettings.class);
    Settings settings = new Settings()
      .setProperty("sonar.role.user.TRK.defaultGroups", "custom-group")
      .setProperty("sonar.role.user.VW.defaultGroups", "custom-group")
      .setProperty("sonar.role.user.SVW.defaultGroups", "custom-group");
    when(persistentSettings.getSettings()).thenReturn(settings);

    new SetDefaultProjectPermissions(persistentSettings).start();

    verify(persistentSettings, never()).saveProperties(any(Map.class));
  }
}