| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
tf> |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class OATHUserRepositoryTest extends MediaWikiIntegrationTestCase { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 78 |
$this->assertNull( $oathUser->getModule() ); |
| 79 |
|
| 80 |
|
| 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 |
|
| 97 |
$this->assertEquals( $module, $oathUser->getModule() ); |
| 98 |
|
| 99 |
|
| 100 |
$this->assertArrayEquals( [ $key ], $repository->findByUser( $user )->getKeys() ); |
| 101 |
|
| 102 |
|
| 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 |
|
| 120 |
$this->assertNull( $oathUser->getModule() ); |
| 121 |
$this->assertEquals( [], $repository->findByUser( $user )->getKeys() ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
} |