Skip to content

A free open source IT asset/license management system

Add helper method for authenticating with passport

Marcus Mooreacd06927ac09
diff --git a/tests/Feature/Api/Assets/AssetIndexTest.php b/tests/Feature/Api/Assets/AssetIndexTest.php
index 0fe3a4d306..778483c1c9 100644
--- a/tests/Feature/Api/Assets/AssetIndexTest.php
+++ b/tests/Feature/Api/Assets/AssetIndexTest.php
@@ -6,7 +6,6 @@ use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -18,14 +17,14 @@ class AssetIndexTest extends TestCase
{
Asset::factory()->count(3)->create();
- Passport::actingAs(User::factory()->superuser()->create());
- $this->getJson(
- route('api.assets.index', [
- 'sort' => 'name',
- 'order' => 'asc',
- 'offset' => '0',
- 'limit' => '20',
- ]))
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(
+ route('api.assets.index', [
+ 'sort' => 'name',
+ 'order' => 'asc',
+ 'offset' => '0',
+ 'limit' => '20',
+ ]))
->assertOk()
->assertJsonStructure([
'total',
@@ -47,35 +46,35 @@ class AssetIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.assets.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.assets.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.assets.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.assets.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.assets.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.assets.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.assets.index'))
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
}
diff --git a/tests/Feature/Api/Assets/AssetsForSelectListTest.php b/tests/Feature/Api/Assets/AssetsForSelectListTest.php
index a3210b9886..cccae38d38 100644
--- a/tests/Feature/Api/Assets/AssetsForSelectListTest.php
+++ b/tests/Feature/Api/Assets/AssetsForSelectListTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Assets;
use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -18,9 +17,9 @@ class AssetsForSelectListTest extends TestCase
Asset::factory()->create(['asset_tag' => '0001']);
Asset::factory()->create(['asset_tag' => '0002']);
- Passport::actingAs(User::factory()->create());
-
- $response = $this->getJson(route('assets.selectlist', ['search' => '000']))->assertOk();
+ $response = $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('assets.selectlist', ['search' => '000']))
+ ->assertOk();
$results = collect($response->json('results'));
@@ -42,35 +41,35 @@ class AssetsForSelectListTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('assets.selectlist', ['search' => '000']))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('assets.selectlist', ['search' => '000']))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('assets.selectlist', ['search' => '000']))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('assets.selectlist', ['search' => '000']))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('assets.selectlist', ['search' => '000']))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseDoesNotContainInResults($assetB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('assets.selectlist', ['search' => '000']))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseDoesNotContainInResults($assetA)
->assertResponseContainsInResults($assetB);
}
diff --git a/tests/Feature/Api/Assets/RequestableAssetsTest.php b/tests/Feature/Api/Assets/RequestableAssetsTest.php
index 7ecddeaa02..8649b1b00b 100644
--- a/tests/Feature/Api/Assets/RequestableAssetsTest.php
+++ b/tests/Feature/Api/Assets/RequestableAssetsTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Assets;
use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -15,8 +14,9 @@ class RequestableAssetsTest extends TestCase
public function testViewingRequestableAssetsRequiresCorrectPermission()
{
- Passport::actingAs(User::factory()->create());
- $this->getJson(route('api.assets.requestable'))->assertForbidden();
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.assets.requestable'))
+ ->assertForbidden();
}
public function testReturnsRequestableAssets()
@@ -24,8 +24,8 @@ class RequestableAssetsTest extends TestCase
$requestableAsset = Asset::factory()->requestable()->create(['asset_tag' => 'requestable']);
$nonRequestableAsset = Asset::factory()->nonrequestable()->create(['asset_tag' => 'non-requestable']);
- Passport::actingAs(User::factory()->viewRequestableAssets()->create());
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
+ ->getJson(route('api.assets.requestable'))
->assertOk()
->assertResponseContainsInRows($requestableAsset, 'asset_tag')
->assertResponseDoesNotContainInRows($nonRequestableAsset, 'asset_tag');
@@ -44,35 +44,35 @@ class RequestableAssetsTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.assets.requestable'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.assets.requestable'))
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
}
diff --git a/tests/Feature/Api/Components/ComponentIndexTest.php b/tests/Feature/Api/Components/ComponentIndexTest.php
index 276f22e6f1..ee83b7a46d 100644
--- a/tests/Feature/Api/Components/ComponentIndexTest.php
+++ b/tests/Feature/Api/Components/ComponentIndexTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Components;
use App\Models\Company;
use App\Models\Component;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -26,35 +25,35 @@ class ComponentIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.components.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.components.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.components.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.components.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.components.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseDoesNotContainInRows($componentB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.components.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.components.index'))
->assertResponseDoesNotContainInRows($componentA)
->assertResponseContainsInRows($componentB);
}
diff --git a/tests/Feature/Api/Consumables/ConsumablesIndexTest.php b/tests/Feature/Api/Consumables/ConsumablesIndexTest.php
index 6e6e809c6c..33c10ed078 100644
--- a/tests/Feature/Api/Consumables/ConsumablesIndexTest.php
+++ b/tests/Feature/Api/Consumables/ConsumablesIndexTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Consumables;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -26,35 +25,35 @@ class ConsumablesIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.consumables.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.consumables.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.consumables.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.consumables.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.consumables.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseDoesNotContainInRows($consumableB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.consumables.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.consumables.index'))
->assertResponseDoesNotContainInRows($consumableA)
->assertResponseContainsInRows($consumableB);
}
diff --git a/tests/Feature/Api/Departments/DepartmentsIndexTest.php b/tests/Feature/Api/Departments/DepartmentsIndexTest.php
index 9570e85307..1067ec74c7 100644
--- a/tests/Feature/Api/Departments/DepartmentsIndexTest.php
+++ b/tests/Feature/Api/Departments/DepartmentsIndexTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Departments;
use App\Models\Company;
use App\Models\Department;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -30,35 +29,35 @@ class DepartmentsIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.departments.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.departments.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.departments.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.departments.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.departments.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseDoesNotContainInRows($departmentB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.departments.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.departments.index'))
->assertResponseDoesNotContainInRows($departmentA)
->assertResponseContainsInRows($departmentB);
}
diff --git a/tests/Feature/Api/Licenses/LicensesIndexTest.php b/tests/Feature/Api/Licenses/LicensesIndexTest.php
index fd85239928..a21a27da73 100644
--- a/tests/Feature/Api/Licenses/LicensesIndexTest.php
+++ b/tests/Feature/Api/Licenses/LicensesIndexTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Licenses;
use App\Models\Company;
use App\Models\License;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -26,35 +25,35 @@ class LicensesIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.licenses.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.licenses.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.licenses.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.licenses.index'))
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.licenses.index'))
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseDoesNotContainInRows($licenseB);
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.licenses.index'))
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.licenses.index'))
->assertResponseDoesNotContainInRows($licenseA)
->assertResponseContainsInRows($licenseB);
}
diff --git a/tests/Feature/Api/Users/UsersForSelectListTest.php b/tests/Feature/Api/Users/UsersForSelectListTest.php
index 8cdf700f04..ba4beb211b 100644
--- a/tests/Feature/Api/Users/UsersForSelectListTest.php
+++ b/tests/Feature/Api/Users/UsersForSelectListTest.php
@@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Users;
use App\Models\Company;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -17,8 +16,8 @@ class UsersForSelectListTest extends TestCase
{
$users = User::factory()->superuser()->count(3)->create();
- Passport::actingAs($users->first());
- $this->getJson(route('api.users.selectlist'))
+ $this->actingAsForApi($users->first())
+ ->getJson(route('api.users.selectlist'))
->assertOk()
->assertJsonStructure([
'results',
@@ -34,8 +33,9 @@ class UsersForSelectListTest extends TestCase
{
User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Skywalker']);
- Passport::actingAs(User::factory()->create());
- $response = $this->getJson(route('api.users.selectlist', ['search' => 'luke sky']))->assertOk();
+ $response = $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.users.selectlist', ['search' => 'luke sky']))
+ ->assertOk();
$results = collect($response->json('results'));
@@ -57,8 +57,9 @@ class UsersForSelectListTest extends TestCase
->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
->create();
- Passport::actingAs($jedi->users->first());
- $response = $this->getJson(route('api.users.selectlist'))->assertOk();
+ $response = $this->actingAsForApi($jedi->users->first())
+ ->getJson(route('api.users.selectlist'))
+ ->assertOk();
$results = collect($response->json('results'));
@@ -85,8 +86,9 @@ class UsersForSelectListTest extends TestCase
->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
->create();
- Passport::actingAs($jedi->users->first());
- $response = $this->getJson(route('api.users.selectlist', ['search' => 'a']))->assertOk();
+ $response = $this->actingAsForApi($jedi->users->first())
+ ->getJson(route('api.users.selectlist', ['search' => 'a']))
+ ->assertOk();
$results = collect($response->json('results'));
diff --git a/tests/Feature/Api/Users/UsersIndexTest.php b/tests/Feature/Api/Users/UsersIndexTest.php
index ed91976ccb..644c5e9e0d 100644
--- a/tests/Feature/Api/Users/UsersIndexTest.php
+++ b/tests/Feature/Api/Users/UsersIndexTest.php
@@ -4,7 +4,6 @@ namespace Tests\Feature\Api\Users;
use App\Models\Company;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -29,36 +28,36 @@ class UsersIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.users.index'))
- ->assertResponseContainsInRows($userA, 'first_name')
- ->assertResponseContainsInRows($userB, 'first_name');
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.users.index'))
+ ->assertResponseContainsInRows($userA, 'first_name')
+ ->assertResponseContainsInRows($userB, 'first_name');
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.users.index'))
- ->assertResponseContainsInRows($userA, 'first_name')
- ->assertResponseContainsInRows($userB, 'first_name');
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.users.index'))
+ ->assertResponseContainsInRows($userA, 'first_name')
+ ->assertResponseContainsInRows($userB, 'first_name');
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.users.index'))
- ->assertResponseContainsInRows($userA, 'first_name')
- ->assertResponseContainsInRows($userB, 'first_name');
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.users.index'))
+ ->assertResponseContainsInRows($userA, 'first_name')
+ ->assertResponseContainsInRows($userB, 'first_name');
$this->settings->enableMultipleFullCompanySupport();
- Passport::actingAs($superUser);
- $this->getJson(route('api.users.index'))
- ->assertResponseContainsInRows($userA, 'first_name')
- ->assertResponseContainsInRows($userB, 'first_name');
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.users.index'))
+ ->assertResponseContainsInRows($userA, 'first_name')
+ ->assertResponseContainsInRows($userB, 'first_name');
- Passport::actingAs($userInCompanyA);
- $this->getJson(route('api.users.index'))
- ->assertResponseContainsInRows($userA, 'first_name')
- ->assertResponseDoesNotContainInRows($userB, 'first_name');
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.users.index'))
+ ->assertResponseContainsInRows($userA, 'first_name')
+ ->assertResponseDoesNotContainInRows($userB, 'first_name');
- Passport::actingAs($userInCompanyB);
- $this->getJson(route('api.users.index'))
- ->assertResponseDoesNotContainInRows($userA, 'first_name')
- ->assertResponseContainsInRows($userB, 'first_name');
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.users.index'))
+ ->assertResponseDoesNotContainInRows($userA, 'first_name')
+ ->assertResponseContainsInRows($userB, 'first_name');
}
}
diff --git a/tests/Feature/Api/Users/UsersSearchTest.php b/tests/Feature/Api/Users/UsersSearchTest.php
index 33f77196f3..349cd1e85c 100644
--- a/tests/Feature/Api/Users/UsersSearchTest.php
+++ b/tests/Feature/Api/Users/UsersSearchTest.php
@@ -3,7 +3,6 @@
namespace Tests\Feature\Api\Users;
use App\Models\User;
-use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -16,8 +15,9 @@ class UsersSearchTest extends TestCase
User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Skywalker']);
User::factory()->create(['first_name' => 'Darth', 'last_name' => 'Vader']);
- Passport::actingAs(User::factory()->viewUsers()->create());
- $response = $this->getJson(route('api.users.index', ['search' => 'luke sky']))->assertOk();
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->create())
+ ->getJson(route('api.users.index', ['search' => 'luke sky']))
+ ->assertOk();
$results = collect($response->json('rows'));
diff --git a/tests/Support/InteractsWithAuthentication.php b/tests/Support/InteractsWithAuthentication.php
new file mode 100644
index 0000000000..27b20e382b
--- /dev/null
+++ b/tests/Support/InteractsWithAuthentication.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Tests\Support;
+
+use Illuminate\Contracts\Auth\Authenticatable;
+use Laravel\Passport\Passport;
+
+trait InteractsWithAuthentication
+{
+ protected function actingAsForApi(Authenticatable $user)
+ {
+ Passport::actingAs($user);
+
+ return $this;
+ }
+}
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 4a5c3da692..30237f3171 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -6,12 +6,14 @@ use App\Http\Middleware\SecurityHeaders;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\Support\CustomTestMacros;
+use Tests\Support\InteractsWithAuthentication;
use Tests\Support\InteractsWithSettings;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use CustomTestMacros;
+ use InteractsWithAuthentication;
use LazilyRefreshDatabase;
private array $globallyDisabledMiddleware = [