From 1e9cb2063bea0dff605779c3343ece0a2307afd7 Mon Sep 17 00:00:00 2001 From: aslan Date: Mon, 29 Jan 2024 09:44:53 +0300 Subject: [PATCH] add test for User Resource --- app/Http/Controllers/FileController.php | 11 +- app/Http/Controllers/UserController.php | 13 ++- app/Models/User.php | 1 + app/Services/PageScrapper.php | 2 +- tests/Feature/UserTest.php | 134 ++++++++++++++++++++++++ tests/TestCase.php | 2 + 6 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 tests/Feature/UserTest.php diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index 00813aa..897ee65 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -66,7 +66,16 @@ class FileController extends Controller $receptionScreens = ReceptionScreen::pluck('name', 'id'); $idsReceptionScreens = $receptionScreens->keys()->toArray(); $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) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index f56505c..c5c9a5a 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -21,7 +21,7 @@ class UserController extends Controller } 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(); return view('users.index', compact('users')); @@ -29,7 +29,7 @@ class UserController extends Controller public function store(UpdateUserRequest $request): RedirectResponse { - abort_if(!Auth::user('admin'), 403); + abort_if(Auth::user()->name !== 'admin', 403); $validated = $request->validated(); @@ -44,21 +44,21 @@ class UserController extends Controller 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'); } 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')); } public function update(UpdateUserRequest $request, User $user): RedirectResponse { - abort_if(!Auth::user('admin'), 403); + abort_if(Auth::user()->name !== 'admin', 403); $validated = $request->validated(); @@ -73,8 +73,7 @@ class UserController extends Controller public function destroy(User $user): RedirectResponse { - abort_if(!Auth::user('admin'), 403); - + abort_if(Auth::user()->name !== 'admin', 403); $user->delete(); return redirect()->route('users.index'); diff --git a/app/Models/User.php b/app/Models/User.php index 08523d3..69460e8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -20,6 +20,7 @@ class User extends Authenticatable * @var array */ protected $fillable = [ + 'id', 'name', 'email', 'password', diff --git a/app/Services/PageScrapper.php b/app/Services/PageScrapper.php index 8dd6658..956cfb1 100644 --- a/app/Services/PageScrapper.php +++ b/app/Services/PageScrapper.php @@ -38,7 +38,7 @@ class PageScrapper $rez = preg_match_all('/src="(.*)">/isU', $content, $arr); $arr[1] = array_unique($arr[1]); foreach ($arr[1] as $el) { - if (!str_starts_with($el, 'https') && str_contains($el, 'upload')) { + if (!str_starts_with($el, 'https') && str_contains($el, 'upload')) { $content = str_replace($el, 'https://mkgtu.ru' . $el, $content); } } diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php new file mode 100644 index 0000000..bb206a2 --- /dev/null +++ b/tests/Feature/UserTest.php @@ -0,0 +1,134 @@ +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); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a..29bd88b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,11 @@ namespace Tests; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + use RefreshDatabase; }