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.

CurrentActionIT.java 11KB

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