123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info 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.tester;
-
- import com.google.common.base.Preconditions;
- import java.util.Collection;
- import java.util.List;
- import java.util.Optional;
- import javax.annotation.CheckForNull;
- import javax.annotation.Nullable;
- import org.junit.rules.TestRule;
- import org.junit.runner.Description;
- import org.junit.runners.model.Statement;
- import org.sonar.db.component.ComponentDto;
- import org.sonar.db.permission.GlobalPermission;
- import org.sonar.db.portfolio.PortfolioDto;
- import org.sonar.db.project.ProjectDto;
- import org.sonar.db.user.GroupDto;
- import org.sonar.db.user.UserDto;
- import org.sonar.server.user.UserSession;
-
- import static com.google.common.base.Preconditions.checkNotNull;
- import static com.google.common.base.Preconditions.checkState;
-
- /**
- * {@code UserSessionRule} is intended to be used as a {@link org.junit.Rule} to easily manage {@link UserSession} in
- * unit tests.
- * <p>
- * It can be used as a {@link org.junit.ClassRule} but be careful not to modify its states from inside tests methods
- * unless you purposely want to have side effects between each tests.
- * </p>
- * <p>
- * One can define user session behavior which should apply on all tests directly on the property, eg.:
- * <pre>
- * {@literal @}Rule
- * public UserSessionRule userSession = UserSessionRule.standalone().login("admin").setOrganizationPermissions(OrganizationPermissions.SYSTEM_ADMIN);
- * </pre>
- * </p>
- * <p>
- * Behavior defined at property-level can obviously be override at test method level. For example, one could define
- * all methods to use an authenticated session such as presented above but can easily overwrite that behavior in a
- * specific test method as follow:
- * <pre>
- * {@literal @}Test
- * public void test_method() {
- * userSession.standalone();
- * {@literal [...]}
- * }
- * </pre>
- * </p>
- * <p>
- * {@code UserSessionRule}, emulates by default an anonymous
- * session. Therefore, call {@code UserSessionRule.standalone()} is equivalent to calling
- * {@code UserSessionRule.standalone().anonymous()}.
- * </p>
- * <p>
- * To emulate an identified user, either use method {@link #logIn(String)} if you want to specify the user's login, or
- * method {@link #logIn()} which will do the same but using the value of {@link #DEFAULT_LOGIN} as the user's login
- * (use the latest override if you don't care about the actual value of the login, it will save noise in your test).
- * </p>
- */
- public class UserSessionRule implements TestRule, UserSession {
- private static final String DEFAULT_LOGIN = "default_login";
-
- private UserSession currentUserSession;
-
- private UserSessionRule() {
- anonymous();
- }
-
- public static UserSessionRule standalone() {
- return new UserSessionRule();
- }
-
- /**
- * Log in with the default login {@link #DEFAULT_LOGIN}
- */
- public UserSessionRule logIn() {
- return logIn(DEFAULT_LOGIN);
- }
-
- /**
- * Log in with the specified login
- */
- public UserSessionRule logIn(String login) {
- setCurrentUserSession(new MockUserSession(login));
- return this;
- }
-
- /**
- * Log in with the specified login
- */
- public UserSessionRule logIn(UserDto userDto) {
- setCurrentUserSession(new MockUserSession(userDto));
- return this;
- }
-
- /**
- * Disconnect/go anonymous
- */
- public UserSessionRule anonymous() {
- setCurrentUserSession(new AnonymousMockUserSession());
- return this;
- }
-
- public UserSessionRule setRoot() {
- ensureMockUserSession().setRoot(true);
- return this;
- }
-
- public UserSessionRule setNonRoot() {
- ensureMockUserSession().setRoot(false);
- return this;
- }
-
- public UserSessionRule setSystemAdministrator() {
- ensureMockUserSession().setSystemAdministrator(true);
- return this;
- }
-
- public UserSessionRule setNonSystemAdministrator() {
- ensureMockUserSession().setSystemAdministrator(false);
- return this;
- }
-
- public UserSessionRule setExternalIdentity(IdentityProvider identityProvider, ExternalIdentity externalIdentity) {
- ensureMockUserSession().setExternalIdentity(identityProvider, externalIdentity);
- return this;
- }
-
- public UserSessionRule setInternalIdentity() {
- ensureMockUserSession().setInternalIdentity();
- return this;
- }
-
- @Override
- public Statement apply(Statement statement, Description description) {
- return this.statement(statement);
- }
-
- private Statement statement(final Statement base) {
- return new Statement() {
- public void evaluate() throws Throwable {
- UserSessionRule.this.before();
-
- try {
- base.evaluate();
- } finally {
- UserSessionRule.this.after();
- }
-
- }
- };
- }
-
- protected void before() {
- setCurrentUserSession(currentUserSession);
- }
-
- protected void after() {
- this.currentUserSession = null;
- }
-
- public void set(UserSession userSession) {
- checkNotNull(userSession);
- setCurrentUserSession(userSession);
- }
-
- public UserSessionRule registerComponents(ComponentDto... componentDtos) {
- ensureAbstractMockUserSession().registerComponents(componentDtos);
- return this;
- }
-
- public UserSessionRule registerProjects(ProjectDto... projectDtos) {
- ensureAbstractMockUserSession().registerProjects(projectDtos);
- return this;
- }
-
- public UserSessionRule registerApplication(ProjectDto application, ProjectDto... appProjects) {
- ensureAbstractMockUserSession().registerApplication(application, appProjects);
- return this;
- }
-
- public UserSessionRule registerApplication(ComponentDto application, ComponentDto... appProjects) {
- ensureAbstractMockUserSession().registerApplication(application, appProjects);
- return this;
- }
-
- public UserSessionRule addProjectPermission(String projectPermission, ComponentDto... components) {
- ensureAbstractMockUserSession().addProjectPermission(projectPermission, components);
- return this;
- }
-
- public UserSessionRule addProjectPermission(String projectPermission, ProjectDto... projectDto) {
- ensureAbstractMockUserSession().addProjectPermission(projectPermission, projectDto);
- return this;
- }
-
- public UserSessionRule addPortfolioPermission(String portfolioPermission, PortfolioDto... portfolioDto) {
- ensureAbstractMockUserSession().addPortfolioPermission(portfolioPermission, portfolioDto);
- return this;
- }
-
- public UserSessionRule addPermission(GlobalPermission permission) {
- ensureAbstractMockUserSession().addPermission(permission);
- return this;
- }
-
- /**
- * Groups that user is member of. User must be logged in. An exception
- * is thrown if session is anonymous.
- */
- public UserSessionRule setGroups(GroupDto... groups) {
- ensureMockUserSession().setGroups(groups);
- return this;
- }
-
- public UserSessionRule setName(@Nullable String s) {
- ensureMockUserSession().setName(s);
- return this;
- }
-
- private AbstractMockUserSession ensureAbstractMockUserSession() {
- checkState(currentUserSession instanceof AbstractMockUserSession, "rule state can not be changed if a UserSession has explicitly been provided");
- return (AbstractMockUserSession) currentUserSession;
- }
-
- private MockUserSession ensureMockUserSession() {
- checkState(currentUserSession instanceof MockUserSession, "rule state can not be changed if a UserSession has explicitly been provided");
- return (MockUserSession) currentUserSession;
- }
-
- private void setCurrentUserSession(UserSession userSession) {
- this.currentUserSession = Preconditions.checkNotNull(userSession);
- }
-
- @Override
- public boolean hasComponentPermission(String permission, ComponentDto component) {
- return currentUserSession.hasComponentPermission(permission, component);
- }
-
- @Override
- public boolean hasProjectPermission(String permission, ProjectDto project) {
- return currentUserSession.hasProjectPermission(permission, project);
- }
-
- @Override
- public boolean hasChildProjectsPermission(String permission, ComponentDto component) {
- return currentUserSession.hasChildProjectsPermission(permission, component);
- }
-
- @Override
- public boolean hasChildProjectsPermission(String permission, ProjectDto component) {
- return currentUserSession.hasChildProjectsPermission(permission, component);
- }
-
- @Override
- public boolean hasComponentUuidPermission(String permission, String componentUuid) {
- return currentUserSession.hasComponentUuidPermission(permission, componentUuid);
- }
-
- @Override
- public List<ComponentDto> keepAuthorizedComponents(String permission, Collection<ComponentDto> components) {
- return currentUserSession.keepAuthorizedComponents(permission, components);
- }
-
- @Override
- public List<ProjectDto> keepAuthorizedProjects(String permission, Collection<ProjectDto> projects) {
- return currentUserSession.keepAuthorizedProjects(permission, projects);
- }
-
- @Override
- @CheckForNull
- public String getLogin() {
- return currentUserSession.getLogin();
- }
-
- @Override
- @CheckForNull
- public String getUuid() {
- return currentUserSession.getUuid();
- }
-
- @Override
- @CheckForNull
- public String getName() {
- return currentUserSession.getName();
- }
-
- @Override
- @CheckForNull
- public Long getLastSonarlintConnectionDate() {
- return currentUserSession.getLastSonarlintConnectionDate();
- }
-
- @Override
- public Collection<GroupDto> getGroups() {
- return currentUserSession.getGroups();
- }
-
- @Override
- public boolean shouldResetPassword() {
- return currentUserSession.shouldResetPassword();
- }
-
- @Override
- public Optional<IdentityProvider> getIdentityProvider() {
- return currentUserSession.getIdentityProvider();
- }
-
- @Override
- public Optional<ExternalIdentity> getExternalIdentity() {
- return currentUserSession.getExternalIdentity();
- }
-
- @Override
- public boolean isLoggedIn() {
- return currentUserSession.isLoggedIn();
- }
-
- @Override
- public boolean isRoot() {
- return currentUserSession.isRoot();
- }
-
- @Override
- public UserSession checkIsRoot() {
- return currentUserSession.checkIsRoot();
- }
-
- @Override
- public UserSession checkLoggedIn() {
- currentUserSession.checkLoggedIn();
- return this;
- }
-
- @Override
- public boolean hasPermission(GlobalPermission permission) {
- return currentUserSession.hasPermission(permission);
- }
-
- @Override
- public UserSession checkPermission(GlobalPermission permission) {
- currentUserSession.checkPermission(permission);
- return this;
- }
-
- @Override
- public UserSession checkComponentPermission(String projectPermission, ComponentDto component) {
- currentUserSession.checkComponentPermission(projectPermission, component);
- return this;
- }
-
- @Override
- public UserSession checkProjectPermission(String projectPermission, ProjectDto project) {
- currentUserSession.checkProjectPermission(projectPermission, project);
- return this;
- }
-
- @Override
- public UserSession checkChildProjectsPermission(String projectPermission, ComponentDto component) {
- currentUserSession.checkChildProjectsPermission(projectPermission, component);
- return this;
- }
-
- @Override
- public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
- currentUserSession.checkChildProjectsPermission(projectPermission, application);
- return this;
- }
-
- @Override
- public UserSession checkComponentUuidPermission(String permission, String componentUuid) {
- currentUserSession.checkComponentUuidPermission(permission, componentUuid);
- return this;
- }
-
- @Override
- public boolean isSystemAdministrator() {
- return currentUserSession.isSystemAdministrator();
- }
-
- @Override
- public UserSession checkIsSystemAdministrator() {
- currentUserSession.checkIsSystemAdministrator();
- return this;
- }
- }
|