first commit

This commit is contained in:
Noor E Ilahi
2026-01-09 12:54:53 +05:30
commit 7ccf44f7da
1070 changed files with 113036 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class ActivityTest extends TestCase
{
private const URI = 'users/activity';
private User $admin;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
}
public function test_update(): void
{
/* @var \Carbon\Carbon $lastActivity */
$lastActivity = $this->admin->last_activity;
$response = $this->actingAs($this->admin)->patchJson(self::URI);
$user = User::find($this->admin->id);
$response->assertOk();
$this->assertNotEquals($lastActivity->toString(), $user->last_activity->toString());
$this->assertTrue($user->online);
}
public function test_unauthorized(): void
{
$response = $this->patchJson(self::URI);
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class CountTest extends TestCase
{
private const URI = 'users/count';
private const USERS_AMOUNT = 10;
private User $admin;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::asAdmin()->withTokens()->create();
UserFactory::createMany(self::USERS_AMOUNT);
}
public function test_count(): void
{
$response = $this->actingAs($this->admin)->getJson(self::URI);
$response->assertOk();
$response->assertJson(['total' => User::count()]);
}
public function test_unauthorized(): void
{
$response = $this->getJson(self::URI);
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class CreateTest extends TestCase
{
private const URI = 'users/create';
/** @var User $admin */
private User $admin;
/** @var User $manager */
private User $manager;
/** @var User $auditor */
private User $auditor;
/** @var User $user */
private User $user;
private array $userData;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
$this->manager = UserFactory::refresh()->asManager()->withTokens()->create();
$this->auditor = UserFactory::refresh()->asAuditor()->withTokens()->create();
$this->user = UserFactory::refresh()->asUser()->withTokens()->create();
$this->userData = UserFactory::createRandomRegistrationModelData();
}
public function test_create_as_admin(): void
{
$this->assertDatabaseMissing('users', $this->userData);
$response = $this->actingAs($this->admin)->postJson(self::URI, $this->userData);
unset($this->userData['password']);
$response->assertOk();
$this->assertDatabaseHas('users', $this->userData);
$responseData = $response->json('res');
unset($responseData['online']);
$this->assertDatabaseHas('users', $responseData);
}
public function test_create_as_manager(): void
{
$this->assertDatabaseMissing('users', $this->userData);
$response = $this->actingAs($this->manager)->postJson(self::URI, $this->userData);
$response->assertForbidden();
}
public function test_create_as_auditor(): void
{
$this->assertDatabaseMissing('users', $this->userData);
$response = $this->actingAs($this->auditor)->postJson(self::URI, $this->userData);
$response->assertForbidden();
}
public function test_create_as_user(): void
{
$this->assertDatabaseMissing('users', $this->userData);
$response = $this->actingAs($this->user)->postJson(self::URI, $this->userData);
$response->assertForbidden();
}
public function test_unauthorized(): void
{
$response = $this->postJson(self::URI);
$response->assertUnauthorized();
}
public function test_without_params(): void
{
$response = $this->actingAs($this->admin)->postJson(self::URI);
$response->assertValidationError();
}
}

View File

@@ -0,0 +1,134 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\Facades\UserFactory;
use Tests\TestCase;
use Faker\Factory as FakerFactory;
class EditTest extends TestCase
{
use WithFaker;
private const URI = '/users/edit';
/** @var User $admin */
private User $admin;
/** @var User $manager */
private User $manager;
/** @var User $auditor */
private User $auditor;
/** @var User $user */
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
$this->manager = UserFactory::refresh()->asManager()->withTokens()->create();
$this->auditor = UserFactory::refresh()->asAuditor()->withTokens()->create();
$this->user = UserFactory::refresh()->asUser()->withTokens()->create();
}
public function test_edit_as_admin(): void
{
$this->user->full_name = $this->faker->name;
$response = $this->actingAs($this->admin)->postJson(self::URI, $this->user->toArray());
$response->assertOk();
$response->assertJson(['res' => $this->user->toArray()]);
$this->assertDatabaseHas('users', $this->user->only('id', 'full_name'));
}
public function test_edit_as_manager(): void
{
$this->user->full_name = $this->faker->name;
$response = $this->actingAs($this->manager)->postJson(self::URI, $this->user->toArray());
$response->assertForbidden();
}
public function test_edit_as_auditor(): void
{
$this->user->full_name = $this->faker->name;
$response = $this->actingAs($this->auditor)->postJson(self::URI, $this->user->toArray());
$response->assertForbidden();
}
public function test_edit_as_user(): void
{
$this->admin->full_name = $this->faker->name;
$response = $this->actingAs($this->user)->postJson(self::URI, $this->admin->toArray());
$response->assertForbidden();
}
public function test_edit_as_your_own_user(): void
{
$faker = FakerFactory::create();
$user = clone $this->user;
$user->full_name = $faker->unique()->firstName;
$user->email = $faker->unique()->email;
$user->password = $faker->unique()->password;
$user->user_language = 'en';
$response = $this->actingAs($this->user)->postJson(
self::URI,
$user->only('id', 'full_name', 'email', 'password', 'user_language')
);
$response->assertOk();
$response->assertJson(['res' => $user->toArray()]);
$this->assertDatabaseHas(
'users',
$user->only('id', 'full_name', 'email', 'user_language')
);
}
public function test_edit_forbidden_field_as_user(): void
{
$user = clone $this->user;
$user->is_admin = true;
$response = $this->actingAs($this->user)->postJson(
self::URI,
$user->only('id', 'is_admin')
);
$response->assertValidationError();
}
public function test_not_existing(): void
{
$this->user->id++;
$this->user->email = 'newemail@example.com';
$response = $this->actingAs($this->admin)->postJson(self::URI, $this->user->toArray());
$response->assertNotFound();
}
public function test_unauthorized(): void
{
$response = $this->postJson(self::URI);
$response->assertUnauthorized();
}
public function test_without_params(): void
{
$response = $this->actingAs($this->admin)->postJson(self::URI);
$response->assertValidationError();
}
}

View File

@@ -0,0 +1,170 @@
<?php
namespace Tests\Feature\Users;
use App\Models\Project;
use App\Models\User;
use Tests\Facades\ProjectFactory;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class ListTest extends TestCase
{
private const URI = 'users/list';
private const USERS_AMOUNT = 10;
/** @var User $admin */
private User $admin;
/** @var User $manager */
private User $manager;
/** @var User $auditor */
private User $auditor;
/** @var User $user */
private User $user;
/** @var User $projectManager */
private User $projectManager;
/** @var User $projectAuditor */
private User $projectAuditor;
/** @var User $projectUser */
private User $projectUser;
/** @var Project $project */
private Project $project;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
$this->manager = UserFactory::refresh()->asManager()->withTokens()->create();
$this->auditor = UserFactory::refresh()->asAuditor()->withTokens()->create();
$this->user = UserFactory::refresh()->asUser()->withTokens()->create();
UserFactory::createMany(self::USERS_AMOUNT);
$this->project = ProjectFactory::create();
$this->projectManager = UserFactory::refresh()->asUser()->withTokens()->create();
$this->projectManager->projects()->attach($this->project->id, ['role_id' => 1]);
$this->projectAuditor = UserFactory::refresh()->asUser()->withTokens()->create();
$this->projectAuditor->projects()->attach($this->project->id, ['role_id' => 3]);
$this->projectUser = UserFactory::refresh()->asUser()->withTokens()->create();
$this->projectUser->projects()->attach($this->project->id, ['role_id' => 2]);
}
public function test_list_as_admin(): void
{
$response = $this->actingAs($this->admin)->getJson(self::URI);
$users = User::withoutGlobalScopes()->setEagerLoads([])->get()->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_list_as_manager(): void
{
$response = $this->actingAs($this->manager)->getJson(self::URI);
$users = User::withoutGlobalScopes()->setEagerLoads([])->get()->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_list_as_auditor(): void
{
$response = $this->actingAs($this->auditor)->getJson(self::URI);
$users = User::withoutGlobalScopes()->setEagerLoads([])->get()->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_list_as_user(): void
{
$response = $this->actingAs($this->user)->getJson(self::URI);
$user = User::withoutGlobalScopes()
->where('id', $this->user->id)
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($user);
}
public function test_list_as_project_manager(): void
{
$response = $this->actingAs($this->projectManager)->getJson(self::URI);
$users = User::withoutGlobalScopes()
->whereHas('projects', function ($query) {
$query->where('project_id', $this->project->id);
})
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_list_as_project_manager_with_global_scope(): void
{
$response = $this->actingAs($this->projectManager)->postJson(self::URI, ['global_scope' => true]);
$users = User::withoutGlobalScope(\App\Scopes\UserAccessScope::class)
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_list_as_project_auditor(): void
{
$response = $this->actingAs($this->projectAuditor)->getJson(self::URI);
$users = User::withoutGlobalScopes()
->whereHas('projects', function ($query) {
$query->where('project_id', $this->project->id);
})
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_list_as_project_user(): void
{
$response = $this->actingAs($this->projectManager)->getJson(self::URI);
$users = User::withoutGlobalScopes()
->whereHas('projects', function ($query) {
$query->where('project_id', $this->project->id);
})
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($users);
}
public function test_unauthorized(): void
{
$response = $this->getJson(self::URI);
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class RemoveTest extends TestCase
{
private const URI = 'users/remove';
/** @var User $admin */
private User $admin;
/** @var User $manager */
private User $manager;
/** @var User $auditor */
private User $auditor;
/** @var User $user */
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
$this->manager = UserFactory::refresh()->asManager()->withTokens()->create();
$this->auditor = UserFactory::refresh()->asAuditor()->withTokens()->create();
$this->user = UserFactory::refresh()->asUser()->withTokens()->create();
}
public function test_remove_as_admin(): void
{
$user = $this->user->makeHidden('online')->toArray();
unset($user['online']);
$this->assertDatabaseHas('users', $user);
$response = $this->actingAs($this->admin)->postJson(self::URI, $this->user->only('id'));
$response->assertOk();
$this->assertSoftDeleted('users', $this->user->only('id'));
}
public function test_remove_as_manager(): void
{
$this->assertDatabaseHas('users', $this->user->makeHidden('online')->toArray());
$response = $this->actingAs($this->manager)->postJson(self::URI, $this->user->only('id'));
$response->assertForbidden();
}
public function test_remove_as_auditor(): void
{
$this->assertDatabaseHas('users', $this->user->makeHidden('online')->toArray());
$response = $this->actingAs($this->auditor)->postJson(self::URI, $this->user->only('id'));
$response->assertForbidden();
}
public function test_remove_as_user(): void
{
$this->assertDatabaseHas('users', $this->user->makeHidden('online')->toArray());
$response = $this->actingAs($this->user)->postJson(self::URI, $this->user->only('id'));
$response->assertForbidden();
}
public function test_unauthorized(): void
{
$response = $this->postJson(self::URI);
$response->assertUnauthorized();
}
public function test_without_params(): void
{
$response = $this->actingAs($this->admin)->postJson(self::URI);
$response->assertValidationError();
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class SendInviteTest extends TestCase
{
private const URI = 'users/send-invite';
/** @var User $admin */
private User $admin;
/** @var User $manager */
private User $manager;
/** @var User $auditor */
private User $auditor;
/** @var User $user */
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
$this->manager = UserFactory::refresh()->asManager()->withTokens()->create();
$this->auditor = UserFactory::refresh()->asAuditor()->withTokens()->create();
$this->user = UserFactory::refresh()->asUser()->withTokens()->create();
}
public function test_send_invite_as_admin(): void
{
$response = $this->actingAs($this->admin)->postJson(self::URI, $this->user->only('id'));
$response->assertOk();
}
public function test_send_invite_as_manager(): void
{
$response = $this->actingAs($this->manager)->postJson(self::URI, $this->user->only('id'));
$response->assertForbidden();
}
public function test_send_invite_as_auditor(): void
{
$response = $this->actingAs($this->auditor)->postJson(self::URI, $this->user->only('id'));
$response->assertForbidden();
}
public function test_send_invite_as_user(): void
{
$response = $this->actingAs($this->user)->postJson(self::URI, $this->user->only('id'));
$response->assertForbidden();
}
}

View File

@@ -0,0 +1,140 @@
<?php
namespace Tests\Feature\Users;
use App\Models\User;
use Tests\Facades\ProjectFactory;
use Tests\Facades\UserFactory;
use Tests\TestCase;
class ShowTest extends TestCase
{
private const URI = 'users/show';
/** @var User $admin */
private User $admin;
/** @var User $manager */
private User $manager;
/** @var User $auditor */
private User $auditor;
/** @var User $user */
private User $user;
/** @var User $projectManager */
private User $projectManager;
/** @var User $projectAuditor */
private User $projectAuditor;
/** @var User $projectUser */
private User $projectUser;
protected function setUp(): void
{
parent::setUp();
$this->admin = UserFactory::refresh()->asAdmin()->withTokens()->create();
$this->manager = UserFactory::refresh()->asManager()->withTokens()->create();
$this->auditor = UserFactory::refresh()->asAuditor()->withTokens()->create();
$this->user = UserFactory::refresh()->asUser()->withTokens()->create();
$project = ProjectFactory::create();
$this->projectManager = UserFactory::refresh()->asUser()->withTokens()->create();
$this->projectManager->projects()->attach($project->id, ['role_id' => 1]);
$this->projectAuditor = UserFactory::refresh()->asUser()->withTokens()->create();
$this->projectAuditor->projects()->attach($project->id, ['role_id' => 3]);
$this->projectUser = UserFactory::refresh()->asUser()->withTokens()->create();
$this->projectUser->projects()->attach($project->id, ['role_id' => 2]);
}
public function test_show_as_admin(): void
{
$response = $this->actingAs($this->admin)->postJson(self::URI, $this->user->only('id'));
$response->assertOk();
$response->assertJson($this->user->toArray());
}
public function test_show_as_manager(): void
{
$response = $this->actingAs($this->manager)->postJson(self::URI, $this->user->only('id'));
$response->assertOk();
$response->assertJson($this->user->toArray());
}
public function test_show_as_auditor(): void
{
$response = $this->actingAs($this->auditor)->postJson(self::URI, $this->user->only('id'));
$response->assertOk();
$response->assertJson($this->user->toArray());
}
public function test_show_as_user(): void
{
$response = $this->actingAs($this->user)->postJson(self::URI, $this->admin->only('id'));
$response->assertForbidden();
}
public function test_show_as_your_own_user(): void
{
$response = $this->actingAs($this->user)->postJson(self::URI, $this->user->only('id'));
$this->user->makeHidden('role');
$response->assertOk();
$response->assertJson($this->user->toArray());
}
public function test_show_as_project_manager(): void
{
$response = $this->actingAs($this->projectManager)->postJson(self::URI, $this->projectUser->only('id'));
$user = User::where('id', $this->projectUser->id)
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($user[0]);
}
public function test_show_as_project_auditor(): void
{
$response = $this
->actingAs($this->projectAuditor)
->postJson(self::URI, $this->projectUser->only('id'));
$user = User::where('id', $this->projectUser->id)
->setEagerLoads([])
->get()
->toArray();
$response->assertOk();
$response->assertExactJson($user[0]);
}
public function test_show_as_project_user(): void
{
$response = $this->actingAs($this->projectUser)->postJson(self::URI, $this->projectAuditor->only('id'));
$response->assertForbidden();
}
public function test_unauthorized(): void
{
$response = $this->postJson(self::URI);
$response->assertUnauthorized();
}
public function test_without_params(): void
{
$response = $this->actingAs($this->admin)->postJson(self::URI);
$response->assertValidationError();
}
}