2023-03-20 15:47:57 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Api\Assets;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
2023-06-21 16:19:56 -07:00
|
|
|
use App\Models\Company;
|
2023-03-20 15:47:57 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
|
|
|
use Tests\TestCase;
|
2024-04-26 06:01:29 -07:00
|
|
|
use Carbon;
|
2023-03-20 15:47:57 -07:00
|
|
|
class AssetIndexTest extends TestCase
|
|
|
|
{
|
2024-04-26 06:01:29 -07:00
|
|
|
public function testAssetApiIndexReturnsExpectedAssets()
|
2023-03-20 15:47:57 -07:00
|
|
|
{
|
|
|
|
Asset::factory()->count(3)->create();
|
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->getJson(
|
|
|
|
route('api.assets.index', [
|
|
|
|
'sort' => 'name',
|
|
|
|
'order' => 'asc',
|
|
|
|
'offset' => '0',
|
|
|
|
'limit' => '20',
|
|
|
|
]))
|
2023-03-20 15:47:57 -07:00
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
|
|
|
|
}
|
2023-06-21 16:19:56 -07:00
|
|
|
|
2024-04-26 06:01:29 -07:00
|
|
|
public function testAssetApiIndexReturnsDisplayUpcomingAuditsDueToday()
|
|
|
|
{
|
|
|
|
$assets = Asset::factory()->count(3)->create(['next_audit_date' => Carbon::now()->format('Y-m-d')]);
|
|
|
|
|
|
|
|
//dd($assets);
|
|
|
|
|
|
|
|
$this->assertTrue(Asset::count() === 3);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->getJson(
|
|
|
|
route('api.asset.to-audit', ['status' => 'due']))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetApiIndexReturnsOverdueForAudit()
|
|
|
|
{
|
|
|
|
Asset::factory()->count(3)->create(['next_audit_date' => Carbon::now()->subDays(1)->format('Y-m-d')]);
|
|
|
|
|
|
|
|
$this->assertTrue(Asset::count() === 3);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->getJson(
|
|
|
|
route('api.asset.to-audit', ['status' => 'overdue']))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAssetApiIndexReturnsDueForExpectedCheckinToday()
|
|
|
|
{
|
|
|
|
Asset::factory()->count(3)->create(['expected_checkin' => Carbon::now()->format('Y-m-d')]);
|
|
|
|
|
|
|
|
$this->assertTrue(Asset::count() === 3);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->getJson(
|
|
|
|
route('api.asset.to-checkin', ['status' => 'due'])
|
|
|
|
)
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetApiIndexReturnsOverdueForExpectedCheckin()
|
|
|
|
{
|
|
|
|
Asset::factory()->count(3)->create(['expected_checkin' => Carbon::now()->subDays(1)->format('Y-m-d')]);
|
|
|
|
|
|
|
|
$this->assertTrue(Asset::count() === 3);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->getJson(route('api.asset.to-checkin', ['status' => 'overdue']))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetApiIndexAdheresToCompanyScoping()
|
2023-06-21 16:19:56 -07:00
|
|
|
{
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$assetA = Asset::factory()->for($companyA)->create();
|
|
|
|
$assetB = Asset::factory()->for($companyB)->create();
|
|
|
|
|
|
|
|
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
|
|
|
|
$userInCompanyA = $companyA->users()->save(User::factory()->viewAssets()->make());
|
|
|
|
$userInCompanyB = $companyB->users()->save(User::factory()->viewAssets()->make());
|
|
|
|
|
|
|
|
$this->settings->disableMultipleFullCompanySupport();
|
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->getJson(route('api.assets.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->getJson(route('api.assets.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyB)
|
|
|
|
->getJson(route('api.assets.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->getJson(route('api.assets.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->getJson(route('api.assets.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyB)
|
|
|
|
->getJson(route('api.assets.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
}
|
2023-03-20 15:47:57 -07:00
|
|
|
}
|