FMCS: throw an error if companies don’t match, updated tests
This commit is contained in:
@@ -29,22 +29,16 @@ class StoreAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS
|
||||
|
||||
$this->settings->enableMultipleFullCompanySupport();
|
||||
|
||||
// attempt to store an accessory for company B
|
||||
$response = $this->actingAsForApi($userInCompanyA)
|
||||
// A user in company A cannot create an accessory assigned to company B — request is rejected.
|
||||
$this->actingAsForApi($userInCompanyA)
|
||||
->postJson(route('api.accessories.store'), [
|
||||
'category_id' => Category::factory()->forAccessories()->create()->id,
|
||||
'name' => 'My Awesome Accessory',
|
||||
'qty' => 1,
|
||||
'company_id' => $companyB->id,
|
||||
])->assertStatusMessageIs('success');
|
||||
])->assertStatusMessageIs('error');
|
||||
|
||||
$accessory = Accessory::withoutGlobalScopes()->findOrFail($response['payload']['id']);
|
||||
|
||||
$this->assertSame($companyA->id, $accessory->company_id);
|
||||
$this->assertDatabaseMissing('accessories', [
|
||||
'name' => 'My Awesome Accessory',
|
||||
'company_id' => $companyB->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('accessories', ['name' => 'My Awesome Accessory']);
|
||||
}
|
||||
|
||||
public function test_can_store_accessory()
|
||||
|
||||
@@ -68,7 +68,7 @@ class UpdateAccessoryTest extends TestCase implements TestsFullMultipleCompanies
|
||||
->patchJson(route('api.accessories.update', $accessory), [
|
||||
'company_id' => $companyB->id,
|
||||
])
|
||||
->assertStatusMessageIs('success');
|
||||
->assertStatusMessageIs('error');
|
||||
|
||||
$this->assertSame($companyA->id, $accessory->fresh()->company_id);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ trait ProvidesDataForFullMultipleCompanySupportTesting
|
||||
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()
|
||||
->createAssets()
|
||||
@@ -24,10 +23,9 @@ trait ProvidesDataForFullMultipleCompanySupportTesting
|
||||
|
||||
return [
|
||||
'actor' => $luke,
|
||||
'company_attempting_to_associate' => $sith,
|
||||
'company_attempting_to_associate' => $jedi,
|
||||
'assertions' => function ($model) use ($jedi) {
|
||||
// Sith is not in Luke's pivot (he belongs only to Jedi), so the
|
||||
// submitted company is rejected and his single pivot company is used.
|
||||
// Luke submits his own company (Jedi) — it is in his pivot so it is honoured.
|
||||
self::assertEquals($jedi->id, $model->company_id);
|
||||
},
|
||||
];
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Unit\Models\Company;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GetIdForCurrentUserTest extends TestCase
|
||||
@@ -24,12 +25,13 @@ class GetIdForCurrentUserTest extends TestCase
|
||||
$this->assertEquals(2000, Company::getIdForCurrentUser(2000));
|
||||
}
|
||||
|
||||
public function test_returns_non_super_users_company_id_when_full_company_support_enabled()
|
||||
public function test_throws_when_non_super_user_submits_company_they_do_not_belong_to()
|
||||
{
|
||||
$this->settings->enableMultipleFullCompanySupport();
|
||||
|
||||
$this->actingAs(User::factory()->forCompany(['id' => 2000])->create());
|
||||
$this->assertEquals(2000, Company::getIdForCurrentUser(1000));
|
||||
$this->expectException(ValidationException::class);
|
||||
Company::getIdForCurrentUser(1000);
|
||||
}
|
||||
|
||||
public function test_returns_null_for_non_super_user_without_company_id_when_full_company_support_enabled()
|
||||
|
||||
Reference in New Issue
Block a user