add new role functionality
This commit is contained in:
52
tests/Feature/StoreRoleTest.php
Normal file
52
tests/Feature/StoreRoleTest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
|
||||
test('guests cannot access the create role page', function () {
|
||||
$this->get('/roles/create')->assertRedirect('/login');
|
||||
});
|
||||
|
||||
test('authenticated users can access the create role page', function () {
|
||||
$this->actingAs(User::factory()->create())
|
||||
->get('/roles/create')
|
||||
->assertOk()
|
||||
->assertViewIs('roles.create');
|
||||
});
|
||||
|
||||
test('authenticated users can create a role', function () {
|
||||
$this->actingAs(User::factory()->create())
|
||||
->post('/roles', ['name' => 'Manager', 'description' => 'Manages things'])
|
||||
->assertRedirect(route('roles.index'))
|
||||
->assertSessionHas('status', 'role-created');
|
||||
|
||||
$this->assertDatabaseHas('roles', ['name' => 'Manager', 'description' => 'Manages things']);
|
||||
});
|
||||
|
||||
test('role name is required', function () {
|
||||
$this->actingAs(User::factory()->create())
|
||||
->post('/roles', ['name' => '', 'description' => 'Some description'])
|
||||
->assertSessionHasErrors('name');
|
||||
});
|
||||
|
||||
test('role name must be unique', function () {
|
||||
Role::factory()->create(['name' => 'Admin']);
|
||||
|
||||
$this->actingAs(User::factory()->create())
|
||||
->post('/roles', ['name' => 'Admin'])
|
||||
->assertSessionHasErrors('name');
|
||||
});
|
||||
|
||||
test('description is optional', function () {
|
||||
$this->actingAs(User::factory()->create())
|
||||
->post('/roles', ['name' => 'Viewer'])
|
||||
->assertRedirect(route('roles.index'));
|
||||
|
||||
$this->assertDatabaseHas('roles', ['name' => 'Viewer', 'description' => null]);
|
||||
});
|
||||
|
||||
test('guests cannot create a role', function () {
|
||||
$this->post('/roles', ['name' => 'Admin'])->assertRedirect('/login');
|
||||
|
||||
$this->assertDatabaseMissing('roles', ['name' => 'Admin']);
|
||||
});
|
||||
Reference in New Issue
Block a user