diff --git a/backend/data/src/main/kotlin/io/tolgee/service/contentDelivery/ContentDeliveryConfigService.kt b/backend/data/src/main/kotlin/io/tolgee/service/contentDelivery/ContentDeliveryConfigService.kt index 9f22543beb2..967a18105b1 100644 --- a/backend/data/src/main/kotlin/io/tolgee/service/contentDelivery/ContentDeliveryConfigService.kt +++ b/backend/data/src/main/kotlin/io/tolgee/service/contentDelivery/ContentDeliveryConfigService.kt @@ -137,7 +137,6 @@ class ContentDeliveryConfigService( id: Long, dto: ContentDeliveryConfigRequest, ): ContentDeliveryConfig { - projectFeatureGuard.checkIfUsed(Feature.BRANCHING, dto.filterBranch) checkMultipleConfigsFeature(projectService.get(projectId), maxCurrentAllowed = 1) val config = get(projectId, id) handleUpdateSlug(config, dto) @@ -145,7 +144,11 @@ class ContentDeliveryConfigService( config.name = dto.name config.pruneBeforePublish = dto.pruneBeforePublish config.zip = dto.zip - config.branch = branchService.getActiveOrDefault(projectId, dto.filterBranch) + val newBranch = branchService.getActiveOrDefault(projectId, dto.filterBranch) + if (newBranch?.isDefault == false || config.branch?.isDefault == false) { + projectFeatureGuard.checkEnabled(Feature.BRANCHING) + } + config.branch = newBranch config.copyPropsFrom(dto) handleUpdateAutoPublish(dto, config) return save(config) diff --git a/e2e/cypress/e2e/branching/contentDeliveryBranching.cy.ts b/e2e/cypress/e2e/branching/contentDeliveryBranching.cy.ts index 76a83b8321f..2d784909ac1 100644 --- a/e2e/cypress/e2e/branching/contentDeliveryBranching.cy.ts +++ b/e2e/cypress/e2e/branching/contentDeliveryBranching.cy.ts @@ -107,6 +107,25 @@ describe('Content delivery with branching', () => { .findDcy('branch-selector') .should('contain', 'feature'); }); + + it('edits branched content delivery when branching gets disabled', () => { + // Seed data is created with BRANCHING enabled so the config has a branch + // stored on the backend. Now simulate the feature being turned off. + setFeature('BRANCHING', false); + cy.reload(); + waitForGlobalLoading(); + + openEditDialog('Main CDN'); + // Branch selector must be hidden when branching is not enabled. + gcy('content-delivery-form-branch').should('not.exist'); + + cy.gcy('content-delivery-form-name').find('input').clear().type('Renamed'); + cy.gcy('content-delivery-form-save').click(); + waitForGlobalLoading(); + // Before the fix the PUT body carried the stale filterBranch which the + // backend rejected with FEATURE_NOT_ENABLED_FOR_PROJECT. + assertMessage('Content delivery successfully updated!'); + }); }); function openEditDialog(name: string) { diff --git a/ee/backend/tests/src/test/kotlin/io/tolgee/ee/api/v2/controllers/ContentDeliveryConfigBranchingTest.kt b/ee/backend/tests/src/test/kotlin/io/tolgee/ee/api/v2/controllers/ContentDeliveryConfigBranchingTest.kt index ea8482607ad..6f3f6fcff00 100644 --- a/ee/backend/tests/src/test/kotlin/io/tolgee/ee/api/v2/controllers/ContentDeliveryConfigBranchingTest.kt +++ b/ee/backend/tests/src/test/kotlin/io/tolgee/ee/api/v2/controllers/ContentDeliveryConfigBranchingTest.kt @@ -160,6 +160,36 @@ class ContentDeliveryConfigBranchingTest : ProjectAuthControllerTest("/v2/projec ).andIsBadRequest.andHasErrorMessage(Message.FEATURE_NOT_ENABLED) } + @Test + @ProjectJWTAuthTestMethod + fun `update default-branch CDN succeeds when branching not enabled and default is echoed`() { + enabledFeaturesProvider.forceEnabled = setOf(Feature.MULTIPLE_CONTENT_DELIVERY_CONFIGS) + performProjectAuthPut( + "content-delivery-configs/${testData.mainBranchCdnConfig.self.id}", + mapOf("name" to "Renamed", "filterBranch" to "main"), + ).andIsOk + } + + @Test + @ProjectJWTAuthTestMethod + fun `update default-branch CDN succeeds when branching not enabled and filterBranch is null`() { + enabledFeaturesProvider.forceEnabled = setOf(Feature.MULTIPLE_CONTENT_DELIVERY_CONFIGS) + performProjectAuthPut( + "content-delivery-configs/${testData.mainBranchCdnConfig.self.id}", + mapOf("name" to "Renamed"), + ).andIsOk + } + + @Test + @ProjectJWTAuthTestMethod + fun `update non-default-branch CDN fails when branching not enabled even if branch is unchanged`() { + enabledFeaturesProvider.forceEnabled = setOf(Feature.MULTIPLE_CONTENT_DELIVERY_CONFIGS) + performProjectAuthPut( + "content-delivery-configs/${testData.featureBranchCdnConfig.self.id}", + mapOf("name" to "Feature CDN", "filterBranch" to "feature"), + ).andIsBadRequest.andHasErrorMessage(Message.FEATURE_NOT_ENABLED) + } + @Test @ProjectJWTAuthTestMethod fun `CDN config filterBranch is derived from branch entity`() {