Skip to content

A free open source IT asset/license management system

Fixed factory for imports

snipe2d19591888fd
diff --git a/database/factories/ImportFactory.php b/database/factories/ImportFactory.php
index a51edb014d..7e3f17d59f 100644
--- a/database/factories/ImportFactory.php
+++ b/database/factories/ImportFactory.php
@@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\Import;
+use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Tests\Support\Importing;
@@ -27,6 +28,13 @@ class ImportFactory extends Factory
'file_path' => Str::random().'.csv',
'filesize' => $this->faker->randomDigitNotNull(),
'field_map' => null,
+ // In production `store()` always sets `created_by = auth()->id()`,
+ // so an import never exists without an owner. Mirror that here:
+ // if the test is already actingAs someone, use them; otherwise
+ // mint a fresh user. This keeps the "actor owns their import"
+ // idiom that existing tests already assume, and lets the API
+ // owner-scope on read/process paths match production behavior.
+ 'created_by' => auth()->id() ?? User::factory(),
];
}
diff --git a/tests/Feature/Importing/Api/ImportUsersTest.php b/tests/Feature/Importing/Api/ImportUsersTest.php
index 287927d0b8..4ce8805a7b 100644
--- a/tests/Feature/Importing/Api/ImportUsersTest.php
+++ b/tests/Feature/Importing/Api/ImportUsersTest.php
@@ -405,9 +405,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
'email' => '[email protected]',
]),
]);
- $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(User::factory()->canImport()->create());
+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$this->assertEquals('[email protected]', $victim->refresh()->email);
@@ -427,9 +427,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
'email' => '[email protected]',
]),
]);
- $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(User::factory()->canImport()->editUsers()->create());
+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$this->assertEquals('[email protected]', $target->refresh()->email);
diff --git a/tests/Feature/Importing/AssetImportCreatedAtTest.php b/tests/Feature/Importing/AssetImportCreatedAtTest.php
index e18ea51030..9c859aebbb 100644
--- a/tests/Feature/Importing/AssetImportCreatedAtTest.php
+++ b/tests/Feature/Importing/AssetImportCreatedAtTest.php
@@ -47,8 +47,11 @@ class AssetImportCreatedAtTest extends TestCase
$csv = "asset tag,item name,category,status\n";
$csv .= "TEST-001,Test Asset Updated,{$category->name},{$statusLabel->name}\n";
- // Perform import with update flag
- $this->actingAsForApi(User::factory()->canImport()->create())
+ // Perform import with update flag. The same user has to upload and
+ // process, because the process endpoint scopes to the file's uploader.
+ $importer = User::factory()->canImport()->create();
+
+ $this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
$this->createFakeUploadedFile('test.csv', $csv),
@@ -58,7 +61,7 @@ class AssetImportCreatedAtTest extends TestCase
// Import the file
$import = Import::latest()->first();
- $this->actingAsForApi(User::factory()->canImport()->create())
+ $this->actingAsForApi($importer)
->postJson(route('api.imports.importFile', $import->id), [
'import-type' => 'asset',
'import-update' => true,
@@ -118,7 +121,11 @@ class AssetImportCreatedAtTest extends TestCase
$csv .= "{$asset->asset_tag},{$asset->name},{$category->name},{$statusLabel->name}\n";
}
- $this->actingAsForApi(User::factory()->canImport()->create())
+ // Same user has to upload and process, because the process endpoint
+ // scopes to the file's uploader.
+ $importer = User::factory()->canImport()->create();
+
+ $this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
$this->createFakeUploadedFile('test1.csv', $csv),
@@ -127,7 +134,7 @@ class AssetImportCreatedAtTest extends TestCase
->assertSuccessful();
$import1 = Import::latest()->first();
- $this->actingAsForApi(User::factory()->canImport()->create())
+ $this->actingAsForApi($importer)
->postJson(route('api.imports.importFile', $import1->id), [
'import-type' => 'asset',
'import-update' => true,
@@ -141,7 +148,7 @@ class AssetImportCreatedAtTest extends TestCase
->assertSuccessful();
// Perform second import update
- $this->actingAsForApi(User::factory()->canImport()->create())
+ $this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
$this->createFakeUploadedFile('test2.csv', $csv),
@@ -150,7 +157,7 @@ class AssetImportCreatedAtTest extends TestCase
->assertSuccessful();
$import2 = Import::latest()->first();
- $this->actingAsForApi(User::factory()->canImport()->create())
+ $this->actingAsForApi($importer)
->postJson(route('api.imports.importFile', $import2->id), [
'import-type' => 'asset',
'import-update' => true,