Debian Codegraph @

tests/phpunit/unit/includes/Rest/Handler/Helper/RestAuthorizeTraitTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler\Helper;
4
5 use MediaWiki\Page\PageIdentityValue;
6 use MediaWiki\Permissions\Authority;
7 use MediaWiki\Permissions\PermissionStatus;
8 use MediaWiki\Permissions\SimpleAuthority;
9 use MediaWiki\Rest\Handler\Helper\RestAuthorizeTrait;
10 use MediaWiki\Rest\LocalizedHttpException;
11 use MediaWiki\User\UserIdentityValue;
12 use MediaWikiUnitTestCase;
13
14 /**
15 * @covers \MediaWiki\Rest\Handler\Helper\RestAuthorizeTrait
16 */
17 class RestAuthorizeTraitTest extends MediaWikiUnitTestCase {
18
19 private function getTraitInstance(): object {
20 return new class () {
21 use RestAuthorizeTrait {
22 authorizeActionOrThrow as public;
23 authorizeReadOrThrow as public;
24 authorizeWriteOrThrow as public;
25 }
26 };
27 }
28
29 public function testPass() {
30 $user = new UserIdentityValue( 1, 'Test' );
31 $authority = new SimpleAuthority( $user, [ 'test' ] );
32 $target = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' );
33
34 $trait = $this->getTraitInstance();
35
36 $trait->authorizeActionOrThrow( $authority, 'test' );
37 $trait->authorizeReadOrThrow( $authority, 'test', $target );
38 $trait->authorizeWriteOrThrow( $authority, 'test', $target );
39
40 $this->addToAssertionCount( 3 );
41 }
42
43 public function testSimplePermissionFailure() {
44 $user = new UserIdentityValue( 1, 'Test' );
45 $authority = new SimpleAuthority( $user, [] );
46 $target = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' );
47
48 $trait = $this->getTraitInstance();
49
50 try {
51 $trait->authorizeActionOrThrow( $authority, 'test' );
52 $this->fail( 'Exception expected' );
53 } catch ( LocalizedHttpException $ex ) {
54 $this->assertSame( 403, $ex->getCode() );
55 }
56
57 try {
58 $trait->authorizeReadOrThrow( $authority, 'test', $target );
59 $this->fail( 'Exception expected' );
60 } catch ( LocalizedHttpException $ex ) {
61 $this->assertSame( 403, $ex->getCode() );
62 }
63
64 try {
65 $trait->authorizeWriteOrThrow( $authority, 'test', $target );
66 $this->fail( 'Exception expected' );
67 } catch ( LocalizedHttpException $ex ) {
68 $this->assertSame( 403, $ex->getCode() );
69 }
70 }
71
72 public function testPermissionFailureWithoutStatusMessage() {
73 $target = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' );
74
75 $authority = $this->createNoOpMock(
76 Authority::class,
77 [ 'authorizeAction', 'authorizeRead', 'authorizeWrite', ]
78 );
79
80 $authority->method( 'authorizeAction' )->willReturn( false );
81 $authority->method( 'authorizeRead' )->willReturn( false );
82 $authority->method( 'authorizeWrite' )->willReturn( false );
83
84 $trait = $this->getTraitInstance();
85
86 try {
87 $trait->authorizeActionOrThrow( $authority, 'test' );
88 $this->fail( 'Exception expected' );
89 } catch ( LocalizedHttpException $ex ) {
90 $this->assertSame( 403, $ex->getCode() );
91 }
92
93 try {
94 $trait->authorizeReadOrThrow( $authority, 'test', $target );
95 $this->fail( 'Exception expected' );
96 } catch ( LocalizedHttpException $ex ) {
97 $this->assertSame( 403, $ex->getCode() );
98 }
99
100 try {
101 $trait->authorizeWriteOrThrow( $authority, 'test', $target );
102 $this->fail( 'Exception expected' );
103 } catch ( LocalizedHttpException $ex ) {
104 $this->assertSame( 403, $ex->getCode() );
105 }
106 }
107
108 public function testRateLimitFailure() {
109 $target = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' );
110
111 $authority = $this->createNoOpMock(
112 Authority::class,
113 [ 'authorizeAction', 'authorizeRead', 'authorizeWrite', ]
114 );
115
116 $rateLimitCallback = static function ( $permission, $b = null, $c = null ) {
117 if ( $b instanceof PermissionStatus ) {
118 $status = $b;
119 } else {
120 $status = $c;
121 }
122
123 $status->setRateLimitExceeded();
124 $status->setPermission( $permission );
125 return false;
126 };
127
128 $authority->method( 'authorizeAction' )->willReturnCallback( $rateLimitCallback );
129 $authority->method( 'authorizeRead' )->willReturnCallback( $rateLimitCallback );
130 $authority->method( 'authorizeWrite' )->willReturnCallback( $rateLimitCallback );
131
132 $trait = $this->getTraitInstance();
133
134 try {
135 $trait->authorizeActionOrThrow( $authority, 'test' );
136 $this->fail( 'Exception expected' );
137 } catch ( LocalizedHttpException $ex ) {
138 $this->assertSame( 429, $ex->getCode() );
139 }
140
141 try {
142 $trait->authorizeReadOrThrow( $authority, 'test', $target );
143 $this->fail( 'Exception expected' );
144 } catch ( LocalizedHttpException $ex ) {
145 $this->assertSame( 429, $ex->getCode() );
146 }
147
148 try {
149 $trait->authorizeWriteOrThrow( $authority, 'test', $target );
150 $this->fail( 'Exception expected' );
151 } catch ( LocalizedHttpException $ex ) {
152 $this->assertSame( 429, $ex->getCode() );
153 }
154 }
155
156 }