HummingBirdAnimeClient/tests/AnimeClient/Auth/HummingbirdAuthTest.php

89 lines
2.3 KiB
PHP
Raw Normal View History

2015-10-19 12:50:46 -04:00
<?php
use Aura\Session\SessionFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Aviat\Ion\Friend;
use Aviat\AnimeClient\Auth\HummingbirdAuth;
class HummingbirdAuthTest extends AnimeClient_TestCase {
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
static $session;
static $sessionHandler;
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
public static function setUpBeforeClass()
{
self::$session = (new SessionFactory)->newInstance([]);
}
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
public function setUp()
{
parent::setUp();
$auth = new HummingbirdAuth($this->container);
$friend = new Friend($auth);
$this->auth = $friend;
$this->container->setInstance('session', self::$session);
2015-10-19 12:50:46 -04:00
}
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
public function dataAuthenticate()
{
$testToken = 'notReallyAValidTokenButThisIsATest';
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
return [
'successful auth call' => [
'username' => 'timw4mailtest',
'password' => 'password',
'response_data' => [
'code' => 201,
'body' => json_encode($testToken)
],
'session_value' => $testToken,
'expected' => TRUE,
],
'unsuccessful auth call' => [
'username' => 'foo',
'password' => 'foobarbaz',
'response_data' => [
'code' => 401,
'body' => '{"error":"Invalid credentials"}',
],
'session_value' => FALSE,
'expected' => FALSE,
]
];
}
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
/**
* @dataProvider dataAuthenticate
*/
public function testAuthenticate($username, $password, $response_data, $session_value, $expected)
{
$this->container->get('config')
->set('hummingbird_username', $username);
$model = new MockBaseApiModel($this->container);
$mock = new MockHandler([
new Response($response_data['code'], [], $response_data['body'])
]);
$handler = HandlerStack::create($mock);
$client = new Client([
'handler' => $handler,
'http_errors' => FALSE // Don't throw an exception for 400/500 class status codes
]);
$model->__set('client', $client);
$this->auth->__set('model', $model);
2015-11-13 11:33:27 -05:00
2015-10-19 12:50:46 -04:00
$actual = $this->auth->authenticate($password);
$this->assertEquals($expected, $actual);
2015-10-19 15:13:18 -04:00
}
2015-11-13 11:33:27 -05:00
2015-10-19 15:13:18 -04:00
public function testIsAuthenticated()
{
$data = $this->dataAuthenticate();
call_user_func_array([$this, 'testAuthenticate'], $data['successful auth call']);
$this->assertTrue($this->auth->is_authenticated());
2015-11-13 11:33:27 -05:00
$this->auth->logout();
2015-10-19 15:13:18 -04:00
$this->assertFalse($this->auth->is_authenticated());
2015-10-19 12:50:46 -04:00
}
}