Skip to content

Beautiful Open-Source Form Builder

5.3 KB
Raw Blame
<?php

use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Mail;
use App\Mail\UserInvitationEmail;

beforeEach(function () {
    $this->user = $this->actingAsProUser();
    $this->workspace = Workspace::factory()->create();
    $this->workspace->users()->attach($this->user, ['role' => 'admin']);
});

it('can list users in a workspace', function () {

    $this->getJson(route('open.workspaces.users.index', ['workspace' => $this->workspace]))
        ->assertSuccessful()
        ->assertJsonCount(1);
});

it('can add a user to a workspace', function () {
    $newUser = User::factory()->create(['email' => '[email protected]']);
    $this->postJson(route('open.workspaces.users.add', ['workspace' => $this->workspace]), [
        'email' => $newUser->email,
        'role' => 'user',
    ])
        ->assertSuccessful()
        ->assertJson([
            'message' => 'User has been successfully added to workspace.'
        ]);

    expect($this->workspace->users()->count())->toBe(2);
});

it('can send an invitation email to a non-existing user', function () {
    Mail::fake();

    $this->postJson(route('open.workspaces.users.add', ['workspace' => $this->workspace]), [
        'email' => '[email protected]',
        'role' => 'user',
    ])
        ->assertSuccessful()
        ->assertJson([
            'message' => 'Registration invitation email sent to user.'
        ]);

    Mail::assertQueued(UserInvitationEmail::class);
});

it('can update user role in a workspace', function () {
    $existingUser = User::factory()->create();
    $this->workspace->users()->attach($existingUser, ['role' => 'user']);

    $this->putJson(route('open.workspaces.users.update-role', [
        'workspace' => $this->workspace,
        'user' => $existingUser
    ]), [
        'role' => 'admin',
    ])
        ->assertSuccessful()
        ->assertJson([
            'message' => 'User role changed successfully.'
        ]);
});

it('returns 404 when updating role for a user outside the workspace', function () {
    $outsideUser = User::factory()->create();

    $this->putJson(route('open.workspaces.users.update-role', [
        'workspace' => $this->workspace,
        'user' => $outsideUser
    ]), [
        'role' => 'admin',
    ])->assertStatus(404);

    expect($this->workspace->users()->whereKey($outsideUser->id)->exists())->toBeFalse();
});

it('can remove a user from a workspace', function () {
    $existingUser = User::factory()->create();
    $this->workspace->users()->attach($existingUser);

    $this->deleteJson(route('open.workspaces.users.remove', [
        'workspace' => $this->workspace,
        'user' => $existingUser
    ]))
        ->assertSuccessful()
        ->assertJson([
            'message' => 'User removed from workspace successfully.'
        ]);

    expect($this->workspace->users()->count())->toBe(1);
});

it('returns 404 when removing a user outside the workspace', function () {
    $outsideUser = User::factory()->create();

    $this->deleteJson(route('open.workspaces.users.remove', [
        'workspace' => $this->workspace,
        'user' => $outsideUser
    ]))->assertStatus(404);

    expect($this->workspace->users()->whereKey($outsideUser->id)->exists())->toBeFalse();
});

it('can leave a workspace when another administrator remains', function () {
    $remainingAdmin = User::factory()->create();
    $this->workspace->users()->attach($remainingAdmin, ['role' => User::ROLE_ADMIN]);
    $this->postJson(route('open.workspaces.leave', ['workspace' => $this->workspace]))
        ->assertSuccessful()
        ->assertJson([
            'message' => 'You have left the workspace successfully.'
        ]);

    expect($this->workspace->users()->count())->toBe(1);
    expect($this->workspace->users()->whereKey($remainingAdmin->id)->exists())->toBeTrue();
});

it('enforces appsumo license member limits when inviting users', function () {
    $licensedOwner = $this->createAppSumoLicensedUser(1);
    $this->actingAs($licensedOwner);
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($licensedOwner, ['role' => 'admin']);
    $workspace->load('users');
    $workspace->flush();

    $newUser = User::factory()->create(['email' => '[email protected]']);

    $this->postJson(route('open.workspaces.users.add', ['workspace' => $workspace]), [
        'email' => $newUser->email,
        'role' => 'user',
    ])
        ->assertStatus(403)
        ->assertJson([
            'message' => 'You have reached the maximum number of users allowed with your license.',
        ]);

    expect($workspace->fresh()->users()->count())->toBe(1);
});

it('allows inviting users when the workspace has an invite_user override', function () {
    Mail::fake();

    $owner = $this->createUser();
    $this->actingAs($owner);
    $workspace = Workspace::factory()->create([
        'plan_overrides' => ['features' => ['invite_user']],
    ]);
    $workspace->users()->attach($owner, ['role' => 'admin']);
    $workspace->flush();
    $owner->flush();

    $this->postJson(route('open.workspaces.users.add', ['workspace' => $workspace]), [
        'email' => '[email protected]',
        'role' => 'user',
    ])
        ->assertSuccessful()
        ->assertJson([
            'message' => 'Registration invitation email sent to user.',
        ]);

    Mail::assertQueued(UserInvitationEmail::class);
});