Scaffold tests

This commit is contained in:
Marcus Moore 2024-10-17 14:12:22 -07:00
parent 159a1d3f43
commit 50fa6ce335
No known key found for this signature in database
4 changed files with 262 additions and 73 deletions

View file

@ -0,0 +1,87 @@
<?php
namespace Tests\Feature\Accessories\Ui;
use App\Models\Accessory;
use App\Models\Category;
use App\Models\Company;
use App\Models\User;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
class CreateAccessoryWithFullMultipleCompanySupportTest extends TestCase
{
public static function userProvider(): Generator
{
yield "User in a company should result in user's company_id being used" => [
function () {
$jedi = Company::factory()->create();
$sith = Company::factory()->create();
$luke = User::factory()->for($jedi)->createAccessories()->create();
return [
'actor' => $luke,
'company_attempting_to_associate' => $sith,
'assertions' => function ($accessory) use ($jedi) {
self::assertEquals($jedi->id, $accessory->company_id);
},
];
}
];
yield "User without a company should result in accessory's company_id being null" => [
function () {
$userInNoCompany = User::factory()->createAccessories()->create(['company_id' => null]);
return [
'actor' => $userInNoCompany,
'company_attempting_to_associate' => Company::factory()->create(),
'assertions' => function ($accessory) {
self::assertNull($accessory->company_id);
},
];
}
];
yield "Super-User assigning across companies should result in accessory's company_id being set to what was provided" => [
function () {
$superUser = User::factory()->superuser()->create(['company_id' => null]);
$company = Company::factory()->create();
return [
'actor' => $superUser,
'company_attempting_to_associate' => $company,
'assertions' => function ($accessory) use ($company) {
self::assertEquals($accessory->company_id, $company->id);
},
];
}
];
}
#[Group('focus')]
#[DataProvider('userProvider')]
public function testAdheresToFullMultipleCompaniesSupportScoping($data)
{
['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs($actor)
->post(route('accessories.store'), [
'redirect_option' => 'index',
'name' => 'My Cool Accessory',
'qty' => '1',
'category_id' => Category::factory()->create()->id,
'company_id' => $company->id,
]);
$accessory = Accessory::withoutGlobalScopes()->where([
'name' => 'My Cool Accessory',
])->sole();
$assertions($accessory);
}
}

View file

@ -10,10 +10,9 @@ use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\Supplier;
use App\Models\User;
use Generator;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Testing\Fluent\AssertableJson;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
class StoreAssetTest extends TestCase
@ -25,77 +24,6 @@ class StoreAssetTest extends TestCase
->assertForbidden();
}
public static function userProvider(): Generator
{
yield "User in a company should result in user's company_id being used" => [
function () {
$jedi = Company::factory()->create();
$sith = Company::factory()->create();
$luke = User::factory()->for($jedi)->createAssets()->create();
return [
'actor' => $luke,
'company_attempting_to_associate' => $sith,
'assertions' => function ($asset) use ($jedi) {
self::assertEquals($jedi->id, $asset->company_id);
},
];
}
];
yield "User without a company should result in asset's company_id being null" => [
function () {
$userInNoCompany = User::factory()->createAssets()->create(['company_id' => null]);
return [
'actor' => $userInNoCompany,
'company_attempting_to_associate' => Company::factory()->create(),
'assertions' => function ($asset) {
self::assertNull($asset->company_id);
},
];
}
];
yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [
function () {
$superUser = User::factory()->superuser()->create(['company_id' => null]);
$company = Company::factory()->create();
return [
'actor' => $superUser,
'company_attempting_to_associate' => $company,
'assertions' => function ($asset) use ($company) {
self::assertEquals($asset->company_id, $company->id);
},
];
}
];
}
/**
* @link https://github.com/snipe/snipe-it/issues/15654
*/
#[DataProvider('userProvider')]
public function testAdheresToFullMultipleCompaniesSupportScoping($data)
{
['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
$this->settings->enableMultipleFullCompanySupport();
$response = $this->actingAsForApi($actor)
->postJson(route('api.assets.store'), [
'asset_tag' => 'random_string',
'company_id' => $company->id,
'model_id' => AssetModel::factory()->create()->id,
'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,
]);
$asset = Asset::withoutGlobalScopes()->findOrFail($response['payload']['id']);
$assertions($asset);
}
public function testAllAssetAttributesAreStored()
{
$company = Company::factory()->create();
@ -636,6 +564,7 @@ class StoreAssetTest extends TestCase
/**
* @link https://app.shortcut.com/grokability/story/24475
*/
#[Group('focus')]
public function testCompanyIdNeedsToBeInteger()
{
$this->actingAsForApi(User::factory()->createAssets()->create())

View file

@ -0,0 +1,88 @@
<?php
namespace Tests\Feature\Assets\Api;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\Statuslabel;
use App\Models\User;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
class StoreAssetWithFullMultipleCompanySupportTest extends TestCase
{
public static function userProvider(): Generator
{
yield "User in a company should result in user's company_id being used" => [
function () {
$jedi = Company::factory()->create();
$sith = Company::factory()->create();
$luke = User::factory()->for($jedi)->createAssets()->create();
return [
'actor' => $luke,
'company_attempting_to_associate' => $sith,
'assertions' => function ($asset) use ($jedi) {
self::assertEquals($jedi->id, $asset->company_id);
},
];
}
];
yield "User without a company should result in asset's company_id being null" => [
function () {
$userInNoCompany = User::factory()->createAssets()->create(['company_id' => null]);
return [
'actor' => $userInNoCompany,
'company_attempting_to_associate' => Company::factory()->create(),
'assertions' => function ($asset) {
self::assertNull($asset->company_id);
},
];
}
];
yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [
function () {
$superUser = User::factory()->superuser()->create(['company_id' => null]);
$company = Company::factory()->create();
return [
'actor' => $superUser,
'company_attempting_to_associate' => $company,
'assertions' => function ($asset) use ($company) {
self::assertEquals($asset->company_id, $company->id);
},
];
}
];
}
/**
* @link https://github.com/snipe/snipe-it/issues/15654
*/
#[Group('focus')]
#[DataProvider('userProvider')]
public function testAdheresToFullMultipleCompaniesSupportScoping($data)
{
['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
$this->settings->enableMultipleFullCompanySupport();
$response = $this->actingAsForApi($actor)
->postJson(route('api.assets.store'), [
'asset_tag' => 'random_string',
'company_id' => $company->id,
'model_id' => AssetModel::factory()->create()->id,
'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,
]);
$asset = Asset::withoutGlobalScopes()->findOrFail($response['payload']['id']);
$assertions($asset);
}
}

View file

@ -0,0 +1,85 @@
<?php
namespace Tests\Feature\Assets\Ui;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\Statuslabel;
use App\Models\User;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
class StoreAssetWithFullMultipleCompanySupportTest extends TestCase
{
public static function userProvider(): Generator
{
yield "User in a company should result in user's company_id being used" => [
function () {
$jedi = Company::factory()->create();
$sith = Company::factory()->create();
$luke = User::factory()->for($jedi)->createAssets()->create();
return [
'actor' => $luke,
'company_attempting_to_associate' => $sith,
'assertions' => function ($asset) use ($jedi) {
self::assertEquals($jedi->id, $asset->company_id);
},
];
}
];
yield "User without a company should result in asset's company_id being null" => [
function () {
$userInNoCompany = User::factory()->createAssets()->create(['company_id' => null]);
return [
'actor' => $userInNoCompany,
'company_attempting_to_associate' => Company::factory()->create(),
'assertions' => function ($asset) {
self::assertNull($asset->company_id);
},
];
}
];
yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [
function () {
$superUser = User::factory()->superuser()->create(['company_id' => null]);
$company = Company::factory()->create();
return [
'actor' => $superUser,
'company_attempting_to_associate' => $company,
'assertions' => function ($asset) use ($company) {
self::assertEquals($asset->company_id, $company->id);
},
];
}
];
}
#[Group('focus')]
#[DataProvider('userProvider')]
public function testAdheresToFullMultipleCompaniesSupportScoping($data)
{
['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs($actor)
->post(route('hardware.store'), [
'asset_tags' => ['1' => '1234'],
'model_id' => AssetModel::factory()->create()->id,
'status_id' => Statuslabel::factory()->create()->id,
'company_id' => $company->id,
]);
$asset = Asset::where('asset_tag', '1234')->sole();
$assertions($asset);
}
}