<?php

namespace Tests\Unit;

use Exception;
use Tests\TestCase;
use AuthStack\Driver\AUDGeneric\Driver;

class AUDDriverTest extends TestCase
{
    /**
     * Test a valid authentication against AUD server.
     *
     * @return void
     * @throws Exception
     */
    public function testValidAUDAuthentication()
    {
        $driver = new Driver();
        $auth = $driver->authenticate(['username' => 'admin', 'password' => 'admin']);

        $this->assertTrue($auth);

        if ($auth){
            $user = $driver->getUser();
            $this->assertInstanceOf('AuthStack\Driver\AUDGeneric\User', $user);
            $this->assertTrue($user->getAttribute('username_readable') === 'admin');
        }
    }

    /**
     * Test getting accounts details from AUD.
     *
     * @return void
     * @throws Exception
     */
    public function testGetAccountDetails()
    {
        $driver = new Driver();
        $auth = $driver->authenticate(['username' => 'admin', 'password' => 'admin']);

        $this->assertTrue($auth);

        if ($auth){
            $user = $driver->getUser();
            $id = $user->getAttribute('id');
            $account = $driver->getUserByPrimaryKey($id);
            $this->assertInstanceOf('Illuminate\Http\JsonResponse', $account);
            $this->assertTrue($account->getData()->account->username_readable === 'admin');
        }
    }
}
