]> source.dussan.org Git - sonarqube.git/blob
213e9cf55aeef6d163d3568fbc372208cb6e871f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.organization.ws;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.server.ws.WebService;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.organization.OrganizationDto;
29 import org.sonar.db.permission.OrganizationPermission;
30 import org.sonar.db.user.GroupDto;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.tester.UserSessionRule;
33 import org.sonar.server.ws.TestResponse;
34 import org.sonar.server.ws.WsActionTester;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
38 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
39 import static org.sonar.test.JsonAssert.assertJson;
40
41 public class SearchMyOrganizationsActionTest {
42   private static final String NO_ORGANIZATIONS_RESPONSE = "{\"organizations\": []}";
43
44   @Rule
45   public DbTester dbTester = DbTester.create(System2.INSTANCE);
46   @Rule
47   public UserSessionRule userSessionRule = UserSessionRule.standalone();
48
49   private DbClient dbClient = dbTester.getDbClient();
50
51   private WsActionTester underTest = new WsActionTester(new SearchMyOrganizationsAction(userSessionRule, dbClient));
52
53   @Test
54   public void verify_definition() {
55     WebService.Action def = underTest.getDef();
56
57     assertThat(def.key()).isEqualTo("search_my_organizations");
58     assertThat(def.isPost()).isFalse();
59     assertThat(def.isInternal()).isTrue();
60     assertThat(def.since()).isEqualTo("6.3");
61     assertThat(def.description()).isEqualTo("List keys of the organizations for which the currently authenticated user has the System Administer permission for.");
62     assertThat(def.responseExample()).isNotNull();
63
64     assertThat(def.params()).isEmpty();
65   }
66
67   @Test
68   public void verify_response_example() {
69     OrganizationDto organization1 = dbTester.organizations().insertForKey("my-org");
70     OrganizationDto organization2 = dbTester.organizations().insertForKey("foo-corp");
71
72     UserDto user = dbTester.users().insertUser();
73     dbTester.users().insertPermissionOnUser(organization1, user, SYSTEM_ADMIN);
74     dbTester.users().insertPermissionOnUser(organization2, user, SYSTEM_ADMIN);
75
76     userSessionRule.logIn(user);
77
78     TestResponse response = underTest.newRequest().execute();
79
80     assertJson(response.getInput()).isSimilarTo(underTest.getDef().responseExampleAsString());
81   }
82
83   @Test
84   public void returns_empty_response_when_user_is_not_logged_in() {
85     TestResponse response = underTest.newRequest().execute();
86
87     assertThat(response.getStatus()).isEqualTo(204);
88     assertThat(response.getInput()).isEmpty();
89   }
90
91   @Test
92   public void returns_empty_array_when_user_is_logged_in_and_has_no_permission_on_anything() {
93     userSessionRule.logIn();
94
95     TestResponse response = underTest.newRequest().execute();
96
97     assertJson(response.getInput()).isSimilarTo(NO_ORGANIZATIONS_RESPONSE);
98   }
99
100   @Test
101   public void returns_organizations_of_authenticated_user_when_user_has_ADMIN_user_permission_on_some_organization() {
102     UserDto user = dbTester.users().insertUser();
103     dbTester.users().insertPermissionOnUser(dbTester.getDefaultOrganization(), user, SYSTEM_ADMIN);
104     OrganizationDto organization1 = dbTester.organizations().insert();
105     dbTester.users().insertPermissionOnUser(organization1, user, SYSTEM_ADMIN);
106     UserDto otherUser = dbTester.users().insertUser();
107     OrganizationDto organization2 = dbTester.organizations().insert();
108     dbTester.users().insertPermissionOnUser(organization2, otherUser, SYSTEM_ADMIN);
109
110     userSessionRule.logIn(user);
111     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo("{\"organizations\": [" +
112       "\"" + dbTester.getDefaultOrganization().getKey() + "\"," +
113       "\"" + organization1.getKey() + "\"" +
114       "]}");
115
116     userSessionRule.logIn(otherUser);
117     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo("{\"organizations\": [" +
118       "\"" + organization2.getKey() + "\"" +
119       "]}");
120
121     userSessionRule.logIn();
122     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo(NO_ORGANIZATIONS_RESPONSE);
123   }
124
125   @Test
126   public void returns_organizations_of_authenticated_user_when_user_has_ADMIN_group_permission_on_some_organization() {
127     UserDto user = dbTester.users().insertUser();
128     GroupDto defaultGroup = dbTester.users().insertGroup(dbTester.getDefaultOrganization());
129     dbTester.users().insertPermissionOnGroup(defaultGroup, ADMINISTER);
130     dbTester.users().insertMember(defaultGroup, user);
131     OrganizationDto organization1 = dbTester.organizations().insert();
132     GroupDto group1 = dbTester.users().insertGroup(organization1);
133     dbTester.users().insertPermissionOnGroup(group1, ADMINISTER);
134     dbTester.users().insertMember(group1, user);
135     UserDto otherUser = dbTester.users().insertUser();
136     OrganizationDto organization2 = dbTester.organizations().insert();
137     GroupDto group2 = dbTester.users().insertGroup(organization2);
138     dbTester.users().insertPermissionOnGroup(group2, ADMINISTER);
139     dbTester.users().insertMember(group2, otherUser);
140
141     userSessionRule.logIn(user);
142     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo("{\"organizations\": [" +
143       "\"" + dbTester.getDefaultOrganization().getKey() + "\"," +
144       "\"" + organization1.getKey() + "\"" +
145       "]}");
146
147     userSessionRule.logIn(otherUser);
148     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo("{\"organizations\": [" +
149       "\"" + organization2.getKey() + "\"" +
150       "]}");
151
152     userSessionRule.logIn();
153     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo(NO_ORGANIZATIONS_RESPONSE);
154   }
155
156   @Test
157   public void returns_organization_of_authenticated_user_only_for_ADMIN_permission() {
158     UserDto user = dbTester.users().insertUser();
159     OrganizationDto organization1 = dbTester.organizations().insert();
160     OrganizationDto organization2 = dbTester.organizations().insert();
161     GroupDto group = dbTester.users().insertGroup(organization2);
162     dbTester.users().insertMember(group, user);
163     OrganizationPermission.all()
164       .filter(p -> p != ADMINISTER)
165       .forEach(p -> {
166         dbTester.users().insertPermissionOnUser(organization1, user, p);
167         dbTester.users().insertPermissionOnGroup(group, p);
168       });
169
170     userSessionRule.logIn(user);
171     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo(NO_ORGANIZATIONS_RESPONSE);
172   }
173
174   @Test
175   public void do_not_return_organization_twice_if_user_has_ADMIN_permission_twice_or_more() {
176     UserDto user = dbTester.users().insertUser();
177     OrganizationDto organization = dbTester.organizations().insert();
178     GroupDto group1 = dbTester.users().insertGroup(organization);
179     dbTester.users().insertPermissionOnGroup(group1, ADMINISTER);
180     dbTester.users().insertPermissionOnUser(organization, user, SYSTEM_ADMIN);
181
182     userSessionRule.logIn(user);
183     assertJson(underTest.newRequest().execute().getInput()).isSimilarTo("{\"organizations\": [" +
184       "\"" + organization.getKey() + "\"" +
185       "]}");
186   }
187 }