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\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');
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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'));

View file

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

View file

@ -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'));

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\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 = [