snipe-it/tests/Feature/Api/Assets/UpdateAssetTest.php

56 lines
2 KiB
PHP
Raw Normal View History

<?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;
use Tests\TestCase;
2024-06-03 16:53:15 -07:00
class UpdateAssetTest extends TestCase
{
public function testEncryptedCustomFieldCanBeUpdated()
{
2024-04-22 10:32:37 -07:00
$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');
$field = CustomField::factory()->testEncrypted()->create();
2024-04-16 16:58:28 -07:00
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
$superuser = User::factory()->superuser()->create();
2024-04-16 17:13:18 -07:00
$this->actingAsForApi($superuser)
->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();
$asset->refresh();
2024-04-16 17:14:17 -07:00
$this->assertEquals('This is encrypted field', Crypt::decrypt($asset->{$field->db_column_name()}));
}
public function testPermissionNeededToUpdateEncryptedField()
{
2024-04-22 10:32:37 -07:00
$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');
$field = CustomField::factory()->testEncrypted()->create();
2024-04-16 16:58:28 -07:00
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
$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 17:13:18 -07:00
// test that a 'normal' user *cannot* change the encrypted custom field
$this->actingAsForApi($normal_user)
->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');
$asset->refresh();
2024-04-16 17:14:17 -07:00
$this->assertEquals("encrypted value should not change", Crypt::decrypt($asset->{$field->db_column_name()}));
}
}