<commit_before><?php
namespace Bolt\Tests;

use Bolt\AccessControl\Password;
use Hautelook\Phpass\PasswordHash;
use Carbon\Carbon;

/**
 * Test for AccessControl\Password
 *
 * @author <NAME> <<EMAIL>>
 */
class PasswordTest extends BoltUnitTest
{
    public function tearDown()
    {
        $this->resetDb();
    }

    public function testSetRandomPassword()
    {
        $app = $this->getApp();
        $this->addDefaultUser($app);
        $entityName = 'Bolt\Storage\Entity\Users';
        $repo = $app['storage']->getRepository($entityName);

        $logger = $this->getMock('\Monolog\Logger', ['info'], ['testlogger']);
        $logger->expects($this->atLeastOnce())
            ->method('info');
        $app['logger.system'] = $logger;

        $password = new Password($app);
        $newPass = $password->setRandomPassword('<PASSWORD>');

        $userEntity = $repo->getUser('admin');

        $hasher = new PasswordHash($app['access_control.hash.strength'], true);
        $compare = $hasher->CheckPassword($newPass, $userEntity->getPassword());

        $this->assertTrue($compare);
        $this->assertEmpty($userEntity->getShadowpassword());
        $this->assertEmpty($userEntity->getShadowtoken());
        $this->assertNull($userEntity->getShadowvalidity());
    }

    public function testResetPasswordConfirmValidUser()
    {
        $app = $this->getApp();
        $this->addDefaultUser($app);
        $entityName = 'Bolt\Storage\Entity\Users';
        $repo = $app['storage']->getRepository($entityName);

        $shadowToken = $app['randomgenerator']->generateString(32);
        $shadowTokenHash = md5($shadowToken . '-' . str_replace('.', '-', '8.8.8.8'));

        $userEntity = $repo->getUser('admin');
        $userEntity->setShadowpassword('<PASSWORD>');
        $userEntity->setShadowtoken($shadowTokenHash);
        $userEntity->setShadowvalidity(Carbon::create()->addHours(1));
        $repo->save($userEntity);

        $password = new Password($app);
        $result = $password->resetPasswordConfirm($shadowToken, '8.8.8.8');
        $userEntity = $repo->getUser('admin');

        $this->assertTrue($result);
        $this->assertEmpty($userEntity->getShadowpassword());
        $this->assertEmpty($userEntity->getShadowtoken());
        $this->assertNull($userEntity->getShadowvalidity());
    }
}
<commit_msg>[Tests] Tests Password::resetPasswordConfirm() for an expired token<commit_after><?php
namespace Bolt\Tests;

use Bolt\AccessControl\Password;
use Hautelook\Phpass\PasswordHash;
use Carbon\Carbon;

/**
 * Test for AccessControl\Password
 *
 * @author <NAME> <<EMAIL>>
 */
class PasswordTest extends BoltUnitTest
{
    public function tearDown()
    {
        $this->resetDb();
    }

    public function testSetRandomPassword()
    {
        $app = $this->getApp();
        $this->addDefaultUser($app);
        $entityName = 'Bolt\Storage\Entity\Users';
        $repo = $app['storage']->getRepository($entityName);

        $logger = $this->getMock('\Monolog\Logger', ['info'], ['testlogger']);
        $logger->expects($this->atLeastOnce())
            ->method('info');
        $app['logger.system'] = $logger;

        $password = new Password($app);
        $newPass = $password->setRandomPassword('admin');

        $userEntity = $repo->getUser('admin');

        $hasher = new PasswordHash($app['access_control.hash.strength'], true);
        $compare = $hasher->CheckPassword($newPass, $userEntity->getPassword());

        $this->assertTrue($compare);
        $this->assertEmpty($userEntity->getShadowpassword());
        $this->assertEmpty($userEntity->getShadowtoken());
        $this->assertNull($userEntity->getShadowvalidity());
    }

    public function testResetPasswordConfirmValidUser()
    {
        $app = $this->getApp();
        $this->addDefaultUser($app);
        $entityName = 'Bolt\Storage\Entity\Users';
        $repo = $app['storage']->getRepository($entityName);

        $shadowToken = $app['randomgenerator']->generateString(32);
        $shadowTokenHash = md5($shadowToken . '-' . str_replace('.', '-', '8.8.8.8'));

        $userEntity = $repo->getUser('admin');
        $userEntity->setShadowpassword('<PASSWORD>');
        $userEntity->setShadowtoken($shadowTokenHash);
        $userEntity->setShadowvalidity(Carbon::create()->addHours(1));
        $repo->save($userEntity);

        $password = new Password($app);
        $result = $password->resetPasswordConfirm($shadowToken, '8.8.8.8');
        $userEntity = $repo->getUser('admin');

        $this->assertTrue($result);
        $this->assertEmpty($userEntity->getShadowpassword());
        $this->assertEmpty($userEntity->getShadowtoken());
        $this->assertNull($userEntity->getShadowvalidity());
    }

    public function testResetPasswordConfirmExpiredToken()
    {
        $app = $this->getApp();
        $this->addDefaultUser($app);
        $entityName = 'Bolt\Storage\Entity\Users';
        $repo = $app['storage']->getRepository($entityName);

        $logger = $this->getMock('\Monolog\Logger', ['error'], ['testlogger']);
        $logger->expects($this->atLeastOnce())
            ->method('error');
        $app['logger.system'] = $logger;

        $shadowToken = $app['randomgenerator']->generateString(32);
        $shadowTokenHash = md5($shadowToken . '-' . str_replace('.', '-', '8.8.8.8'));

        $userEntity = $repo->getUser('admin');
        $userEntity->setShadowpassword('<PASSWORD>');
        $userEntity->setShadowtoken($shadowTokenHash);
        $userEntity->setShadowvalidity(Carbon::create()->addHours(-1));
        $repo->save($userEntity);

        $password = new Password($app);
        $result = $password->resetPasswordConfirm($shadowToken, '8.8.8.8');
        $userEntity = $repo->getUser('admin');

        $this->assertFalse($result);
    }
}

{'fraction_non_alphanumeric': 0.18276986442423201, 'fraction_numerical': 0.006178136262227562, 'mean_word_length': 3.29160530191458, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4933162', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1682, 'n_words': 231}