1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Controllers\Passport;
7
8use App\Http\Controllers\Passport\AuthorizationController;
9use App\Models\OAuth\Client;
10use Illuminate\Contracts\Auth\StatefulGuard;
11use Laravel\Passport\Contracts\AuthorizationViewResponse;
12use League\OAuth2\Server\AuthorizationServer;
13use Mockery;
14use Nyholm\Psr7\Factory\Psr17Factory;
15use Tests\TestCase;
16
17class AuthorizationControllerTest extends TestCase
18{
19 private $controller;
20
21 public function testAuthorizeNormalizes()
22 {
23 $client = Client::factory()->create();
24
25 $request = (new Psr17Factory())
26 ->createServerRequest('GET', $GLOBALS['cfg']['app']['url'].'/oauth/authorize')
27 ->withQueryParams(['client_id' => $client->getKey(), 'scope' => 'one two three']);
28
29 $actual = $this->invokeMethod($this->controller, 'normalizeRequestScopes', [$request])->getQueryParams()['scope'];
30
31 $this->assertSame($actual, 'identify one three two');
32 }
33
34 public function testNormalizeEmptyScopes()
35 {
36 $scopes = [];
37 $actual = $this->invokeMethod($this->controller, 'normalizeScopes', [$scopes]);
38
39 $this->assertSame($actual, ['identify']);
40 }
41
42 public function testNormalizeIdentifyScope()
43 {
44 $scopes = ['identify'];
45 $actual = $this->invokeMethod($this->controller, 'normalizeScopes', [$scopes]);
46
47 $this->assertSame($actual, ['identify']);
48 }
49
50 public function testNormalizeMultipleScopes()
51 {
52 $scopes = ['read', 'identify'];
53 $actual = $this->invokeMethod($this->controller, 'normalizeScopes', [$scopes]);
54
55 $this->assertSame($actual, ['identify', 'read']);
56 }
57
58 public function testNormalizeIdentifyNotRequested()
59 {
60 $scopes = ['read'];
61 $actual = $this->invokeMethod($this->controller, 'normalizeScopes', [$scopes]);
62
63 $this->assertSame($actual, ['identify', 'read']);
64 }
65
66 protected function setUp(): void
67 {
68 parent::setUp();
69
70 $this->controller = new AuthorizationController(
71 Mockery::mock(AuthorizationServer::class),
72 Mockery::mock(StatefulGuard::class),
73 Mockery::mock(AuthorizationViewResponse::class),
74 );
75 }
76
77 protected function tearDown(): void
78 {
79 parent::tearDown();
80
81 Mockery::close();
82 }
83}