Admin UserEditPage test
This commit is contained in:
parent
396a1dcc07
commit
a2ba06898f
|
|
@ -79,7 +79,7 @@ class UserController extends Controller
|
|||
'email' => ['required', 'email', 'unique:users'],
|
||||
]);
|
||||
|
||||
// Genearte a random password
|
||||
// Generate a random password
|
||||
$randomPassword = Str::random(12);
|
||||
|
||||
$user = \App\Models\User::make([
|
||||
|
|
@ -103,4 +103,13 @@ class UserController extends Controller
|
|||
|
||||
return redirect('/admin/users');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, User $user)
|
||||
{
|
||||
if (! Auth::user()->is_admin) {
|
||||
abort(403);
|
||||
}
|
||||
$user->delete();
|
||||
return redirect()->route('admin.users.index')->with('success', 'User deleted successfully');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
<x-layout.app>
|
||||
<x-card.card class="mx-auto max-w-lg">
|
||||
<x-card.heading>Edit User</x-card.heading>
|
||||
<x-card.heading>
|
||||
Edit User
|
||||
<x-slot:right_side>
|
||||
@if($user->id != Auth::user()->id)
|
||||
<form method="POST" action="{{ route('admin.users.destroy',['user' => $user->id]) }}">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<x-form.red-trash-button type="submit" />
|
||||
</form>
|
||||
@endif
|
||||
</x-slot:right_side>
|
||||
</x-card.heading>
|
||||
<x-form.form method="PATCH" action="{{ route('admin.users.update', $user) }}">
|
||||
<x-form.body-grid>
|
||||
<x-form.field name="first_name" label_text="First Name" colspan="3" value="{{ $user->first_name }}" />
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<x-card.card>
|
||||
<x-table.table with_title_area>
|
||||
<x-slot:title class="ml-3">Users</x-slot:title>
|
||||
<x-slot:subtitle class="ml-3">Click name to edit</x-slot:subtitle>
|
||||
<x-slot:subtitle class="ml-3">Click name to edit or delete</x-slot:subtitle>
|
||||
<x-slot:title_block_right class="mr-3">
|
||||
<x-form.button href="{{ route('admin.users.create') }}">New User</x-form.button>
|
||||
</x-slot:title_block_right>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use App\Models\User;
|
|||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
use function Pest\Laravel\actingAs;
|
||||
use function Pest\Laravel\delete;
|
||||
use function Pest\Laravel\get;
|
||||
use function Pest\Laravel\patch;
|
||||
|
||||
|
|
@ -168,3 +169,35 @@ it('allows an administrator to modify a user', function () {
|
|||
->assertSee($newData['judging_preference'])
|
||||
->assertSee($newSchool->name);
|
||||
});
|
||||
it('has a delete link for the user if not the current user', function () {
|
||||
// Arrange
|
||||
actingAs($this->adminUser);
|
||||
// Act & Assert
|
||||
$response = get(route('admin.users.edit', $this->users[0]));
|
||||
$response->assertOk();
|
||||
$response->assertSee(route('admin.users.destroy', $this->users[0]));
|
||||
$response->assertSee('<input type="hidden" name="_method" value="DELETE">', false);
|
||||
});
|
||||
it('does not show a delete link for the current user', function () {
|
||||
// Arrange
|
||||
actingAs($this->adminUser);
|
||||
// Act & Assert
|
||||
$response = get(route('admin.users.edit', $this->adminUser));
|
||||
$response->assertOk();
|
||||
$response->assertDontSee('<input type="hidden" name="_method" value="DELETE">', false);
|
||||
});
|
||||
it('allows an administrator to destroy a user', function () {
|
||||
// Arrange
|
||||
$newUser = User::factory()->create();
|
||||
actingAs($this->adminUser);
|
||||
// Act & Assert
|
||||
assert($newUser->exists());
|
||||
$response = delete(route('admin.users.destroy', $newUser));
|
||||
/** @noinspection PhpUnhandledExceptionInspection */
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertSessionHas('success', 'User deleted successfully')
|
||||
->assertRedirect(route('admin.users.index'));
|
||||
|
||||
expect(User::find($newUser->id))->toBeNull();
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue