add test for User Resource
This commit is contained in:
parent
50ffc6a456
commit
1e9cb2063b
|
@ -66,7 +66,16 @@ class FileController extends Controller
|
||||||
$receptionScreens = ReceptionScreen::pluck('name', 'id');
|
$receptionScreens = ReceptionScreen::pluck('name', 'id');
|
||||||
$idsReceptionScreens = $receptionScreens->keys()->toArray();
|
$idsReceptionScreens = $receptionScreens->keys()->toArray();
|
||||||
$idReceptionScreen = $file->reception_screen_id;
|
$idReceptionScreen = $file->reception_screen_id;
|
||||||
return view('files.edit', compact('receptionScreens', 'idsReceptionScreens', 'idReceptionScreen', 'files', 'file'));
|
return view(
|
||||||
|
'files.edit',
|
||||||
|
compact(
|
||||||
|
'receptionScreens',
|
||||||
|
'idsReceptionScreens',
|
||||||
|
'idReceptionScreen',
|
||||||
|
'files',
|
||||||
|
'file'
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(UpdateFileRequest $request, File $file)
|
public function update(UpdateFileRequest $request, File $file)
|
||||||
|
|
|
@ -21,7 +21,7 @@ class UserController extends Controller
|
||||||
}
|
}
|
||||||
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
{
|
{
|
||||||
abort_if(!Auth::user('admin'), 403);
|
abort_if(Auth::user()->name !== 'admin', 403);
|
||||||
|
|
||||||
$users = User::all();
|
$users = User::all();
|
||||||
return view('users.index', compact('users'));
|
return view('users.index', compact('users'));
|
||||||
|
@ -29,7 +29,7 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function store(UpdateUserRequest $request): RedirectResponse
|
public function store(UpdateUserRequest $request): RedirectResponse
|
||||||
{
|
{
|
||||||
abort_if(!Auth::user('admin'), 403);
|
abort_if(Auth::user()->name !== 'admin', 403);
|
||||||
|
|
||||||
$validated = $request->validated();
|
$validated = $request->validated();
|
||||||
|
|
||||||
|
@ -44,21 +44,21 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
{
|
{
|
||||||
abort_if(!Auth::user('admin'), 403);
|
abort_if(Auth::user()->name !== 'admin', 403);
|
||||||
|
|
||||||
return view('users.create');
|
return view('users.create');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
{
|
{
|
||||||
abort_if(!Auth::user('admin'), 403);
|
abort_if(Auth::user()->name !== 'admin', 403);
|
||||||
|
|
||||||
return view('users.edit', compact('user'));
|
return view('users.edit', compact('user'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
||||||
{
|
{
|
||||||
abort_if(!Auth::user('admin'), 403);
|
abort_if(Auth::user()->name !== 'admin', 403);
|
||||||
|
|
||||||
$validated = $request->validated();
|
$validated = $request->validated();
|
||||||
|
|
||||||
|
@ -73,8 +73,7 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function destroy(User $user): RedirectResponse
|
public function destroy(User $user): RedirectResponse
|
||||||
{
|
{
|
||||||
abort_if(!Auth::user('admin'), 403);
|
abort_if(Auth::user()->name !== 'admin', 403);
|
||||||
|
|
||||||
$user->delete();
|
$user->delete();
|
||||||
|
|
||||||
return redirect()->route('users.index');
|
return redirect()->route('users.index');
|
||||||
|
|
|
@ -20,6 +20,7 @@ class User extends Authenticatable
|
||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
|
'id',
|
||||||
'name',
|
'name',
|
||||||
'email',
|
'email',
|
||||||
'password',
|
'password',
|
||||||
|
|
|
@ -0,0 +1,134 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class UserTest extends TestCase
|
||||||
|
{
|
||||||
|
private User $adminUser;
|
||||||
|
private User $noAdminUser;
|
||||||
|
private array $user;
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->user = User::factory()->make()->only([
|
||||||
|
'name',
|
||||||
|
'email',
|
||||||
|
'password'
|
||||||
|
]);
|
||||||
|
$this->adminUser = User::factory()->create([
|
||||||
|
'name' => 'admin',
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'password' => 123456
|
||||||
|
]);
|
||||||
|
$this->noAdminUser = User::factory()->create([
|
||||||
|
'name' => 'noadmin',
|
||||||
|
'email' => 'notest@example.com',
|
||||||
|
'password' => 'no123456'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
public function testUsersPage(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->adminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->get(route('users.index'));
|
||||||
|
|
||||||
|
$response->assertOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNoAdminNoSeeUsersPage(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->noAdminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->get(route('users.index'));
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateUserPage(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->adminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->get(route('users.create'));
|
||||||
|
|
||||||
|
$response->assertOk();
|
||||||
|
}
|
||||||
|
public function testNoAdminCreateUserPage(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->noAdminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->get(route('users.create'));
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStoreUser(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->adminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->post(route('users.store', $this->user));
|
||||||
|
|
||||||
|
$response->assertRedirect(route('users.index'));
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('users', $this->user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNoAdminNoStoreUser(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->noAdminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->post(route('users.store', $this->user));
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
|
|
||||||
|
$this->assertDatabaseMissing('users', $this->user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEditUserPage(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->adminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->get(route('users.edit', $this->adminUser));
|
||||||
|
|
||||||
|
$response->assertOk();
|
||||||
|
}
|
||||||
|
public function testNoAdminEditUserPage(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->noAdminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->get(route('users.edit', $this->noAdminUser));
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdateUser(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->adminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->patch(route('users.update', $this->noAdminUser), $this->user);
|
||||||
|
|
||||||
|
$response->assertRedirect(route('users.index'));
|
||||||
|
|
||||||
|
$dataWithOutHashPassword = $this->user;
|
||||||
|
unset($dataWithOutHashPassword['password']);
|
||||||
|
$this->assertDatabaseHas('users', $dataWithOutHashPassword);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNoAdminNoUpdateUser(): void
|
||||||
|
{
|
||||||
|
$response = $this->actingAs($this->noAdminUser)
|
||||||
|
->withSession(['banned' => false])
|
||||||
|
->patch(route('users.update', $this->noAdminUser), $this->user);
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
|
|
||||||
|
$noAdminData = $this->noAdminUser->only(['name', 'email', 'password']);
|
||||||
|
$this->assertDatabaseHas('users', $noAdminData);
|
||||||
|
$this->assertDatabaseMissing('users', $this->user);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||||
|
|
||||||
abstract class TestCase extends BaseTestCase
|
abstract class TestCase extends BaseTestCase
|
||||||
{
|
{
|
||||||
use CreatesApplication;
|
use CreatesApplication;
|
||||||
|
use RefreshDatabase;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue