Debian Codegraph @

extensions/OATHAuth/tests/phpunit/integration/OATHUserRepositoryTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Extension\OATHAuth\Tests\Integration;
22
23 use MediaWiki\Extension\OATHAuth\Key\TOTPKey;
24 use MediaWiki\Extension\OATHAuth\OATHAuthServices;
25 use MediaWiki\Extension\OATHAuth\OATHUserRepository;
26 use MediaWiki\User\CentralId\CentralIdLookup;
27 use MediaWiki\User\CentralId\CentralIdLookupFactory;
28 use MediaWikiIntegrationTestCase;
29 use Psr\Log\LoggerInterface;
30 use Wikimedia\ObjectCache\EmptyBagOStuff;
31 use Wikimedia\Rdbms\IConnectionProvider;
32 use Wikimedia\TestingAccessWrapper;
33
34 /**
35 * @author Taavi Väänänen <[email protected]tf>
36 * @group Database
37 * @coversDefaultClass \MediaWiki\Extension\OATHAuth\OATHUserRepository
38 */
39 class OATHUserRepositoryTest extends MediaWikiIntegrationTestCase {
40 /**
41 * @covers ::findByUser
42 * @covers ::loadKeysFromDatabase
43 * @covers ::createKey
44 * @covers ::updateKey
45 * @covers ::remove
46 */
47 public function testLookupCreateRemoveKey(): void {
48 $user = $this->getTestUser()->getUser();
49
50 $dbProvider = $this->createMock( IConnectionProvider::class );
51 $dbProvider->method( 'getPrimaryDatabase' )->with( 'virtual-oathauth' )->willReturn( $this->getDb() );
52 $dbProvider->method( 'getReplicaDatabase' )->with( 'virtual-oathauth' )->willReturn( $this->getDb() );
53
54 $moduleRegistry = OATHAuthServices::getInstance( $this->getServiceContainer() )->getModuleRegistry();
55 $module = $moduleRegistry->getModuleByKey( 'totp' );
56
57 $lookup = $this->createMock( CentralIdLookup::class );
58 $lookup->method( 'centralIdFromLocalUser' )
59 ->with( $user )
60 ->willReturn( 12345 );
61 $lookupFactory = $this->createMock( CentralIdLookupFactory::class );
62 $lookupFactory->method( 'getLookup' )->willReturn( $lookup );
63
64 $logger = $this->createMock( LoggerInterface::class );
65
66 $repository = new OATHUserRepository(
67 $dbProvider,
68 new EmptyBagOStuff(),
69 $moduleRegistry,
70 $lookupFactory,
71 $logger
72 );
73
74 $oathUser = $repository->findByUser( $user );
75 $this->assertEquals( 12345, $oathUser->getCentralId() );
76 $this->assertEquals( [], $oathUser->getKeys() );
77 // $this->expectDeprecationAndContinue( '/OATHUser::getModule/' );
78 $this->assertNull( $oathUser->getModule() );
79
80 /** @var TOTPKey $key */
81 $key = $repository->createKey(
82 $oathUser,
83 $module,
84 TOTPKey::newFromRandom()->jsonSerialize(),
85 '127.0.0.1'
86 );
87
88 $this->assertNotEmpty(
89 $this->getDb()->newSelectQueryBuilder()
90 ->select( '1' )
91 ->from( 'oathauth_devices' )
92 ->where( [ 'oad_user' => $oathUser->getCentralId() ] )
93 );
94
95 $this->assertArrayEquals( [ $key ], $oathUser->getKeys() );
96 // $this->expectDeprecationAndContinue( '/OATHUser::getModule/' );
97 $this->assertEquals( $module, $oathUser->getModule() );
98
99 // Test looking it up again from the database
100 $this->assertArrayEquals( [ $key ], $repository->findByUser( $user )->getKeys() );
101
102 // Use a scratch code, which causes the key to be updated.
103 TestingAccessWrapper::newFromObject( $key )->recoveryCodes = [ 'new scratch tokens' ];
104 $repository->updateKey( $oathUser, $key );
105
106 $this->assertEquals(
107 [ 'new scratch tokens' ],
108 $repository->findByUser( $user )->getKeys()[0]->getScratchTokens()
109 );
110
111 $repository->removeKey(
112 $oathUser,
113 $key,
114 '127.0.0.1',
115 true
116 );
117
118 $this->assertEquals( [], $oathUser->getKeys() );
119 // $this->expectDeprecationAndContinue( '/OATHUser::getModule/' );
120 $this->assertNull( $oathUser->getModule() );
121 $this->assertEquals( [], $repository->findByUser( $user )->getKeys() );
122 }
123
124 /**
125 * @covers ::findByUser
126 * @covers ::loadKeysFromDatabase
127 */
128 public function testUserWithNoCentralId() {
129 $user = $this->getTestUser()->getUser();
130
131 $lookup = $this->createMock( CentralIdLookup::class );
132 $lookup->method( 'centralIdFromLocalUser' )
133 ->with( $user )
134 ->willReturn( 0 );
135 $lookupFactory = $this->createMock( CentralIdLookupFactory::class );
136 $lookupFactory->method( 'getLookup' )->willReturn( $lookup );
137
138 $repository = new OATHUserRepository(
139 $this->createNoOpMock( IConnectionProvider::class ),
140 new EmptyBagOStuff(),
141 OATHAuthServices::getInstance( $this->getServiceContainer() )->getModuleRegistry(),
142 $lookupFactory,
143 $this->createMock( LoggerInterface::class ),
144 );
145
146 $oathUser = $repository->findByUser( $user );
147 $this->assertSame( 0, $oathUser->getCentralId() );
148 $this->assertEquals( [], $oathUser->getKeys() );
149 }
150 }