Debian Codegraph @

tests/phpunit/includes/auth/CheckBlocksSecondaryAuthenticationProviderTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Auth;
4
5 use MediaWiki\Auth\AuthenticationResponse;
6 use MediaWiki\Auth\AuthManager;
7 use MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider;
8 use MediaWiki\Block\DatabaseBlock;
9 use MediaWiki\Config\HashConfig;
10 use MediaWiki\Context\RequestContext;
11 use MediaWiki\MainConfigNames;
12 use MediaWiki\Request\FauxRequest;
13 use MediaWiki\Tests\Unit\Auth\AuthenticationProviderTestTrait;
14 use MediaWiki\User\User;
15 use MediaWikiIntegrationTestCase;
16 use PHPUnit\Framework\MockObject\MockObject;
17 use Wikimedia\TestingAccessWrapper;
18
19 /**
20 * @group AuthManager
21 * @group Database
22 * @covers \MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider
23 */
24 class CheckBlocksSecondaryAuthenticationProviderTest extends MediaWikiIntegrationTestCase {
25 use AuthenticationProviderTestTrait;
26
27 public function testConstructor() {
28 $provider = new CheckBlocksSecondaryAuthenticationProvider();
29 $providerPriv = TestingAccessWrapper::newFromObject( $provider );
30 $config = new HashConfig( [
31 MainConfigNames::BlockDisablesLogin => false
32 ] );
33 $this->initProvider( $provider, $config );
34 $this->assertSame( false, $providerPriv->blockDisablesLogin );
35
36 $provider = new CheckBlocksSecondaryAuthenticationProvider(
37 [ 'blockDisablesLogin' => true ]
38 );
39 $providerPriv = TestingAccessWrapper::newFromObject( $provider );
40 $config = new HashConfig( [
41 MainConfigNames::BlockDisablesLogin => false
42 ] );
43 $this->initProvider( $provider, $config );
44 $this->assertSame( true, $providerPriv->blockDisablesLogin );
45 }
46
47 public function testBasics() {
48 $provider = new CheckBlocksSecondaryAuthenticationProvider();
49 $user = $this->getTestSysop()->getUser();
50
51 $this->assertEquals(
52 AuthenticationResponse::newAbstain(),
53 $provider->beginSecondaryAccountCreation( $user, $user, [] )
54 );
55 }
56
57 /**
58 * @dataProvider provideGetAuthenticationRequests
59 * @param string $action
60 * @param array $response
61 */
62 public function testGetAuthenticationRequests( $action, $response ) {
63 $provider = new CheckBlocksSecondaryAuthenticationProvider();
64
65 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
66 }
67
68 public static function provideGetAuthenticationRequests() {
69 return [
70 [ AuthManager::ACTION_LOGIN, [] ],
71 [ AuthManager::ACTION_CREATE, [] ],
72 [ AuthManager::ACTION_LINK, [] ],
73 [ AuthManager::ACTION_CHANGE, [] ],
74 [ AuthManager::ACTION_REMOVE, [] ],
75 ];
76 }
77
78 /**
79 * @param array $blockOptions Options for DatabaseBlock
80 * @return User
81 */
82 private function getBlockedUser( array $blockOptions ): User {
83 $user = $this->getMutableTestUser()->getUser();
84 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
85 $block = new DatabaseBlock( $blockOptions + [
86 'address' => $user,
87 'by' => $this->getTestSysop()->getUser(),
88 'reason' => __METHOD__,
89 'expiry' => time() + 100500,
90 ] );
91 $blockStore->insertBlock( $block );
92 if ( $block->getType() === DatabaseBlock::TYPE_IP ) {
93 // When an ip is blocked, the provided user object needs to know the ip
94 // That allows BlockManager::getUserBlock to load the ip block for this user
95 $request = $this->getMockBuilder( FauxRequest::class )
96 ->onlyMethods( [ 'getIP' ] )->getMock();
97 $request->method( 'getIP' )
98 ->willReturn( $blockOptions['address'] );
99 // The global request is used by User::getRequest
100 RequestContext::getMain()->setRequest( $request );
101 // The ip from request is only used for the global user
102 RequestContext::getMain()->setUser( $user );
103 }
104
105 return $user;
106 }
107
108 /**
109 * @param array $blockOptions Options for DatabaseBlock
110 * @return User
111 */
112 private function getIpBlockedUser( array $blockOptions ) {
113 static $ip = 10;
114 return $this->getBlockedUser( [
115 'address' => '10.10.10.' . $ip++,
116 ] + $blockOptions );
117 }
118
119 /**
120 * @param array $blockOptions Options for DatabaseBlock
121 * @return User
122 */
123 private function getGloballyIpBlockedUser( array $blockOptions ) {
124 static $ip = 100;
125 $user = $this->getMutableTestUser()->getUser();
126 TestingAccessWrapper::newFromObject( $user )->mGlobalBlock = new DatabaseBlock( $blockOptions + [
127 'address' => '10.10.10.' . $ip++,
128 'by' => $this->getTestSysop()->getUser(),
129 'reason' => __METHOD__,
130 'expiry' => time() + 100500,
131 ] );
132 return $user;
133 }
134
135 /**
136 * @param string $blockType One of 'user', 'ip', 'global-ip', 'none'
137 * @param array $blockOptions Options for DatabaseBlock
138 * @return User
139 */
140 private function getAnyBlockedUser( string $blockType, array $blockOptions = [] ) {
141 if ( $blockType === 'user' ) {
142 $user = $this->getBlockedUser( $blockOptions );
143 } elseif ( $blockType === 'ip' ) {
144 $user = $this->getIpBlockedUser( $blockOptions );
145 } elseif ( $blockType === 'global-ip' ) {
146 $user = $this->getGloballyIpBlockedUser( $blockOptions );
147 } elseif ( $blockType === 'none' ) {
148 $user = $this->getTestUser()->getUser();
149 } else {
150 $this->fail( 'Invalid block type' );
151 }
152 return $user;
153 }
154
155 /**
156 * @dataProvider provideBeginSecondaryAuthentication
157 */
158 public function testBeginSecondaryAuthentication(
159 string $blockType,
160 array $blockOptions,
161 bool $blockDisablesLogin,
162 string $expectedResponseStatus
163 ) {
164 /** @var AuthManager|MockObject $authManager */
165 $authManager = $this->createNoOpMock( AuthManager::class );
166 $provider = new CheckBlocksSecondaryAuthenticationProvider(
167 [ 'blockDisablesLogin' => $blockDisablesLogin ]
168 );
169 $this->initProvider( $provider, new HashConfig(), null, $authManager );
170
171 $user = $this->getAnyBlockedUser( $blockType, $blockOptions );
172
173 $response = $provider->beginSecondaryAuthentication( $user, [] );
174 $this->assertEquals( $expectedResponseStatus, $response->status );
175 }
176
177 public static function provideBeginSecondaryAuthentication() {
178 // Only fail authentication when $wgBlockDisablesLogin is set, the block is not partial,
179 // and not an IP block. Global blocks could in theory go either way, but GlobalBlocking
180 // extension blocks are always IP blocks so we mock them as such.
181 return [
182 // block type (user/ip/global/none), block options, wgBlockDisablesLogin, expected response status
183 'block does not disable login' => [ 'user', [], false, AuthenticationResponse::ABSTAIN ],
184 'not blocked' => [ 'none', [], true, AuthenticationResponse::PASS ],
185 'partial block' => [ 'user', [ 'sitewide' => false ], true, AuthenticationResponse::PASS ],
186 'ip block' => [ 'ip', [], true, AuthenticationResponse::PASS ],
187 'block' => [ 'user', [], true, AuthenticationResponse::FAIL ],
188 'global block' => [ 'global-ip', [], true, AuthenticationResponse::PASS ],
189 ];
190 }
191
192 }