2014-05-04 21:23:12 +02:00
|
|
|
<?php
|
2014-05-06 18:15:35 +02:00
|
|
|
class AuthTest extends AbstractTest
|
2014-05-04 21:23:12 +02:00
|
|
|
{
|
|
|
|
public function testValidPassword()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
2014-05-06 13:07:24 +02:00
|
|
|
|
2014-05-04 21:23:12 +02:00
|
|
|
$user = $this->prepareValidUser();
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
});
|
2014-05-12 18:59:17 +02:00
|
|
|
|
|
|
|
$this->assert->isTrue(Auth::isLoggedIn());
|
2014-05-18 20:52:35 +02:00
|
|
|
|
2014-05-28 21:22:31 +02:00
|
|
|
$this->assertValidUser(UserModel::getByName($user->getName()));
|
|
|
|
$this->assertValidUser(Auth::getCurrentUser());
|
2014-05-12 18:59:17 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 12:53:50 +02:00
|
|
|
public function testAutoLogin()
|
|
|
|
{
|
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
|
|
|
UserModel::save($user);
|
|
|
|
|
2014-05-28 21:22:31 +02:00
|
|
|
$this->assert->isNull($user->getLastLoginTime());
|
|
|
|
|
2014-05-26 12:53:50 +02:00
|
|
|
$token = base64_encode('existing') . '|' . base64_encode('bleee');
|
|
|
|
$_COOKIE['auth'] = TextHelper::encrypt($token);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
|
|
|
Auth::tryAutoLogin();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->isTrue(Auth::isLoggedIn());
|
|
|
|
|
2014-05-28 21:22:31 +02:00
|
|
|
$this->assertValidUser(UserModel::getByName($user->getName()));
|
|
|
|
$this->assertValidUser(Auth::getCurrentUser());
|
2014-05-26 12:53:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAutoLoginInvalidToken()
|
|
|
|
{
|
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$token = 'bleblebleąćęłóśńźż';
|
|
|
|
$_COOKIE['auth'] = TextHelper::encrypt($token);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
|
|
|
Auth::tryAutoLogin();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->isFalse(Auth::isLoggedIn());
|
|
|
|
|
|
|
|
$token = 'bleblebleą|ćęłóśńźż';
|
|
|
|
$_COOKIE['auth'] = TextHelper::encrypt($token);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
|
|
|
Auth::tryAutoLogin();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->isFalse(Auth::isLoggedIn());
|
|
|
|
|
|
|
|
$_COOKIE['auth'] = 'complete nonsense';
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
|
|
|
Auth::tryAutoLogin();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->isFalse(Auth::isLoggedIn());
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:59:17 +02:00
|
|
|
public function testLoginViaEmail()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
2014-05-12 18:59:17 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
|
|
|
$user->setConfirmedEmail('godzilla@whitestar.gov');
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function() use ($user)
|
|
|
|
{
|
|
|
|
Auth::login($user->getConfirmedEmail(), 'bleee', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->isTrue(Auth::isLoggedIn());
|
2014-05-04 21:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogout()
|
|
|
|
{
|
|
|
|
$this->assert->isFalse(Auth::isLoggedIn());
|
|
|
|
$this->testValidPassword();
|
|
|
|
$this->assert->isTrue(Auth::isLoggedIn());
|
|
|
|
Auth::setCurrentUser(null);
|
|
|
|
$this->assert->isFalse(Auth::isLoggedIn());
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:59:17 +02:00
|
|
|
public function testInvalidUserName()
|
2014-05-04 21:23:12 +02:00
|
|
|
{
|
|
|
|
$this->assert->throws(function()
|
|
|
|
{
|
|
|
|
Auth::login('non-existing', 'wrong-password', false);
|
2014-05-12 18:59:17 +02:00
|
|
|
}, 'invalid user name');
|
2014-05-04 21:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidPassword()
|
|
|
|
{
|
|
|
|
$user = $this->prepareValidUser();
|
2014-05-07 00:34:02 +02:00
|
|
|
$user->setPassword('blee2');
|
2014-05-04 21:23:12 +02:00
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->throws(function()
|
|
|
|
{
|
|
|
|
Auth::login('existing', 'wrong-password', false);
|
|
|
|
}, 'invalid password');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBanned()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
2014-05-06 13:07:24 +02:00
|
|
|
|
2014-05-04 21:23:12 +02:00
|
|
|
$user = $this->prepareValidUser();
|
|
|
|
$user->ban();
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->throws(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
}, 'You are banned');
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:10:19 +02:00
|
|
|
public function testStaffConfirmationEnabledFail()
|
2014-05-04 21:23:12 +02:00
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = true;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
2014-05-04 21:23:12 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
2014-05-07 20:39:48 +02:00
|
|
|
$user->setStaffConfirmed(false);
|
2014-05-04 21:23:12 +02:00
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->throws(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
}, 'staff hasn\'t confirmed');
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:10:19 +02:00
|
|
|
public function testStaffConfirmationEnabledPass()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = true;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
2014-05-12 20:10:19 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
|
|
|
$user->setStaffConfirmed(true);
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
|
|
|
Auth::login('existing', 'bleee', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->isTrue(Auth::isLoggedIn());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStaffConfirmationDisabledPass()
|
2014-05-04 21:23:12 +02:00
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = false;
|
2014-05-04 21:23:12 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
2014-05-07 20:39:48 +02:00
|
|
|
$user->setStaffConfirmed(false);
|
2014-05-04 21:23:12 +02:00
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
});
|
2014-05-12 20:10:19 +02:00
|
|
|
|
|
|
|
$this->assert->isTrue(Auth::isLoggedIn());
|
2014-05-04 21:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMailConfirmationEnabledFail1()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = true;
|
2014-05-04 21:23:12 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
2014-05-07 20:39:48 +02:00
|
|
|
$user->setStaffConfirmed(false);
|
2014-05-04 21:23:12 +02:00
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->throws(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
}, 'need e-mail address confirmation');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMailConfirmationEnabledFail2()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = true;
|
2014-05-04 21:23:12 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
2014-05-07 20:39:48 +02:00
|
|
|
$user->setStaffConfirmed(false);
|
2014-05-07 09:26:04 +02:00
|
|
|
$user->setUnconfirmedEmail('test@example.com');
|
2014-05-04 21:23:12 +02:00
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->throws(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
}, 'need e-mail address confirmation');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMailConfirmationEnabledPass()
|
|
|
|
{
|
2014-05-15 10:32:53 +02:00
|
|
|
Core::getConfig()->registration->staffActivation = false;
|
|
|
|
Core::getConfig()->registration->needEmailForRegistering = true;
|
2014-05-04 21:23:12 +02:00
|
|
|
|
|
|
|
$user = $this->prepareValidUser();
|
2014-05-07 20:39:48 +02:00
|
|
|
$user->setStaffConfirmed(false);
|
2014-05-07 09:26:04 +02:00
|
|
|
$user->setConfirmedEmail('test@example.com');
|
2014-05-04 21:23:12 +02:00
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function()
|
|
|
|
{
|
2014-05-07 00:34:02 +02:00
|
|
|
Auth::login('existing', 'bleee', false);
|
2014-05-04 21:23:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function prepareValidUser()
|
|
|
|
{
|
|
|
|
$user = UserModel::spawn();
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Registered));
|
|
|
|
$user->setName('existing');
|
2014-05-07 00:34:02 +02:00
|
|
|
$user->setPassword('bleee');
|
2014-05-04 21:23:12 +02:00
|
|
|
return $user;
|
|
|
|
}
|
2014-05-28 21:22:31 +02:00
|
|
|
|
|
|
|
private function assertValidUser($user)
|
|
|
|
{
|
|
|
|
$this->assert->areEqual('existing', $user->getName());
|
|
|
|
$this->assert->areEqual(time(), $user->getLastLoginTime());
|
|
|
|
}
|
2014-05-04 21:23:12 +02:00
|
|
|
}
|