Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix API vs Web TeamsHasPermissions, add Tests #89

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/HasTeams.php
Original file line number Diff line number Diff line change
@@ -154,7 +154,8 @@ public function hasTeamPermission($team, string $permission)
}

if (in_array(HasApiTokens::class, class_uses_recursive($this)) &&
! $this->tokenCan($permission)) {
! $this->tokenCan($permission) &&
$this->currentAccessToken() !== null) {
return false;
}

26 changes: 26 additions & 0 deletions tests/TeamBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Team;
use Laravel\Jetstream\Tests\Fixtures\User;
use Laravel\Sanctum\Sanctum;
use Laravel\Sanctum\TransientToken;

class TeamBehaviorTest extends OrchestraTestCase
@@ -54,6 +55,15 @@ public function test_team_relationship_methods()
$otherUser->teams()->attach($team, ['role' => 'editor']);
$otherUser = $otherUser->fresh();

$this->assertTrue($otherUser->belongsToTeam($team));
$this->assertFalse($otherUser->ownsTeam($team));

$this->assertTrue($otherUser->hasTeamPermission($team, 'foo'));
$this->assertFalse($otherUser->hasTeamPermission($team, 'bar'));

$this->assertTrue($team->userHasPermission($otherUser, 'foo'));
$this->assertFalse($team->userHasPermission($otherUser, 'bar'));

$otherUser->withAccessToken(new TransientToken);

$this->assertTrue($otherUser->belongsToTeam($team));
@@ -88,9 +98,25 @@ public function test_has_team_permission_checks_token_permissions()
'password' => 'secret',
]);

$authToken = new Sanctum;
$adam = $authToken->actingAs($adam, ['bar'], []);

$team->users()->attach($adam, ['role' => 'admin']);

$this->assertFalse($adam->hasTeamPermission($team, 'foo'));

$john = User::forceCreate([
'name' => 'John Doe',
'email' => 'john@doe.com',
'password' => 'secret',
]);

$authToken = new Sanctum;
$john = $authToken->actingAs($john, ['foo'], []);

$team->users()->attach($john, ['role' => 'admin']);

$this->assertTrue($john->hasTeamPermission($team, 'foo'));
}

protected function migrate()