You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchGlobalPermissionsActionTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.permission.ws;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonar.api.resources.Qualifiers;
  24. import org.sonar.api.resources.ResourceTypes;
  25. import org.sonar.core.permission.GlobalPermissions;
  26. import org.sonar.db.component.ResourceTypesRule;
  27. import org.sonar.db.user.UserDto;
  28. import org.sonar.server.exceptions.ForbiddenException;
  29. import org.sonar.server.exceptions.UnauthorizedException;
  30. import org.sonar.server.l18n.I18nRule;
  31. import org.sonar.server.permission.PermissionService;
  32. import org.sonar.server.permission.PermissionServiceImpl;
  33. import org.sonarqube.ws.Permissions;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
  36. import static org.sonar.db.permission.GlobalPermission.SCAN;
  37. public class SearchGlobalPermissionsActionTest extends BasePermissionWsTest<SearchGlobalPermissionsAction> {
  38. private I18nRule i18n = new I18nRule();
  39. private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
  40. private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
  41. @Override
  42. protected SearchGlobalPermissionsAction buildWsAction() {
  43. return new SearchGlobalPermissionsAction(db.getDbClient(), userSession, i18n, permissionService);
  44. }
  45. @Before
  46. public void setUp() {
  47. initI18nMessages();
  48. }
  49. @Test
  50. public void search() {
  51. loginAsAdmin();
  52. UserDto user = db.users().insertUser();
  53. db.users().insertPermissionOnUser(user, SCAN);
  54. Permissions.WsSearchGlobalPermissionsResponse result = newRequest()
  55. .executeProtobuf(Permissions.WsSearchGlobalPermissionsResponse.class);
  56. assertThat(result.getPermissionsCount()).isEqualTo(GlobalPermissions.ALL.size());
  57. for (Permissions.Permission permission : result.getPermissionsList()) {
  58. if (permission.getKey().equals(SCAN_EXECUTION)) {
  59. assertThat(permission.getUsersCount()).isEqualTo(1);
  60. } else {
  61. assertThat(permission.getUsersCount()).isZero();
  62. }
  63. }
  64. }
  65. @Test
  66. public void supports_protobuf_response() {
  67. loginAsAdmin();
  68. Permissions.WsSearchGlobalPermissionsResponse result = newRequest()
  69. .executeProtobuf(Permissions.WsSearchGlobalPermissionsResponse.class);
  70. assertThat(result).isNotNull();
  71. }
  72. @Test
  73. public void fail_if_not_admin() {
  74. userSession.logIn();
  75. expectedException.expect(ForbiddenException.class);
  76. newRequest()
  77. .execute();
  78. }
  79. @Test
  80. public void fail_if_not_logged_in() {
  81. userSession.anonymous();
  82. expectedException.expect(UnauthorizedException.class);
  83. newRequest().execute();
  84. }
  85. private void initI18nMessages() {
  86. i18n.put("global_permissions.admin", "Administer System");
  87. i18n.put("global_permissions.admin.desc", "Ability to perform all administration functions for the instance: " +
  88. "global configuration and personalization of default dashboards.");
  89. i18n.put("global_permissions.profileadmin", "Administer Quality Profiles");
  90. i18n.put("global_permissions.profileadmin.desc", "Ability to perform any action on the quality profiles.");
  91. i18n.put("global_permissions.gateadmin", "Administer Quality Gates");
  92. i18n.put("global_permissions.gateadmin.desc", "Ability to perform any action on the quality gates.");
  93. i18n.put("global_permissions.scan", "Execute Analysis");
  94. i18n.put("global_permissions.scan.desc", "Ability to execute analyses, and to get all settings required to perform the analysis, " +
  95. "even the secured ones like the scm account password, the jira account password, and so on.");
  96. i18n.put("global_permissions.provisioning", "Create Projects");
  97. i18n.put("global_permissions.provisioning.desc", "Ability to initialize project structure before first analysis.");
  98. }
  99. }