namespace MediaWiki\Auth;
+use Wikimedia\ScopedCallback;
+
/**
* @group AuthManager
* @group Database
} );
}
- return new \ScopedCallback( function () {
+ return new ScopedCallback( function () {
\Hooks::clear( 'AlternateUserMailer' );
\Hooks::register( 'AlternateUserMailer', function () {
return false;
}
public function testTestUserCanAuthenticate() {
+ $user = self::getMutableTestUser()->getUser();
+
$dbw = wfGetDB( DB_MASTER );
$passwordFactory = new \PasswordFactory();
'user_newpassword' => \PasswordFactory::newInvalidPassword()->toString(),
'user_newpass_time' => null,
],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
- $this->assertFalse( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+ $this->assertFalse( $provider->testUserCanAuthenticate( $user->getName() ) );
$dbw->update(
'user',
'user_newpassword' => $pwhash,
'user_newpass_time' => null,
],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
- $this->assertTrue( $provider->testUserCanAuthenticate( 'UTSysop' ) );
- $this->assertTrue( $provider->testUserCanAuthenticate( 'uTSysop' ) );
+ $this->assertTrue( $provider->testUserCanAuthenticate( $user->getName() ) );
+ $this->assertTrue( $provider->testUserCanAuthenticate( lcfirst( $user->getName() ) ) );
$dbw->update(
'user',
'user_newpassword' => $pwhash,
'user_newpass_time' => $dbw->timestamp( time() - 10 ),
],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
$providerPriv->newPasswordExpiry = 100;
- $this->assertTrue( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+ $this->assertTrue( $provider->testUserCanAuthenticate( $user->getName() ) );
$providerPriv->newPasswordExpiry = 1;
- $this->assertFalse( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+ $this->assertFalse( $provider->testUserCanAuthenticate( $user->getName() ) );
$dbw->update(
'user',
'user_newpassword' => \PasswordFactory::newInvalidPassword()->toString(),
'user_newpass_time' => null,
],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
}
}
public function testAuthentication() {
+ $user = self::getMutableTestUser()->getUser();
+
$password = 'TemporaryPassword';
$hash = ':A:' . md5( $password );
$dbw = wfGetDB( DB_MASTER );
$dbw->update(
'user',
[ 'user_newpassword' => $hash, 'user_newpass_time' => $dbw->timestamp( time() - 10 ) ],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
$req = new PasswordAuthenticationRequest();
);
// Validation failure
- $req->username = 'UTSysop';
+ $req->username = $user->getName();
$req->password = $password;
$this->validity = \Status::newFatal( 'arbitrary-failure' );
$ret = $provider->beginPrimaryAuthentication( $reqs );
$this->manager->removeAuthenticationSessionData( null );
$this->validity = \Status::newGood();
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $user->getName() ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
$this->manager->removeAuthenticationSessionData( null );
$this->validity = \Status::newGood();
- $req->username = 'uTSysop';
+ $req->username = lcfirst( $user->getName() );
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $user->getName() ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
- $req->username = 'UTSysop';
+ $req->username = $user->getName();
// Expired password
$providerPriv->newPasswordExpiry = 1;
'wrongpassword',
$ret->message->getKey()
);
-
}
/**
$oldpass = 'OldTempPassword';
$newpass = 'NewTempPassword';
- $hash = ':A:' . md5( $oldpass );
$dbw = wfGetDB( DB_MASTER );
+ $oldHash = $dbw->selectField( 'user', 'user_newpassword', [ 'user_name' => $cuser ] );
+ $cb = new ScopedCallback( function () use ( $dbw, $cuser, $oldHash ) {
+ $dbw->update( 'user', [ 'user_newpassword' => $oldHash ], [ 'user_name' => $cuser ] );
+ } );
+
+ $hash = ':A:' . md5( $oldpass );
$dbw->update(
'user',
[ 'user_newpassword' => $hash, 'user_newpass_time' => $dbw->timestamp( time() + 10 ) ],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_name' => $cuser ]
);
- $dbw = wfGetDB( DB_MASTER );
- $oldHash = $dbw->selectField( 'user', 'user_newpassword', [ 'user_name' => $cuser ] );
- $cb = new \ScopedCallback( function () use ( $dbw, $cuser, $oldHash ) {
- $dbw->update( 'user', [ 'user_newpassword' => $oldHash ], [ 'user_name' => $cuser ] );
- } );
-
$provider = $this->getProvider();
// Sanity check
$changeReq->password = $newpass;
$resetMailer = $this->hookMailer();
$provider->providerChangeAuthenticationData( $changeReq );
- \ScopedCallback::consume( $resetMailer );
+ ScopedCallback::consume( $resetMailer );
$loginReq->password = $oldpass;
$ret = $provider->beginPrimaryAuthentication( $loginReqs );
}
public function testProviderChangeAuthenticationDataEmail() {
+ $user = self::getMutableTestUser()->getUser();
+
$dbw = wfGetDB( DB_MASTER );
$dbw->update(
'user',
[ 'user_newpass_time' => $dbw->timestamp( time() - 5 * 3600 ) ],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
- $user = \User::newFromName( 'UTSysop' );
- $reset = new \ScopedCallback( function ( $email ) use ( $user ) {
- $user->setEmail( $email );
- $user->saveSettings();
- }, [ $user->getEmail() ] );
-
- $user->setEmail( 'test@localhost.localdomain' );
- $user->saveSettings();
-
$req = TemporaryPasswordAuthenticationRequest::newRandom();
$req->username = $user->getName();
$req->mailpassword = true;
$provider = $this->getProvider( [ 'emailEnabled' => false ] );
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$this->assertEquals( \StatusValue::newFatal( 'passwordreset-emaildisabled' ), $status );
- $req->hasBackchannel = true;
- $status = $provider->providerAllowsAuthenticationDataChange( $req, true );
- $this->assertFalse( $status->hasMessage( 'passwordreset-emaildisabled' ) );
- $req->hasBackchannel = false;
$provider = $this->getProvider( [ 'passwordReminderResendTime' => 10 ] );
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$dbw->update(
'user',
[ 'user_newpass_time' => $dbw->timestamp( time() + 5 * 3600 ) ],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_id' => $user->getId() ]
);
$provider = $this->getProvider( [ 'passwordReminderResendTime' => 0 ] );
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$this->assertEquals( \StatusValue::newGood(), $status );
- $req->caller = 'UTSysop';
+ $req->caller = $user->getName();
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$this->assertEquals( \StatusValue::newGood(), $status );
$mailed = false;
$resetMailer = $this->hookMailer( function ( $headers, $to, $from, $subject, $body )
- use ( &$mailed, $req )
+ use ( &$mailed, $req, $user )
{
$mailed = true;
- $this->assertSame( 'test@localhost.localdomain', $to[0]->address );
+ $this->assertSame( $user->getEmail(), $to[0]->address );
$this->assertContains( $req->password, $body );
return false;
} );
$provider->providerChangeAuthenticationData( $req );
- \ScopedCallback::consume( $resetMailer );
+ ScopedCallback::consume( $resetMailer );
$this->assertTrue( $mailed );
$priv = \TestingAccessWrapper::newFromObject( $provider );
$this->assertEquals( $expect, $provider->beginPrimaryAccountCreation( $user, $user, $reqs ) );
$this->assertNull( $this->manager->getAuthenticationSessionData( 'no-email' ) );
- // We have to cheat a bit to avoid having to add a new user to
- // the database to test the actual setting of the password works right
- $user = \User::newFromName( 'UTSysop' );
+ $user = self::getMutableTestUser()->getUser();
$req->username = $authreq->username = $user->getName();
$req->password = $authreq->password = 'NewPassword';
- $expect = AuthenticationResponse::newPass( 'UTSysop' );
+ $expect = AuthenticationResponse::newPass( $user->getName() );
$expect->createRequest = $req;
$res2 = $provider->beginPrimaryAccountCreation( $user, $user, $reqs );
public function testAccountCreationEmail() {
$creator = \User::newFromName( 'Foo' );
- $user = \User::newFromName( 'UTSysop' );
- $reset = new \ScopedCallback( function ( $email ) use ( $user ) {
- $user->setEmail( $email );
- $user->saveSettings();
- }, [ $user->getEmail() ] );
+ $user = self::getMutableTestUser()->getUser();
$user->setEmail( null );
$req = TemporaryPasswordAuthenticationRequest::newRandom();
$provider = $this->getProvider( [ 'emailEnabled' => false ] );
$status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
$this->assertEquals( \StatusValue::newFatal( 'emaildisabled' ), $status );
- $req->hasBackchannel = true;
- $status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
- $this->assertFalse( $status->hasMessage( 'emaildisabled' ) );
- $req->hasBackchannel = false;
$provider = $this->getProvider( [ 'emailEnabled' => true ] );
$status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
$this->assertEquals( \StatusValue::newFatal( 'noemailcreate' ), $status );
- $req->hasBackchannel = true;
- $status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
- $this->assertFalse( $status->hasMessage( 'noemailcreate' ) );
- $req->hasBackchannel = false;
$user->setEmail( 'test@localhost.localdomain' );
$status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
return false;
} );
- $expect = AuthenticationResponse::newPass( 'UTSysop' );
+ $expect = AuthenticationResponse::newPass( $user->getName() );
$expect->createRequest = clone( $req );
- $expect->createRequest->username = 'UTSysop';
+ $expect->createRequest->username = $user->getName();
$res = $provider->beginPrimaryAccountCreation( $user, $creator, [ $req ] );
$this->assertEquals( $expect, $res );
$this->assertTrue( $this->manager->getAuthenticationSessionData( 'no-email' ) );
$this->assertSame( 'byemail', $provider->finishAccountCreation( $user, $creator, $res ) );
$this->assertTrue( $mailed );
- \ScopedCallback::consume( $resetMailer );
+ ScopedCallback::consume( $resetMailer );
$this->assertTrue( $mailed );
}