diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2018-10-23 16:39:20 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-11-16 20:21:04 +0100 |
commit | f714aa7a66fd87a39898390edd0c15f0c77dd248 (patch) | |
tree | b65a64780009f497e3c9f4263af07df51cea2f10 /server | |
parent | f0e05b42339d0736bf117c631bde4530cc1f356a (diff) | |
download | sonarqube-f714aa7a66fd87a39898390edd0c15f0c77dd248.tar.gz sonarqube-f714aa7a66fd87a39898390edd0c15f0c77dd248.zip |
SONAR-11323 Return personal organization info
* Return personalOrganization in api/users/current
* Replace type by personal in api/alm_integrations/show_org
* Update AlmOrganization type with personal flag instead of type field
Diffstat (limited to 'server')
12 files changed, 346 insertions, 246 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTesting.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTesting.java index ea3f929bda0..8be30e35c60 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTesting.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTesting.java @@ -45,8 +45,6 @@ public class UserTesting { .setExternalIdentityProvider(randomAlphanumeric(40)) .setSalt(randomAlphanumeric(40)) .setCryptedPassword(randomAlphanumeric(40)) - // Default quality gate should be set explicitly when needed in tests - .setOrganizationUuid("_NOT_SET_") .setCreatedAt(nextLong()) .setUpdatedAt(nextLong()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java index 9ba876f3538..137fbcc95df 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java @@ -41,6 +41,7 @@ import org.sonarqube.ws.Users.CurrentWsResponse; import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Strings.emptyToNull; +import static java.lang.String.format; import static java.util.Collections.singletonList; import static java.util.Optional.empty; import static java.util.Optional.of; @@ -113,6 +114,7 @@ public class CurrentAction implements UsersWsAction { UserDto user = dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin); checkState(user != null, "User login '%s' cannot be found", userLogin); Collection<String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, singletonList(userLogin)).get(userLogin); + Optional<OrganizationDto> personalOrganization = getPersonalOrganization(dbSession, user); CurrentWsResponse.Builder builder = newBuilder() .setIsLoggedIn(true) @@ -128,6 +130,7 @@ public class CurrentAction implements UsersWsAction { setNullable(emptyToNull(user.getEmail()), u -> builder.setAvatar(avatarResolver.create(user))); setNullable(user.getExternalLogin(), builder::setExternalIdentity); setNullable(user.getExternalIdentityProvider(), builder::setExternalProvider); + personalOrganization.ifPresent(org -> builder.setPersonalOrganization(org.getKey())); return builder.build(); } @@ -139,6 +142,15 @@ public class CurrentAction implements UsersWsAction { .collect(toList()); } + private Optional<OrganizationDto> getPersonalOrganization(DbSession dbSession, UserDto user) { + String personalOrganizationUuid = user.getOrganizationUuid(); + if (personalOrganizationUuid == null) { + return Optional.empty(); + } + return Optional.of(dbClient.organizationDao().selectByUuid(dbSession, personalOrganizationUuid) + .orElseThrow(() -> new IllegalStateException(format("Organization uuid '%s' does not exist", personalOrganizationUuid)))); + } + private CurrentWsResponse.Homepage buildHomepage(DbSession dbSession, UserDto user) { if (noHomepageSet(user)) { return defaultHomepage(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java new file mode 100644 index 00000000000..2a5b2577a60 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java @@ -0,0 +1,297 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.user.ws; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.ResourceType; +import org.sonar.api.resources.ResourceTypeTree; +import org.sonar.api.resources.ResourceTypes; +import org.sonar.api.utils.System2; +import org.sonar.core.platform.PluginRepository; +import org.sonar.db.DbClient; +import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.user.UserDto; +import org.sonar.server.issue.ws.AvatarResolverImpl; +import org.sonar.server.organization.DefaultOrganizationProvider; +import org.sonar.server.organization.TestDefaultOrganizationProvider; +import org.sonar.server.organization.TestOrganizationFlags; +import org.sonar.server.permission.PermissionService; +import org.sonar.server.permission.PermissionServiceImpl; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsActionTester; +import org.sonarqube.ws.Users.CurrentWsResponse; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.web.UserRole.USER; + +public class CurrentActionHomepageTest { + @Rule + public UserSessionRule userSessionRule = UserSessionRule.standalone(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private DbClient dbClient = db.getDbClient(); + private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + + private PluginRepository pluginRepository = mock(PluginRepository.class); + private MapSettings settings = new MapSettings(); + private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone(); + private HomepageTypesImpl homepageTypes = new HomepageTypesImpl(settings.asConfig(), organizationFlags, db.getDbClient()); + private PermissionService permissionService = new PermissionServiceImpl(new ResourceTypes(new ResourceTypeTree[] { + ResourceTypeTree.builder().addType(ResourceType.builder(Qualifiers.PROJECT).build()).build()})); + + private WsActionTester ws = new WsActionTester( + new CurrentAction(userSessionRule, dbClient, defaultOrganizationProvider, new AvatarResolverImpl(), homepageTypes, pluginRepository, permissionService)); + + @Test + public void return_homepage_when_set_to_MY_PROJECTS() { + UserDto user = db.users().insertUser(u -> u.setHomepageType("MY_PROJECTS")); + userSessionRule.logIn(user); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType) + .containsExactly(CurrentWsResponse.HomepageType.MY_PROJECTS); + } + + @Test + public void return_homepage_when_set_to_portfolios() { + UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIOS")); + userSessionRule.logIn(user); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType) + .containsExactly(CurrentWsResponse.HomepageType.PORTFOLIOS); + } + + @Test + public void return_homepage_when_set_to_a_portfolio() { + withGovernancePlugin(); + ComponentDto portfolio = db.components().insertPrivatePortfolio(db.getDefaultOrganization()); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter(portfolio.uuid())); + userSessionRule.logIn(user).addProjectPermission(USER, portfolio); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent) + .containsExactly(CurrentWsResponse.HomepageType.PORTFOLIO, portfolio.getKey()); + } + + @Test + public void return_default_when_set_to_a_portfolio_but_no_rights_on_this_portfolio() { + withGovernancePlugin(); + ComponentDto portfolio = db.components().insertPrivatePortfolio(db.getDefaultOrganization()); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter(portfolio.uuid())); + userSessionRule.logIn(user); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType) + .containsExactly(CurrentWsResponse.HomepageType.PROJECTS); + } + + @Test + public void return_homepage_when_set_to_an_application() { + withGovernancePlugin(); + ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization()); + UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid())); + userSessionRule.logIn(user).addProjectPermission(USER, application); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent) + .containsExactly(CurrentWsResponse.HomepageType.APPLICATION, application.getKey()); + } + + @Test + public void return_default_homepage_when_set_to_an_application_but_no_rights_on_this_application() { + withGovernancePlugin(); + ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization()); + UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid())); + userSessionRule.logIn(user); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType) + .containsExactly(CurrentWsResponse.HomepageType.PROJECTS); + } + + @Test + public void return_homepage_when_set_to_a_project() { + ComponentDto project = db.components().insertPrivateProject(); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(project.uuid())); + userSessionRule.logIn(user).addProjectPermission(USER, project); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent) + .containsExactly(CurrentWsResponse.HomepageType.PROJECT, project.getKey()); + } + + @Test + public void return_default_homepage_when_set_to_a_project_but_no_rights_on_this_project() { + ComponentDto project = db.components().insertPrivateProject(); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(project.uuid())); + userSessionRule.logIn(user); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType) + .containsExactly(CurrentWsResponse.HomepageType.PROJECTS); + } + + @Test + public void return_homepage_when_set_to_an_organization() { + OrganizationDto organizationDto = db.organizations().insert(); + UserDto user = db.users().insertUser(u -> u.setHomepageType("ORGANIZATION").setHomepageParameter(organizationDto.getUuid())); + userSessionRule.logIn(user); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getOrganization) + .containsExactly(CurrentWsResponse.HomepageType.ORGANIZATION, organizationDto.getKey()); + } + + @Test + public void return_homepage_when_set_to_a_branch() { + ComponentDto project = db.components().insertMainBranch(); + ComponentDto branch = db.components().insertProjectBranch(project); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(branch.uuid())); + userSessionRule.logIn(user).addProjectPermission(USER, project); + + CurrentWsResponse response = call(); + + assertThat(response.getHomepage()) + .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent, CurrentWsResponse.Homepage::getBranch) + .containsExactly(CurrentWsResponse.HomepageType.PROJECT, branch.getKey(), branch.getBranch()); + } + + @Test + public void fallback_when_user_homepage_project_does_not_exist_in_db() { + UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter("not-existing-project-uuid")); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage()).isNotNull(); + } + + @Test + public void fallback_when_user_homepage_organization_does_not_exist_in_db() { + UserDto user = db.users().insertUser(u -> u.setHomepageType("ORGANIZATION").setHomepageParameter("not-existing-organization-uuid")); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage()).isNotNull(); + } + + @Test + public void fallback_when_user_homepage_portfolio_does_not_exist_in_db() { + withGovernancePlugin(); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter("not-existing-portfolio-uuid")); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage()).isNotNull(); + } + + @Test + public void fallback_when_user_homepage_application_does_not_exist_in_db() { + withGovernancePlugin(); + UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter("not-existing-application-uuid")); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage()).isNotNull(); + } + + @Test + public void fallback_when_user_homepage_application_and_governance_plugin_is_not_installed() { + withoutGovernancePlugin(); + ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization()); + UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid())); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage().getType().toString()).isEqualTo("PROJECTS"); + } + + @Test + public void fallback_to_PROJECTS_when_on_SonarQube() { + UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter("not-existing-project-uuid")); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage().getType().toString()).isEqualTo("PROJECTS"); + } + + @Test + public void fallback_to_MY_PROJECTS_when_on_SonarCloud() { + onSonarCloud(); + UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter("not-existing-project-uuid")); + userSessionRule.logIn(user.getLogin()); + + CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); + + assertThat(response.getHomepage().getType().toString()).isEqualTo("MY_PROJECTS"); + } + + private CurrentWsResponse call() { + return ws.newRequest().executeProtobuf(CurrentWsResponse.class); + } + + private void onSonarCloud() { + settings.setProperty("sonar.sonarcloud.enabled", true); + } + + private void withGovernancePlugin() { + when(pluginRepository.hasPlugin("governance")).thenReturn(true); + } + + private void withoutGovernancePlugin() { + when(pluginRepository.hasPlugin("governance")).thenReturn(false); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java index 50b3c606650..a07acb7b511 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java @@ -30,14 +30,11 @@ import org.sonar.api.resources.ResourceTypes; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.core.platform.PluginRepository; -import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; -import org.sonar.db.organization.OrganizationDbTester; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.user.UserDto; import org.sonar.server.issue.ws.AvatarResolverImpl; -import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.organization.TestOrganizationFlags; import org.sonar.server.permission.PermissionService; @@ -49,7 +46,6 @@ import org.sonarqube.ws.Users.CurrentWsResponse; import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.sonar.api.web.UserRole.USER; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; @@ -66,22 +62,19 @@ public class CurrentActionTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - private DbClient dbClient = db.getDbClient(); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private OrganizationDbTester organizationDbTester = db.organizations(); - private PluginRepository pluginRepository = mock(PluginRepository.class); private MapSettings settings = new MapSettings(); private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone(); private HomepageTypesImpl homepageTypes = new HomepageTypesImpl(settings.asConfig(), organizationFlags, db.getDbClient()); private PermissionService permissionService = new PermissionServiceImpl(new ResourceTypes(new ResourceTypeTree[] { ResourceTypeTree.builder().addType(ResourceType.builder(Qualifiers.PROJECT).build()).build()})); + private WsActionTester ws = new WsActionTester( - new CurrentAction(userSessionRule, dbClient, defaultOrganizationProvider, new AvatarResolverImpl(), homepageTypes, pluginRepository, permissionService)); + new CurrentAction(userSessionRule, db.getDbClient(), TestDefaultOrganizationProvider.from(db), new AvatarResolverImpl(), homepageTypes, pluginRepository, permissionService)); @Test public void return_user_info() { - db.users().insertUser(user -> user + UserDto user = db.users().insertUser(u -> u .setLogin("obiwan.kenobi") .setName("Obiwan Kenobi") .setEmail("obiwan.kenobi@starwars.com") @@ -118,8 +111,8 @@ public class CurrentActionTest { assertThat(response) .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::hasAvatar, CurrentWsResponse::getLocal, - CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider) - .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", false, true, "obiwan", "sonarqube"); + CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::hasPersonalOrganization) + .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", false, true, "obiwan", "sonarqube", false); assertThat(response.hasEmail()).isFalse(); assertThat(response.getScmAccountsList()).isEmpty(); assertThat(response.getGroupsList()).isEmpty(); @@ -167,146 +160,34 @@ public class CurrentActionTest { } @Test - public void return_homepage_when_set_to_MY_PROJECTS() { - UserDto user = db.users().insertUser(u -> u.setHomepageType("MY_PROJECTS")); - userSessionRule.logIn(user); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType) - .containsExactly(CurrentWsResponse.HomepageType.MY_PROJECTS); - } - - @Test - public void return_homepage_when_set_to_portfolios() { - UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIOS")); - userSessionRule.logIn(user); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType) - .containsExactly(CurrentWsResponse.HomepageType.PORTFOLIOS); - } - - @Test - public void return_homepage_when_set_to_a_portfolio() { - withGovernancePlugin(); - ComponentDto portfolio = db.components().insertPrivatePortfolio(db.getDefaultOrganization()); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter(portfolio.uuid())); - userSessionRule.logIn(user).addProjectPermission(USER, portfolio); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent) - .containsExactly(CurrentWsResponse.HomepageType.PORTFOLIO, portfolio.getKey()); - } - - @Test - public void return_default_when_set_to_a_portfolio_but_no_rights_on_this_portfolio() { - withGovernancePlugin(); - ComponentDto portfolio = db.components().insertPrivatePortfolio(db.getDefaultOrganization()); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter(portfolio.uuid())); - userSessionRule.logIn(user); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType) - .containsExactly(CurrentWsResponse.HomepageType.PROJECTS); - } - - @Test - public void return_homepage_when_set_to_an_application() { - withGovernancePlugin(); - ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization()); - UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid())); - userSessionRule.logIn(user).addProjectPermission(USER, application); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent) - .containsExactly(CurrentWsResponse.HomepageType.APPLICATION, application.getKey()); - } - - @Test - public void return_default_homepage_when_set_to_an_application_but_no_rights_on_this_application() { - withGovernancePlugin(); - ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization()); - UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid())); + public void return_personal_organization() { + OrganizationDto organization = db.organizations().insert(); + UserDto user = db.users().insertUser(u -> u.setOrganizationUuid(organization.getUuid())); userSessionRule.logIn(user); CurrentWsResponse response = call(); - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType) - .containsExactly(CurrentWsResponse.HomepageType.PROJECTS); + assertThat(response.getPersonalOrganization()).isEqualTo(organization.getKey()); } @Test - public void return_homepage_when_set_to_a_project() { - ComponentDto project = db.components().insertPrivateProject(); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(project.uuid())); - userSessionRule.logIn(user).addProjectPermission(USER, project); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent) - .containsExactly(CurrentWsResponse.HomepageType.PROJECT, project.getKey()); - } - - @Test - public void return_default_homepage_when_set_to_a_project_but_no_rights_on_this_project() { - ComponentDto project = db.components().insertPrivateProject(); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(project.uuid())); - userSessionRule.logIn(user); + public void fail_with_ISE_when_user_login_in_db_does_not_exist() { + db.users().insertUser(usert -> usert.setLogin("another")); + userSessionRule.logIn("obiwan.kenobi"); - CurrentWsResponse response = call(); + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("User login 'obiwan.kenobi' cannot be found"); - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType) - .containsExactly(CurrentWsResponse.HomepageType.PROJECTS); + call(); } @Test - public void return_homepage_when_set_to_an_organization() { - - OrganizationDto organizationDto = organizationDbTester.insert(); - UserDto user = db.users().insertUser(u -> u.setHomepageType("ORGANIZATION").setHomepageParameter(organizationDto.getUuid())); + public void fail_with_ISE_when_personal_organization_does_not_exist() { + UserDto user = db.users().insertUser(u -> u.setOrganizationUuid("Unknown")); userSessionRule.logIn(user); - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getOrganization) - .containsExactly(CurrentWsResponse.HomepageType.ORGANIZATION, organizationDto.getKey()); - } - - @Test - public void return_homepage_when_set_to_a_branch() { - ComponentDto project = db.components().insertMainBranch(); - ComponentDto branch = db.components().insertProjectBranch(project); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(branch.uuid())); - userSessionRule.logIn(user).addProjectPermission(USER, project); - - CurrentWsResponse response = call(); - - assertThat(response.getHomepage()) - .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent, CurrentWsResponse.Homepage::getBranch) - .containsExactly(CurrentWsResponse.HomepageType.PROJECT, branch.getKey(), branch.getBranch()); - } - - @Test - public void fail_with_ISE_when_user_login_in_db_does_not_exist() { - db.users().insertUser(usert -> usert.setLogin("another")); - userSessionRule.logIn("obiwan.kenobi"); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("User login 'obiwan.kenobi' cannot be found"); + expectedException.expectMessage("Organization uuid 'Unknown' does not exist"); call(); } @@ -352,7 +233,6 @@ public class CurrentActionTest { db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), obiwan); db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), obiwan); - String response = ws.newRequest().execute().getInput(); assertJson(response).isSimilarTo(getClass().getResource("current-example.json")); @@ -375,91 +255,4 @@ public class CurrentActionTest { return ws.newRequest().executeProtobuf(CurrentWsResponse.class); } - @Test - public void fallback_when_user_homepage_project_does_not_exist_in_db() { - UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter("not-existing-project-uuid")); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage()).isNotNull(); - } - - @Test - public void fallback_when_user_homepage_organization_does_not_exist_in_db() { - UserDto user = db.users().insertUser(u -> u.setHomepageType("ORGANIZATION").setHomepageParameter("not-existing-organization-uuid")); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage()).isNotNull(); - } - - @Test - public void fallback_when_user_homepage_portfolio_does_not_exist_in_db() { - withGovernancePlugin(); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter("not-existing-portfolio-uuid")); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage()).isNotNull(); - } - - @Test - public void fallback_when_user_homepage_application_does_not_exist_in_db() { - withGovernancePlugin(); - UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter("not-existing-application-uuid")); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage()).isNotNull(); - } - - @Test - public void fallback_when_user_homepage_application_and_governance_plugin_is_not_installed() { - withoutGovernancePlugin(); - ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization()); - UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid())); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage().getType().toString()).isEqualTo("PROJECTS"); - } - - @Test - public void fallback_to_PROJECTS_when_on_SonarQube() { - UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter("not-existing-project-uuid")); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage().getType().toString()).isEqualTo("PROJECTS"); - } - - @Test - public void fallback_to_MY_PROJECTS_when_on_SonarCloud() { - onSonarCloud(); - UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter("not-existing-project-uuid")); - userSessionRule.logIn(user.getLogin()); - - CurrentWsResponse response = ws.newRequest().executeProtobuf(CurrentWsResponse.class); - - assertThat(response.getHomepage().getType().toString()).isEqualTo("MY_PROJECTS"); - } - - private void onSonarCloud() { - settings.setProperty("sonar.sonarcloud.enabled", true); - } - - private void withGovernancePlugin(){ - when(pluginRepository.hasPlugin("governance")).thenReturn(true); - } - - private void withoutGovernancePlugin(){ - when(pluginRepository.hasPlugin("governance")).thenReturn(false); - } - } diff --git a/server/sonar-web/src/main/js/app/types.ts b/server/sonar-web/src/main/js/app/types.ts index df5915bbfd0..01b41f6dbfd 100644 --- a/server/sonar-web/src/main/js/app/types.ts +++ b/server/sonar-web/src/main/js/app/types.ts @@ -28,7 +28,7 @@ export interface AlmApplication extends IdentityProvider { } export interface AlmOrganization extends OrganizationBase { key: string; - type: 'ORGANIZATION' | 'USER'; + personal: boolean; } export interface AlmRepository { diff --git a/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoOrganizationCreate-test.tsx b/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoOrganizationCreate-test.tsx index 4d6539e500c..b58cd5adf3d 100644 --- a/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoOrganizationCreate-test.tsx +++ b/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoOrganizationCreate-test.tsx @@ -44,7 +44,7 @@ it('should render prefilled and create org', async () => { const onOrgCreated = jest.fn(); const wrapper = shallowRender({ almInstallId: 'id-foo', - almOrganization: { ...organization, type: 'ORGANIZATION' }, + almOrganization: { ...organization, personal: false }, createOrganization, onOrgCreated }); @@ -61,7 +61,7 @@ it('should render prefilled and create org', async () => { it('should display choice between import or creation', () => { const wrapper = shallowRender({ almInstallId: 'id-foo', - almOrganization: { ...organization, type: 'ORGANIZATION' }, + almOrganization: { ...organization, personal: false }, unboundOrganizations: [organization] }); expect(wrapper).toMatchSnapshot(); @@ -79,7 +79,7 @@ it('should bind existing organization', async () => { const onOrgCreated = jest.fn(); const wrapper = shallowRender({ almInstallId: 'id-foo', - almOrganization: { ...organization, type: 'ORGANIZATION' }, + almOrganization: { ...organization, personal: false }, onOrgCreated, unboundOrganizations: [organization] }); diff --git a/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoPersonalOrganizationBind-test.tsx b/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoPersonalOrganizationBind-test.tsx index d735122f941..dd1eebc4620 100644 --- a/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoPersonalOrganizationBind-test.tsx +++ b/server/sonar-web/src/main/js/apps/create/organization/__tests__/AutoPersonalOrganizationBind-test.tsx @@ -57,7 +57,7 @@ function shallowRender(props: Partial<AutoPersonalOrganizationBind['props']> = { description: 'description-foo', key: 'key-foo', name: 'name-foo', - type: 'USER', + personal: true, url: 'http://example.com/foo' }} importPersonalOrg={{ key: 'personalorg', name: 'Personal Org' }} diff --git a/server/sonar-web/src/main/js/apps/create/organization/__tests__/CreateOrganization-test.tsx b/server/sonar-web/src/main/js/apps/create/organization/__tests__/CreateOrganization-test.tsx index 8aa237fc1d0..eb9fce91998 100644 --- a/server/sonar-web/src/main/js/apps/create/organization/__tests__/CreateOrganization-test.tsx +++ b/server/sonar-web/src/main/js/apps/create/organization/__tests__/CreateOrganization-test.tsx @@ -34,20 +34,20 @@ jest.mock('../../../../api/billing', () => ({ jest.mock('../../../../api/alm-integration', () => ({ getAlmAppInfo: jest.fn().mockResolvedValue({ application: { - installationUrl: 'https://alm.installation.url', backgroundColor: 'blue', iconPath: 'icon/path', + installationUrl: 'https://alm.installation.url', key: 'github', name: 'GitHub' } }), getAlmOrganization: jest.fn().mockResolvedValue({ + avatar: 'https://avatars3.githubusercontent.com/u/37629810?v=4', + description: 'Continuous Code Quality', key: 'sonarsource', name: 'SonarSource', - description: 'Continuous Code Quality', - url: 'https://www.sonarsource.com', - avatar: 'https://avatars3.githubusercontent.com/u/37629810?v=4', - type: 'ORGANIZATION' + personal: false, + url: 'https://www.sonarsource.com' }) })); @@ -95,7 +95,7 @@ it('should render with auto personal organization bind page', async () => { key: 'foo', name: 'Foo', avatar: 'https://avatars3.githubusercontent.com/u/37629810?v=4', - type: 'USER' + personal: true }); const wrapper = shallowRender({ currentUser: { ...user, externalProvider: 'github', personalOrganization: 'foo' }, diff --git a/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/AutoOrganizationCreate-test.tsx.snap b/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/AutoOrganizationCreate-test.tsx.snap index f11e164d3a1..56a6fe28fbb 100644 --- a/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/AutoOrganizationCreate-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/AutoOrganizationCreate-test.tsx.snap @@ -11,7 +11,7 @@ exports[`should display choice between import or creation 1`] = ` "description": "description-foo", "key": "key-foo", "name": "name-foo", - "type": "ORGANIZATION", + "personal": false, "url": "http://example.com/foo", } } @@ -73,7 +73,7 @@ exports[`should render prefilled and create org 1`] = ` "description": "description-foo", "key": "key-foo", "name": "name-foo", - "type": "ORGANIZATION", + "personal": false, "url": "http://example.com/foo", } } @@ -111,7 +111,7 @@ exports[`should render prefilled and create org 1`] = ` "description": "description-foo", "key": "key-foo", "name": "name-foo", - "type": "ORGANIZATION", + "personal": false, "url": "http://example.com/foo", } } diff --git a/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/CreateOrganization-test.tsx.snap b/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/CreateOrganization-test.tsx.snap index d43925de3dc..11acf9d7aba 100644 --- a/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/CreateOrganization-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/create/organization/__tests__/__snapshots__/CreateOrganization-test.tsx.snap @@ -64,7 +64,7 @@ exports[`should render with auto personal organization bind page 2`] = ` "avatar": "https://avatars3.githubusercontent.com/u/37629810?v=4", "key": "foo", "name": "Foo", - "type": "USER", + "personal": true, } } importPersonalOrg={ @@ -244,7 +244,7 @@ exports[`should render with auto tab selected and manual disabled 2`] = ` "description": "Continuous Code Quality", "key": "sonarsource", "name": "SonarSource", - "type": "ORGANIZATION", + "personal": false, "url": "https://www.sonarsource.com", } } diff --git a/server/sonar-web/src/main/js/helpers/__tests__/almIntegrations-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/almIntegrations-test.ts index 7a54b6b68bd..cfaba659431 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/almIntegrations-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/almIntegrations-test.ts @@ -36,8 +36,8 @@ it('#isVSTS', () => { }); it('#isPersonal', () => { - expect(isPersonal({ key: 'foo', name: 'Foo', type: 'USER' })).toBeTruthy(); - expect(isPersonal({ key: 'foo', name: 'Foo', type: 'ORGANIZATION' })).toBeFalsy(); + expect(isPersonal({ key: 'foo', name: 'Foo', personal: true })).toBeTruthy(); + expect(isPersonal({ key: 'foo', name: 'Foo', personal: false })).toBeFalsy(); }); it('#sanitizeAlmId', () => { diff --git a/server/sonar-web/src/main/js/helpers/almIntegrations.ts b/server/sonar-web/src/main/js/helpers/almIntegrations.ts index fdfe7abd17c..29af8561f5a 100644 --- a/server/sonar-web/src/main/js/helpers/almIntegrations.ts +++ b/server/sonar-web/src/main/js/helpers/almIntegrations.ts @@ -39,7 +39,7 @@ export function isVSTS(almKey?: string) { } export function isPersonal(organization?: AlmOrganization) { - return Boolean(organization && organization.type === 'USER'); + return Boolean(organization && organization.personal); } export function sanitizeAlmId(almKey?: string) { |