szurubooru/tests/Tests/JobTests/AddUserJobTest.php

307 lines
8 KiB
PHP
Raw Normal View History

2014-05-07 00:34:02 +02:00
<?php
class AddUserJobTest extends AbstractTest
{
public function testSaving()
{
$this->grantAccess('registerAccount');
$user1 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
2014-05-07 00:34:02 +02:00
]);
});
2014-05-07 09:26:04 +02:00
//first user = admin
2014-05-07 00:34:02 +02:00
$this->assert->areEqual('dummy', $user1->getName());
$this->assert->areEquivalent(new AccessRank(AccessRank::Admin), $user1->getAccessRank());
$this->assert->isFalse(empty($user1->getPasswordSalt()));
$this->assert->isFalse(empty($user1->getPasswordHash()));
$user2 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy2',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
2014-05-07 00:34:02 +02:00
]);
});
2014-05-07 09:26:04 +02:00
//any other user = non-admin
2014-05-07 00:34:02 +02:00
$this->assert->areEquivalent(new AccessRank(AccessRank::Registered), $user2->getAccessRank());
}
public function testTooShortPassword()
{
$this->grantAccess('registerAccount');
$this->assert->throws(function()
{
Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
2014-05-15 10:32:53 +02:00
JobArgs::ARG_NEW_PASSWORD => str_repeat('s', Core::getConfig()->registration->passMinLength - 1),
2014-05-07 00:34:02 +02:00
]);
}, 'Password must have at least');
}
2014-05-07 09:26:04 +02:00
public function testSkippingMailingUponFailing() //yo dog
{
2014-05-15 10:32:53 +02:00
Core::getConfig()->registration->needEmailForRegistering = true;
2014-05-07 09:26:04 +02:00
Mailer::mockSending();
$this->assert->areEqual(0, Mailer::getMailCounter());
$this->grantAccess('registerAccount');
$this->assert->throws(function()
{
Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
2014-05-15 10:32:53 +02:00
JobArgs::ARG_NEW_PASSWORD => str_repeat('s', Core::getConfig()->registration->passMinLength - 1),
JobArgs::ARG_NEW_EMAIL => 'godzilla@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
}, 'Password must have at least');
$this->assert->areEqual(0, Mailer::getMailCounter());
}
2014-05-07 00:34:02 +02:00
public function testVeryLongPassword()
{
$this->grantAccess('registerAccount');
$pass = str_repeat('s', 10000);
$user = $this->assert->doesNotThrow(function() use ($pass)
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => $pass,
2014-05-07 00:34:02 +02:00
]);
});
$this->assert->isTrue(strlen($user->getPasswordHash()) < 100);
2014-05-15 10:32:53 +02:00
Core::getConfig()->registration->needEmailForRegistering = false;
2014-05-07 00:34:02 +02:00
$this->assert->doesNotThrow(function() use ($pass)
{
Auth::login('dummy', $pass, false);
});
$this->assert->throws(function() use ($pass)
{
Auth::login('dummy', $pass . '!', false);
}, 'Invalid password');
}
public function testDuplicateNames()
{
$this->grantAccess('registerAccount');
$this->assert->doesNotThrow(function()
{
Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
2014-05-07 00:34:02 +02:00
]);
});
$this->assert->throws(function()
{
Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
2014-05-07 00:34:02 +02:00
]);
2014-05-13 00:02:25 +02:00
}, 'User with this name is already registered');
2014-05-07 00:34:02 +02:00
}
public function testAccessRankDenial()
{
$this->grantAccess('registerAccount');
$this->assert->throws(function()
{
Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_ACCESS_RANK => 'power-user',
2014-05-07 00:34:02 +02:00
]);
}, 'Insufficient privileges');
}
2014-05-07 09:26:04 +02:00
public function testEmailsMixedConfirmation()
{
2014-05-15 10:32:53 +02:00
Core::getConfig()->registration->needEmailForRegistering = true;
2014-05-07 09:26:04 +02:00
Mailer::mockSending();
$this->assert->areEqual(0, Mailer::getMailCounter());
2014-05-15 10:32:53 +02:00
Core::getConfig()->privileges->changeUserEmailNoConfirm = 'admin';
2014-05-07 09:26:04 +02:00
$this->grantAccess('registerAccount');
$user1 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
});
//first user = admin = has confirmed e-mail automatically
2014-05-14 18:07:31 +02:00
$this->assert->isNull($user1->getUnconfirmedEmail());
2014-05-07 09:26:04 +02:00
$this->assert->areEqual('godzilla@whitestar.gov', $user1->getConfirmedEmail());
$user2 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy2',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla2@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
});
//any other user = non-admin = has to confirmed e-mail manually
$this->assert->areEqual('godzilla2@whitestar.gov', $user2->getUnconfirmedEmail());
2014-05-14 18:07:31 +02:00
$this->assert->isNull($user2->getConfirmedEmail());
2014-05-07 09:26:04 +02:00
$this->assert->areEqual(1, Mailer::getMailCounter());
}
public function testEmailsEveryoneMustConfirm()
{
2014-05-15 10:32:53 +02:00
Core::getConfig()->registration->needEmailForRegistering = true;
2014-05-07 09:26:04 +02:00
Mailer::mockSending();
$this->assert->areEqual(0, Mailer::getMailCounter());
2014-05-15 10:32:53 +02:00
Core::getConfig()->privileges->changeUserEmailNoConfirm = 'nobody';
2014-05-07 09:26:04 +02:00
$this->grantAccess('registerAccount');
$user1 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
});
$this->assert->areEqual('godzilla@whitestar.gov', $user1->getUnconfirmedEmail());
2014-05-14 18:07:31 +02:00
$this->assert->isNull($user1->getConfirmedEmail());
2014-05-07 09:26:04 +02:00
$user2 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy2',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla2@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
});
$this->assert->areEqual('godzilla2@whitestar.gov', $user2->getUnconfirmedEmail());
2014-05-14 18:07:31 +02:00
$this->assert->isNull($user2->getConfirmedEmail());
2014-05-07 09:26:04 +02:00
$this->assert->areEqual(2, Mailer::getMailCounter());
}
public function testEmailsEveryoneSkipConfirm()
{
2014-05-15 10:32:53 +02:00
Core::getConfig()->registration->needEmailForRegistering = true;
2014-05-07 09:26:04 +02:00
Mailer::mockSending();
$this->assert->areEqual(0, Mailer::getMailCounter());
2014-05-15 10:32:53 +02:00
Core::getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous';
2014-05-07 09:26:04 +02:00
$this->grantAccess('registerAccount');
$user1 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
});
2014-05-14 18:07:31 +02:00
$this->assert->isNull($user1->getUnconfirmedEmail());
2014-05-07 09:26:04 +02:00
$this->assert->areEqual('godzilla@whitestar.gov', $user1->getConfirmedEmail());
$user2 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy2',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla2@whitestar.gov',
2014-05-07 09:26:04 +02:00
]);
});
2014-05-14 18:07:31 +02:00
$this->assert->isNull($user2->getUnconfirmedEmail());
2014-05-07 09:26:04 +02:00
$this->assert->areEqual('godzilla2@whitestar.gov', $user2->getConfirmedEmail());
$this->assert->areEqual(0, Mailer::getMailCounter());
}
2014-05-07 17:39:40 +02:00
public function testEmailsTwoUsersSameMail()
{
2014-05-15 10:32:53 +02:00
Core::getConfig()->registration->needEmailForRegistering = true;
2014-05-07 17:39:40 +02:00
Mailer::mockSending();
$this->assert->areEqual(0, Mailer::getMailCounter());
$this->grantAccess('registerAccount');
$user1 = $this->assert->doesNotThrow(function()
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla@whitestar.gov',
2014-05-07 17:39:40 +02:00
]);
});
$user2 = $this->assert->throws(function()
2014-05-07 17:39:40 +02:00
{
return Api::run(
new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => 'dummy2',
JobArgs::ARG_NEW_PASSWORD => 'sekai',
JobArgs::ARG_NEW_EMAIL => 'godzilla@whitestar.gov',
2014-05-07 17:39:40 +02:00
]);
}, 'User with this e-mail is already registered');
2014-05-07 17:39:40 +02:00
}
public function testLogBuffering()
{
$this->testSaving();
$logPath = Logger::getLogPath();
$x = file_get_contents($logPath);
$lines = explode("\n", $x);
$this->assert->areEqual(2, count($lines));
}
2014-05-07 00:34:02 +02:00
}