Skip to content

Commit

Permalink
all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed May 30, 2023
1 parent 4b47d0f commit 8f6b72f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 14 deletions.
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
12 changes: 10 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<php>
<ini name="memory_limit" value="-1"/>
<env name="FIXTURE_SCHEMA_METADATA" value="./vendor/cakephp/cakephp/tests/schema.php"/>
</php>

<testsuites>
Expand All @@ -19,4 +18,13 @@
</testsuite>
</testsuites>

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

</phpunit>
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
@@ -1,28 +1,58 @@
<?php

namespace ApiTokenAuthenticator\Test\Authentication\Authenticator;

use Cake\Core\Configure;
use Cake\Http\ServerRequest;
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 function testAuthenticateViaHeaderToken()
public $fixtures = ['plugin.ApiTokenAuthenticator.Users',];

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

public function setUp(): void
{
$identifiers = new IdentifierCollection([
parent::setUp();

$this->identifiers = new IdentifierCollection([
'Authentication.Token' => [
'header' => 'Token',
],
]);
$request = new ServerRequest();
//$request->withAddedHeader('Token', 'token-1');
$options = Configure::read('ApiTokenAuthenticator');
$this->request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/testpath'],
[],
['username' => 'rrd', 'password' => 'webmania']
);

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

$result = $tokenAuth->authenticate($request);
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());
}
}
13 changes: 9 additions & 4 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@
],
]);

if (!getenv('DB_URL')) {
putenv('DB_URL=sqlite:///:memory:');
}
ConnectionManager::setConfig('test', ['url' => getenv('DB_URL')]);
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');

Expand Down

0 comments on commit 8f6b72f

Please sign in to comment.