szurubooru/tests/MiscTests/AuthTest.php

150 lines
3.5 KiB
PHP
Raw Normal View History

2014-05-04 21:23:12 +02:00
<?php
class AuthTest extends AbstractTest
2014-05-04 21:23:12 +02:00
{
public function testValidPassword()
{
2014-05-06 13:07:24 +02:00
getConfig()->registration->staffActivation = false;
getConfig()->registration->needEmailForRegistering = false;
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
});
}
public function testLogout()
{
$this->assert->isFalse(Auth::isLoggedIn());
$this->testValidPassword();
$this->assert->isTrue(Auth::isLoggedIn());
Auth::setCurrentUser(null);
$this->assert->isFalse(Auth::isLoggedIn());
}
public function testInvalidUser()
{
$this->assert->throws(function()
{
Auth::login('non-existing', 'wrong-password', false);
}, 'invalid username');
}
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-06 13:07:24 +02:00
getConfig()->registration->staffActivation = false;
getConfig()->registration->needEmailForRegistering = false;
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');
}
public function testStaffConfirmationEnabled()
{
getConfig()->registration->staffActivation = true;
2014-05-06 13:07:24 +02:00
getConfig()->registration->needEmailForRegistering = false;
2014-05-04 21:23:12 +02:00
$user = $this->prepareValidUser();
$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');
}
public function testStaffConfirmationDisabled()
{
getConfig()->registration->staffActivation = false;
2014-05-06 13:07:24 +02:00
getConfig()->registration->needEmailForRegistering = false;
2014-05-04 21:23:12 +02:00
$user = $this->prepareValidUser();
$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
});
}
public function testMailConfirmationEnabledFail1()
{
2014-05-06 13:07:24 +02:00
getConfig()->registration->staffActivation = false;
2014-05-04 21:23:12 +02:00
getConfig()->registration->needEmailForRegistering = true;
$user = $this->prepareValidUser();
$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-06 13:07:24 +02:00
getConfig()->registration->staffActivation = false;
2014-05-04 21:23:12 +02:00
getConfig()->registration->needEmailForRegistering = true;
$user = $this->prepareValidUser();
$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-06 13:07:24 +02:00
getConfig()->registration->staffActivation = false;
2014-05-04 21:23:12 +02:00
getConfig()->registration->needEmailForRegistering = true;
$user = $this->prepareValidUser();
$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;
}
}