2024-04-16 07:34:28 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Api\Assets;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\CustomField;
|
|
|
|
use App\Models\User;
|
2024-04-16 17:14:17 -07:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2024-04-16 07:34:28 -07:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AssetUpdateTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testEncryptedCustomFieldCanBeUpdated()
|
|
|
|
{
|
|
|
|
$field = CustomField::factory()->testEncrypted()->create();
|
2024-04-16 16:58:28 -07:00
|
|
|
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
|
2024-04-16 07:34:28 -07:00
|
|
|
$superuser = User::factory()->superuser()->create();
|
|
|
|
|
2024-04-16 17:13:18 -07:00
|
|
|
$this->actingAsForApi($superuser)
|
2024-04-16 07:34:28 -07:00
|
|
|
->patchJson(route('api.assets.update', $asset->id), [
|
|
|
|
$field->db_column_name() => 'This is encrypted field'
|
|
|
|
])
|
|
|
|
->assertStatusMessageIs('success')
|
2024-04-16 17:13:18 -07:00
|
|
|
->assertOk();
|
|
|
|
|
2024-04-16 07:34:28 -07:00
|
|
|
$asset->refresh();
|
2024-04-16 17:14:17 -07:00
|
|
|
$this->assertEquals('This is encrypted field', Crypt::decrypt($asset->{$field->db_column_name()}));
|
2024-04-16 07:34:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPermissionNeededToUpdateEncryptedField()
|
|
|
|
{
|
|
|
|
$field = CustomField::factory()->testEncrypted()->create();
|
2024-04-16 16:58:28 -07:00
|
|
|
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
|
2024-04-16 07:34:28 -07:00
|
|
|
$normal_user = User::factory()->editAssets()->create();
|
|
|
|
|
2024-04-16 17:14:17 -07:00
|
|
|
$asset->{$field->db_column_name()} = Crypt::encrypt("encrypted value should not change");
|
2024-04-16 17:13:18 -07:00
|
|
|
$asset->save();
|
2024-04-16 07:34:28 -07:00
|
|
|
|
2024-04-16 17:13:18 -07:00
|
|
|
// test that a 'normal' user *cannot* change the encrypted custom field
|
|
|
|
$this->actingAsForApi($normal_user)
|
2024-04-16 07:34:28 -07:00
|
|
|
->patchJson(route('api.assets.update', $asset->id), [
|
|
|
|
$field->db_column_name() => 'Some Other Value Entirely!'
|
|
|
|
])
|
|
|
|
->assertStatusMessageIs('success')
|
|
|
|
->assertOk()
|
2024-04-16 17:13:18 -07:00
|
|
|
->assertMessagesAre('Asset updated successfully, but encrypted custom fields were not due to permissions');
|
|
|
|
|
2024-04-16 07:34:28 -07:00
|
|
|
$asset->refresh();
|
2024-04-16 17:14:17 -07:00
|
|
|
$this->assertEquals("encrypted value should not change", Crypt::decrypt($asset->{$field->db_column_name()}));
|
2024-04-16 07:34:28 -07:00
|
|
|
}
|
|
|
|
}
|