<commit_before><?php

namespace Firesphere\YubiAuth\Tests;

use Firesphere\YubiAuth\YubikeyForm;
use Firesphere\YubiAuth\YubikeyLoginForm;
use Firesphere\YubiAuth\YubikeyLoginHandler;
use Firesphere\YubiAuth\YubikeyMemberAuthenticator;
use PHPUnit_Framework_TestCase;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\IdentityStore;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;

/**
 * Class YubiAuthenticatorTest
 *
 * @mixin PHPUnit_Framework_TestCase
 */
class YubiAuthenticatorTest extends SapphireTest
{

    protected static $fixture_file = '../fixtures/Member.yml';

    /**
     * @var YubikeyLoginHandler
     */
    protected $handler;
    /**
     * @var YubikeyLoginForm
     */
    protected $form;

    /**
     * @var YubikeyMemberAuthenticator
     */
    protected $authenticator;

    protected $request;

    public function setUp()
    {
        parent::setUp();
        $this->objFromFixture(Member::class, 'admin');
        $validator = new MockYubiValidate('apikey', '1234');
        $this->authenticator = Injector::inst()->get(YubikeyMemberAuthenticator::class);
        $this->handler = Injector::inst()->get(YubikeyLoginHandler::class, true, [Security::login_url(), $this->authenticator]);
        $this->form = Injector::inst()->get(
            YubikeyLoginForm::class,
            true,
            [$this->handler, YubikeyMemberAuthenticator::class, '']
        );
        Injector::inst()->registerService($validator, 'Yubikey\\Validate');
    }

    public function tearDown()
    {
        parent::tearDown();
    }

    public function testNoYubikey()
    {
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);

        $this->handler->doLogin(
            [
                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey(
            ['yubiauth' => ''],
            YubikeyForm::create($this->handler),
            $request
        );
        $member = Member::get()->filter(['Email' => '<EMAIL>'])->first();
        $this->assertGreaterThan(0, $member->NoYubikeyCount);
        $this->assertEquals(null, $member->Yubikey);
    }

    public function testNoYubikeyLockout()
    {
        /** @var Member $member */
        $member = Member::get()->filter(array('Email' => '<EMAIL>'))->first();
        $member->NoYubikeyCount = 25;
        $member->write();
        $failedLoginCount = $member->FailedLoginCount;
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);
        $this->handler->doLogin(
            [

                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey(
            ['yubiauth' => ''],
            YubikeyForm::create($this->handler),
            $request
        );
        $member = Member::get()->filter(array('Email' => '<EMAIL>'))->first();
        $this->assertGreaterThan($failedLoginCount, $member->FailedLoginCount);
    }

    public function testYubikey()
    {
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);
        $this->handler->doLogin(
            [

                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey([
            // This OTP is _not_ valid in real situations
            'yubiauth'  => 'jjjjjjucbuipyhde.cybcpnbiixcjkbbyd.ydenhnjkn'
            ],
            YubikeyForm::create($this->handler),
            $request
        );
        $result = Security::getCurrentUser();
        $this->assertEquals(Member::class, $result->ClassName);
        $this->assertEquals('ccccccfinfgr', $result->Yubikey);
        $this->assertEquals(1, $result->YubiAuthEnabled);
        $this->assertEquals('<EMAIL>', $result->Email);
        $this->assertEquals(true, $result->YubiAuthEnabled);
        $result->write();
    }

    public function testYubikeyAfterSuccess()
    {
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);
        $member = Member::get()->filter(['Email' => '<EMAIL>'])->first();
        $member->YubiAuthEnabled = true;
        $member->Yubikey = 'ccccccfinfgr';
        $member->NoYubikeyCount = 50;
        $member->write();
        Injector::inst()->get(IdentityStore::class)->logOut();
        $failedLoginCount = $member->FailedLoginCount;
        $this->handler->doLogin(
            [

                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey(['yubiauth' => ''], YubikeyForm::create($this->handler), $request);
        $resultNoYubi = Security::getCurrentUser();
        $this->assertEquals(null, $resultNoYubi);
        $member = Member::get()->filter(array('Email' => '<EMAIL>'))->first();
        $this->assertGreaterThan($failedLoginCount, $member->FailedLoginCount);
    }

}
<commit_msg>Attempting to fix the test<commit_after><?php

namespace Firesphere\YubiAuth\Tests;

use Firesphere\YubiAuth\YubikeyForm;
use Firesphere\YubiAuth\YubikeyLoginForm;
use Firesphere\YubiAuth\YubikeyLoginHandler;
use Firesphere\YubiAuth\YubikeyMemberAuthenticator;
use PHPUnit_Framework_TestCase;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\IdentityStore;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;

/**
 * Class YubiAuthenticatorTest
 *
 * @mixin PHPUnit_Framework_TestCase
 */
class YubiAuthenticatorTest extends SapphireTest
{

    protected static $fixture_file = '../fixtures/Member.yml';

    /**
     * @var YubikeyLoginHandler
     */
    protected $handler;
    /**
     * @var YubikeyLoginForm
     */
    protected $form;

    /**
     * @var YubikeyMemberAuthenticator
     */
    protected $authenticator;

    protected $request;

    public function setUp()
    {
        parent::setUp();
        $this->objFromFixture(Member::class, 'admin');
        $validator = new MockYubiValidate('apikey', '1234');
        $this->authenticator = Injector::inst()->get(YubikeyMemberAuthenticator::class);
        $this->handler = Injector::inst()->createWithArgs(YubikeyLoginHandler::class, [Security::login_url(), $this->authenticator]);
        $this->form = Injector::inst()->get(
            YubikeyLoginForm::class,
            true,
            [$this->handler, YubikeyMemberAuthenticator::class, '']
        );
        Injector::inst()->registerService($validator, 'Yubikey\\Validate');
    }

    public function tearDown()
    {
        parent::tearDown();
    }

    public function testNoYubikey()
    {
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);

        $this->handler->doLogin(
            [
                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey(
            ['yubiauth' => ''],
            YubikeyForm::create($this->handler),
            $request
        );
        $member = Member::get()->filter(['Email' => '<EMAIL>'])->first();
        $this->assertGreaterThan(0, $member->NoYubikeyCount);
        $this->assertEquals(null, $member->Yubikey);
    }

    public function testNoYubikeyLockout()
    {
        /** @var Member $member */
        $member = Member::get()->filter(array('Email' => '<EMAIL>'))->first();
        $member->NoYubikeyCount = 25;
        $member->write();
        $failedLoginCount = $member->FailedLoginCount;
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);
        $this->handler->doLogin(
            [

                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey(
            ['yubiauth' => ''],
            YubikeyForm::create($this->handler),
            $request
        );
        $member = Member::get()->filter(array('Email' => '<EMAIL>'))->first();
        $this->assertGreaterThan($failedLoginCount, $member->FailedLoginCount);
    }

    public function testYubikey()
    {
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);
        $this->handler->doLogin(
            [

                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey([
            // This OTP is _not_ valid in real situations
            'yubiauth'  => '<PASSWORD>buipyhde.cy<PASSWORD>iix<PASSWORD>.y<PASSWORD>'
            ],
            YubikeyForm::create($this->handler),
            $request
        );
        $result = Security::getCurrentUser();
        $this->assertEquals(Member::class, $result->ClassName);
        $this->assertEquals('ccccccfinfgr', $result->Yubikey);
        $this->assertEquals(1, $result->YubiAuthEnabled);
        $this->assertEquals('<EMAIL>', $result->Email);
        $this->assertEquals(true, $result->YubiAuthEnabled);
        $result->write();
    }

    public function testYubikeyAfterSuccess()
    {
        $request = new HTTPRequest('POST', '/');
        $request->setSession(new Session(['hi' => 'bye']));
        $this->handler->setRequest($request);
        $member = Member::get()->filter(['Email' => '<EMAIL>'])->first();
        $member->YubiAuthEnabled = true;
        $member->Yubikey = 'ccccccfinfgr';
        $member->NoYubikeyCount = 50;
        $member->write();
        Injector::inst()->get(IdentityStore::class)->logOut();
        $failedLoginCount = $member->FailedLoginCount;
        $this->handler->doLogin(
            [

                'Email'    => '<EMAIL>',
                'Password' => 'password',
            ],
            $this->form,
            $request
        );
        $this->handler->validateYubikey(['yubiauth' => ''], YubikeyForm::create($this->handler), $request);
        $resultNoYubi = Security::getCurrentUser();
        $this->assertEquals(null, $resultNoYubi);
        $member = Member::get()->filter(array('Email' => '<EMAIL>'))->first();
        $this->assertGreaterThan($failedLoginCount, $member->FailedLoginCount);
    }

}

{'fraction_non_alphanumeric': 0.16699323120769505, 'fraction_numerical': 0.0017812611328820805, 'mean_word_length': 2.4276556776556775, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 241, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5433604', 'n_tokens_mistral': 3414, 'n_tokens_neox': 3368, 'n_words': 467}