Add test around company scoping in custom report

This commit is contained in:
Marcus Moore 2023-08-08 17:21:39 -07:00
parent 1405e17251
commit c752e1670c
No known key found for this signature in database

View file

@ -3,7 +3,9 @@
namespace Tests\Feature\Reports;
use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
use Illuminate\Testing\TestResponse;
use League\Csv\Reader;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@ -25,9 +27,95 @@ class CustomReportTest extends TestCase
])->assertOk()
->assertHeader('content-type', 'text/csv; charset=UTF-8');
$reader = Reader::createFromString($response->streamedContent());
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
}
$this->assertTrue(collect($reader->getRecords())->pluck(0)->contains('Asset A'));
$this->assertTrue(collect($reader->getRecords())->pluck(0)->contains('Asset B'));
public function testCustomAssetReportAdheresToCompanyScoping()
{
[$companyA, $companyB] = Company::factory()->count(2)->create();
Asset::factory()->for($companyA)->create(['name' => 'Asset A']);
Asset::factory()->for($companyB)->create(['name' => 'Asset B']);
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
$userInCompanyA = $companyA->users()->save(User::factory()->canViewReports()->make());
$userInCompanyB = $companyB->users()->save(User::factory()->canViewReports()->make());
$this->settings->disableMultipleFullCompanySupport();
$response = $this->actingAs($superUser)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$response = $this->actingAs($userInCompanyA)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$response = $this->actingAs($userInCompanyB)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$this->settings->enableMultipleFullCompanySupport();
$response = $this->actingAs($superUser)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$response = $this->actingAs($userInCompanyA)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseDoesNotContain($response, 'Asset B');
$response = $this->actingAs($userInCompanyB)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseDoesNotContain($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
}
private function assertResponseContains(TestResponse $response, string $needle)
{
$this->assertTrue(
collect(Reader::createFromString($response->streamedContent())->getRecords())
->pluck(0)
->contains($needle)
);
}
private function assertResponseDoesNotContain(TestResponse $response, string $needle)
{
$this->assertFalse(
collect(Reader::createFromString($response->streamedContent())->getRecords())
->pluck(0)
->contains($needle)
);
}
}