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 Laravel\Passport\Passport;
|
2023-04-12 17:28:47 -07:00
|
|
|
use Tests\Support\InteractsWithSettings;
|
2023-03-20 15:47:57 -07:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AssetIndexTest extends TestCase
|
|
|
|
{
|
2023-04-12 17:28:47 -07:00
|
|
|
use InteractsWithSettings;
|
|
|
|
|
2023-03-20 15:47:57 -07:00
|
|
|
public function testAssetIndexReturnsExpectedAssets()
|
|
|
|
{
|
|
|
|
Asset::factory()->count(3)->create();
|
|
|
|
|
|
|
|
Passport::actingAs(User::factory()->superuser()->create());
|
|
|
|
$this->getJson(
|
|
|
|
route('api.assets.index', [
|
|
|
|
'sort' => 'name',
|
|
|
|
'order' => 'asc',
|
|
|
|
'offset' => '0',
|
|
|
|
'limit' => '20',
|
|
|
|
]))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
|
|
|
|
}
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
public function testAssetIndexAdheresToCompanyScoping()
|
|
|
|
{
|
|
|
|
[$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();
|
|
|
|
|
|
|
|
Passport::actingAs($superUser);
|
2023-06-22 14:41:56 -07:00
|
|
|
$this->getJson(route('api.assets.index'))
|
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
Passport::actingAs($userInCompanyA);
|
2023-06-22 14:41:56 -07:00
|
|
|
$this->getJson(route('api.assets.index'))
|
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
Passport::actingAs($userInCompanyB);
|
2023-06-22 14:41:56 -07:00
|
|
|
$this->getJson(route('api.assets.index'))
|
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
Passport::actingAs($superUser);
|
2023-06-22 14:41:56 -07:00
|
|
|
$this->getJson(route('api.assets.index'))
|
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
Passport::actingAs($userInCompanyA);
|
2023-06-22 14:41:56 -07:00
|
|
|
$this->getJson(route('api.assets.index'))
|
|
|
|
->assertResponseContainsInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
|
|
|
|
Passport::actingAs($userInCompanyB);
|
2023-06-22 14:41:56 -07:00
|
|
|
$this->getJson(route('api.assets.index'))
|
|
|
|
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
|
|
|
|
->assertResponseContainsInRows($assetB, 'asset_tag');
|
2023-06-21 16:19:56 -07:00
|
|
|
}
|
2023-03-20 15:47:57 -07:00
|
|
|
}
|