Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UserSessionRule.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.tester;
  21. import com.google.common.base.Preconditions;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Optional;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import org.junit.rules.TestRule;
  28. import org.junit.runner.Description;
  29. import org.junit.runners.model.Statement;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.permission.GlobalPermission;
  32. import org.sonar.db.portfolio.PortfolioDto;
  33. import org.sonar.db.project.ProjectDto;
  34. import org.sonar.db.user.GroupDto;
  35. import org.sonar.db.user.UserDto;
  36. import org.sonar.server.user.UserSession;
  37. import static com.google.common.base.Preconditions.checkNotNull;
  38. import static com.google.common.base.Preconditions.checkState;
  39. /**
  40. * {@code UserSessionRule} is intended to be used as a {@link org.junit.Rule} to easily manage {@link UserSession} in
  41. * unit tests.
  42. * <p>
  43. * It can be used as a {@link org.junit.ClassRule} but be careful not to modify its states from inside tests methods
  44. * unless you purposely want to have side effects between each tests.
  45. * </p>
  46. * <p>
  47. * One can define user session behavior which should apply on all tests directly on the property, eg.:
  48. * <pre>
  49. * {@literal @}Rule
  50. * public UserSessionRule userSession = UserSessionRule.standalone().login("admin").setOrganizationPermissions(OrganizationPermissions.SYSTEM_ADMIN);
  51. * </pre>
  52. * </p>
  53. * <p>
  54. * Behavior defined at property-level can obviously be override at test method level. For example, one could define
  55. * all methods to use an authenticated session such as presented above but can easily overwrite that behavior in a
  56. * specific test method as follow:
  57. * <pre>
  58. * {@literal @}Test
  59. * public void test_method() {
  60. * userSession.standalone();
  61. * {@literal [...]}
  62. * }
  63. * </pre>
  64. * </p>
  65. * <p>
  66. * {@code UserSessionRule}, emulates by default an anonymous
  67. * session. Therefore, call {@code UserSessionRule.standalone()} is equivalent to calling
  68. * {@code UserSessionRule.standalone().anonymous()}.
  69. * </p>
  70. * <p>
  71. * To emulate an identified user, either use method {@link #logIn(String)} if you want to specify the user's login, or
  72. * method {@link #logIn()} which will do the same but using the value of {@link #DEFAULT_LOGIN} as the user's login
  73. * (use the latest override if you don't care about the actual value of the login, it will save noise in your test).
  74. * </p>
  75. */
  76. public class UserSessionRule implements TestRule, UserSession {
  77. private static final String DEFAULT_LOGIN = "default_login";
  78. private UserSession currentUserSession;
  79. private UserSessionRule() {
  80. anonymous();
  81. }
  82. public static UserSessionRule standalone() {
  83. return new UserSessionRule();
  84. }
  85. /**
  86. * Log in with the default login {@link #DEFAULT_LOGIN}
  87. */
  88. public UserSessionRule logIn() {
  89. return logIn(DEFAULT_LOGIN);
  90. }
  91. /**
  92. * Log in with the specified login
  93. */
  94. public UserSessionRule logIn(String login) {
  95. setCurrentUserSession(new MockUserSession(login));
  96. return this;
  97. }
  98. /**
  99. * Log in with the specified login
  100. */
  101. public UserSessionRule logIn(UserDto userDto) {
  102. setCurrentUserSession(new MockUserSession(userDto));
  103. return this;
  104. }
  105. /**
  106. * Disconnect/go anonymous
  107. */
  108. public UserSessionRule anonymous() {
  109. setCurrentUserSession(new AnonymousMockUserSession());
  110. return this;
  111. }
  112. public UserSessionRule setRoot() {
  113. ensureMockUserSession().setRoot(true);
  114. return this;
  115. }
  116. public UserSessionRule setNonRoot() {
  117. ensureMockUserSession().setRoot(false);
  118. return this;
  119. }
  120. public UserSessionRule setSystemAdministrator() {
  121. ensureMockUserSession().setSystemAdministrator(true);
  122. return this;
  123. }
  124. public UserSessionRule setNonSystemAdministrator() {
  125. ensureMockUserSession().setSystemAdministrator(false);
  126. return this;
  127. }
  128. public UserSessionRule setExternalIdentity(IdentityProvider identityProvider, ExternalIdentity externalIdentity) {
  129. ensureMockUserSession().setExternalIdentity(identityProvider, externalIdentity);
  130. return this;
  131. }
  132. public UserSessionRule setInternalIdentity() {
  133. ensureMockUserSession().setInternalIdentity();
  134. return this;
  135. }
  136. @Override
  137. public Statement apply(Statement statement, Description description) {
  138. return this.statement(statement);
  139. }
  140. private Statement statement(final Statement base) {
  141. return new Statement() {
  142. public void evaluate() throws Throwable {
  143. UserSessionRule.this.before();
  144. try {
  145. base.evaluate();
  146. } finally {
  147. UserSessionRule.this.after();
  148. }
  149. }
  150. };
  151. }
  152. protected void before() {
  153. setCurrentUserSession(currentUserSession);
  154. }
  155. protected void after() {
  156. this.currentUserSession = null;
  157. }
  158. public void set(UserSession userSession) {
  159. checkNotNull(userSession);
  160. setCurrentUserSession(userSession);
  161. }
  162. public UserSessionRule registerComponents(ComponentDto... componentDtos) {
  163. ensureAbstractMockUserSession().registerComponents(componentDtos);
  164. return this;
  165. }
  166. public UserSessionRule registerProjects(ProjectDto... projectDtos) {
  167. ensureAbstractMockUserSession().registerProjects(projectDtos);
  168. return this;
  169. }
  170. public UserSessionRule registerApplication(ProjectDto application, ProjectDto... appProjects) {
  171. ensureAbstractMockUserSession().registerApplication(application, appProjects);
  172. return this;
  173. }
  174. public UserSessionRule registerApplication(ComponentDto application, ComponentDto... appProjects) {
  175. ensureAbstractMockUserSession().registerApplication(application, appProjects);
  176. return this;
  177. }
  178. public UserSessionRule addProjectPermission(String projectPermission, ComponentDto... components) {
  179. ensureAbstractMockUserSession().addProjectPermission(projectPermission, components);
  180. return this;
  181. }
  182. public UserSessionRule addProjectPermission(String projectPermission, ProjectDto... projectDto) {
  183. ensureAbstractMockUserSession().addProjectPermission(projectPermission, projectDto);
  184. return this;
  185. }
  186. public UserSessionRule addPortfolioPermission(String portfolioPermission, PortfolioDto... portfolioDto) {
  187. ensureAbstractMockUserSession().addPortfolioPermission(portfolioPermission, portfolioDto);
  188. return this;
  189. }
  190. public UserSessionRule addPermission(GlobalPermission permission) {
  191. ensureAbstractMockUserSession().addPermission(permission);
  192. return this;
  193. }
  194. /**
  195. * Groups that user is member of. User must be logged in. An exception
  196. * is thrown if session is anonymous.
  197. */
  198. public UserSessionRule setGroups(GroupDto... groups) {
  199. ensureMockUserSession().setGroups(groups);
  200. return this;
  201. }
  202. public UserSessionRule setName(@Nullable String s) {
  203. ensureMockUserSession().setName(s);
  204. return this;
  205. }
  206. private AbstractMockUserSession ensureAbstractMockUserSession() {
  207. checkState(currentUserSession instanceof AbstractMockUserSession, "rule state can not be changed if a UserSession has explicitly been provided");
  208. return (AbstractMockUserSession) currentUserSession;
  209. }
  210. private MockUserSession ensureMockUserSession() {
  211. checkState(currentUserSession instanceof MockUserSession, "rule state can not be changed if a UserSession has explicitly been provided");
  212. return (MockUserSession) currentUserSession;
  213. }
  214. private void setCurrentUserSession(UserSession userSession) {
  215. this.currentUserSession = Preconditions.checkNotNull(userSession);
  216. }
  217. @Override
  218. public boolean hasComponentPermission(String permission, ComponentDto component) {
  219. return currentUserSession.hasComponentPermission(permission, component);
  220. }
  221. @Override
  222. public boolean hasProjectPermission(String permission, ProjectDto project) {
  223. return currentUserSession.hasProjectPermission(permission, project);
  224. }
  225. @Override
  226. public boolean hasChildProjectsPermission(String permission, ComponentDto component) {
  227. return currentUserSession.hasChildProjectsPermission(permission, component);
  228. }
  229. @Override
  230. public boolean hasChildProjectsPermission(String permission, ProjectDto component) {
  231. return currentUserSession.hasChildProjectsPermission(permission, component);
  232. }
  233. @Override
  234. public boolean hasComponentUuidPermission(String permission, String componentUuid) {
  235. return currentUserSession.hasComponentUuidPermission(permission, componentUuid);
  236. }
  237. @Override
  238. public List<ComponentDto> keepAuthorizedComponents(String permission, Collection<ComponentDto> components) {
  239. return currentUserSession.keepAuthorizedComponents(permission, components);
  240. }
  241. @Override
  242. public List<ProjectDto> keepAuthorizedProjects(String permission, Collection<ProjectDto> projects) {
  243. return currentUserSession.keepAuthorizedProjects(permission, projects);
  244. }
  245. @Override
  246. @CheckForNull
  247. public String getLogin() {
  248. return currentUserSession.getLogin();
  249. }
  250. @Override
  251. @CheckForNull
  252. public String getUuid() {
  253. return currentUserSession.getUuid();
  254. }
  255. @Override
  256. @CheckForNull
  257. public String getName() {
  258. return currentUserSession.getName();
  259. }
  260. @Override
  261. @CheckForNull
  262. public Long getLastSonarlintConnectionDate() {
  263. return currentUserSession.getLastSonarlintConnectionDate();
  264. }
  265. @Override
  266. public Collection<GroupDto> getGroups() {
  267. return currentUserSession.getGroups();
  268. }
  269. @Override
  270. public boolean shouldResetPassword() {
  271. return currentUserSession.shouldResetPassword();
  272. }
  273. @Override
  274. public Optional<IdentityProvider> getIdentityProvider() {
  275. return currentUserSession.getIdentityProvider();
  276. }
  277. @Override
  278. public Optional<ExternalIdentity> getExternalIdentity() {
  279. return currentUserSession.getExternalIdentity();
  280. }
  281. @Override
  282. public boolean isLoggedIn() {
  283. return currentUserSession.isLoggedIn();
  284. }
  285. @Override
  286. public boolean isRoot() {
  287. return currentUserSession.isRoot();
  288. }
  289. @Override
  290. public UserSession checkIsRoot() {
  291. return currentUserSession.checkIsRoot();
  292. }
  293. @Override
  294. public UserSession checkLoggedIn() {
  295. currentUserSession.checkLoggedIn();
  296. return this;
  297. }
  298. @Override
  299. public boolean hasPermission(GlobalPermission permission) {
  300. return currentUserSession.hasPermission(permission);
  301. }
  302. @Override
  303. public UserSession checkPermission(GlobalPermission permission) {
  304. currentUserSession.checkPermission(permission);
  305. return this;
  306. }
  307. @Override
  308. public UserSession checkComponentPermission(String projectPermission, ComponentDto component) {
  309. currentUserSession.checkComponentPermission(projectPermission, component);
  310. return this;
  311. }
  312. @Override
  313. public UserSession checkProjectPermission(String projectPermission, ProjectDto project) {
  314. currentUserSession.checkProjectPermission(projectPermission, project);
  315. return this;
  316. }
  317. @Override
  318. public UserSession checkChildProjectsPermission(String projectPermission, ComponentDto component) {
  319. currentUserSession.checkChildProjectsPermission(projectPermission, component);
  320. return this;
  321. }
  322. @Override
  323. public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
  324. currentUserSession.checkChildProjectsPermission(projectPermission, application);
  325. return this;
  326. }
  327. @Override
  328. public UserSession checkComponentUuidPermission(String permission, String componentUuid) {
  329. currentUserSession.checkComponentUuidPermission(permission, componentUuid);
  330. return this;
  331. }
  332. @Override
  333. public boolean isSystemAdministrator() {
  334. return currentUserSession.isSystemAdministrator();
  335. }
  336. @Override
  337. public UserSession checkIsSystemAdministrator() {
  338. currentUserSession.checkIsSystemAdministrator();
  339. return this;
  340. }
  341. }