| 1 |
<?php |
| 2 |
|
| 3 |
use LoginNotify\LoginNotify; |
| 4 |
use MediaWiki\CheckUser as CU; |
| 5 |
use MediaWiki\Config\HashConfig; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\Logger\LoggerFactory; |
| 9 |
use MediaWiki\Request\FauxRequest; |
| 10 |
use MediaWiki\User\CentralId\LocalIdLookup; |
| 11 |
use MediaWiki\User\User; |
| 12 |
use MediaWiki\User\UserFactory; |
| 13 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 14 |
use Wikimedia\TestingAccessWrapper; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class LoginNotifyTest extends MediaWikiIntegrationTestCase { |
| 24 |
|
| 25 |
|
| 26 |
private $inst; |
| 27 |
|
| 28 |
|
| 29 |
private $userFactory; |
| 30 |
|
| 31 |
public function setUpLoginNotify( $configValues = [] ) { |
| 32 |
$day = 86400; |
| 33 |
$config = new HashConfig( $configValues + [ |
| 34 |
"LoginNotifyAttemptsKnownIP" => 15, |
| 35 |
"LoginNotifyExpiryKnownIP" => 7 * $day, |
| 36 |
"LoginNotifyAttemptsNewIP" => 5, |
| 37 |
"LoginNotifyExpiryNewIP" => 14 * $day, |
| 38 |
"LoginNotifyCheckKnownIPs" => true, |
| 39 |
"LoginNotifyEnableOnSuccess" => true, |
| 40 |
"LoginNotifySecretKey" => "Secret Stuff!", |
| 41 |
"SecretKey" => "", |
| 42 |
"LoginNotifyCookieExpire" => 180 * $day, |
| 43 |
"LoginNotifyCookieDomain" => null, |
| 44 |
"LoginNotifyMaxCookieRecords" => 6, |
| 45 |
"LoginNotifyCacheLoginIPExpiry" => 60 * $day, |
| 46 |
"LoginNotifyUseCheckUser" => false, |
| 47 |
"LoginNotifyUseSeenTable" => true, |
| 48 |
"LoginNotifyUseCentralId" => false, |
| 49 |
"LoginNotifySeenExpiry" => 180 * $day, |
| 50 |
"LoginNotifySeenBucketSize" => 15 * $day, |
| 51 |
"UpdateRowsPerQuery" => 100, |
| 52 |
] ); |
| 53 |
$services = $this->getServiceContainer(); |
| 54 |
$this->inst = TestingAccessWrapper::newFromObject( |
| 55 |
new LoginNotify( |
| 56 |
new ServiceOptions( LoginNotify::CONSTRUCTOR_OPTIONS, $config ), |
| 57 |
new HashBagOStuff, |
| 58 |
LoggerFactory::getInstance( 'LoginNotify' ), |
| 59 |
$services->getStatsdDataFactory(), |
| 60 |
$services->getDBLoadBalancerFactory(), |
| 61 |
$services->getJobQueueGroup(), |
| 62 |
new LocalIdLookup( |
| 63 |
new HashConfig( [ |
| 64 |
'SharedDB' => false, |
| 65 |
'SharedTables' => [], |
| 66 |
'LocalDatabases' => [] |
| 67 |
] ), |
| 68 |
$services->getDBLoadBalancerFactory(), |
| 69 |
$services->getHideUserUtils() |
| 70 |
), |
| 71 |
$services->getAuthManager() |
| 72 |
) |
| 73 |
); |
| 74 |
$this->inst->setLogger( LoggerFactory::getInstance( 'LoginNotify' ) ); |
| 75 |
$this->userFactory = $this->getServiceContainer()->getUserFactory(); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function testGetIPNetwork( $ip, $expected ) { |
| 82 |
$this->setUpLoginNotify(); |
| 83 |
$actual = $this->inst->getIPNetwork( $ip ); |
| 84 |
$this->assertSame( $expected, $actual ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function provideGetIPNetwork() { |
| 88 |
return [ |
| 89 |
[ '127.0.0.1', '127.0.0.' ], |
| 90 |
[ '118.221.191.18', '118.221.191.' ], |
| 91 |
[ '::1', '0:0:0:0:' ], |
| 92 |
[ '1:2:3:4:5:6:7:8', '1:2:3:4:' ], |
| 93 |
[ '1:ffe1::7:8', '1:FFE1:0:0:' ], |
| 94 |
[ 'd3::1:2:3:4:5:6', 'D3:0:1:2:' ] |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
public function testGetIPNetworkInvalid() { |
| 99 |
$this->setUpLoginNotify(); |
| 100 |
$this->expectException( UnexpectedValueException::class ); |
| 101 |
$this->inst->getIPNetwork( 'localhost' ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testGenerateUserCookieRecord( $username, $year, $salt, $expected ) { |
| 108 |
$this->setUpLoginNotify(); |
| 109 |
$actual = $this->inst->generateUserCookieRecord( $username, $year, $salt ); |
| 110 |
$this->assertEquals( $expected, $actual ); |
| 111 |
} |
| 112 |
|
| 113 |
public static function provideGenerateUserCookieRecord() { |
| 114 |
return [ |
| 115 |
[ 'Foo', 2011, 'a4321f', '2011-a4321f-8oerxg4l59zpiu0by7m2to1b4cjeer4' ], |
| 116 |
[ 'Foo', 2011, 'A4321f', '2011-A4321f-in65gc2i9czojfopkeieijc0ek8j5vu' ], |
| 117 |
[ 'Foo', 2015, 'a4321f', '2015-a4321f-2hf2zh9h3afv79b1u4l474ozc0by2xe' ], |
| 118 |
[ 'FOo', 2011, 'a4321f', '2011-a4321f-d0dhdzxg3te3yd3np6xdfrwdrckop7m' ], |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testIsUserRecordGivenCookie( $cookieOptions, $expected, $desc ) { |
| 126 |
$this->setUpLoginNotify(); |
| 127 |
$user = $this->userFactory->newFromName( 'Foo', UserFactory::RIGOR_NONE ); |
| 128 |
if ( is_string( $cookieOptions ) ) { |
| 129 |
$cookieRecord = $cookieOptions; |
| 130 |
} else { |
| 131 |
[ $userName, $year, $salt ] = $cookieOptions + [ false, false, false ]; |
| 132 |
$cookieRecord = $this->inst->generateUserCookieRecord( $userName, $year, $salt ); |
| 133 |
} |
| 134 |
$actual = $this->inst->isUserRecordGivenCookie( $user, $cookieRecord ); |
| 135 |
$this->assertEquals( $expected, $actual, "For {$user->getName()} on test $desc." ); |
| 136 |
} |
| 137 |
|
| 138 |
public static function provideIsUserRecordGivenCookie() { |
| 139 |
$cookie1 = [ 'Foo2' ]; |
| 140 |
$cookie2 = [ 'Foo' ]; |
| 141 |
$cookie3 = [ 'Foo', gmdate( 'Y' ) - 2 ]; |
| 142 |
$cookie4 = [ 'Foo', gmdate( 'Y' ), 'RAND' ]; |
| 143 |
$cookie5 = [ 'Foo', gmdate( 'Y' ) - 4 ]; |
| 144 |
return [ |
| 145 |
[ '2015-in65gc2i9czojfopkeieijc0ek8j5vu', false, "no salt" ], |
| 146 |
[ '2011-A4321f-in65gc2i9czojfopkeieijc0ek8j5vu', false, "too old" ], |
| 147 |
[ $cookie1, false, "name mismatch" ], |
| 148 |
[ $cookie2, true, "normal" ], |
| 149 |
[ $cookie3, true, "2 year old" ], |
| 150 |
[ $cookie4, true, "Specific salt" ], |
| 151 |
[ $cookie5, false, "4 year old" ], |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
public function testGetPrevLoginCookie() { |
| 156 |
$this->setUpLoginNotify(); |
| 157 |
$req = new FauxRequest(); |
| 158 |
$res1 = $this->inst->getPrevLoginCookie( $req ); |
| 159 |
$this->assertSame( '', $res1, "no cookie set" ); |
| 160 |
|
| 161 |
$req->setCookie( 'loginnotify_prevlogins', 'foo', '' ); |
| 162 |
$res2 = $this->inst->getPrevLoginCookie( $req ); |
| 163 |
$this->assertEquals( 'foo', $res2, "get dummy cookie" ); |
| 164 |
} |
| 165 |
|
| 166 |
public function testGetKey() { |
| 167 |
$this->setUpLoginNotify(); |
| 168 |
$user1 = $this->userFactory->newFromName( 'Foo_bar' ); |
| 169 |
|
| 170 |
$user2 = $this->userFactory->newFromName( 'Foo__bar' ); |
| 171 |
$user3 = $this->userFactory->newFromName( 'Somebody' ); |
| 172 |
|
| 173 |
$this->assertEquals( |
| 174 |
'global:loginnotify:new:ok2qitd5efi25tzjy2l3el4n57g6l3l', |
| 175 |
$this->inst->getKey( $user1, 'new' ) |
| 176 |
); |
| 177 |
$this->assertEquals( |
| 178 |
'global:loginnotify:known:ok2qitd5efi25tzjy2l3el4n57g6l3l', |
| 179 |
$this->inst->getKey( $user1, 'known' ) |
| 180 |
); |
| 181 |
$this->assertEquals( |
| 182 |
'global:loginnotify:new:ok2qitd5efi25tzjy2l3el4n57g6l3l', |
| 183 |
$this->inst->getKey( $user2, 'new' ) |
| 184 |
); |
| 185 |
$this->assertEquals( |
| 186 |
'global:loginnotify:new:tuwpi7e2h9pidovmaxxswk6aq327ewg', |
| 187 |
$this->inst->getKey( $user3, 'new' ) |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
public function testCheckAndIncKey() { |
| 192 |
$this->setUpLoginNotify(); |
| 193 |
$key = 'global:loginnotify:new:tuwpi7e2h9pidovmaxxswk6aq327ewg'; |
| 194 |
for ( $i = 1; $i < 5; $i++ ) { |
| 195 |
$res = $this->inst->checkAndIncKey( $key, 5, 3600 ); |
| 196 |
$this->assertFalse( $res, "key check numb $i" ); |
| 197 |
} |
| 198 |
$this->assertEquals( 5, $this->inst->checkAndIncKey( $key, 5, 3600 ) ); |
| 199 |
for ( $i = 1; $i < 5; $i++ ) { |
| 200 |
$res = $this->inst->checkAndIncKey( $key, 5, 3600 ); |
| 201 |
$this->assertFalse( $res, "key check numb $i+5" ); |
| 202 |
} |
| 203 |
$this->assertEquals( 10, $this->inst->checkAndIncKey( $key, 5, 3600 ) ); |
| 204 |
|
| 205 |
$key2 = 'global:loginnotify:known:tuwpi7e2h9pidovmaxxswk6aq327ewg'; |
| 206 |
for ( $i = 1; $i < 5; $i++ ) { |
| 207 |
$res = $this->inst->checkAndIncKey( $key2, 1, 3600 ); |
| 208 |
$this->assertEquals( $i, $res, "key check interval 1 numb $i" ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function testClearCounters( $key ) { |
| 216 |
$this->setUpLoginNotify(); |
| 217 |
$user = $this->userFactory->newFromName( "Fred" ); |
| 218 |
$key = $this->inst->getKey( $user, $key ); |
| 219 |
|
| 220 |
$this->inst->checkAndIncKey( $key, 1, 3600 ); |
| 221 |
$res = $this->inst->checkAndIncKey( $key, 1, 3600 ); |
| 222 |
$this->assertEquals( 2, $res, "prior to clear" ); |
| 223 |
$this->inst->clearCounters( $user ); |
| 224 |
$res = $this->inst->checkAndIncKey( $key, 1, 3600 ); |
| 225 |
$this->assertSame( 1, $res, "after clear" ); |
| 226 |
} |
| 227 |
|
| 228 |
public static function provideClearCounters() { |
| 229 |
return [ |
| 230 |
[ 'new' ], |
| 231 |
[ 'known' ], |
| 232 |
]; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function testCheckAndGenerateCookie( |
| 241 |
$cookie, |
| 242 |
$expectedSeenBefore, |
| 243 |
$expectedNewCookie, |
| 244 |
$desc |
| 245 |
) { |
| 246 |
$this->setUpLoginNotify(); |
| 247 |
$user = $this->userFactory->newFromName( 'Foo' ); |
| 248 |
[ $actualSeenBefore, $actualNewCookie ] = |
| 249 |
$this->inst->checkAndGenerateCookie( $user, $cookie ); |
| 250 |
|
| 251 |
$this->assertEquals( $expectedSeenBefore, $actualSeenBefore, |
| 252 |
"[Seen before] $desc" |
| 253 |
); |
| 254 |
$newCookieParts = explode( '.', $actualNewCookie, 2 ); |
| 255 |
if ( !isset( $newCookieParts[1] ) ) { |
| 256 |
$newCookieParts[1] = ''; |
| 257 |
} |
| 258 |
$this->assertTrue( |
| 259 |
$this->inst->isUserRecordGivenCookie( $user, $newCookieParts[0] ), |
| 260 |
"[Cookie new entry] $desc" |
| 261 |
); |
| 262 |
$this->assertEquals( $expectedNewCookie, $newCookieParts[1], "[Cookie] $desc" ); |
| 263 |
} |
| 264 |
|
| 265 |
public static function provideCheckAndGenerateCookie() { |
| 266 |
$y = gmdate( 'Y' ); |
| 267 |
$oldYear = $y - 4; |
| 268 |
|
| 269 |
$cookie1 = '2023-1skf7bc-5j58f37f3t9m7rhwrrpvi4jigsyn750'; |
| 270 |
$cookie2 = '2023-99isim-ab5ms8f8581tpocpumgb78tof6f96yj'; |
| 271 |
$cookieOld = '2001-1skf7bc-48j27cdz9fmkv2yf667axiwvdy9z673'; |
| 272 |
$cookieOtherUser = "$y-1veusdo-tr049njztrrvkkz4tk3kre8rm1zb134"; |
| 273 |
return [ |
| 274 |
[ '', false, '', "no cookie" ], |
| 275 |
[ |
| 276 |
$cookieOtherUser, |
| 277 |
false, |
| 278 |
$cookieOtherUser, |
| 279 |
"not in cookie" |
| 280 |
], |
| 281 |
[ |
| 282 |
"$cookieOtherUser.$y-.$y-abcdefg-8oerxg4l59zpiu0by7m2to1b4cjeer4.$oldYear-" . |
| 283 |
"1234567-tpnsk00419wba6vjh1upif21qtst1cv", |
| 284 |
false, |
| 285 |
"$cookieOtherUser.$y-abcdefg-8oerxg4l59zpiu0by7m2to1b4cjeer4", |
| 286 |
"old values in cookie" |
| 287 |
], |
| 288 |
[ |
| 289 |
$cookieOld, |
| 290 |
false, |
| 291 |
"", |
| 292 |
"Only old value" |
| 293 |
], |
| 294 |
[ |
| 295 |
$cookie1, |
| 296 |
true, |
| 297 |
"", |
| 298 |
"Normal success" |
| 299 |
], |
| 300 |
[ |
| 301 |
"$cookieOtherUser.$cookie1.$cookie2." |
| 302 |
. "$y-1234567-tpnsk00419wba6vjh1upif21qtst1cv.$cookie1", |
| 303 |
true, |
| 304 |
"$cookieOtherUser.$y-1234567-tpnsk00419wba6vjh1upif21qtst1cv", |
| 305 |
"Remove all of current user." |
| 306 |
], |
| 307 |
[ |
| 308 |
"$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 309 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 310 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 311 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 312 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 313 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 314 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 315 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 316 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 317 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 318 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser", |
| 319 |
false, |
| 320 |
"$cookieOtherUser.$cookieOtherUser.$cookieOtherUser." |
| 321 |
. "$cookieOtherUser.$cookieOtherUser.$cookieOtherUser", |
| 322 |
"Limit max number of records." |
| 323 |
] |
| 324 |
]; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function testValidateCookieRecord( $cookie, $expected ) { |
| 331 |
$this->setUpLoginNotify(); |
| 332 |
$this->assertEquals( $expected, $this->inst->validateCookieRecord( $cookie ) ); |
| 333 |
} |
| 334 |
|
| 335 |
public static function provideValidateCookieRecord() { |
| 336 |
$y = gmdate( 'Y' ); |
| 337 |
return [ |
| 338 |
[ 'fdakslnknfaknasf', false ], |
| 339 |
[ '--', false ], |
| 340 |
[ '91-ffff-fdaskfjlasflasd', false ], |
| 341 |
[ '2011-ffff-fdaskfjlasflasd', false ], |
| 342 |
[ "$y-1veusdo-tr049njztrrvkkz4tk3kre8rm1zb134", true ], |
| 343 |
[ "1991-1veusdo-tr049njztrrvkkz4tk3kre8rm1zb134", false ], |
| 344 |
]; |
| 345 |
} |
| 346 |
|
| 347 |
public function testUserIsInCache() { |
| 348 |
$this->setUpLoginNotify( [ "LoginNotifyUseCheckUser" => true ] ); |
| 349 |
$u = $this->userFactory->newFromName( 'Xyzzy' ); |
| 350 |
$this->assertSame( |
| 351 |
LoginNotify::USER_NO_INFO, |
| 352 |
$this->inst->userIsInCache( $u, new FauxRequest() ) |
| 353 |
); |
| 354 |
|
| 355 |
$this->inst->cacheLoginIP( $u ); |
| 356 |
$this->assertSame( |
| 357 |
LoginNotify::USER_KNOWN, |
| 358 |
$this->inst->userIsInCache( $u, new FauxRequest() ) |
| 359 |
); |
| 360 |
|
| 361 |
$request = new FauxRequest(); |
| 362 |
$request->setIP( '10.1.2.3' ); |
| 363 |
|
| 364 |
$this->assertSame( |
| 365 |
LoginNotify::USER_NOT_KNOWN, |
| 366 |
$this->inst->userIsInCache( $u, $request ) |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
public static function provideRecordFailureKnownCacheOrTable() { |
| 371 |
return [ |
| 372 |
[ 'cache' ], |
| 373 |
[ 'table' ] |
| 374 |
]; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function testRecordFailureKnownCacheOrTable( $type ) { |
| 382 |
$config = [ |
| 383 |
'LoginNotifyUseCheckUser' => $type === 'cache', |
| 384 |
'LoginNotifyUseSeenTable' => $type !== 'cache' |
| 385 |
]; |
| 386 |
$this->setupRecordFailure( $config ); |
| 387 |
$user = $this->getTestUser()->getUser(); |
| 388 |
$this->inst->recordKnown( $user ); |
| 389 |
|
| 390 |
|
| 391 |
$this->inst->recordFailure( $user ); |
| 392 |
$this->assertNotificationCount( $user, 'login-fail-known', 0 ); |
| 393 |
|
| 394 |
|
| 395 |
$this->inst->recordFailure( $user ); |
| 396 |
$this->assertNotificationCount( $user, 'login-fail-known', 1 ); |
| 397 |
$this->assertNotificationCount( $user, 'login-fail-new', 0 ); |
| 398 |
|
| 399 |
|
| 400 |
$this->runJobs( [ 'numJobs' => 0 ], [ 'type' => 'LoginNotifyChecks' ] ); |
| 401 |
} |
| 402 |
|
| 403 |
public function testRecordFailureKnownCheckUser() { |
| 404 |
$this->setupRecordFailureWithCheckUser(); |
| 405 |
$helper = new TestRecentChangesHelper; |
| 406 |
$user = $this->getTestUser()->getUser(); |
| 407 |
|
| 408 |
|
| 409 |
$rc = $helper->makeEditRecentChange( $user, 'LoginNotifyTest', |
| 410 |
1, 1, 1, wfTimestampNow(), 0, 0 ); |
| 411 |
CU\Hooks::updateCheckUserData( $rc ); |
| 412 |
|
| 413 |
|
| 414 |
$this->inst->recordFailure( $user ); |
| 415 |
$this->runJobs(); |
| 416 |
$this->assertNotificationCount( $user, 'login-fail-known', 0 ); |
| 417 |
|
| 418 |
|
| 419 |
$this->inst->recordFailure( $user ); |
| 420 |
$this->runJobs(); |
| 421 |
$this->assertNotificationCount( $user, 'login-fail-known', 1 ); |
| 422 |
$this->assertNotificationCount( $user, 'login-fail-new', 0 ); |
| 423 |
} |
| 424 |
|
| 425 |
public function testRecordFailureUnknownCheckUser() { |
| 426 |
$this->setupRecordFailureWithCheckUser(); |
| 427 |
$helper = new TestRecentChangesHelper; |
| 428 |
$user = $this->getTestUser()->getUser(); |
| 429 |
|
| 430 |
|
| 431 |
$rc = $helper->makeEditRecentChange( $user, 'LoginNotifyTest', |
| 432 |
1, 1, 1, wfTimestampNow(), 0, 0 ); |
| 433 |
CU\Hooks::updateCheckUserData( $rc ); |
| 434 |
|
| 435 |
|
| 436 |
RequestContext::getMain()->getRequest()->setIP( '127.1.0.0' ); |
| 437 |
$this->inst->recordFailure( $user ); |
| 438 |
$this->runJobs(); |
| 439 |
$this->assertNotificationCount( $user, 'login-fail-new', 0 ); |
| 440 |
|
| 441 |
|
| 442 |
$this->inst->recordFailure( $user ); |
| 443 |
$this->runJobs(); |
| 444 |
$this->assertNotificationCount( $user, 'login-fail-new', 1 ); |
| 445 |
$this->assertNotificationCount( $user, 'login-fail-known', 0 ); |
| 446 |
} |
| 447 |
|
| 448 |
public function testRecordFailureSeenExpired() { |
| 449 |
$this->setupRecordFailure( [ |
| 450 |
'LoginNotifyAttemptsKnownIP' => 1, |
| 451 |
'LoginNotifyAttemptsNewIP' => 1, |
| 452 |
] ); |
| 453 |
$user = $this->getTestUser()->getUser(); |
| 454 |
$day = 86400; |
| 455 |
|
| 456 |
|
| 457 |
$this->inst->setFakeTime( 2 * $day ); |
| 458 |
$this->inst->recordKnown( $user ); |
| 459 |
|
| 460 |
|
| 461 |
$this->inst->setFakeTime( 32 * $day ); |
| 462 |
$this->inst->recordFailure( $user ); |
| 463 |
$this->assertNotificationCount( $user, 'login-fail-new', 0 ); |
| 464 |
$this->assertNotificationCount( $user, 'login-fail-known', 1 ); |
| 465 |
|
| 466 |
|
| 467 |
$this->inst->setFakeTime( 210 * $day ); |
| 468 |
$this->inst->recordFailure( $user ); |
| 469 |
$this->assertNotificationCount( $user, 'login-fail-new', 1 ); |
| 470 |
$this->assertNotificationCount( $user, 'login-fail-known', 1 ); |
| 471 |
} |
| 472 |
|
| 473 |
public function testRecordFailureNoPassword() { |
| 474 |
|
| 475 |
$this->setupRecordFailure( [ |
| 476 |
'LoginNotifyAttemptsKnownIP' => 1, |
| 477 |
'LoginNotifyAttemptsNewIP' => 1, |
| 478 |
] ); |
| 479 |
$user = $this->getTestUser()->getUser(); |
| 480 |
$this->getDb()->newUpdateQueryBuilder() |
| 481 |
->update( 'user' ) |
| 482 |
->set( [ 'user_password' => '' ] ) |
| 483 |
->where( [ 'user_id' => $user->getId() ] ) |
| 484 |
->caller( __METHOD__ ) |
| 485 |
->execute(); |
| 486 |
$this->inst->recordFailure( $user ); |
| 487 |
$this->assertNotificationCount( $user, 'login-fail-known', 0 ); |
| 488 |
$this->assertNotificationCount( $user, 'login-fail-new', 0 ); |
| 489 |
} |
| 490 |
|
| 491 |
public function testSendSuccessNoticeCheckUser() { |
| 492 |
$this->setupRecordFailureWithCheckUser(); |
| 493 |
$helper = new TestRecentChangesHelper; |
| 494 |
$user = $this->getTestUser()->getUser(); |
| 495 |
$user->setEmail( '[email protected]' ); |
| 496 |
$user->confirmEmail(); |
| 497 |
$user->saveSettings(); |
| 498 |
|
| 499 |
$emailSent = false; |
| 500 |
$this->setTemporaryHook( 'EchoAbortEmailNotification', |
| 501 |
static function () use ( &$emailSent ) { |
| 502 |
$emailSent = true; |
| 503 |
return false; |
| 504 |
} |
| 505 |
); |
| 506 |
|
| 507 |
|
| 508 |
$rc = $helper->makeEditRecentChange( $user, 'LoginNotifyTest', |
| 509 |
1, 1, 1, wfTimestampNow(), 0, 0 ); |
| 510 |
CU\Hooks::updateCheckUserData( $rc ); |
| 511 |
|
| 512 |
|
| 513 |
RequestContext::getMain()->getRequest()->setIP( '127.1.0.0' ); |
| 514 |
$this->inst->sendSuccessNotice( $user ); |
| 515 |
$this->runJobs(); |
| 516 |
$this->assertTrue( $emailSent ); |
| 517 |
} |
| 518 |
|
| 519 |
public function testSendSuccessNoticeSeen() { |
| 520 |
$this->setupRecordFailure(); |
| 521 |
$user = $this->getTestUser()->getUser(); |
| 522 |
$user->setEmail( '[email protected]' ); |
| 523 |
$user->confirmEmail(); |
| 524 |
$user->saveSettings(); |
| 525 |
|
| 526 |
$emailSent = false; |
| 527 |
$this->setTemporaryHook( 'EchoAbortEmailNotification', |
| 528 |
static function () use ( &$emailSent ) { |
| 529 |
$emailSent = true; |
| 530 |
return false; |
| 531 |
} |
| 532 |
); |
| 533 |
|
| 534 |
|
| 535 |
$this->inst->recordKnown( $user ); |
| 536 |
|
| 537 |
|
| 538 |
RequestContext::getMain()->getRequest()->setIP( '127.1.0.0' ); |
| 539 |
$this->inst->sendSuccessNotice( $user ); |
| 540 |
$this->assertTrue( $emailSent ); |
| 541 |
} |
| 542 |
|
| 543 |
private function setupRecordFailure( $config = [] ) { |
| 544 |
$config += [ |
| 545 |
'LoginNotifyAttemptsKnownIP' => 2, |
| 546 |
'LoginNotifyAttemptsNewIP' => 2, |
| 547 |
]; |
| 548 |
$this->setUpLoginNotify( $config ); |
| 549 |
$this->overrideConfigValues( $config ); |
| 550 |
} |
| 551 |
|
| 552 |
private function setupRecordFailureWithCheckUser() { |
| 553 |
$this->markTestSkippedIfExtensionNotLoaded( 'CheckUser' ); |
| 554 |
$this->setupRecordFailure( [ 'LoginNotifyUseCheckUser' => true ] ); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
private function assertNotificationCount( $user, $type, $expected ) { |
| 565 |
$this->newSelectQueryBuilder() |
| 566 |
->select( 'COUNT(*)' ) |
| 567 |
->from( 'echo_notification' ) |
| 568 |
->join( 'echo_event', null, 'event_id=notification_event' ) |
| 569 |
->where( [ |
| 570 |
'notification_user' => $user->getId(), |
| 571 |
'event_type' => $type |
| 572 |
] ) |
| 573 |
->assertFieldValue( $expected ); |
| 574 |
} |
| 575 |
|
| 576 |
public static function providePackedSignedInt64ToDecimal() { |
| 577 |
return [ |
| 578 |
[ '0000000000000000', 0 ], |
| 579 |
[ '0000000000000001', 1 ], |
| 580 |
[ 'ffffffffffffffff', -1 ], |
| 581 |
[ '7fffffffffffffff', '9223372036854775807' ], |
| 582 |
[ '8000000000000000', '-9223372036854775808' ], |
| 583 |
]; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public function testPackedSignedInt64ToDecimal( $hexInput, $expected ) { |
| 592 |
$class = TestingAccessWrapper::newFromClass( LoginNotify::class ); |
| 593 |
$input = hex2bin( $hexInput ); |
| 594 |
$result = $class->packedSignedInt64ToDecimal( $input ); |
| 595 |
$this->assertSame( (string)$expected, (string)$result ); |
| 596 |
} |
| 597 |
|
| 598 |
public function testPurgeSeen() { |
| 599 |
$this->setupLoginNotify( [ 'UpdateRowsPerQuery' => 1 ] ); |
| 600 |
$user = $this->getTestUser()->getUser(); |
| 601 |
$day = 86400; |
| 602 |
$this->inst->setFakeTime( 0 ); |
| 603 |
$this->inst->recordKnown( $user ); |
| 604 |
$this->inst->setFakeTime( 90 * $day ); |
| 605 |
$this->inst->recordKnown( $user ); |
| 606 |
$this->inst->setFakeTime( 210 * $day ); |
| 607 |
$this->inst->recordKnown( $user ); |
| 608 |
|
| 609 |
$this->assertSeenCount( 3 ); |
| 610 |
|
| 611 |
$this->inst->setFakeTime( 300 * $day ); |
| 612 |
$minId = $this->inst->getMinExpiredId(); |
| 613 |
$this->assertGreaterThan( 0, $minId ); |
| 614 |
|
| 615 |
$nextId = $this->inst->purgeSeen( $minId ); |
| 616 |
$this->assertSeenCount( 2 ); |
| 617 |
$this->assertGreaterThan( $minId, $nextId ); |
| 618 |
|
| 619 |
$nextId = $this->inst->purgeSeen( $nextId ); |
| 620 |
$this->assertGreaterThan( $minId, $nextId ); |
| 621 |
$this->assertSeenCount( 1 ); |
| 622 |
|
| 623 |
$nextId = $this->inst->purgeSeen( $nextId ); |
| 624 |
$this->assertNull( $nextId ); |
| 625 |
} |
| 626 |
|
| 627 |
public function testPurgeViaJob() { |
| 628 |
$this->setupLoginNotify( [ 'UpdateRowsPerQuery' => 1 ] ); |
| 629 |
$user = $this->getTestUser()->getUser(); |
| 630 |
|
| 631 |
$this->inst->setFakeTime( 0 ); |
| 632 |
$this->inst->recordUserInSeenTable( $user ); |
| 633 |
$this->assertSeenCount( 1 ); |
| 634 |
|
| 635 |
$this->inst->setFakeTime( null ); |
| 636 |
$this->inst->recordUserInSeenTable( $user ); |
| 637 |
$this->assertSeenCount( 2 ); |
| 638 |
|
| 639 |
$this->runJobs(); |
| 640 |
$this->assertSeenCount( 1 ); |
| 641 |
} |
| 642 |
|
| 643 |
private function assertSeenCount( $expected ) { |
| 644 |
$this->newSelectQueryBuilder() |
| 645 |
->select( 'COUNT(*)' ) |
| 646 |
->from( 'loginnotify_seen_net' ) |
| 647 |
->assertFieldValue( $expected ); |
| 648 |
} |
| 649 |
} |