MediaWiki  REL1_31
BotPasswordSessionProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
5 use Psr\Log\LogLevel;
7 use Wikimedia\TestingAccessWrapper;
8 
15 
16  private $config;
17 
18  private function getProvider( $name = null, $prefix = null ) {
20 
21  $params = [
22  'priority' => 40,
23  'sessionCookieName' => $name,
24  'sessionCookieOptions' => [],
25  ];
26  if ( $prefix !== null ) {
27  $params['sessionCookieOptions']['prefix'] = $prefix;
28  }
29 
30  if ( !$this->config ) {
31  $this->config = new \HashConfig( [
32  'CookiePrefix' => 'wgCookiePrefix',
33  'EnableBotPasswords' => true,
34  'BotPasswordsDatabase' => false,
35  'SessionProviders' => $wgSessionProviders + [
38  'args' => [ $params ],
39  ]
40  ],
41  ] );
42  }
43  $manager = new SessionManager( [
44  'config' => new \MultiConfig( [ $this->config, \RequestContext::getMain()->getConfig() ] ),
45  'logger' => new \Psr\Log\NullLogger,
46  'store' => new TestBagOStuff,
47  ] );
48 
49  return $manager->getProvider( BotPasswordSessionProvider::class );
50  }
51 
52  protected function setUp() {
53  parent::setUp();
54 
55  $this->setMwGlobals( [
56  'wgEnableBotPasswords' => true,
57  'wgBotPasswordsDatabase' => false,
58  'wgCentralIdLookupProvider' => 'local',
59  'wgGrantPermissions' => [
60  'test' => [ 'read' => true ],
61  ],
62  ] );
63  }
64 
65  public function addDBDataOnce() {
66  $passwordFactory = new \PasswordFactory();
67  $passwordFactory->init( \RequestContext::getMain()->getConfig() );
68  $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
69 
70  $sysop = static::getTestSysop()->getUser();
71  $userId = \CentralIdLookup::factory( 'local' )->centralIdFromName( $sysop->getName() );
72 
73  $dbw = wfGetDB( DB_MASTER );
74  $dbw->delete(
75  'bot_passwords',
76  [ 'bp_user' => $userId, 'bp_app_id' => 'BotPasswordSessionProvider' ],
77  __METHOD__
78  );
79  $dbw->insert(
80  'bot_passwords',
81  [
82  'bp_user' => $userId,
83  'bp_app_id' => 'BotPasswordSessionProvider',
84  'bp_password' => $passwordHash->toString(),
85  'bp_token' => 'token!',
86  'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
87  'bp_grants' => '["test"]',
88  ],
89  __METHOD__
90  );
91  }
92 
93  public function testConstructor() {
94  try {
95  $provider = new BotPasswordSessionProvider();
96  $this->fail( 'Expected exception not thrown' );
97  } catch ( \InvalidArgumentException $ex ) {
98  $this->assertSame(
99  'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: priority must be specified',
100  $ex->getMessage()
101  );
102  }
103 
104  try {
105  $provider = new BotPasswordSessionProvider( [
106  'priority' => SessionInfo::MIN_PRIORITY - 1
107  ] );
108  $this->fail( 'Expected exception not thrown' );
109  } catch ( \InvalidArgumentException $ex ) {
110  $this->assertSame(
111  'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
112  $ex->getMessage()
113  );
114  }
115 
116  try {
117  $provider = new BotPasswordSessionProvider( [
118  'priority' => SessionInfo::MAX_PRIORITY + 1
119  ] );
120  $this->fail( 'Expected exception not thrown' );
121  } catch ( \InvalidArgumentException $ex ) {
122  $this->assertSame(
123  'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
124  $ex->getMessage()
125  );
126  }
127 
128  $provider = new BotPasswordSessionProvider( [
129  'priority' => 40
130  ] );
131  $priv = TestingAccessWrapper::newFromObject( $provider );
132  $this->assertSame( 40, $priv->priority );
133  $this->assertSame( '_BPsession', $priv->sessionCookieName );
134  $this->assertSame( [], $priv->sessionCookieOptions );
135 
136  $provider = new BotPasswordSessionProvider( [
137  'priority' => 40,
138  'sessionCookieName' => null,
139  ] );
140  $priv = TestingAccessWrapper::newFromObject( $provider );
141  $this->assertSame( '_BPsession', $priv->sessionCookieName );
142 
143  $provider = new BotPasswordSessionProvider( [
144  'priority' => 40,
145  'sessionCookieName' => 'Foo',
146  'sessionCookieOptions' => [ 'Bar' ],
147  ] );
148  $priv = TestingAccessWrapper::newFromObject( $provider );
149  $this->assertSame( 'Foo', $priv->sessionCookieName );
150  $this->assertSame( [ 'Bar' ], $priv->sessionCookieOptions );
151  }
152 
153  public function testBasics() {
154  $provider = $this->getProvider();
155 
156  $this->assertTrue( $provider->persistsSessionId() );
157  $this->assertFalse( $provider->canChangeUser() );
158 
159  $this->assertNull( $provider->newSessionInfo() );
160  $this->assertNull( $provider->newSessionInfo( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) );
161  }
162 
163  public function testProvideSessionInfo() {
164  $provider = $this->getProvider();
165  $request = new \FauxRequest;
166  $request->setCookie( '_BPsession', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'wgCookiePrefix' );
167 
168  if ( !defined( 'MW_API' ) ) {
169  $this->assertNull( $provider->provideSessionInfo( $request ) );
170  define( 'MW_API', 1 );
171  }
172 
173  $info = $provider->provideSessionInfo( $request );
174  $this->assertInstanceOf( SessionInfo::class, $info );
175  $this->assertSame( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', $info->getId() );
176 
177  $this->config->set( 'EnableBotPasswords', false );
178  $this->assertNull( $provider->provideSessionInfo( $request ) );
179  $this->config->set( 'EnableBotPasswords', true );
180 
181  $this->assertNull( $provider->provideSessionInfo( new \FauxRequest ) );
182  }
183 
184  public function testNewSessionInfoForRequest() {
185  $provider = $this->getProvider();
186  $user = static::getTestSysop()->getUser();
187  $request = $this->getMockBuilder( \FauxRequest::class )
188  ->setMethods( [ 'getIP' ] )->getMock();
189  $request->expects( $this->any() )->method( 'getIP' )
190  ->will( $this->returnValue( '127.0.0.1' ) );
191  $bp = \BotPassword::newFromUser( $user, 'BotPasswordSessionProvider' );
192 
193  $session = $provider->newSessionForRequest( $user, $bp, $request );
194  $this->assertInstanceOf( Session::class, $session );
195 
196  $this->assertEquals( $session->getId(), $request->getSession()->getId() );
197  $this->assertEquals( $user->getName(), $session->getUser()->getName() );
198 
199  $this->assertEquals( [
200  'centralId' => $bp->getUserCentralId(),
201  'appId' => $bp->getAppId(),
202  'token' => $bp->getToken(),
203  'rights' => [ 'read' ],
204  ], $session->getProviderMetadata() );
205 
206  $this->assertEquals( [ 'read' ], $session->getAllowedUserRights() );
207  }
208 
209  public function testCheckSessionInfo() {
210  $logger = new \TestLogger( true );
211  $provider = $this->getProvider();
212  $provider->setLogger( $logger );
213 
214  $user = static::getTestSysop()->getUser();
215  $request = $this->getMockBuilder( \FauxRequest::class )
216  ->setMethods( [ 'getIP' ] )->getMock();
217  $request->expects( $this->any() )->method( 'getIP' )
218  ->will( $this->returnValue( '127.0.0.1' ) );
219  $bp = \BotPassword::newFromUser( $user, 'BotPasswordSessionProvider' );
220 
221  $data = [
222  'provider' => $provider,
223  'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
224  'userInfo' => UserInfo::newFromUser( $user, true ),
225  'persisted' => false,
226  'metadata' => [
227  'centralId' => $bp->getUserCentralId(),
228  'appId' => $bp->getAppId(),
229  'token' => $bp->getToken(),
230  ],
231  ];
232  $dataMD = $data['metadata'];
233 
234  foreach ( array_keys( $data['metadata'] ) as $key ) {
235  $data['metadata'] = $dataMD;
236  unset( $data['metadata'][$key] );
237  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
238  $metadata = $info->getProviderMetadata();
239 
240  $this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
241  $this->assertSame( [
242  [ LogLevel::INFO, 'Session "{session}": Missing metadata: {missing}' ]
243  ], $logger->getBuffer() );
244  $logger->clearBuffer();
245  }
246 
247  $data['metadata'] = $dataMD;
248  $data['metadata']['appId'] = 'Foobar';
249  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
250  $metadata = $info->getProviderMetadata();
251  $this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
252  $this->assertSame( [
253  [ LogLevel::INFO, 'Session "{session}": No BotPassword for {centralId} {appId}' ],
254  ], $logger->getBuffer() );
255  $logger->clearBuffer();
256 
257  $data['metadata'] = $dataMD;
258  $data['metadata']['token'] = 'Foobar';
259  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
260  $metadata = $info->getProviderMetadata();
261  $this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
262  $this->assertSame( [
263  [ LogLevel::INFO, 'Session "{session}": BotPassword token check failed' ],
264  ], $logger->getBuffer() );
265  $logger->clearBuffer();
266 
267  $request2 = $this->getMockBuilder( \FauxRequest::class )
268  ->setMethods( [ 'getIP' ] )->getMock();
269  $request2->expects( $this->any() )->method( 'getIP' )
270  ->will( $this->returnValue( '10.0.0.1' ) );
271  $data['metadata'] = $dataMD;
272  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
273  $metadata = $info->getProviderMetadata();
274  $this->assertFalse( $provider->refreshSessionInfo( $info, $request2, $metadata ) );
275  $this->assertSame( [
276  [ LogLevel::INFO, 'Session "{session}": Restrictions check failed' ],
277  ], $logger->getBuffer() );
278  $logger->clearBuffer();
279 
280  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
281  $metadata = $info->getProviderMetadata();
282  $this->assertTrue( $provider->refreshSessionInfo( $info, $request, $metadata ) );
283  $this->assertSame( [], $logger->getBuffer() );
284  $this->assertEquals( $dataMD + [ 'rights' => [ 'read' ] ], $metadata );
285  }
286 
287  public function testGetAllowedUserRights() {
288  $logger = new \TestLogger( true );
289  $provider = $this->getProvider();
290  $provider->setLogger( $logger );
291 
293  $backendPriv = TestingAccessWrapper::newFromObject( $backend );
294 
295  try {
296  $provider->getAllowedUserRights( $backend );
297  $this->fail( 'Expected exception not thrown' );
298  } catch ( \InvalidArgumentException $ex ) {
299  $this->assertSame( 'Backend\'s provider isn\'t $this', $ex->getMessage() );
300  }
301 
302  $backendPriv->provider = $provider;
303  $backendPriv->providerMetadata = [ 'rights' => [ 'foo', 'bar', 'baz' ] ];
304  $this->assertSame( [ 'foo', 'bar', 'baz' ], $provider->getAllowedUserRights( $backend ) );
305  $this->assertSame( [], $logger->getBuffer() );
306 
307  $backendPriv->providerMetadata = [ 'foo' => 'bar' ];
308  $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
309  $this->assertSame( [
310  [
311  LogLevel::DEBUG,
312  'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
313  'No provider metadata, returning no rights allowed'
314  ]
315  ], $logger->getBuffer() );
316  $logger->clearBuffer();
317 
318  $backendPriv->providerMetadata = [ 'rights' => 'bar' ];
319  $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
320  $this->assertSame( [
321  [
322  LogLevel::DEBUG,
323  'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
324  'No provider metadata, returning no rights allowed'
325  ]
326  ], $logger->getBuffer() );
327  $logger->clearBuffer();
328 
329  $backendPriv->providerMetadata = null;
330  $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
331  $this->assertSame( [
332  [
333  LogLevel::DEBUG,
334  'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
335  'No provider metadata, returning no rights allowed'
336  ]
337  ], $logger->getBuffer() );
338  $logger->clearBuffer();
339  }
340 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWiki\Session\BotPasswordSessionProviderTest
Session Database MediaWiki\Session\BotPasswordSessionProvider.
Definition: BotPasswordSessionProviderTest.php:14
MultiConfig
Provides a fallback sequence for Config objects.
Definition: MultiConfig.php:28
$params
$params
Definition: styleTest.css.php:40
MediaWiki\Session\BotPasswordSessionProviderTest\testGetAllowedUserRights
testGetAllowedUserRights()
Definition: BotPasswordSessionProviderTest.php:287
MediaWiki\Session\BotPasswordSessionProviderTest\setUp
setUp()
Definition: BotPasswordSessionProviderTest.php:52
MediaWiki\Session\UserInfo\newFromUser
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition: UserInfo.php:116
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Session\BotPasswordSessionProviderTest\testCheckSessionInfo
testCheckSessionInfo()
Definition: BotPasswordSessionProviderTest.php:209
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
DB_MASTER
const DB_MASTER
Definition: defines.php:26
BotPassword\newFromUser
static newFromUser(User $user, $appId, $flags=self::READ_NORMAL)
Load a BotPassword from the database.
Definition: BotPassword.php:92
MediaWiki\Session\TestUtils\getDummySessionBackend
static getDummySessionBackend()
If you need a SessionBackend for testing but don't want to create a real one, use this.
Definition: TestUtils.php:65
MediaWiki\Session\SessionInfo\MAX_PRIORITY
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
MediaWiki\Session\TestBagOStuff
BagOStuff with utility functions for MediaWiki\\Session\\* testing.
Definition: TestBagOStuff.php:8
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:50
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:434
MediaWiki\Session\SessionInfo
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
MediaWiki\Session\BotPasswordSessionProviderTest\addDBDataOnce
addDBDataOnce()
Stub.
Definition: BotPasswordSessionProviderTest.php:65
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
$wgSessionProviders
$wgSessionProviders
MediaWiki\Session\SessionProvider configuration.
Definition: DefaultSettings.php:4971
MediaWiki\Session\BotPasswordSessionProviderTest\getProvider
getProvider( $name=null, $prefix=null)
Definition: BotPasswordSessionProviderTest.php:18
MediaWiki\Session\BotPasswordSessionProviderTest\testConstructor
testConstructor()
Definition: BotPasswordSessionProviderTest.php:93
MediaWiki\Session\BotPasswordSessionProviderTest\testNewSessionInfoForRequest
testNewSessionInfoForRequest()
Definition: BotPasswordSessionProviderTest.php:184
MediaWiki\Session\BotPasswordSessionProviderTest\testProvideSessionInfo
testProvideSessionInfo()
Definition: BotPasswordSessionProviderTest.php:163
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
MediaWiki\Session\BotPasswordSessionProviderTest\testBasics
testBasics()
Definition: BotPasswordSessionProviderTest.php:153
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2806
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:46
MediaWiki\Session\SessionInfo\MIN_PRIORITY
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36
MediaWiki\Session\BotPasswordSessionProviderTest\$config
$config
Definition: BotPasswordSessionProviderTest.php:16