Skip to content

Commit

Permalink
refactor: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
otaaaviio committed Jun 22, 2024
1 parent ed32d4c commit c2342b4
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 74 deletions.
7 changes: 5 additions & 2 deletions resources/js/stores/guilds.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ export const guilds = {
state.currentGuild = null;
toast.success('Left guild successfully');
})
.catch(() => {
toast.error('An error occurred');
.catch((err) => {
if(err.response?.status === 401)
toast.error('You are the only admin, you cannot leave the guild');
else
toast.error('An error occurred');
});
}
},
Expand Down
27 changes: 23 additions & 4 deletions tests/Integration/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
uses()->group('Auth Test');

test('can login in system', function () {
// arrange
$user = User::factory()->create();

// act
$response = $this->postJson('api/auth/login', [
'email' => $user->email,
'password' => 'password',
]);

// assert
$response->assertStatus(StatusCode::HTTP_OK);
$response->assertJsonStructure([
'message',
Expand All @@ -30,32 +33,43 @@
});

test('cannot login with invalid credentials', function () {
// arrange
User::factory()->make();

// act
$response = $this->postJson('api/auth/login', [
'email' => '[email protected]',
'password' => 'password',
]);

// assert
$response->assertStatus(StatusCode::HTTP_UNAUTHORIZED);
});

test('can logout', function () {
// arrange
$user = User::factory()->create();

// act
$response = $this->actingAs($user)->postJson('api/auth/logout');

// assert
$response->assertStatus(StatusCode::HTTP_OK);
});

test('can register a new user', function () {
// arrange
$payload = [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'password' => 'password',
'password_confirmation' => 'password',
];

// act
$response = $this->postJson('api/auth/register', $payload);

// assert
$response->assertStatus(StatusCode::HTTP_CREATED);
$response->assertJsonStructure([
'user',
Expand All @@ -64,10 +78,13 @@
});

test('should get user authenticated', function () {
// arrange
$user = User::factory()->create();

// act
$response = $this->actingAs($user)->getJson('api/auth/user');

// assert
$response->assertStatus(StatusCode::HTTP_OK);
$response->assertJson([
'user' => [
Expand All @@ -79,12 +96,15 @@
});

test('cannot access user authenticated route without authentication', function () {
// act
$response = $this->getJson('api/auth/user');

// assert
$response->assertStatus(StatusCode::HTTP_UNAUTHORIZED);
});

test('should send a email to new user', function () {
// arrange
Queue::fake();

$payload = [
Expand All @@ -94,10 +114,9 @@
'password_confirmation' => 'password',
];

$res = $this->postJson('api/auth/register', $payload);

$res->assertStatus(StatusCode::HTTP_CREATED);

// act & assert
$this->postJson('api/auth/register', $payload)
->assertStatus(StatusCode::HTTP_CREATED);
Queue::assertPushed(SendWelcomeMail::class, function ($job) use ($payload) {
return $job->user->email === $payload['email'];
});
Expand Down
14 changes: 11 additions & 3 deletions tests/Integration/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
uses()->group('Channel Test');

test('should register a new channel in a existing guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);

// act & assert
$this->actingAs($user)
->postJson('api/guilds/'.$guild->id.'/channels', [
'name' => 'Channel Test',
Expand All @@ -34,13 +36,15 @@
});

test('should att a channel', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);
$channel = Channel::factory()->create([
'guild_id' => $guild->id,
]);

// act & assert
$this->actingAs($user)
->putJson('api/guilds/'.$guild->id.'/channels/'.$channel->id, [
'name' => 'Channel Test',
Expand All @@ -58,13 +62,15 @@
});

test('should delete a channel', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);
$channel = Channel::factory()->create([
'guild_id' => $guild->id,
]);

// act & assert
$this->actingAs($user)
->deleteJson('api/guilds/'.$guild->id.'/channels/'.$channel->id, [
'name' => 'Channel Test',
Expand All @@ -77,8 +83,10 @@
});

test('cannot manage a non existing channel', function () {
// arrange
$user = User::factory()->create();

// act & assert
$this->actingAs($user)
->deleteJson('api/guilds/'. 99999 .'/channels/'. 99999, [
'name' => 'Channel Test',
Expand All @@ -87,14 +95,14 @@
});

test('should join a channel', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Member]);

$channel = Channel::factory()->create([
'guild_id' => $guild->id,
]);
$channel = Channel::factory()->create(['guild_id' => $guild->id]);

// act & assert
$this->actingAs($user)
->getJson('api/guilds/'.$guild->id.'/channels/'.$channel->id)
->assertStatus(StatusCode::HTTP_OK)
Expand Down
51 changes: 33 additions & 18 deletions tests/Integration/GuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
uses()->group('Guild Test');

test('should create a new Guild', function () {
// arrange
$user = User::factory()->create();
$payload = [
'name' => $this->faker->name,
'description' => $this->faker->sentence,
'icon' => $this->faker->imageUrl(),
];

$res = $this->actingAs($user)->postJson('/api/guilds', $payload);

$res->assertStatus(StatusCode::HTTP_CREATED)
// act & assert
$this->actingAs($user)->postJson('/api/guilds', $payload)
->assertStatus(StatusCode::HTTP_CREATED)
->assertJsonStructure([
'guild' => [
'id',
Expand All @@ -36,6 +37,7 @@
});

test('should update a Guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);
Expand All @@ -46,7 +48,8 @@
'icon' => $this->faker->imageUrl(),
];

$this->actingAs($user)->putJson('/api/guilds/'.$guild->id, $payload)
// act & assert
$this->actingAs($user)->putJson('/api/guilds/' . $guild->id, $payload)
->assertStatus(StatusCode::HTTP_OK)
->assertJsonStructure([
'message',
Expand All @@ -60,11 +63,13 @@
});

test('should return a specific a Guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);

$this->actingAs($user)->getJson('/api/guilds/'.$guild->id)
// act & assert
$this->actingAs($user)->getJson('/api/guilds/' . $guild->id)
->assertStatus(StatusCode::HTTP_OK)
->assertJsonStructure([
'guild' => [
Expand All @@ -79,10 +84,12 @@
});

test('should return authenticated user guilds', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Member]);

// act & assert
$this->actingAs($user)->getJson('/api/guilds')
->assertStatus(StatusCode::HTTP_OK)
->assertJsonStructure([
Expand All @@ -99,11 +106,13 @@
});

test('should return all guilds acting as admin user', function () {
// arrange
$user = User::factory([
'is_super_admin' => true,
])->create();
Guild::factory(5)->create();

// act & assert
$this->actingAs($user)->getJson('/api/allGuilds')
->assertStatus(StatusCode::HTTP_OK)
->assertJsonStructure([
Expand All @@ -119,32 +128,36 @@
});

test('cannot return all guilds acting as non-admin user', function () {
// arrange
$user = User::factory(
['is_super_admin' => false]
)->create();
Guild::factory(5)->create();

// act & assert
$this->actingAs($user)->getJson('/api/allGuilds')
->assertStatus(StatusCode::HTTP_UNAUTHORIZED)
->assertJson(['message' => 'Unauthorized']);
});

test('should return guild invite code', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Member]);

$this->actingAs($user)->getJson('/api/guilds/inviteCode/'.$guild->id)
// act & assert
$this->actingAs($user)->getJson('/api/guilds/inviteCode/' . $guild->id)
->assertStatus(StatusCode::HTTP_OK)
->assertJson([
'invite_code' => $guild->invite_code,
]);
->assertJson(['invite_code' => $guild->invite_code]);
});

test('should entry into a Guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();

// act & assert
$this->actingAs($user)->postJson('/api/guilds/entry/', ['invite_code' => $guild->invite_code])
->assertStatus(StatusCode::HTTP_OK)
->assertJsonStructure([
Expand All @@ -159,37 +172,39 @@
});

test('should delete a Guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);

$this->actingAs($user)->deleteJson('/api/guilds/'.$guild->id)
// act & assert
$this->actingAs($user)->deleteJson('/api/guilds/' . $guild->id)
->assertStatus(StatusCode::HTTP_OK)
->assertJson([
'message' => 'Guild successfully deleted',
]);
});

test('should leave a Guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Member]);

$this->actingAs($user)->postJson('/api/guilds/leave/'.$guild->id)
// act & assert
$this->actingAs($user)->postJson('/api/guilds/leave/' . $guild->id)
->assertStatus(StatusCode::HTTP_OK)
->assertJsonStructure([
'message',
]);
->assertJsonStructure(['message']);
});

test('an admin cannot leave their Guild', function () {
// arrange
$user = User::factory()->create();
$guild = Guild::factory()->create();
$guild->members()->attach($user->id, ['role' => Role::Admin]);

$this->actingAs($user)->postJson('/api/guilds/leave/'.$guild->id)
// act & assert
$this->actingAs($user)->postJson('/api/guilds/leave/' . $guild->id)
->assertStatus(StatusCode::HTTP_UNAUTHORIZED)
->assertJsonStructure([
'message',
]);
->assertJsonStructure(['message']);
});
Loading

0 comments on commit c2342b4

Please sign in to comment.