forked from SonarSource/sonarqube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-8676 add ws api/navigation/organization
- Loading branch information
Showing
5 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
server/sonar-server/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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.server.ui.ws; | ||
|
||
import org.sonar.api.server.ws.Request; | ||
import org.sonar.api.server.ws.Response; | ||
import org.sonar.api.server.ws.WebService; | ||
import org.sonar.api.utils.text.JsonWriter; | ||
import org.sonar.api.web.UserRole; | ||
import org.sonar.db.DbClient; | ||
import org.sonar.db.DbSession; | ||
import org.sonar.db.organization.OrganizationDto; | ||
import org.sonar.server.organization.DefaultOrganizationProvider; | ||
import org.sonar.server.user.UserSession; | ||
|
||
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; | ||
|
||
public class OrganizationAction implements NavigationWsAction { | ||
|
||
private static final String ACTION_NAME = "organization"; | ||
private static final String PARAM_ORGANIZATION = "organization"; | ||
|
||
private final DbClient dbClient; | ||
private final DefaultOrganizationProvider defaultOrganizationProvider; | ||
private final UserSession userSession; | ||
|
||
public OrganizationAction(DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, UserSession userSession) { | ||
this.dbClient = dbClient; | ||
this.defaultOrganizationProvider = defaultOrganizationProvider; | ||
this.userSession = userSession; | ||
} | ||
|
||
@Override | ||
public void define(WebService.NewController context) { | ||
WebService.NewAction projectNavigation = context.createAction(ACTION_NAME) | ||
.setDescription("Get information concerning organization navigation for the current user") | ||
.setHandler(this) | ||
.setInternal(true) | ||
.setResponseExample(getClass().getResource("organization-example.json")) | ||
.setSince("6.3"); | ||
|
||
projectNavigation.createParam(PARAM_ORGANIZATION) | ||
.setRequired(true) | ||
.setDescription("the organization key") | ||
.setExampleValue("my-org"); | ||
} | ||
|
||
@Override | ||
public void handle(Request request, Response response) throws Exception { | ||
String organizationKey = request.mandatoryParam(PARAM_ORGANIZATION); | ||
try (DbSession dbSession = dbClient.openSession(false)) { | ||
OrganizationDto organization = checkFoundWithOptional( | ||
dbClient.organizationDao().selectByKey(dbSession, organizationKey), | ||
"No organization with key '%s'", organizationKey); | ||
|
||
JsonWriter json = response.newJsonWriter(); | ||
json.beginObject(); | ||
writeOrganization(json, organization); | ||
json.endObject() | ||
.close(); | ||
} | ||
} | ||
|
||
private void writeOrganization(JsonWriter json, OrganizationDto organization) { | ||
json.name("organization") | ||
.beginObject() | ||
.prop("isDefault", organization.getKey().equals(defaultOrganizationProvider.get().getKey())) | ||
.prop("canAdmin", userSession.hasOrganizationPermission(organization.getUuid(), UserRole.ADMIN)) | ||
.endObject(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
server/sonar-server/src/main/resources/org/sonar/server/ui/ws/organization-example.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"organization": { | ||
"isDefault": true, | ||
"canAdmin": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
server/sonar-server/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* 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.server.ui.ws; | ||
|
||
import javax.annotation.Nullable; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.sonar.api.server.ws.WebService; | ||
import org.sonar.api.utils.System2; | ||
import org.sonar.db.DbClient; | ||
import org.sonar.db.DbTester; | ||
import org.sonar.db.organization.OrganizationDto; | ||
import org.sonar.server.organization.DefaultOrganizationProvider; | ||
import org.sonar.server.organization.TestDefaultOrganizationProvider; | ||
import org.sonar.server.tester.UserSessionRule; | ||
import org.sonar.server.ws.TestRequest; | ||
import org.sonar.server.ws.TestResponse; | ||
import org.sonar.server.ws.WsActionTester; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.sonar.test.JsonAssert.assertJson; | ||
|
||
public class OrganizationActionTest { | ||
@Rule | ||
public DbTester dbTester = DbTester.create(System2.INSTANCE); | ||
@Rule | ||
public UserSessionRule userSession = UserSessionRule.standalone(); | ||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
private DbClient dbClient = dbTester.getDbClient(); | ||
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester); | ||
|
||
private WsActionTester underTest = new WsActionTester(new OrganizationAction(dbClient, defaultOrganizationProvider, userSession)); | ||
|
||
@Test | ||
public void verify_definition() { | ||
WebService.Action def = underTest.getDef(); | ||
|
||
assertThat(def.isInternal()).isTrue(); | ||
assertThat(def.description()).isEqualTo("Get information concerning organization navigation for the current user"); | ||
assertThat(def.since()).isEqualTo("6.3"); | ||
|
||
assertThat(def.params()).hasSize(1); | ||
WebService.Param organization = def.param("organization"); | ||
assertThat(organization.description()).isEqualTo("the organization key"); | ||
assertThat(organization.isRequired()).isTrue(); | ||
assertThat(organization.exampleValue()).isEqualTo("my-org"); | ||
} | ||
|
||
@Test | ||
public void fails_with_IAE_if_parameter_organization_is_not_specified() { | ||
expectedException.expect(IllegalArgumentException.class); | ||
expectedException.expectMessage("The 'organization' parameter is missing"); | ||
|
||
executeRequest(null); | ||
} | ||
|
||
@Test | ||
public void verify_example() { | ||
|
||
OrganizationDto defaultOrganization = dbTester.getDefaultOrganization(); | ||
userSession.login().addOrganizationPermission(defaultOrganization.getUuid(), "admin"); | ||
|
||
TestResponse response = executeRequest(defaultOrganization); | ||
|
||
assertJson(response.getInput()) | ||
.isSimilarTo(underTest.getDef().responseExampleAsString()); | ||
} | ||
|
||
@Test | ||
public void returns_non_admin_and_default_true_when_user_not_logged_in_and_key_is_the_default_organization() { | ||
TestResponse response = executeRequest(dbTester.getDefaultOrganization()); | ||
|
||
verifyResponse(response, true, false); | ||
} | ||
|
||
@Test | ||
public void returns_non_admin_and_default_true_when_user_logged_in_but_not_admin_and_key_is_the_default_organization() { | ||
userSession.login(); | ||
|
||
TestResponse response = executeRequest(dbTester.getDefaultOrganization()); | ||
|
||
verifyResponse(response, true, false); | ||
} | ||
|
||
@Test | ||
public void returns_admin_and_default_true_when_user_logged_in_and_admin_and_key_is_the_default_organization() { | ||
OrganizationDto defaultOrganization = dbTester.getDefaultOrganization(); | ||
userSession.login().addOrganizationPermission(defaultOrganization.getUuid(), "admin"); | ||
|
||
TestResponse response = executeRequest(defaultOrganization); | ||
|
||
verifyResponse(response, true, true); | ||
} | ||
|
||
@Test | ||
public void returns_non_admin_and_default_true_when_user_not_logged_in_and_key_is_not_the_default_organization() { | ||
OrganizationDto organization = dbTester.organizations().insert(); | ||
TestResponse response = executeRequest(organization); | ||
|
||
verifyResponse(response, false, false); | ||
} | ||
|
||
@Test | ||
public void returns_non_admin_and_default_false_when_user_not_logged_in_and_key_is_not_the_default_organization() { | ||
OrganizationDto organization = dbTester.organizations().insert(); | ||
|
||
TestResponse response = executeRequest(organization); | ||
|
||
verifyResponse(response, false, false); | ||
} | ||
|
||
@Test | ||
public void returns_non_admin_and_default_false_when_user_logged_in_but_not_admin_and_key_is_not_the_default_organization() { | ||
OrganizationDto organization = dbTester.organizations().insert(); | ||
userSession.login(); | ||
|
||
TestResponse response = executeRequest(organization); | ||
|
||
verifyResponse(response, false, false); | ||
} | ||
|
||
@Test | ||
public void returns_admin_and_default_false_when_user_logged_in_and_admin_and_key_is_not_the_default_organization() { | ||
OrganizationDto organization = dbTester.organizations().insert(); | ||
userSession.login().addOrganizationPermission(organization.getUuid(), "admin"); | ||
|
||
TestResponse response = executeRequest(organization); | ||
|
||
verifyResponse(response, false, true); | ||
} | ||
|
||
private TestResponse executeRequest(@Nullable OrganizationDto organization) { | ||
TestRequest request = underTest.newRequest(); | ||
if (organization != null) { | ||
request.setParam("organization", organization.getKey()); | ||
} | ||
return request.execute(); | ||
} | ||
|
||
private static void verifyResponse(TestResponse response, boolean isDefault, boolean canAdmin) { | ||
assertJson(response.getInput()) | ||
.isSimilarTo("{" + | ||
" \"organization\": {" + | ||
" \"isDefault\": " + isDefault + "," + | ||
" \"canAdmin\": " + canAdmin + | ||
" }" + | ||
"}"); | ||
} | ||
} |