Write tests - Write tests for what was done to this point that will be kept #11

Merged
okorpheus merged 61 commits from write-tests into master 2024-07-05 21:21:32 +00:00
5 changed files with 61 additions and 8 deletions
Showing only changes of commit a2ba06898f - Show all commits

View File

@ -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');
}
}

View File

@ -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 }}" />

View File

@ -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>

View File

@ -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();
});

View File

@ -10,13 +10,13 @@ use function Pest\Laravel\get;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->adminUser = User::factory()->admin()->create();
$this->adminUser = User::factory()->admin()->create();
$this->nonAdminUser = User::factory()->create();
$this->tabUser = User::factory()->tab()->create();
$this->users = User::factory(3)->create();
$this->schools = [];
$this->tabUser = User::factory()->tab()->create();
$this->users = User::factory(3)->create();
$this->schools = [];
foreach ($this->users as $user) {
$school = School::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
}