auditionadmin/tests/Feature/app/Http/Controllers/Admin/UserControllerTest.php

221 lines
9.2 KiB
PHP

<?php
use App\Mail\NewUserPassword;
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->user = User::factory()->create();
});
afterEach(function () {
Mockery::close();
});
describe('UserController::index', function () {
it('denies access to a non-admin user', function () {
$this->get(route('admin.users.index'))->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.users.index'))->assertRedirect(route('dashboard'));
actAsTab();
$this->get(route('admin.users.index'))->assertRedirect(route('dashboard'));
});
it('allows access for an admin user', function () {
actAsAdmin();
$users = User::factory()->count(5)->create();
$response = $this->get(route('admin.users.index'));
$response->assertOk()->assertViewIs('admin.users.index')->assertViewHas('users');
// Check if each $users is in the array of users sent to the view
$userIdsSentToView = $response->viewData('users')->pluck('id')->toArray();
expect(in_array($this->user->id, $userIdsSentToView))->toBeTrue();
foreach ($users as $user) {
expect(in_array($user->id, $userIdsSentToView))->toBeTrue();
}
});
});
describe('UserController::edit', function () {
it('denies access to a non-admin user', function () {
$this->get(route('admin.users.edit', $this->user))->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.users.edit', $this->user))->assertRedirect(route('dashboard'));
actAsTab();
$this->get(route('admin.users.edit', $this->user))->assertRedirect(route('dashboard'));
});
it('allows access for an admin user', function () {
$schools = School::factory()->count(5)->create();
actAsAdmin();
$response = $this->get(route('admin.users.edit', $this->user));
$response->assertOk()->assertViewIs('admin.users.edit')->assertViewHas(['schools', 'user']);
expect($response->viewData('user')->id)->toEqual($this->user->id);
foreach ($schools as $school) {
expect(in_array($school->id, $response->viewData('schools')->pluck('id')->toArray()))->toBeTrue();
}
});
});
describe('UserController::create', function () {
it('denies access to a non-admin user', function () {
$this->get(route('admin.users.create'))->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.users.create'))->assertRedirect(route('dashboard'));
actAsTab();
$this->get(route('admin.users.create'))->assertRedirect(route('dashboard'));
});
it('allows access for an admin user', function () {
actAsAdmin();
$schools = School::factory()->count(5)->create();
$response = $this->get(route('admin.users.create'));
$response->assertOk()->assertViewIs('admin.users.create')->assertViewHas(['schools']);
foreach ($schools as $school) {
expect(in_array($school->id, $response->viewData('schools')->pluck('id')->toArray()))->toBeTrue();
}
});
});
describe('UserController::update', function () {
beforeEach(function () {
$this->oldSchool = School::factory()->create();
$this->newSchool = School::factory()->create();
$this->oldUser = User::create([
'first_name' => 'Old',
'last_name' => 'Name',
'email' => 'picard@starfleet.com',
'cell_phone' => '1701',
'judging_preference' => 'light counting',
'school_id' => $this->oldSchool->id,
'password' => \Illuminate\Support\Facades\Hash::make('password'),
]);
});
it('denies access to a non-admin user', function () {
$this->patch(route('admin.users.update', $this->user))->assertRedirect(route('home'));
actAsNormal();
$this->patch(route('admin.users.update', $this->user))->assertRedirect(route('dashboard'));
actAsTab();
$this->patch(route('admin.users.update', $this->user))->assertRedirect(route('dashboard'));
});
it('updates user profile information', function () {
actAsAdmin();
$response = $this->patch(route('admin.users.update', $this->oldUser), [
'first_name' => 'New',
'last_name' => 'Family',
'email' => 'skywalker@rebellion.org',
'cell_phone' => '555-555-5555',
'judging_preference' => 'light sabers',
'school_id' => $this->newSchool->id,
]);
//file_put_contents(storage_path('debug.html'), $response->getContent());
$response->assertRedirect(route('admin.users.index'));
$this->oldUser->refresh();
expect($this->oldUser->first_name)->toBe('New')
->and($this->oldUser->last_name)->toBe('Family')
->and($this->oldUser->email)->toBe('skywalker@rebellion.org')
->and($this->oldUser->cell_phone)->toBe('555-555-5555')
->and($this->oldUser->judging_preference)->toBe('light sabers')
->and($this->oldUser->school_id)->toBe($this->newSchool->id);
});
it('assigns privileges to a user', function () {
actAsAdmin();
$this->patch(route('admin.users.update', $this->oldUser), [
'first_name' => 'Jean Luc',
'last_name' => 'Picard',
'email' => 'skywalker@rebellion.org',
'cell_phone' => '1701',
'judging_preference' => 'light sabers',
'school_id' => $this->newSchool->id,
'is_admin' => 'on',
'is_tab' => 'on',
'head_director' => 'on',
]);
//file_put_contents(storage_path('debug.html'), $response->getContent());
$this->oldUser->refresh();
expect($this->oldUser->is_admin)->toBeTruthy()
->and($this->oldUser->is_tab)->toBeTruthy();
$this->patch(route('admin.users.update', $this->oldUser), [
'first_name' => 'Luke',
'last_name' => 'Skywalker',
'email' => 'skywalker@rebellion.org',
'cell_phone' => '555-555-5555',
'judging_preference' => 'light sabers',
'school_id' => $this->newSchool->id,
]);
$this->oldUser->refresh();
expect($this->oldUser->is_admin)->toBeFalsy()
->and($this->oldUser->is_tab)->toBeFalsy();
});
});
describe('UserController::store', function () {
it('denies access to a non-admin user', function () {
$this->post(route('admin.users.store', $this->user))->assertRedirect(route('home'));
actAsNormal();
$this->post(route('admin.users.store', $this->user))->assertRedirect(route('dashboard'));
actAsTab();
$this->post(route('admin.users.store', $this->user))->assertRedirect(route('dashboard'));
});
it('creates a new user', function () {
actAsAdmin();
$school = School::factory()->create();
$response = $this->post(route('admin.users.store', [
'first_name' => 'Jean Luc',
'last_name' => 'Picard',
'email' => 'picard@starfleet.com',
'cell_phone' => '1701',
'judging_preference' => 'light counting',
'school_id' => $school->id,
]));
//file_put_contents(storage_path('debug.html'), $response->getContent());
$response->assertRedirect(route('admin.users.index'));
$user = User::orderBy('id', 'desc')->first();
expect($user->first_name)->toBe('Jean Luc')
->and($user->last_name)->toBe('Picard')
->and($user->email)->toBe('picard@starfleet.com')
->and($user->cell_phone)->toBe('1701')
->and($user->judging_preference)->toBe('light counting')
->and($user->school->id)->toBe($school->id);
});
it('sends an email upon user creation', function () {
Mail::fake();
actAsAdmin();
$school = School::factory()->create();
$this->post(route('admin.users.store', [
'first_name' => 'Jean Luc',
'last_name' => 'Picard',
'email' => 'picard@starfleet.com',
'cell_phone' => '1701',
'judging_preference' => 'light counting',
'school_id' => $school->id,
]));
Mail::assertSent(NewUserPassword::class, function ($mail) {
return $mail->hasTo('picard@starfleet.com');
});
});
});
describe('UserController::destroy', function () {
it('denies access to a non-admin user', function () {
$this->delete(route('admin.users.destroy', $this->user))->assertRedirect(route('home'));
actAsNormal();
$this->delete(route('admin.users.destroy', $this->user))->assertRedirect(route('dashboard'));
actAsTab();
$this->delete(route('admin.users.destroy', $this->user))->assertRedirect(route('dashboard'));
});
it('deletes a user', function () {
actAsAdmin();
$response = $this->delete(route('admin.users.destroy', $this->user));
$response->assertRedirect(route('admin.users.index'));
$response->assertSessionHas('success', 'User deleted successfully');
expect(User::where('id', $this->user->id)->exists())->toBeFalsy();
});
});