-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
68,807 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
/vendor/* | ||
composer.phar | ||
.vscode/launch.json | ||
|
||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
]; | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/TestCase/Authentication/Authenticator/ProvisoryTokenAuthenticatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |