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.

CurrentActionTest.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.user.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.resources.Qualifiers;
  24. import org.sonar.api.resources.ResourceType;
  25. import org.sonar.api.resources.ResourceTypeTree;
  26. import org.sonar.api.resources.ResourceTypes;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.core.platform.PlatformEditionProvider;
  30. import org.sonar.db.DbTester;
  31. import org.sonar.db.component.ComponentDto;
  32. import org.sonar.db.user.UserDto;
  33. import org.sonar.server.issue.AvatarResolverImpl;
  34. import org.sonar.server.permission.PermissionService;
  35. import org.sonar.server.permission.PermissionServiceImpl;
  36. import org.sonar.server.tester.UserSessionRule;
  37. import org.sonar.server.ws.WsActionTester;
  38. import org.sonarqube.ws.Users.CurrentWsResponse;
  39. import static com.google.common.collect.Lists.newArrayList;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  42. import static org.mockito.Mockito.mock;
  43. import static org.sonar.api.web.UserRole.USER;
  44. import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
  45. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  46. import static org.sonar.db.permission.GlobalPermission.SCAN;
  47. import static org.sonar.db.user.GroupTesting.newGroupDto;
  48. import static org.sonar.test.JsonAssert.assertJson;
  49. public class CurrentActionTest {
  50. @Rule
  51. public UserSessionRule userSession = UserSessionRule.standalone();
  52. @Rule
  53. public DbTester db = DbTester.create(System2.INSTANCE);
  54. private final PlatformEditionProvider platformEditionProvider = mock(PlatformEditionProvider.class);
  55. private final HomepageTypesImpl homepageTypes = new HomepageTypesImpl();
  56. private final PermissionService permissionService = new PermissionServiceImpl(new ResourceTypes(new ResourceTypeTree[] {
  57. ResourceTypeTree.builder().addType(ResourceType.builder(Qualifiers.PROJECT).build()).build()}));
  58. private final WsActionTester ws = new WsActionTester(
  59. new CurrentAction(userSession, db.getDbClient(), new AvatarResolverImpl(), homepageTypes, platformEditionProvider, permissionService));
  60. @Test
  61. public void return_user_info() {
  62. UserDto user = db.users().insertUser(u -> u
  63. .setLogin("obiwan.kenobi")
  64. .setName("Obiwan Kenobi")
  65. .setEmail("obiwan.kenobi@starwars.com")
  66. .setLocal(true)
  67. .setExternalLogin("obiwan")
  68. .setExternalIdentityProvider("sonarqube")
  69. .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket"))
  70. .setOnboarded(false));
  71. userSession.logIn(user);
  72. CurrentWsResponse response = call();
  73. assertThat(response)
  74. .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::getEmail, CurrentWsResponse::getAvatar,
  75. CurrentWsResponse::getLocal,
  76. CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getScmAccountsList, CurrentWsResponse::getShowOnboardingTutorial)
  77. .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com", "f5aa64437a1821ffe8b563099d506aef", true, "obiwan", "sonarqube",
  78. newArrayList("obiwan:github", "obiwan:bitbucket"), true);
  79. }
  80. @Test
  81. public void return_minimal_user_info() {
  82. UserDto user = db.users().insertUser(u -> u
  83. .setLogin("obiwan.kenobi")
  84. .setName("Obiwan Kenobi")
  85. .setEmail(null)
  86. .setLocal(true)
  87. .setExternalLogin("obiwan")
  88. .setExternalIdentityProvider("sonarqube")
  89. .setScmAccounts((String) null));
  90. userSession.logIn(user);
  91. CurrentWsResponse response = call();
  92. assertThat(response)
  93. .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::hasAvatar, CurrentWsResponse::getLocal,
  94. CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getUsingSonarLintConnectedMode, CurrentWsResponse::getSonarLintAdSeen)
  95. .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", false, true, "obiwan", "sonarqube", false, false);
  96. assertThat(response.hasEmail()).isFalse();
  97. assertThat(response.getScmAccountsList()).isEmpty();
  98. assertThat(response.getGroupsList()).isEmpty();
  99. assertThat(response.getPermissions().getGlobalList()).isEmpty();
  100. }
  101. @Test
  102. public void convert_empty_email_to_null() {
  103. UserDto user = db.users().insertUser(u -> u
  104. .setLogin("obiwan.kenobi")
  105. .setEmail(""));
  106. userSession.logIn(user);
  107. CurrentWsResponse response = call();
  108. assertThat(response.hasEmail()).isFalse();
  109. }
  110. @Test
  111. public void return_group_membership() {
  112. UserDto user = db.users().insertUser();
  113. userSession.logIn(user);
  114. db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), user);
  115. db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), user);
  116. CurrentWsResponse response = call();
  117. assertThat(response.getGroupsList()).containsOnly("Jedi", "Rebel");
  118. }
  119. @Test
  120. public void return_permissions() {
  121. UserDto user = db.users().insertUser();
  122. userSession
  123. .logIn(user)
  124. .addPermission(SCAN)
  125. .addPermission(ADMINISTER_QUALITY_PROFILES);
  126. CurrentWsResponse response = call();
  127. assertThat(response.getPermissions().getGlobalList()).containsOnly("profileadmin", "scan");
  128. }
  129. @Test
  130. public void fail_with_ISE_when_user_login_in_db_does_not_exist() {
  131. db.users().insertUser(usert -> usert.setLogin("another"));
  132. userSession.logIn("obiwan.kenobi");
  133. assertThatThrownBy(this::call)
  134. .isInstanceOf(IllegalStateException.class)
  135. .hasMessage("User login 'obiwan.kenobi' cannot be found");
  136. }
  137. @Test
  138. public void anonymous() {
  139. userSession
  140. .anonymous()
  141. .addPermission(SCAN)
  142. .addPermission(PROVISION_PROJECTS);
  143. CurrentWsResponse response = call();
  144. assertThat(response.getIsLoggedIn()).isFalse();
  145. assertThat(response.getPermissions().getGlobalList()).containsOnly("scan", "provisioning");
  146. assertThat(response)
  147. .extracting(CurrentWsResponse::hasLogin, CurrentWsResponse::hasName, CurrentWsResponse::hasEmail, CurrentWsResponse::hasLocal,
  148. CurrentWsResponse::hasExternalIdentity, CurrentWsResponse::hasExternalProvider)
  149. .containsOnly(false);
  150. assertThat(response.getScmAccountsList()).isEmpty();
  151. assertThat(response.getGroupsList()).isEmpty();
  152. }
  153. @Test
  154. public void json_example() {
  155. ComponentDto componentDto = db.components().insertPrivateProject(u -> u.setUuid("UUID-of-the-death-star").setDbKey("death-star-key"));
  156. UserDto obiwan = db.users().insertUser(user -> user
  157. .setLogin("obiwan.kenobi")
  158. .setName("Obiwan Kenobi")
  159. .setEmail("obiwan.kenobi@starwars.com")
  160. .setLocal(true)
  161. .setExternalLogin("obiwan.kenobi")
  162. .setExternalIdentityProvider("sonarqube")
  163. .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket"))
  164. .setOnboarded(true)
  165. .setHomepageType("PROJECT")
  166. .setHomepageParameter("UUID-of-the-death-star"));
  167. userSession
  168. .logIn(obiwan)
  169. .addPermission(SCAN)
  170. .addPermission(ADMINISTER_QUALITY_PROFILES)
  171. .addProjectPermission(USER, db.components().getProjectDto(componentDto));
  172. db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), obiwan);
  173. db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), obiwan);
  174. String response = ws.newRequest().execute().getInput();
  175. assertJson(response).isSimilarTo(getClass().getResource("current-example.json"));
  176. }
  177. @Test
  178. public void handle_givenSonarLintUserInDatabase_returnSonarLintUserFromTheEndpoint() {
  179. UserDto user = db.users().insertUser(u -> u.setLastSonarlintConnectionDate(System.currentTimeMillis()));
  180. userSession.logIn(user);
  181. CurrentWsResponse response = call();
  182. assertThat(response.getUsingSonarLintConnectedMode()).isTrue();
  183. }
  184. @Test
  185. public void handle_givenSonarLintAdSeenUserInDatabase_returnSonarLintAdSeenUserFromTheEndpoint() {
  186. UserDto user = db.users().insertUser(u -> u.setSonarlintAdSeen(true));
  187. userSession.logIn(user);
  188. CurrentWsResponse response = call();
  189. assertThat(response.getSonarLintAdSeen()).isTrue();
  190. }
  191. @Test
  192. public void test_definition() {
  193. WebService.Action definition = ws.getDef();
  194. assertThat(definition.key()).isEqualTo("current");
  195. assertThat(definition.description()).isEqualTo("Get the details of the current authenticated user.");
  196. assertThat(definition.since()).isEqualTo("5.2");
  197. assertThat(definition.isPost()).isFalse();
  198. assertThat(definition.isInternal()).isTrue();
  199. assertThat(definition.responseExampleAsString()).isNotEmpty();
  200. assertThat(definition.params()).isEmpty();
  201. assertThat(definition.changelog()).hasSize(3);
  202. }
  203. private CurrentWsResponse call() {
  204. return ws.newRequest().executeProtobuf(CurrentWsResponse.class);
  205. }
  206. }