Skip to content

Beautiful Open-Source Form Builder

21.5 KB
Raw Blame
<?php

use App\Models\User;

it('can not create more than 1 workspace for free user', function () {
    $user = $this->actingAsUser();

    $this->postJson(route('open.workspaces.create'), [
        'name' => 'Workspace Test',
        'icon' => '🧪',
    ])
        ->assertSuccessful()
        ->assertJson([
            'type' => 'success',
            'message' => 'Workspace created.',
        ]);

    expect($user->workspaces()->count())->toBe(1);

    // Try to create another workspace
    $this->postJson(route('open.workspaces.create'), [
        'name' => 'Workspace Test 2',
        'icon' => '🧪',
    ])
        ->assertStatus(403)
        ->assertJson([
            'message' => 'You have reached the workspace limit for Free plan. Upgrade to create additional workspaces.',
        ]);
});

it('can create and delete Workspace', function () {
    $user = $this->actingAsProUser();

    for ($i = 1; $i <= 3; $i++) {
        $this->postJson(route('open.workspaces.create'), [
            'name' => 'Workspace Test - ' . $i,
            'icon' => '🧪',
        ])
            ->assertSuccessful()
            ->assertJson([
                'type' => 'success',
                'message' => 'Workspace created.',
            ]);
    }

    expect($user->workspaces()->count())->toBe(3);

    $i = 0;
    foreach ($user->workspaces as $workspace) {
        $i++;
        if ($i !== 3) {
            $this->deleteJson(route('open.workspaces.delete', $workspace))
                ->assertSuccessful()
                ->assertJson([
                    'type' => 'success',
                    'message' => 'Workspace successfully deleted.',
                ]);
        } else {
            // Last workspace can not delete
            $this->deleteJson(route('open.workspaces.delete', $workspace))
                ->assertStatus(403);
        }
    }
});

it('can update workspace', function () {
    $user = $this->actingAsUser();

    $this->postJson(route('open.workspaces.create'), [
        'name' => 'Workspace Test',
        'icon' => '🧪',
    ])
        ->assertSuccessful()
        ->assertJson([
            'type' => 'success',
            'message' => 'Workspace created.',
        ]);

    expect($user->workspaces()->count())->toBe(1);

    $workspace = $user->workspaces()->first();
    $this->putJson(route('open.workspaces.update', $workspace), [
        'name' => 'Workspace Test Updated',
        'icon' => '🔬',
    ])
        ->assertSuccessful()
        ->assertJson([
            'type' => 'success',
            'message' => 'Workspace updated.',
        ]);
});

it('can save custom domain for workspace', function () {
    $this->mock(\App\Service\CustomDomainOwnershipService::class, function ($mock) {
        $mock->shouldReceive('verify')->andReturnTrue();
    });
    $user = $this->actingAsProUser();
    $workspace = $this->createUserWorkspace($user);

    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        '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->forceFill(['custom_domains' => ['example.com']])->save();

    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        '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), [
        'custom_domains' => ['invalid-domain']
    ])
        ->assertStatus(422)
        ->assertJson([
            'message' => 'Invalid domain: invalid-domain',
        ]);

    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        'custom_domains' => ['https://example.com']
    ])
        ->assertStatus(422)
        ->assertJson([
            'message' => 'Invalid domain: https://example.com',
        ]);
});

it('prevents duplicate custom domains across workspaces', function () {
    $this->mock(\App\Service\CustomDomainOwnershipService::class, function ($mock) {
        $mock->shouldReceive('verify')->andReturnTrue();
    });
    $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), [
        'custom_domains' => ['example.com']
    ])
        ->assertSuccessful();

    // Try to set same domain for second workspace
    $this->putJson(route('open.workspaces.save-custom-domains', $workspace2), [
        '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->forceFill(['custom_domains' => ['example.com']])->save();

    // Same workspace should be able to "update" to the same domain
    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        'custom_domains' => ['example.com']
    ])
        ->assertSuccessful();

    $workspace->refresh();
    expect($workspace->custom_domains)->toBe(['example.com']);
});

it('accepts multi-part TLD domains like .co.uk', function () {
    $this->mock(\App\Service\CustomDomainOwnershipService::class, function ($mock) {
        $mock->shouldReceive('verify')->andReturnTrue();
    });
    $user = $this->actingAsProUser();
    $workspace = $this->createUserWorkspace($user);

    // Test .co.uk domain
    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        'custom_domains' => ['example.co.uk']
    ])
        ->assertSuccessful();

    $workspace->refresh();
    expect($workspace->custom_domains)->toBe(['example.co.uk']);

    // Test .co.nz domain
    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        'custom_domains' => ['test.co.nz']
    ])
        ->assertSuccessful();

    $workspace->refresh();
    expect($workspace->custom_domains)->toBe(['test.co.nz']);

    // Test .com.au domain
    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        'custom_domains' => ['domain.com.au']
    ])
        ->assertSuccessful();

    $workspace->refresh();
    expect($workspace->custom_domains)->toBe(['domain.com.au']);

    // Test subdomain with multi-part TLD
    $this->putJson(route('open.workspaces.save-custom-domains', $workspace), [
        'custom_domains' => ['subdomain.example.co.uk']
    ])
        ->assertSuccessful();

    $workspace->refresh();
    expect($workspace->custom_domains)->toBe(['subdomain.example.co.uk']);
});

it('includes users_count attribute', function () {
    $user = $this->actingAsUser();
    $workspace = $this->createUserWorkspace($user);

    // Initially should have 1 user (the creator)
    expect($workspace->users_count)->toBe(1);

    // Add another user to the workspace
    $user2 = \App\Models\User::factory()->create();
    $workspace->users()->attach($user2, ['role' => 'admin']);

    // Clear cache and check count
    $workspace->flush();
    expect($workspace->fresh()->users_count)->toBe(2);
});

it('uses appsumo license limits for workspace file size and custom domains', function () {
    $user = $this->createAppSumoLicensedUser(3);
    $workspace = $this->createUserWorkspace($user);
    $workspace->load('users');
    $workspace->flush();

    expect($workspace->fresh()->plan_tier)->toBe('pro');
    expect($workspace->fresh()->max_file_size)->toBe(75000000);
    expect($workspace->fresh()->custom_domain_count_limit)->toBeNull();
});

describe('Custom Code Settings', function () {
    it('can save custom code settings for workspace', function () {
        $user = $this->actingAsBusinessUser();
        $workspace = $this->createUserWorkspace($user);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_code' => '<script>console.log("test")</script>',
            'custom_css' => 'body { color: red; }',
        ])->assertSuccessful();

        $workspace->refresh();
        expect($workspace->settings['custom_code'])->toBe('<script>console.log("test")</script>');
        expect($workspace->settings['custom_css'])->toBe('body { color: red; }');
    });

    it('prevents free users from saving custom code settings', function () {
        $user = $this->actingAsUser();
        $workspace = $this->createUserWorkspace($user);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_code' => '<script>test</script>',
        ])->assertStatus(402);
    });

    it('prevents pro users from saving custom code settings', function () {
        $user = $this->actingAsProUser();
        $workspace = $this->createUserWorkspace($user);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_code' => '<script>test</script>',
        ])->assertStatus(402);
    });

    it('validates custom CSS with CssOnlyRule', function () {
        $user = $this->actingAsBusinessUser();
        $workspace = $this->createUserWorkspace($user);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_css' => '<script>evil()</script>',
        ])->assertStatus(422);
    });

    it('allows nullable custom code and css', function () {
        $user = $this->actingAsBusinessUser();
        $workspace = $this->createUserWorkspace($user);
        $workspace->update(['settings' => ['custom_code' => 'old', 'custom_css' => 'old']]);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_code' => null,
            'custom_css' => null,
        ])->assertSuccessful();

        $workspace->refresh();
        expect($workspace->settings['custom_code'])->toBeNull();
        expect($workspace->settings['custom_css'])->toBeNull();
    });

    it('preserves other settings when saving custom code', function () {
        $user = $this->actingAsBusinessUser();
        $workspace = $this->createUserWorkspace($user);
        $workspace->update(['settings' => ['email_settings' => ['host' => 'smtp.test.com']]]);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_code' => '<script>test</script>',
        ])->assertSuccessful();

        $workspace->refresh();
        expect($workspace->settings['custom_code'])->toBe('<script>test</script>');
        expect($workspace->settings['email_settings']['host'])->toBe('smtp.test.com');
    });

    it('prevents non-admin users from saving custom code settings', function () {
        $admin = $this->createBusinessUser();
        $workspace = $this->createUserWorkspace($admin);

        $readonlyUser = $this->createBusinessUser();
        $workspace->users()->attach($readonlyUser, ['role' => 'user']);
        $this->actingAsBusinessUser($readonlyUser);

        $this->putJson(route('open.workspaces.save-custom-code-settings', $workspace), [
            'custom_code' => '<script>test</script>',
        ])->assertStatus(403);
    });
});

describe('External File Link Settings', function () {
    it('defaults external file links to 24 hours', function () {
        $user = $this->actingAsUser();
        $workspace = $this->createUserWorkspace($user);

        expect(app(\App\Service\Forms\ExternalSubmissionFileLinkPolicy::class)->expirationHours($workspace))
            ->toBe(24);
    });

    it('can save external file link settings for a workspace', function () {
        $user = $this->actingAsUser();
        $workspace = $this->createUserWorkspace($user);
        $workspace->update(['settings' => ['custom_code' => '<script>test</script>']]);

        $this->putJson(route('open.workspaces.save-external-file-link-settings', $workspace), [
            'expires_in_hours' => 168,
        ])->assertSuccessful();

        $workspace->refresh();

        expect($workspace->settings['external_file_links']['expires_in_hours'])->toBe(168);
        expect($workspace->settings['custom_code'])->toBe('<script>test</script>');
    });

    it('validates external file link settings', function () {
        $user = $this->actingAsUser();
        $workspace = $this->createUserWorkspace($user);

        $this->putJson(route('open.workspaces.save-external-file-link-settings', $workspace), [
            'expires_in_hours' => 48,
        ])->assertUnprocessable()->assertJsonValidationErrors('expires_in_hours');
    });

    it('prevents non-admin users from saving external file link settings', function () {
        $admin = $this->createUser();
        $workspace = $this->createUserWorkspace($admin);

        $member = $this->createUser();
        $workspace->users()->attach($member, ['role' => 'user']);
        $this->actingAsUser($member);

        $this->putJson(route('open.workspaces.save-external-file-link-settings', $workspace), [
            'expires_in_hours' => 168,
        ])->assertForbidden();
    });
});

describe('SMTP settings redaction', function () {
    it('exposes email_settings to admin users', function () {
        $admin = $this->actingAsProUser();
        $workspace = $this->createUserWorkspace($admin);
        $workspace->update(['settings' => [
            'email_settings' => [
                'host' => 'smtp.test.com',
                'port' => 587,
                'username' => '[email protected]',
                'password' => 'super-secret-password',
            ],
        ]]);

        $response = $this->getJson(route('open.workspaces.index'))
            ->assertSuccessful();

        $ws = collect($response->json())->firstWhere('id', $workspace->id);
        expect($ws['settings']['email_settings']['password'])->toBe('super-secret-password');
        expect($ws['settings']['email_settings']['host'])->toBe('smtp.test.com');
    });

    it('hides email_settings from non-admin user role', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);
        $workspace->update(['settings' => [
            'email_settings' => [
                'host' => 'smtp.test.com',
                'port' => 587,
                'username' => '[email protected]',
                'password' => 'super-secret-password',
            ],
        ]]);

        $member = $this->createUser();
        $workspace->users()->attach($member, ['role' => 'user']);
        $this->actingAsUser($member);

        $response = $this->getJson(route('open.workspaces.index'))
            ->assertSuccessful();

        $ws = collect($response->json())->firstWhere('id', $workspace->id);
        expect($ws['settings']['email_settings'] ?? null)->toBeNull();
    });

    it('hides email_settings from readonly role', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);
        $workspace->update(['settings' => [
            'email_settings' => [
                'host' => 'smtp.test.com',
                'port' => 587,
                'username' => '[email protected]',
                'password' => 'super-secret-password',
            ],
        ]]);

        $readonlyUser = $this->createUser();
        $workspace->users()->attach($readonlyUser, ['role' => 'readonly']);
        $this->actingAsUser($readonlyUser);

        $response = $this->getJson(route('open.workspaces.index'))
            ->assertSuccessful();

        $ws = collect($response->json())->firstWhere('id', $workspace->id);
        expect($ws['settings']['email_settings'] ?? null)->toBeNull();
    });

    it('preserves non-sensitive settings for non-admin users', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);
        $workspace->update(['settings' => [
            'custom_code' => '<script>tracking</script>',
            'email_settings' => [
                'host' => 'smtp.test.com',
                'password' => 'secret',
            ],
        ]]);

        $member = $this->createUser();
        $workspace->users()->attach($member, ['role' => 'user']);
        $this->actingAsUser($member);

        $response = $this->getJson(route('open.workspaces.index'))
            ->assertSuccessful();

        $ws = collect($response->json())->firstWhere('id', $workspace->id);
        expect($ws['settings']['custom_code'])->toBe('<script>tracking</script>');
        expect($ws['settings']['email_settings'] ?? null)->toBeNull();
    });
});


describe('Workspace member authorization', function () {
    it('prevents a readonly member from deleting a workspace they were invited to', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);
        $form = $this->createForm($admin, $workspace);

        $readonly = $this->createUser();
        $this->createUserWorkspace($readonly);
        $workspace->users()->attach($readonly, ['role' => User::ROLE_READONLY]);

        expect($readonly->workspaces()->count())->toBeGreaterThan(1);

        $this->actingAsUser($readonly);

        $this->deleteJson(route('open.workspaces.delete', $workspace))
            ->assertForbidden()
            ->assertJson([
                'message' => 'You need to be an admin of this workspace to do this.',
            ]);

        expect($workspace->fresh())->not->toBeNull();
        expect($form->fresh())->not->toBeNull();
        expect($form->fresh()->workspace_id)->toBe($workspace->id);
    });

    it('prevents a regular member from deleting a workspace they were invited to', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);

        $member = $this->createUser();
        $this->createUserWorkspace($member);
        $workspace->users()->attach($member, ['role' => User::ROLE_USER]);

        expect($member->workspaces()->count())->toBeGreaterThan(1);

        $this->actingAsUser($member);

        $this->deleteJson(route('open.workspaces.delete', $workspace))
            ->assertForbidden()
            ->assertJson([
                'message' => 'You need to be an admin of this workspace to do this.',
            ]);

        expect($workspace->fresh())->not->toBeNull();
    });

    it('prevents a non-member from deleting a workspace', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);

        $outsider = $this->createProUser();
        $this->createUserWorkspace($outsider);
        $this->actingAsUser($outsider);

        $this->deleteJson(route('open.workspaces.delete', $workspace))
            ->assertForbidden();

        expect($workspace->fresh())->not->toBeNull();
    });

    it('allows an admin to delete a workspace they own when they have another workspace', function () {
        $admin = $this->actingAsProUser();
        $workspace = $this->createUserWorkspace($admin);
        $this->createUserWorkspace($admin);

        $this->deleteJson(route('open.workspaces.delete', $workspace))
            ->assertSuccessful()
            ->assertJson([
                'message' => 'Workspace successfully deleted.',
                'workspace_id' => $workspace->id,
            ]);

        expect($workspace->fresh())->toBeNull();
    });

    it('still prevents an admin from deleting their last workspace', function () {
        $admin = $this->actingAsUser();
        $workspace = $this->createUserWorkspace($admin);

        expect($admin->workspaces()->count())->toBe(1);

        $this->deleteJson(route('open.workspaces.delete', $workspace))
            ->assertForbidden()
            ->assertJson([
                'message' => 'You cannot delete your last workspace. Delete your account instead.',
            ]);

        expect($workspace->fresh())->not->toBeNull();
    });

    it('prevents a readonly member from renaming a workspace', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);
        $originalName = $workspace->name;

        $readonly = $this->createUser();
        $workspace->users()->attach($readonly, ['role' => User::ROLE_READONLY]);
        $this->actingAsUser($readonly);

        $this->putJson(route('open.workspaces.update', $workspace), [
            'name' => 'Hijacked Workspace',
            'icon' => '💀',
        ])
            ->assertForbidden()
            ->assertJson([
                'message' => 'You cannot update this workspace.',
            ]);

        expect($workspace->fresh()->name)->toBe($originalName);
    });

    it('allows a regular member to update workspace information', function () {
        $admin = $this->createProUser();
        $workspace = $this->createUserWorkspace($admin);

        $member = $this->createUser();
        $workspace->users()->attach($member, ['role' => User::ROLE_USER]);
        $this->actingAsUser($member);

        $this->putJson(route('open.workspaces.update', $workspace), [
            'name' => 'Shared Workspace',
            'emoji' => '✏️',
        ])
            ->assertSuccessful()
            ->assertJson([
                'message' => 'Workspace updated.',
            ]);

        expect($workspace->fresh()->name)->toBe('Shared Workspace');
        expect($workspace->fresh()->icon)->toBe('✏️');
    });
});