<?php

namespace AuthStack\Driver\LDAP\tests\units;

use AuthStack\Driver\LDAP\User;
use Buckhill\LDAP\EntryCollection;
use mageekguy\atoum;
use function Buckhill\config;
use AuthStack\Driver\LDAP\LDAP as Driver;

class LDAP extends atoum
{
    public function testCreateDriverInstance()
    {
        $this
            ->given(config()->set('LDAPTest', $this->configWithMultipleOU()))
            ->and($ldap = Driver::initialize('LDAPTest', 2))
            ->then
            ->object($ldap)->isInstanceOf(Driver::class)
            ->boolean($ldap->connect())->isTrue()
            ->integer($ldap->getAuthSourceId())->isEqualTo(2)
            ->boolean($ldap->elevate())->isTrue()
        ;
    }

    public function testAuthUser()
    {
        $this
            ->given(config()->set('LDAPTest', $this->configWithMultipleOU()))
            ->and($ldap = Driver::initialize('LDAPTest', 2))
            ->and($ldap->connect())
            ->then
            ->boolean($ldap->authenticate(['username' => 'jbloggs', 'password' => 'bloggs']))->isTrue()
            ->object($ldap->getUser()->getCollection())->isInstanceOf(EntryCollection::class)
            ;
    }

    public function testFindUserByUsername()
    {
        $this
            ->given(config()->set('LDAPTest', $this->configWithMultipleOU()))
            ->and($ldap = Driver::initialize('LDAPTest', 2))
            ->and($ldap->connect())
            ->then
            ->object($ldap->getUserByUsername('nikola@buckhill.co.uk'))->isInstanceOf(User::class)
            ->boolean($ldap->getUserByUsername('this user does not exist'))->isFalse()
            ;
    }

    protected function config()
    {
        return [

            'filters' => [
                [
                    'id'        => 'c192',
                    'title'     => 'Group Membership',
                    'filter'    => 'member=uid={{ uid }},ou=users,dc=buckhill,dc=co,dc=uk',
                    'baseDN'    => 'ou=groups,dc=buckhill,dc=co,dc=uk',
                    'attributes' => ['cn']
                ],

                [
                    'id'        => 'c210',
                    'title'     => 'GitLab Membership',
                    'filter'    => 'member=uid={{ uid }},ou=users,dc=buckhill,dc=co,dc=uk',
                    'baseDN'    => 'ou=GitLab,dc=buckhill,dc=co,dc=uk',
                    'attributes' => ['cn']
                ],
            ],

            'user_auth' => [
                'dc'    => 'dc=buckhill,dc=co,dc=uk',
                'ou'    => 'ou=users',
                'uid'   => 'cn'
            ],

            'attributes' => [
                [
                    'id' => 'id-objectclass',
                    'name' => 'objectclass',
                    'value' => '{{objectclass}}',
                    'enabled' => false,
                    'renamed' => 'objectclass',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-uid',
                    'name' => 'uid',
                    'value' => '{{uid}}',
                    'enabled' => true,
                    'renamed' => 'uid',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-cn',
                    'name' => 'cn',
                    'value' => '{{cn}}',
                    'enabled' => true,
                    'renamed' => 'cn',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-registeredaddress',
                    'name' => 'registeredaddress',
                    'value' => '{{registeredaddress}}',
                    'enabled' => true,
                    'renamed' => 'registeredaddress',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-destinationindicator',
                    'name' => 'destinationindicator',
                    'value' => '{{destinationindicator}}',
                    'enabled' => true,
                    'renamed' => 'destinationindicator',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-identity',
                    'name' => 'identity',
                    'value' => '{{identity}}',
                    'enabled' => false,
                    'renamed' => 'identity',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-givenname',
                    'name' => 'givenname',
                    'value' => '{{givenname}}',
                    'enabled' => true,
                    'renamed' => 'givenname',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-sn',
                    'name' => 'sn',
                    'value' => '{{sn}}',
                    'enabled' => true,
                    'renamed' => 'sn',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-o',
                    'name' => 'o',
                    'value' => '{{o}}',
                    'enabled' => true,
                    'renamed' => 'o',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-mail',
                    'name' => 'mail',
                    'value' => '{{mail}}',
                    'enabled' => true,
                    'renamed' => 'mail',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-telephonenumber',
                    'name' => 'telephonenumber',
                    'value' => '{{telephonenumber}}',
                    'enabled' => true,
                    'renamed' => 'telephonenumber',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-postaladdress',
                    'name' => 'postaladdress',
                    'value' => '{{postaladdress}}',
                    'enabled' => true,
                    'renamed' => 'postaladdress',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-l',
                    'name' => 'l',
                    'value' => '{{l}}',
                    'enabled' => true,
                    'renamed' => 'l',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-postalcode',
                    'name' => 'postalcode',
                    'value' => '{{postalcode}}',
                    'enabled' => true,
                    'renamed' => 'postalcode',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-st',
                    'name' => 'st',
                    'value' => '{{st}}',
                    'enabled' => true,
                    'renamed' => 'st',
                    'source_id' => 1,
                ],

                [
                    'id' => 'id-userpassword',
                    'name' => 'userpassword',
                    'value' => '{{userpassword}}',
                    'enabled' => true,
                    'renamed' => 'userpassword',
                    'source_id' => 1,
                ],

                [
                    'id' => 'cn-c192',
                    'name' => 'cn',
                    'value' => '{{cn}}',
                    'enabled' => true,
                    'renamed' => 'memberOf',
                    'source_id' => 'c192',
                ],

                [
                    'id' => 'cn-c210',
                    'name' => 'cn',
                    'value' => '{{cn}}',
                    'enabled' => true,
                    'renamed' => 'memberOfGitlab',
                    'source_id' => 'c210',
                ]
            ],

            'connection' => [
                'TLS'   => false,
                'host'  => '10.0.0.188',
                'port'  => '389',
                'options' => [
                    [
                        'name' => LDAP_OPT_PROTOCOL_VERSION,
                        'value' => 3
                    ]
                ],
                'username' => 'cn=admin,dc=buckhill,dc=co,dc=uk',
                'password' => 'letmein123'
            ]

        ];
    }

    protected function configWithMultipleOU()
    {
        return array_merge($this->config(), [
            'user_auth' => [
                'dc'    => 'dc=buckhill,dc=co,dc=uk',
                'ou'    => ['ou=users', 'ou=inactive,ou=users'],
                'uid'   => 'uid'
            ]
        ]);
    }
}