Beautiful Open-Source Form Builder
Implement Custom Domain Validation and Testing for Workspaces (#785)
Chirag Chhatrala492479d54d46
diff --git a/api/app/Http/Requests/Workspace/CustomDomainRequest.php b/api/app/Http/Requests/Workspace/CustomDomainRequest.phpindex a400412cb..3f6ea35d1 100644--- a/api/app/Http/Requests/Workspace/CustomDomainRequest.php+++ b/api/app/Http/Requests/Workspace/CustomDomainRequest.php@@ -36,17 +36,28 @@ class CustomDomainRequest extends FormRequestreturn ! empty(trim($domain));})->each(function ($domain) use (&$errors) {if (! preg_match(self::CUSTOM_DOMAINS_REGEX, $domain)) {- $errors[] = 'Invalid domain: '.$domain;+ $errors[] = 'Invalid domain: ' . $domain;}});if (count($errors)) {- $fail($errors);+ $fail(implode(' ', $errors));+ }++ // Check if domains are already used by other workspaces+ foreach ($domains as $domain) {+ $exists = Workspace::where('id', '!=', $this->workspace->id)+ ->whereJsonContains('custom_domains', $domain)+ ->exists();++ if ($exists) {+ $fail("The domain {$domain} is already in use by another workspace.");+ }}$limit = $this->workspace->custom_domain_count_limit;if ($limit && $domains->count() > $limit) {- $fail('You can only add '.$limit.' domain(s).');+ $fail('You can only add ' . $limit . ' domain(s).');}$this->customDomains = $domains->toArray();diff --git a/api/tests/Feature/WorkspaceTest.php b/api/tests/Feature/WorkspaceTest.phpindex 9a79c146b..72bf5ba6b 100644--- a/api/tests/Feature/WorkspaceTest.php+++ b/api/tests/Feature/WorkspaceTest.php@@ -61,3 +61,87 @@ it('can update workspace', function () {'message' => 'Workspace updated.',]);});++it('can save custom domain for workspace', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);++ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace->id]), [+ 'custom_domains' => ['example.com']+ ])+ ->assertSuccessful();++ $workspace->refresh();+ expect($workspace->custom_domains)->toBe(['example.com']);+});++it('can set custom domain to null', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $workspace->update(['custom_domains' => ['example.com']]);++ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace->id]), [+ 'custom_domains' => []+ ])+ ->assertSuccessful();++ $workspace->refresh();+ expect($workspace->custom_domains)->toBe([]);+});++it('validates custom domain format', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);++ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace->id]), [+ 'custom_domains' => ['invalid-domain']+ ])+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'Invalid domain: invalid-domain',+ ]);++ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace->id]), [+ 'custom_domains' => ['https://example.com']+ ])+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'Invalid domain: https://example.com',+ ]);+});++it('prevents duplicate custom domains across workspaces', function () {+ $user = $this->actingAsProUser();+ $workspace1 = $this->createUserWorkspace($user);+ $workspace2 = $this->createUserWorkspace($user);++ // Set domain for first workspace+ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace1->id]), [+ 'custom_domains' => ['example.com']+ ])+ ->assertSuccessful();++ // Try to set same domain for second workspace+ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace2->id]), [+ 'custom_domains' => ['example.com']+ ])+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'The domain example.com is already in use by another workspace.',+ ]);+});++it('allows same workspace to update its own custom domain', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $workspace->update(['custom_domains' => ['example.com']]);++ // Same workspace should be able to "update" to the same domain+ $this->putJson(route('open.workspaces.save-custom-domains', [$workspace->id]), [+ 'custom_domains' => ['example.com']+ ])+ ->assertSuccessful();++ $workspace->refresh();+ expect($workspace->custom_domains)->toBe(['example.com']);+});diff --git a/client/components/pages/settings/WorkSpaceCustomDomains.vue b/client/components/pages/settings/WorkSpaceCustomDomains.vueindex aa5bd9187..c99bd50b3 100644--- a/client/components/pages/settings/WorkSpaceCustomDomains.vue+++ b/client/components/pages/settings/WorkSpaceCustomDomains.vue@@ -111,9 +111,7 @@ const saveChanges = () => {useAlert().success("Custom domains saved.")}).catch((error) => {- useAlert().error(- "Failed to update custom domains: " + error.response.data.message,- )+ useAlert().error(error.response._data.message ?? 'Failed to update custom domains')}).finally(() => {customDomainsLoading.value = false