mirror of
https://github.com/snipe/snipe-it.git
synced 2025-02-21 03:15:45 -08:00
Scaffold test before removing scopeCompanyables call from AssetsController@selectlist
This commit is contained in:
parent
fd55c99b87
commit
effd969284
81
tests/Feature/Api/Assets/AssetsForSelectListTest.php
Normal file
81
tests/Feature/Api/Assets/AssetsForSelectListTest.php
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Api\Assets;
|
||||||
|
|
||||||
|
use App\Models\Asset;
|
||||||
|
use App\Models\Company;
|
||||||
|
use App\Models\User;
|
||||||
|
use Laravel\Passport\Passport;
|
||||||
|
use Tests\Support\InteractsWithResponses;
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AssetsForSelectListTest extends TestCase
|
||||||
|
{
|
||||||
|
use InteractsWithResponses;
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
public function testAssetsCanBeSearchedForByAssetTag()
|
||||||
|
{
|
||||||
|
Asset::factory()->create(['asset_tag' => '0001']);
|
||||||
|
Asset::factory()->create(['asset_tag' => '0002']);
|
||||||
|
|
||||||
|
Passport::actingAs(User::factory()->create());
|
||||||
|
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']))->assertOk();
|
||||||
|
|
||||||
|
$results = collect($response->json('results'));
|
||||||
|
|
||||||
|
$this->assertEquals(2, $results->count());
|
||||||
|
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, '0001')));
|
||||||
|
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, '0002')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssetsAreScopedToCompanyWhenMultipleCompanySupportEnabled()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
||||||
|
|
||||||
|
$assetA = Asset::factory()->for($companyA)->create(['asset_tag' => '0001']);
|
||||||
|
$assetB = Asset::factory()->for($companyB)->create(['asset_tag' => '0002']);
|
||||||
|
|
||||||
|
$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);
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
|
||||||
|
$this->assertResponseContainsInResults($response, $assetA);
|
||||||
|
$this->assertResponseContainsInResults($response, $assetB);
|
||||||
|
|
||||||
|
Passport::actingAs($userInCompanyA);
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
|
||||||
|
$this->assertResponseContainsInResults($response, $assetA);
|
||||||
|
$this->assertResponseContainsInResults($response, $assetB);
|
||||||
|
|
||||||
|
Passport::actingAs($userInCompanyB);
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
|
||||||
|
$this->assertResponseContainsInResults($response, $assetA);
|
||||||
|
$this->assertResponseContainsInResults($response, $assetB);
|
||||||
|
|
||||||
|
$this->settings->enableMultipleFullCompanySupport();
|
||||||
|
|
||||||
|
Passport::actingAs($superUser);
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
|
||||||
|
$this->assertResponseContainsInResults($response, $assetA);
|
||||||
|
$this->assertResponseContainsInResults($response, $assetB);
|
||||||
|
|
||||||
|
Passport::actingAs($userInCompanyA);
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
|
||||||
|
$this->assertResponseContainsInResults($response, $assetA);
|
||||||
|
$this->assertResponseDoesNotContainInResults($response, $assetB);
|
||||||
|
|
||||||
|
Passport::actingAs($userInCompanyB);
|
||||||
|
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
|
||||||
|
$this->assertResponseDoesNotContainInResults($response, $assetA);
|
||||||
|
$this->assertResponseContainsInResults($response, $assetB);
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,20 @@ trait InteractsWithResponses
|
||||||
$this->assertFalse(collect($response['rows'])->pluck($property)->contains($model->{$property}));
|
$this->assertFalse(collect($response['rows'])->pluck($property)->contains($model->{$property}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function assertResponseContainsInResults(TestResponse $response, Model $model, string $property = 'id')
|
||||||
|
{
|
||||||
|
$this->guardAgainstNullProperty($model, $property);
|
||||||
|
|
||||||
|
$this->assertTrue(collect($response->json('results'))->pluck('id')->contains($model->{$property}));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function assertResponseDoesNotContainInResults(TestResponse $response, Model $model, string $property = 'id')
|
||||||
|
{
|
||||||
|
$this->guardAgainstNullProperty($model, $property);
|
||||||
|
|
||||||
|
$this->assertFalse(collect($response->json('results'))->pluck('id')->contains($model->{$property}));
|
||||||
|
}
|
||||||
|
|
||||||
private function guardAgainstNullProperty(Model $model, string $property): void
|
private function guardAgainstNullProperty(Model $model, string $property): void
|
||||||
{
|
{
|
||||||
if (is_null($model->{$property})) {
|
if (is_null($model->{$property})) {
|
||||||
|
|
Loading…
Reference in a new issue