Add helper method for authenticating with passport

This commit is contained in:
Marcus Moore 2023-06-22 17:37:30 -07:00
parent a35d83d14a
commit acd06927ac
No known key found for this signature in database
12 changed files with 156 additions and 143 deletions

View file

@ -6,7 +6,6 @@ use App\Models\Asset;
use App\Models\Company; use App\Models\Company;
use App\Models\User; use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson; use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -18,14 +17,14 @@ class AssetIndexTest extends TestCase
{ {
Asset::factory()->count(3)->create(); Asset::factory()->count(3)->create();
Passport::actingAs(User::factory()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create())
$this->getJson( ->getJson(
route('api.assets.index', [ route('api.assets.index', [
'sort' => 'name', 'sort' => 'name',
'order' => 'asc', 'order' => 'asc',
'offset' => '0', 'offset' => '0',
'limit' => '20', 'limit' => '20',
])) ]))
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'total', 'total',
@ -47,35 +46,35 @@ class AssetIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.assets.index')) ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.assets.index')) ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.assets.index')) ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.assets.index')) ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.assets.index')) ->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseDoesNotContainInRows($assetB, 'asset_tag'); ->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.assets.index')) ->getJson(route('api.assets.index'))
->assertResponseDoesNotContainInRows($assetA, 'asset_tag') ->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Assets;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Company; use App\Models\Company;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -18,9 +17,9 @@ class AssetsForSelectListTest extends TestCase
Asset::factory()->create(['asset_tag' => '0001']); Asset::factory()->create(['asset_tag' => '0001']);
Asset::factory()->create(['asset_tag' => '0002']); Asset::factory()->create(['asset_tag' => '0002']);
Passport::actingAs(User::factory()->create()); $response = $this->actingAsForApi(User::factory()->create())
->getJson(route('assets.selectlist', ['search' => '000']))
$response = $this->getJson(route('assets.selectlist', ['search' => '000']))->assertOk(); ->assertOk();
$results = collect($response->json('results')); $results = collect($response->json('results'));
@ -42,35 +41,35 @@ class AssetsForSelectListTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('assets.selectlist', ['search' => '000'])) ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA) ->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB); ->assertResponseContainsInResults($assetB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('assets.selectlist', ['search' => '000'])) ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA) ->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB); ->assertResponseContainsInResults($assetB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('assets.selectlist', ['search' => '000'])) ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA) ->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB); ->assertResponseContainsInResults($assetB);
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('assets.selectlist', ['search' => '000'])) ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA) ->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB); ->assertResponseContainsInResults($assetB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('assets.selectlist', ['search' => '000'])) ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA) ->assertResponseContainsInResults($assetA)
->assertResponseDoesNotContainInResults($assetB); ->assertResponseDoesNotContainInResults($assetB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('assets.selectlist', ['search' => '000'])) ->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseDoesNotContainInResults($assetA) ->assertResponseDoesNotContainInResults($assetA)
->assertResponseContainsInResults($assetB); ->assertResponseContainsInResults($assetB);
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Assets;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Company; use App\Models\Company;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -15,8 +14,9 @@ class RequestableAssetsTest extends TestCase
public function testViewingRequestableAssetsRequiresCorrectPermission() public function testViewingRequestableAssetsRequiresCorrectPermission()
{ {
Passport::actingAs(User::factory()->create()); $this->actingAsForApi(User::factory()->create())
$this->getJson(route('api.assets.requestable'))->assertForbidden(); ->getJson(route('api.assets.requestable'))
->assertForbidden();
} }
public function testReturnsRequestableAssets() public function testReturnsRequestableAssets()
@ -24,8 +24,8 @@ class RequestableAssetsTest extends TestCase
$requestableAsset = Asset::factory()->requestable()->create(['asset_tag' => 'requestable']); $requestableAsset = Asset::factory()->requestable()->create(['asset_tag' => 'requestable']);
$nonRequestableAsset = Asset::factory()->nonrequestable()->create(['asset_tag' => 'non-requestable']); $nonRequestableAsset = Asset::factory()->nonrequestable()->create(['asset_tag' => 'non-requestable']);
Passport::actingAs(User::factory()->viewRequestableAssets()->create()); $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertOk() ->assertOk()
->assertResponseContainsInRows($requestableAsset, 'asset_tag') ->assertResponseContainsInRows($requestableAsset, 'asset_tag')
->assertResponseDoesNotContainInRows($nonRequestableAsset, 'asset_tag'); ->assertResponseDoesNotContainInRows($nonRequestableAsset, 'asset_tag');
@ -44,35 +44,35 @@ class RequestableAssetsTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag') ->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseDoesNotContainInRows($assetB, 'asset_tag'); ->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.assets.requestable')) ->getJson(route('api.assets.requestable'))
->assertResponseDoesNotContainInRows($assetA, 'asset_tag') ->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag'); ->assertResponseContainsInRows($assetB, 'asset_tag');
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Components;
use App\Models\Company; use App\Models\Company;
use App\Models\Component; use App\Models\Component;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -26,35 +25,35 @@ class ComponentIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.components.index')) ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA) ->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB); ->assertResponseContainsInRows($componentB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.components.index')) ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA) ->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB); ->assertResponseContainsInRows($componentB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.components.index')) ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA) ->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB); ->assertResponseContainsInRows($componentB);
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.components.index')) ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA) ->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB); ->assertResponseContainsInRows($componentB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.components.index')) ->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA) ->assertResponseContainsInRows($componentA)
->assertResponseDoesNotContainInRows($componentB); ->assertResponseDoesNotContainInRows($componentB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.components.index')) ->getJson(route('api.components.index'))
->assertResponseDoesNotContainInRows($componentA) ->assertResponseDoesNotContainInRows($componentA)
->assertResponseContainsInRows($componentB); ->assertResponseContainsInRows($componentB);
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Consumables;
use App\Models\Company; use App\Models\Company;
use App\Models\Consumable; use App\Models\Consumable;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -26,35 +25,35 @@ class ConsumablesIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.consumables.index')) ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA) ->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB); ->assertResponseContainsInRows($consumableB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.consumables.index')) ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA) ->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB); ->assertResponseContainsInRows($consumableB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.consumables.index')) ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA) ->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB); ->assertResponseContainsInRows($consumableB);
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.consumables.index')) ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA) ->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB); ->assertResponseContainsInRows($consumableB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.consumables.index')) ->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA) ->assertResponseContainsInRows($consumableA)
->assertResponseDoesNotContainInRows($consumableB); ->assertResponseDoesNotContainInRows($consumableB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.consumables.index')) ->getJson(route('api.consumables.index'))
->assertResponseDoesNotContainInRows($consumableA) ->assertResponseDoesNotContainInRows($consumableA)
->assertResponseContainsInRows($consumableB); ->assertResponseContainsInRows($consumableB);
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Departments;
use App\Models\Company; use App\Models\Company;
use App\Models\Department; use App\Models\Department;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -30,35 +29,35 @@ class DepartmentsIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.departments.index')) ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA) ->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB); ->assertResponseContainsInRows($departmentB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.departments.index')) ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA) ->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB); ->assertResponseContainsInRows($departmentB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.departments.index')) ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA) ->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB); ->assertResponseContainsInRows($departmentB);
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.departments.index')) ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA) ->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB); ->assertResponseContainsInRows($departmentB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.departments.index')) ->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA) ->assertResponseContainsInRows($departmentA)
->assertResponseDoesNotContainInRows($departmentB); ->assertResponseDoesNotContainInRows($departmentB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.departments.index')) ->getJson(route('api.departments.index'))
->assertResponseDoesNotContainInRows($departmentA) ->assertResponseDoesNotContainInRows($departmentA)
->assertResponseContainsInRows($departmentB); ->assertResponseContainsInRows($departmentB);
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Licenses;
use App\Models\Company; use App\Models\Company;
use App\Models\License; use App\Models\License;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -26,35 +25,35 @@ class LicensesIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.licenses.index')) ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA) ->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB); ->assertResponseContainsInRows($licenseB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.licenses.index')) ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA) ->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB); ->assertResponseContainsInRows($licenseB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.licenses.index')) ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA) ->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB); ->assertResponseContainsInRows($licenseB);
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.licenses.index')) ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA) ->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB); ->assertResponseContainsInRows($licenseB);
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.licenses.index')) ->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA) ->assertResponseContainsInRows($licenseA)
->assertResponseDoesNotContainInRows($licenseB); ->assertResponseDoesNotContainInRows($licenseB);
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.licenses.index')) ->getJson(route('api.licenses.index'))
->assertResponseDoesNotContainInRows($licenseA) ->assertResponseDoesNotContainInRows($licenseA)
->assertResponseContainsInRows($licenseB); ->assertResponseContainsInRows($licenseB);
} }

View file

@ -5,7 +5,6 @@ namespace Tests\Feature\Api\Users;
use App\Models\Company; use App\Models\Company;
use App\Models\User; use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson; use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -17,8 +16,8 @@ class UsersForSelectListTest extends TestCase
{ {
$users = User::factory()->superuser()->count(3)->create(); $users = User::factory()->superuser()->count(3)->create();
Passport::actingAs($users->first()); $this->actingAsForApi($users->first())
$this->getJson(route('api.users.selectlist')) ->getJson(route('api.users.selectlist'))
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'results', 'results',
@ -34,8 +33,9 @@ class UsersForSelectListTest extends TestCase
{ {
User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Skywalker']); User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Skywalker']);
Passport::actingAs(User::factory()->create()); $response = $this->actingAsForApi(User::factory()->create())
$response = $this->getJson(route('api.users.selectlist', ['search' => 'luke sky']))->assertOk(); ->getJson(route('api.users.selectlist', ['search' => 'luke sky']))
->assertOk();
$results = collect($response->json('results')); $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'])) ->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
->create(); ->create();
Passport::actingAs($jedi->users->first()); $response = $this->actingAsForApi($jedi->users->first())
$response = $this->getJson(route('api.users.selectlist'))->assertOk(); ->getJson(route('api.users.selectlist'))
->assertOk();
$results = collect($response->json('results')); $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'])) ->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
->create(); ->create();
Passport::actingAs($jedi->users->first()); $response = $this->actingAsForApi($jedi->users->first())
$response = $this->getJson(route('api.users.selectlist', ['search' => 'a']))->assertOk(); ->getJson(route('api.users.selectlist', ['search' => 'a']))
->assertOk();
$results = collect($response->json('results')); $results = collect($response->json('results'));

View file

@ -4,7 +4,6 @@ namespace Tests\Feature\Api\Users;
use App\Models\Company; use App\Models\Company;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; use Tests\TestCase;
@ -29,36 +28,36 @@ class UsersIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport(); $this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.users.index')) ->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name') ->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name'); ->assertResponseContainsInRows($userB, 'first_name');
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.users.index')) ->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name') ->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name'); ->assertResponseContainsInRows($userB, 'first_name');
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.users.index')) ->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name') ->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name'); ->assertResponseContainsInRows($userB, 'first_name');
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser); $this->actingAsForApi($superUser)
$this->getJson(route('api.users.index')) ->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name') ->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name'); ->assertResponseContainsInRows($userB, 'first_name');
Passport::actingAs($userInCompanyA); $this->actingAsForApi($userInCompanyA)
$this->getJson(route('api.users.index')) ->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name') ->assertResponseContainsInRows($userA, 'first_name')
->assertResponseDoesNotContainInRows($userB, 'first_name'); ->assertResponseDoesNotContainInRows($userB, 'first_name');
Passport::actingAs($userInCompanyB); $this->actingAsForApi($userInCompanyB)
$this->getJson(route('api.users.index')) ->getJson(route('api.users.index'))
->assertResponseDoesNotContainInRows($userA, 'first_name') ->assertResponseDoesNotContainInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name'); ->assertResponseContainsInRows($userB, 'first_name');
} }
} }

View file

@ -3,7 +3,6 @@
namespace Tests\Feature\Api\Users; namespace Tests\Feature\Api\Users;
use App\Models\User; use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
use Tests\TestCase; 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' => 'Luke', 'last_name' => 'Skywalker']);
User::factory()->create(['first_name' => 'Darth', 'last_name' => 'Vader']); User::factory()->create(['first_name' => 'Darth', 'last_name' => 'Vader']);
Passport::actingAs(User::factory()->viewUsers()->create()); $response = $this->actingAsForApi(User::factory()->viewUsers()->create())
$response = $this->getJson(route('api.users.index', ['search' => 'luke sky']))->assertOk(); ->getJson(route('api.users.index', ['search' => 'luke sky']))
->assertOk();
$results = collect($response->json('rows')); $results = collect($response->json('rows'));

View file

@ -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;
}
}

View file

@ -6,12 +6,14 @@ use App\Http\Middleware\SecurityHeaders;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\Support\CustomTestMacros; use Tests\Support\CustomTestMacros;
use Tests\Support\InteractsWithAuthentication;
use Tests\Support\InteractsWithSettings; use Tests\Support\InteractsWithSettings;
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
use CreatesApplication; use CreatesApplication;
use CustomTestMacros; use CustomTestMacros;
use InteractsWithAuthentication;
use LazilyRefreshDatabase; use LazilyRefreshDatabase;
private array $globallyDisabledMiddleware = [ private array $globallyDisabledMiddleware = [