From 8f6b72fdc7d0d9a706b2d76f40572be82f364b33 Mon Sep 17 00:00:00 2001 From: rrd108 Date: Tue, 30 May 2023 19:12:33 +0200 Subject: [PATCH] all tests passing --- composer.json | 5 ++ phpunit.xml.dist | 12 ++++- tests/Fixture/UsersFixture.php | 40 ++++++++++++++++ .../ProvisoryTokenAuthenticatorTest.php | 46 +++++++++++++++---- tests/bootstrap.php | 13 ++++-- 5 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 tests/Fixture/UsersFixture.php diff --git a/composer.json b/composer.json index 9a91ad7..6d1b8be 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,11 @@ "ApiTokenAuthenticator\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "ApiTokenAuthenticator\\Test\\": "tests/" + } + }, "authors": [ { "name": "rrd", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4e1c76a..9e9499c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,7 +10,6 @@ - @@ -19,4 +18,13 @@ - \ No newline at end of file + + + + + + + + + diff --git a/tests/Fixture/UsersFixture.php b/tests/Fixture/UsersFixture.php new file mode 100644 index 0000000..c7c5164 --- /dev/null +++ b/tests/Fixture/UsersFixture.php @@ -0,0 +1,40 @@ + ['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', + ], + ]; +} diff --git a/tests/TestCase/Authentication/Authenticator/ProvisoryTokenAuthenticatorTest.php b/tests/TestCase/Authentication/Authenticator/ProvisoryTokenAuthenticatorTest.php index 7fa4487..2242aab 100644 --- a/tests/TestCase/Authentication/Authenticator/ProvisoryTokenAuthenticatorTest.php +++ b/tests/TestCase/Authentication/Authenticator/ProvisoryTokenAuthenticatorTest.php @@ -1,28 +1,58 @@ 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()); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 15acc5a..cfba071 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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');