Skip to content

Commit

Permalink
Add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cl8n committed Dec 3, 2022
1 parent c81c7d9 commit 48a135a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions tests/Models/OAuth/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\OAuth\Token;
use App\Models\User;
use Database\Factories\OAuth\RefreshTokenFactory;
use Ds\Set;
use Illuminate\Support\Facades\Event;
use Laravel\Passport\Passport;
use Tests\TestCase;
Expand Down Expand Up @@ -129,6 +130,46 @@ public function testDelegationRequiresChatBot(?string $group, ?string $expectedE
$this->createToken(null, ['delegate'], $client);
}

/**
* @dataProvider implyScopesDataProvider
*/
public function testImplyScopes(string $scope, bool $can): void
{
$user = User::factory()->create();
$token = $this->createToken($user, ['*']);

$this->assertSame($can, $token->can($scope));
}

public function testLovedScope(): void
{
$client = Client::factory()->create();
config()->set('osu.loved.oauth_client_id', $client->getKey());

$this->expectNotToPerformAssertions();

$this->createToken(null, ['loved'], $client);
}

public function testLovedScopeInvalidClient(): void
{
$client = Client::factory()->create();
config()->set('osu.loved.oauth_client_id', $client->getKey() + 1);

$this->expectException(InvalidScopeException::class);

$this->createToken(null, ['loved'], $client);
}

public function testLovedScopeNoClientConfigured(): void
{
config()->set('osu.loved.oauth_client_id', null);

$this->expectException(InvalidScopeException::class);

$this->createToken(null, ['loved']);
}

/**
* @dataProvider scopesDataProvider
*
Expand Down Expand Up @@ -233,12 +274,24 @@ public function delegationRequiresChatBotDataProvider()
];
}

public function implyScopesDataProvider(): array
{
$cannotImply = new Set(['delegate', 'loved']);

return Passport::scopes()
->pluck('id')
->map(fn (string $id) => [$id, !$cannotImply->contains($id)])
->values()
->toArray();
}

public function scopesDataProvider()
{
return [
'null is not a valid scope' => [null, InvalidScopeException::class],
'empty scope should fail' => [[], InvalidScopeException::class],
'all scope is allowed' => [['*'], null],
'loved scope is not allowed' => [['loved'], InvalidScopeException::class],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function regularOAuthScopesDataProvider()

foreach (Passport::scopes()->pluck('id') as $scope) {
// just skip over any scopes that require special conditions for now.
if (in_array($scope, ['chat.write', 'delegate'], true)) {
if (in_array($scope, ['chat.write', 'delegate', 'loved'], true)) {
continue;
}

Expand Down

0 comments on commit 48a135a

Please sign in to comment.