Moved invitation from jetstream to API; Deactived moved jetstream features

This commit is contained in:
Constantin Graf
2024-07-15 17:17:56 +02:00
committed by Constantin Graf
parent 555417dbbd
commit fd8d596e9b
25 changed files with 294 additions and 348 deletions

View File

@@ -11,14 +11,13 @@ use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\URL;
use Laravel\Jetstream\Mail\TeamInvitation;
use Tests\TestCase;
class InviteTeamMemberTest extends TestCase
{
use RefreshDatabase;
public function test_team_members_can_be_invited_to_team(): void
public function test_team_members_can_no_longer_be_invited_to_team_over_jetstream(): void
{
// Arrange
Mail::fake();
@@ -31,54 +30,12 @@ class InviteTeamMemberTest extends TestCase
]);
// Assert
Mail::assertSent(TeamInvitation::class);
$this->assertCount(1, $user->currentTeam->fresh()->teamInvitations);
$response->assertStatus(403);
$response->assertSee('Moved to API');
Mail::assertNothingSent();
}
public function test_team_member_can_not_be_invited_to_team_if_already_on_team(): void
{
// Arrange
Mail::fake();
$user = User::factory()->withPersonalOrganization()->create();
$existingUser = User::factory()->create();
$user->currentTeam->users()->attach($existingUser, ['role' => 'admin']);
$this->actingAs($user);
// Act
$response = $this->post('/teams/'.$user->currentTeam->id.'/members', [
'email' => $existingUser->email,
'role' => 'admin',
]);
// Assert
$response->assertInvalid(['email'], 'addTeamMember');
Mail::assertNotSent(TeamInvitation::class);
$this->assertCount(0, $user->currentTeam->fresh()->teamInvitations);
}
public function test_team_member_can_be_invited_to_team_if_already_on_team_as_placeholder(): void
{
// Arrange
Mail::fake();
$user = User::factory()->withPersonalOrganization()->create();
$existingUser = User::factory()->create([
'is_placeholder' => true,
]);
$user->currentTeam->users()->attach($existingUser, ['role' => Role::Employee->value]);
$this->actingAs($user);
// Act
$response = $this->post('/teams/'.$user->currentTeam->id.'/members', [
'email' => $existingUser->email,
'role' => Role::Employee->value,
]);
// Assert
Mail::assertSent(TeamInvitation::class);
$this->assertCount(1, $user->currentTeam->fresh()->teamInvitations);
}
public function test_team_member_invitations_can_be_cancelled(): void
public function test_team_member_invitations_can_no_longer_be_cancelled_over_jetstream(): void
{
// Arrange
Mail::fake();
@@ -94,7 +51,8 @@ class InviteTeamMemberTest extends TestCase
$response = $this->delete('/team-invitations/'.$invitation->id);
// Assert
$this->assertCount(0, $user->currentTeam->fresh()->teamInvitations);
$response->assertStatus(403);
$this->assertCount(1, $user->currentTeam->fresh()->teamInvitations);
}
public function test_team_member_invitations_can_be_accepted(): void
@@ -153,6 +111,7 @@ class InviteTeamMemberTest extends TestCase
$response = $this->get($acceptUrl);
// Assert
$response->assertRedirect();
$user->refresh();
$this->assertDatabaseMissing(User::class, ['id' => $placeholder->id]);
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);

View File

@@ -12,8 +12,9 @@ class LeaveTeamTest extends TestCase
{
use RefreshDatabase;
public function test_users_can_leave_teams(): void
public function test_users_can_no_longer_leave_team_over_jetstream(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$user->currentTeam->users()->attach(
@@ -22,19 +23,11 @@ class LeaveTeamTest extends TestCase
$this->actingAs($otherUser);
// Act
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$this->assertCount(1, $user->currentTeam->fresh()->users);
}
public function test_team_owners_cant_leave_their_own_team(): void
{
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$user->id);
$response->assertSessionHasErrorsIn('removeTeamMember', ['team']);
$this->assertNotNull($user->currentTeam->fresh());
// Assert
$response->assertStatus(403);
$this->assertCount(2, $user->currentTeam->fresh()->users);
}
}

View File

@@ -12,31 +12,20 @@ class RemoveTeamMemberTest extends TestCase
{
use RefreshDatabase;
public function test_team_members_can_be_removed_from_teams(): void
public function test_team_members_can_no_longer_be_removed_from_teams_over_jetstream_endpoints(): void
{
// Arrange
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
$user->currentTeam->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
$response = $this->withoutExceptionHandling()->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
// Act
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$this->assertCount(1, $user->currentTeam->fresh()->users);
}
public function test_only_team_owner_can_remove_team_members(): void
{
$user = User::factory()->withPersonalOrganization()->create();
$user->currentTeam->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
$this->actingAs($otherUser);
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$user->id);
$response->assertForbidden();
// Assert
$response->assertStatus(403);
$response->assertSee('Moved to API');
}
}

View File

@@ -13,7 +13,7 @@ class UpdateTeamMemberRoleTest extends TestCase
{
use RefreshDatabase;
public function test_team_member_roles_can_be_updated(): void
public function test_team_member_roles_can_no_longer_be_updated_over_jetstream(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
@@ -28,73 +28,8 @@ class UpdateTeamMemberRoleTest extends TestCase
'role' => Role::Employee->value,
]);
// Assert
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), Role::Employee->value,
));
}
public function test_team_member_roles_can_not_be_updated_to_placeholder(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$user->currentTeam->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id, [
'role' => 'placeholder',
]);
// Assert
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), 'admin'
));
}
public function test_team_member_roles_can_be_updated_to_owner_which_changes_ownership(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$otherUser = User::factory()->create();
$user->currentTeam->users()->attach($otherUser, ['role' => 'admin']);
// Act
$response = $this->withoutExceptionHandling()->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->getKey(), [
'role' => Role::Owner->value,
]);
// Assert
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), Role::Owner->value
));
$this->assertSame($user->currentTeam->fresh()->user_id, $otherUser->getKey());
}
public function test_only_team_owner_can_update_team_member_roles(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$user->currentTeam->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
$this->actingAs($otherUser);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id, [
'role' => Role::Employee->value,
]);
// Assert
$response->assertStatus(403);
$this->assertTrue($otherUser->fresh()->hasTeamRole(
$user->currentTeam->fresh(), 'admin'
));
$response->assertSee('Moved to API');
}
}

View File

@@ -6,9 +6,11 @@ namespace Tests\Unit\Endpoint\Api\V1;
use App\Enums\Role;
use App\Http\Controllers\Api\V1\InvitationController;
use App\Mail\OrganizationInvitationMail;
use App\Models\Member;
use App\Models\OrganizationInvitation;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Laravel\Jetstream\Mail\TeamInvitation;
use Laravel\Passport\Passport;
use PHPUnit\Framework\Attributes\UsesClass;
@@ -107,6 +109,74 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
$response->assertJsonPath('message', 'The selected role is invalid.');
}
public function test_store_fails_if_user_invites_user_who_is_already_member_of_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'invitations:create',
]);
Passport::actingAs($data->user);
$member = Member::factory()->forOrganization($data->organization)->create();
// Act
$response = $this->postJson(route('api.v1.invitations.store', $data->organization->getKey()), [
'email' => $member->user->email,
'role' => Role::Employee->value,
]);
// Assert
$response->assertStatus(400);
$response->assertJsonPath('message', 'User is already a member of the organization');
}
public function test_store_fails_if_user_invites_user_who_is_already_invited_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'invitations:create',
]);
Passport::actingAs($data->user);
$invitation = OrganizationInvitation::factory()->forOrganization($data->organization)->create();
// Act
$response = $this->postJson(route('api.v1.invitations.store', $data->organization->getKey()), [
'email' => $invitation->email,
'role' => Role::Employee->value,
]);
// Assert
$response->assertInvalid([
'email' => 'The email has already been invited to the organization. Please wait for the user to accept the invitation or resend the invitation email.',
]);
$response->assertStatus(422);
}
public function test_store_works_if_user_invites_user_who_is_also_a_placeholder(): void
{
// Arrange
$data = $this->createUserWithPermission([
'invitations:create',
]);
$user = User::factory()->placeholder()->create();
$member = Member::factory()->forOrganization($data->organization)->forUser($user)->role(Role::Placeholder)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.invitations.store', $data->organization->getKey()), [
'email' => $user->email,
'role' => Role::Employee->value,
]);
// Assert
$response->assertStatus(204);
$invitation = OrganizationInvitation::first();
$this->assertNotNull($invitation);
$this->assertEquals($user->email, $invitation->email);
$this->assertEquals(Role::Employee->value, $invitation->role);
Mail::assertQueued(fn (OrganizationInvitationMail $mail): bool => $mail->invitation->is($invitation));
Mail::assertNothingSent();
}
public function test_store_invites_user_to_organization(): void
{
// Arrange
@@ -127,6 +197,8 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
$this->assertNotNull($invitation);
$this->assertEquals('test@asdf.at', $invitation->email);
$this->assertEquals(Role::Employee->value, $invitation->role);
Mail::assertQueued(fn (OrganizationInvitationMail $mail): bool => $mail->invitation->is($invitation));
Mail::assertNothingSent();
}
public function test_resend_fails_if_user_has_no_permission_to_resend_the_invitation(): void
@@ -182,9 +254,9 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
]));
// Assert
Mail::assertSent(fn (TeamInvitation $mail): bool => $mail->invitation->is($invitation));
Mail::assertNothingQueued();
$response->assertStatus(204);
Mail::assertQueued(fn (OrganizationInvitationMail $mail): bool => $mail->invitation->is($invitation));
Mail::assertNothingSent();
}
public function test_delete_fails_if_user_has_no_permission_to_remove_invitations(): void

View File

@@ -280,12 +280,11 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
{
$data = $this->createUserWithPermission([
'members:invite-placeholder',
'invitations:create',
]);
$user = User::factory()->create([
'is_placeholder' => true,
]);
$member = Member::factory()->forUser($user)->forOrganization($data->organization)->create();
$member = Member::factory()->forUser($user)->forOrganization($data->organization)->role(Role::Placeholder)->create();
Passport::actingAs($data->user);
// Act

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mail;
use App\Mail\OrganizationInvitationMail;
use App\Models\Organization;
use App\Models\OrganizationInvitation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCaseWithDatabase;
#[CoversClass(OrganizationInvitationMail::class)]
#[UsesClass(OrganizationInvitationMail::class)]
class OrganizationInvitationMailTest extends TestCaseWithDatabase
{
public function test_mail_renders_content_correctly(): void
{
// Arrange
$organization = Organization::factory()->create();
$invitation = OrganizationInvitation::factory()->forOrganization($organization)->create();
$mail = new OrganizationInvitationMail($invitation);
// Act
$rendered = $mail->render();
// Assert
$this->assertStringContainsString('You have been invited to join the '.$invitation->organization->name.' organization', $rendered);
}
}