Disable shields for testing

Hey, I'm using the filament-shield package. How do you disable all the shields for testing? In my testcase I do
protected function setUp(): void
{
parent::setUp();

$this->actingAs(User::factory()->create());
}
protected function setUp(): void
{
parent::setUp();

$this->actingAs(User::factory()->create());
}
But I still get a 403. I guess it's because of shields
16 Replies
LeandroFerreira
LeandroFerreira5mo ago
You should implement the FilamentUser interface https://filamentphp.com/docs/3.x/panels/installation#allowing-users-to-access-a-panel You could create the user and add the role
$user = User::factory()->create();
$user->assignRole('admin');
$user = User::factory()->create();
$user->assignRole('admin');
JJSanders
JJSanders5mo ago
Then I get: There is no role named 'admin' for guard 'web'
LeandroFerreira
LeandroFerreira5mo ago
What roles does it have?
JJSanders
JJSanders5mo ago
I guess none because I didn't seed anything I Guess I need to run my shieldseeder before every test
LeandroFerreira
LeandroFerreira5mo ago
You could do it and add protected $seed = true; in the TestCase
namespace Tests;

use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

use function Pest\Laravel\actingAs;

abstract class TestCase extends BaseTestCase
{
protected $seed = true;

use CreatesApplication;

protected function setUp(): void
{
parent::setUp();

$user = User::factory()->create();
$user->assignRole('your-role-here');
actingAs($user);
}
}
namespace Tests;

use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

use function Pest\Laravel\actingAs;

abstract class TestCase extends BaseTestCase
{
protected $seed = true;

use CreatesApplication;

protected function setUp(): void
{
parent::setUp();

$user = User::factory()->create();
$user->assignRole('your-role-here');
actingAs($user);
}
}
JJSanders
JJSanders5mo ago
But then it will run all seeders. I did $this->seed(ShieldSeeder::class); It solved the error regarding logging in.
LeandroFerreira
LeandroFerreira5mo ago
ok did it work?
JJSanders
JJSanders5mo ago
It got me passed logging in but didn't solve my test. B/c I had t he same error before logging in on my smoke test. But still getting: Expected response status code [>=200 <300] but received 403.
LeandroFerreira
LeandroFerreira5mo ago
The user cannot access the resource..
JJSanders
JJSanders5mo ago
Weird. This should work
protected function setUp(): void
{
parent::setUp();
$this->seed(ShieldSeeder::class);
$user = User::factory()->create();
$user->assignRole('super_admin');
$this->actingAs($user);
}
protected function setUp(): void
{
parent::setUp();
$this->seed(ShieldSeeder::class);
$user = User::factory()->create();
$user->assignRole('super_admin');
$this->actingAs($user);
}
LeandroFerreira
LeandroFerreira5mo ago
I agree.. did you implement the FilamentUser interface?
JJSanders
JJSanders5mo ago
Not sure what you mean by this Do you mean if I can access the application from a browser?
JJSanders
JJSanders5mo ago
It does implement that interface
LeandroFerreira
LeandroFerreira5mo ago
ok, what is the test?
JJSanders
JJSanders5mo ago
$this->get(ProductResource::getUrl('edit', [
'record' => $product,
]))->assertSuccessful();
$this->get(ProductResource::getUrl('edit', [
'record' => $product,
]))->assertSuccessful();
Want results from more Discord servers?
Add your server
More Posts