diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/OrganizationServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/OrganizationServiceCEImpl.java index e7728f337d..3f5d803bb8 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/OrganizationServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/OrganizationServiceCEImpl.java @@ -1,5 +1,6 @@ package com.appsmith.server.services.ce; +import com.appsmith.external.helpers.AppsmithBeanUtils; import com.appsmith.server.acl.AclPermission; import com.appsmith.server.acl.AppsmithRole; import com.appsmith.server.acl.RoleGraph; @@ -216,7 +217,22 @@ public class OrganizationServiceCEImpl extends BaseService update(String id, Organization resource) { - return repository.updateById(id, resource, MANAGE_ORGANIZATIONS) + Mono findOrganizationMono = repository.findById(id, MANAGE_ORGANIZATIONS) + .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.ORGANIZATION, id))); + + // In case the update is not used to update the policies, then set the policies to null to ensure that the + // existing policies are not overwritten. + if (resource.getPolicies().isEmpty()) { + resource.setPolicies(null); + } + + return findOrganizationMono + .map(existingOrganization -> { + AppsmithBeanUtils.copyNewFieldValuesIntoOldObject(resource, existingOrganization); + return existingOrganization; + }) + .flatMap(this::validateObject) + .flatMap(repository::save) .flatMap(analyticsService::sendUpdateEvent); } diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/OrganizationServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/OrganizationServiceTest.java index 4c907aeedc..ad35ad57d3 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/OrganizationServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/OrganizationServiceTest.java @@ -262,12 +262,11 @@ public class OrganizationServiceTest { Mono createOrganization = organizationService.create(organization); Mono updateOrganization = createOrganization - .map(t -> { - t.setDomain("abc.com"); - return t; - }) - .flatMap(t -> organizationService.update(t.getId(), t)) - .flatMap(t -> organizationService.getById(t.getId())); + .flatMap(t -> { + Organization newOrganization = new Organization(); + newOrganization.setDomain("abc.com"); + return organizationService.update(t.getId(), newOrganization); + }); StepVerifier.create(updateOrganization) .assertNext(t -> { @@ -280,6 +279,40 @@ public class OrganizationServiceTest { .verifyComplete(); } + /** + * This test tests for updating the organization with an empty name. + * The organization name should not be empty. + */ + @Test + @WithUserDetails(value = "api_user") + public void inValidUpdateOrganizationEmptyName() { + Policy manageOrgAppPolicy = Policy.builder().permission(ORGANIZATION_MANAGE_APPLICATIONS.getValue()) + .users(Set.of("api_user")) + .build(); + + Policy manageOrgPolicy = Policy.builder().permission(MANAGE_ORGANIZATIONS.getValue()) + .users(Set.of("api_user")) + .build(); + + Organization organization = new Organization(); + organization.setName("Test Update Name"); + organization.setDomain("example.com"); + organization.setWebsite("https://example.com"); + organization.setSlug("test-update-name"); + Mono createOrganization = organizationService.create(organization); + Mono updateOrganization = createOrganization + .flatMap(t -> { + Organization newOrganization = new Organization(); + newOrganization.setName(""); + return organizationService.update(t.getId(), newOrganization); + }); + + StepVerifier.create(updateOrganization) + .expectErrorMatches(throwable -> throwable instanceof AppsmithException && + throwable.getMessage().equals(AppsmithError.INVALID_PARAMETER.getMessage(FieldName.NAME))) + .verify(); + } + @Test @WithUserDetails(value = "api_user") public void uniqueSlugs() {