Skip to content

Commit

Permalink
Merge branch 'tests' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed May 30, 2023
2 parents 2320a75 + 8f6b72f commit d690c90
Show file tree
Hide file tree
Showing 8 changed files with 68,807 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/vendor/*
composer.phar
.vscode/launch.json

.phpunit.result.cache
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"ApiTokenAuthenticator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ApiTokenAuthenticator\\Test\\": "tests/"
}
},
"authors": [
{
"name": "rrd",
Expand Down
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="./tests/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>

<php>
<ini name="memory_limit" value="-1"/>
</php>

<testsuites>
<testsuite name="tokenAuthentication">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

</phpunit>
Binary file added src/composer
Binary file not shown.
68,612 changes: 68,612 additions & 0 deletions src/phpunit

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ApiTokenAuthenticator\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class UsersFixture extends TestFixture
{
/**
* Fields property
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer', 'autoIncrement' => true, 'null' => false],
'email' => ['type' => 'string', 'length' => 255, 'null' => false],
'password' => ['type' => 'string', 'length' => 255, 'null' => false],
'token' => ['type' => 'string', 'length' => 255, 'null' => false],
'created' => 'datetime',
'modified' => 'datetime',
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
],
];

/**
* Records property
*
* @var array
*/
public $records = [
[
'email' => 'rrd',
'password' => 'webmania',
'token' => 'token-1',
'created' => '2023-05-30 18:00:00',
'modified' => '2023-05-30 18:00:00',
],
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace ApiTokenAuthenticator\Test\Authentication\Authenticator;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use Cake\Http\ServerRequestFactory;
use Authentication\Authenticator\Result;
use Authentication\Identifier\IdentifierCollection;
use ApiTokenAuthenticator\Authentication\Authenticator\ProvisoryTokenAuthenticator;

class ProvisoryTokenAuthenticatorTest extends TestCase
{
public $fixtures = ['plugin.ApiTokenAuthenticator.Users',];

private $identifiers;
private $request;
private $tokenAuth;

public function setUp(): void
{
parent::setUp();

$this->identifiers = new IdentifierCollection([
'Authentication.Token' => [
'header' => 'Token',
],
]);
$this->request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/testpath'],
[],
['username' => 'rrd', 'password' => 'webmania']
);

$options = Configure::read('ApiTokenAuthenticator');
$this->tokenAuth = new ProvisoryTokenAuthenticator($this->identifiers, $options);
}

public function testAuthenticateMissingHeaderToken()
{
$result = $this->tokenAuth->authenticate($this->request);
$this->assertSame(Result::FAILURE_CREDENTIALS_MISSING, $result->getStatus());
}

public function testAuthenticateWithValidToken()
{
$requestWithHeader = $this->request->withAddedHeader('Token', 'token-1');
$result = $this->tokenAuth->authenticate($requestWithHeader);
$this->assertSame(Result::SUCCESS, $result->getStatus());
}

public function testAuthenticateWithInvalidToken()
{
$requestWithHeader = $this->request->withAddedHeader('Token', 'gauranga');
$result = $this->tokenAuth->authenticate($requestWithHeader);
$this->assertSame(Result::FAILURE_IDENTITY_NOT_FOUND, $result->getStatus());
}
}
58 changes: 58 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Routing\Router;
use Cake\Utility\Security;

$findRoot = function ($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);
throw new Exception('Cannot find the root of the application, unable to run tests');
};
$root = $findRoot(__FILE__);
unset($findRoot);
chdir($root);

require_once 'vendor/cakephp/cakephp/src/basics.php';
require_once 'vendor/autoload.php';

define('ROOT', $root . DS . 'tests' . DS . 'test_app' . DS);
define('APP', ROOT . 'App' . DS);
define('TMP', sys_get_temp_dir() . DS);
define('CONFIG', ROOT . DS . 'config' . DS);

Configure::write('debug', true);
Configure::write('App', [
'namespace' => 'TestApp',
'paths' => [
'plugins' => [ROOT . 'Plugin' . DS],
'templates' => [ROOT . 'templates' . DS],
],
]);

ConnectionManager::setConfig('test', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlite',
'database' => ':memory:',
'encoding' => 'utf8',
'timezone' => 'UTC',
'quoteIdentifiers' => false,
]);

Router::reload();
Security::setSalt('YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');

Plugin::getCollection()->add(new \Authentication\Plugin());

$_SERVER['PHP_SELF'] = '/';

Configure::load('ApiTokenAuthenticator.apiTokenAuthenticator');

0 comments on commit d690c90

Please sign in to comment.