<commit_before><?php

namespace Adldap\Laravel\Tests;

use Mockery;
use Adldap\Models\User;
use Adldap\Laravel\Facades\Adldap;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\App;

class AdldapTest extends FunctionalTestCase
{
    public function testConfigurationNotFoundException()
    {
        $this->app['config']->set('adldap', null);

        $this->setExpectedException('Adldap\Laravel\Exceptions\ConfigurationMissingException');

        App::make('adldap');
    }

    public function testRegistration()
    {
        $this->assertTrue(app()->register('Adldap\Laravel\AdldapServiceProvider'));
        $this->assertTrue(app()->register('Adldap\Laravel\AdldapAuthServiceProvider'));
    }

    public function testContractResolve()
    {
        $this->app['config']->set('adldap.auto_connect', false);

        $adldap = $this->app->make('Adldap\Contracts\Adldap');

        $this->assertInstanceOf('Adldap\Adldap', $adldap);
        $this->assertInstanceOf('Adldap\Contracts\Adldap', $adldap);
    }

    public function testAuthPasses()
    {
        $mockedBuilder = Mockery::mock('Adldap\Query\Builder');

        $rawAttributes = [
            'samaccountname' => ['jdoe'],
            'mail' => ['<EMAIL>'],
            'cn' => ['<NAME>'],
        ];

        $adUser = (new User([], $mockedBuilder))->setRawAttributes($rawAttributes);

        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedSearch->shouldReceive('first')->once()->andReturn($adUser);
        $mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);
        Adldap::shouldReceive('authenticate')->once()->andReturn(true);

        $this->assertTrue(Auth::attempt(['email' => '<EMAIL>', 'password' => '<PASSWORD>']));

        $user = Auth::user();

        $this->assertEquals('<EMAIL>', $user->email);
        $this->assertTrue(\Hash::check('12345', $user->password));
    }

    public function testAuthPassesWithPersistentAdldapUser()
    {
        $this->testAuthPasses();

        $this->assertInstanceOf('Adldap\Models\User', \Auth::user()->adldapUser);
        $this->assertInstanceOf('Adldap\Models\User', auth()->user()->adldapUser);
    }

    public function testAuthPassesWithoutPersistentAdldapUser()
    {
        $this->app['config']->set('adldap_auth.bind_user_to_model', false);

        $this->testAuthPasses();

        $this->assertNull(\Auth::user()->adldapUser);
        $this->assertNull(auth()->user()->adldapUser);
    }

    public function testAuthFails()
    {
        $mockedBuilder = Mockery::mock('Adldap\Query\Builder');

        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedSearch->shouldReceive('first')->once()->andReturn(null);
        $mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);

        $this->assertFalse(Auth::attempt(['email' => '<EMAIL>', 'password' => '<PASSWORD>']));
    }

    public function testAuthFailsWhenUserFound()
    {
        $mockedBuilder = Mockery::mock('Adldap\Query\Builder');

        $rawAttributes = [
            'samaccountname' => ['jdoe'],
            'mail' => ['<EMAIL>'],
            'cn' => ['<NAME>'],
        ];

        $adUser = (new User([], $mockedBuilder))->setRawAttributes($rawAttributes);

        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedSearch->shouldReceive('first')->once()->andReturn($adUser);
        $mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);
        Adldap::shouldReceive('authenticate')->once()->andReturn(false);

        $this->assertFalse(Auth::attempt(['email' => '<EMAIL>', 'password' => '<PASSWORD>']));
    }

    public function testCredentialsKeyDoesNotExist()
    {
        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);

        $nonExistantInputKey = 'non-existant-key';

        $this->setExpectedException('ErrorException');

        Auth::attempt([$nonExistantInputKey => '<EMAIL>', 'password' => '<PASSWORD>']);
    }
}
<commit_msg>update tests<commit_after><?php

namespace Adldap\Laravel\Tests;

use Mockery;
use Adldap\Models\User;
use Adldap\Laravel\Facades\Adldap;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\App;

class AdldapTest extends FunctionalTestCase
{
    public function testConfigurationNotFoundException()
    {
        $this->app['config']->set('adldap', null);

        $this->setExpectedException('Adldap\Laravel\Exceptions\ConfigurationMissingException');

        App::make('adldap');
    }

    public function testRegistration()
    {
        $this->assertTrue(app()->register('Adldap\Laravel\AdldapServiceProvider'));
        $this->assertTrue(app()->register('Adldap\Laravel\AdldapAuthServiceProvider'));
    }

    public function testContractResolve()
    {
        $this->app['config']->set('adldap.auto_connect', false);

        $adldap = $this->app->make('Adldap\Contracts\Adldap');

        $this->assertInstanceOf('Adldap\Adldap', $adldap);
        $this->assertInstanceOf('Adldap\Contracts\Adldap', $adldap);
    }

    public function testAuthPasses()
    {
        $mockedBuilder = Mockery::mock('Adldap\Query\Builder');

        $rawAttributes = [
            'samaccountname' => ['jdoe'],
            'mail' => ['<EMAIL>'],
            'cn' => ['<NAME>'],
        ];

        $adUser = (new User([], $mockedBuilder))->setRawAttributes($rawAttributes);

        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedSearch->shouldReceive('first')->once()->andReturn($adUser);
        $mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);
        Adldap::shouldReceive('authenticate')->once()->andReturn(true);

        $this->assertTrue(Auth::attempt(['username' => 'jdoe', 'password' => '<PASSWORD>']));

        $user = Auth::user();

        $this->assertEquals('<EMAIL>', $user->email);
        $this->assertTrue(\Hash::check('12345', $user->password));
    }

    public function testAuthPassesWithPersistentAdldapUser()
    {
        $this->testAuthPasses();

        $this->assertInstanceOf('Adldap\Models\User', \Auth::user()->adldapUser);
        $this->assertInstanceOf('Adldap\Models\User', auth()->user()->adldapUser);
    }

    public function testAuthPassesWithoutPersistentAdldapUser()
    {
        $this->app['config']->set('adldap_auth.bind_user_to_model', false);

        $this->testAuthPasses();

        $this->assertNull(\Auth::user()->adldapUser);
        $this->assertNull(auth()->user()->adldapUser);
    }

    public function testAuthFails()
    {
        $mockedBuilder = Mockery::mock('Adldap\Query\Builder');

        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedSearch->shouldReceive('first')->once()->andReturn(null);
        $mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);

        $this->assertFalse(Auth::attempt(['username' => 'jdoe', 'password' => '<PASSWORD>']));
    }

    public function testAuthFailsWhenUserFound()
    {
        $mockedBuilder = Mockery::mock('Adldap\Query\Builder');

        $rawAttributes = [
            'samaccountname' => ['jdoe'],
            'mail' => ['<EMAIL>'],
            'cn' => ['<NAME>'],
        ];

        $adUser = (new User([], $mockedBuilder))->setRawAttributes($rawAttributes);

        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedSearch->shouldReceive('first')->once()->andReturn($adUser);
        $mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);
        Adldap::shouldReceive('authenticate')->once()->andReturn(false);

        $this->assertFalse(Auth::attempt(['username' => 'jdoe', 'password' => '<PASSWORD>']));
    }

    public function testCredentialsKeyDoesNotExist()
    {
        $mockedSearch = Mockery::mock('Adldap\Classes\Search');

        $mockedUsers = Mockery::mock('Adldap\Classes\Users');

        $mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);

        Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);

        $nonExistantInputKey = 'non-existant-key';

        $this->setExpectedException('ErrorException');

        Auth::attempt([$nonExistantInputKey => '<EMAIL>', 'password' => '<PASSWORD>']);
    }
}

{'fraction_non_alphanumeric': 0.18754459280399552, 'fraction_numerical': 0.0010192640913260626, 'mean_word_length': 3.7218479307025985, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 212, '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': '5176800', 'n_tokens_mistral': 3041, 'n_tokens_neox': 2915, 'n_words': 307}