forked from aslan/applicant-site
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');
|
||||
$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)
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -20,6 +20,7 @@ class User extends Authenticatable
|
|||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
use CreatesApplication;
|
||||
use RefreshDatabase;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue