J. King
3 years ago
9 changed files with 212 additions and 48 deletions
@ -0,0 +1,31 @@ |
|||||
|
<?php |
||||
|
/** @license MIT |
||||
|
* Copyright 2017 J. King, Dustin Wilson et al. |
||||
|
* See LICENSE and AUTHORS files for details */ |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\REST\Miniflux; |
||||
|
|
||||
|
use JKingWeb\Arsse\Arsse; |
||||
|
use JKingWeb\Arsse\User\ExceptionConflict; |
||||
|
|
||||
|
class Token { |
||||
|
protected const TOKEN_LENGTH = 32; |
||||
|
|
||||
|
public function tokenGenerate(string $user, ?string $label = null): string { |
||||
|
// Miniflux produces tokens in base64url alphabet |
||||
|
$t = str_replace(["+", "/"], ["-", "_"], base64_encode(random_bytes(self::TOKEN_LENGTH))); |
||||
|
return Arsse::$db->tokenCreate($user, "miniflux.login", $t, null, $label); |
||||
|
} |
||||
|
|
||||
|
public function tokenList(string $user): array { |
||||
|
if (!Arsse::$db->userExists($user)) { |
||||
|
throw new ExceptionConflict("doesNotExist", ["action" => __FUNCTION__, "user" => $user]); |
||||
|
} |
||||
|
$out = []; |
||||
|
foreach (Arsse::$db->tokenList($user, "miniflux.login") as $r) { |
||||
|
$out[] = ['label' => $r['data'], 'id' => $r['id']]; |
||||
|
} |
||||
|
return $out; |
||||
|
} |
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
<?php |
||||
|
/** @license MIT |
||||
|
* Copyright 2017 J. King, Dustin Wilson et al. |
||||
|
* See LICENSE and AUTHORS files for details */ |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\TestCase\REST\Miniflux\PDO; |
||||
|
|
||||
|
/** @covers \JKingWeb\Arsse\REST\Miniflux\Token<extended> |
||||
|
* @group optional */ |
||||
|
class TestToken extends \JKingWeb\Arsse\TestCase\REST\Miniflux\TestV1 { |
||||
|
use \JKingWeb\Arsse\Test\PDOTest; |
||||
|
} |
@ -0,0 +1,70 @@ |
|||||
|
<?php |
||||
|
/** @license MIT |
||||
|
* Copyright 2017 J. King, Dustin Wilson et al. |
||||
|
* See LICENSE and AUTHORS files for details */ |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\TestCase\REST\Miniflux; |
||||
|
|
||||
|
use JKingWeb\Arsse\Arsse; |
||||
|
use JKingWeb\Arsse\Database; |
||||
|
use JKingWeb\Arsse\Db\Transaction; |
||||
|
use JKingWeb\Arsse\REST\Miniflux\Token; |
||||
|
use JKingWeb\Arsse\Test\Result; |
||||
|
|
||||
|
/** @covers \JKingWeb\Arsse\REST\Miniflux\Token<extended> */ |
||||
|
class TestToken extends \JKingWeb\Arsse\Test\AbstractTest { |
||||
|
protected const NOW = "2020-12-09T22:35:10.023419Z"; |
||||
|
protected const TOKEN = "Tk2o9YubmZIL2fm2w8Z4KlDEQJz532fNSOcTG0s2_xc="; |
||||
|
|
||||
|
protected $h; |
||||
|
protected $transaction; |
||||
|
|
||||
|
public function setUp(): void { |
||||
|
self::clearData(); |
||||
|
self::setConf(); |
||||
|
// create a mock database interface |
||||
|
Arsse::$db = \Phake::mock(Database::class); |
||||
|
$this->transaction = \Phake::mock(Transaction::class); |
||||
|
\Phake::when(Arsse::$db)->begin->thenReturn($this->transaction); |
||||
|
$this->h = new Token(); |
||||
|
} |
||||
|
|
||||
|
public function tearDown(): void { |
||||
|
self::clearData(); |
||||
|
} |
||||
|
|
||||
|
protected function v($value) { |
||||
|
return $value; |
||||
|
} |
||||
|
|
||||
|
public function testGenerateTokens(): void { |
||||
|
\Phake::when(Arsse::$db)->tokenCreate->thenReturn("RANDOM TOKEN"); |
||||
|
$this->assertSame("RANDOM TOKEN", $this->h->tokenGenerate("ook", "Eek")); |
||||
|
\Phake::verify(Arsse::$db)->tokenCreate("ook", "miniflux.login", \Phake::capture($token), null, "Eek"); |
||||
|
$this->assertRegExp("/^[A-Za-z0-9_\-]{43}=$/", $token); |
||||
|
} |
||||
|
|
||||
|
public function testListTheTokensOfAUser(): void { |
||||
|
$out = [ |
||||
|
['id' => "TOKEN 1", 'data' => "Ook"], |
||||
|
['id' => "TOKEN 2", 'data' => "Eek"], |
||||
|
['id' => "TOKEN 3", 'data' => "Ack"], |
||||
|
]; |
||||
|
$exp = [ |
||||
|
['label' => "Ook", 'id' => "TOKEN 1"], |
||||
|
['label' => "Eek", 'id' => "TOKEN 2"], |
||||
|
['label' => "Ack", 'id' => "TOKEN 3"], |
||||
|
]; |
||||
|
\Phake::when(Arsse::$db)->tokenList->thenReturn(new Result($this->v($out))); |
||||
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true); |
||||
|
$this->assertSame($exp, $this->h->tokenList("john.doe@example.com")); |
||||
|
\Phake::verify(Arsse::$db)->tokenList("john.doe@example.com", "miniflux.login"); |
||||
|
} |
||||
|
|
||||
|
public function testListTheTokensOfAMissingUser(): void { |
||||
|
\Phake::when(Arsse::$db)->userExists->thenReturn(false); |
||||
|
$this->assertException("doesNotExist", "User", "ExceptionConflict"); |
||||
|
$this->h->tokenList("john.doe@example.com"); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue