From 22c2629078fc712f72a0b4cac6bde75144a2cbe2 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 25 Mar 2019 10:45:05 -0400 Subject: [PATCH] Partial tests for new CLI features --- lib/CLI.php | 38 ++++++++++++++++++++++++++++++------- lib/REST/Fever/User.php | 6 +++--- tests/cases/CLI/TestCLI.php | 24 +++++++++++++++++++---- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/CLI.php b/lib/CLI.php index efb1f997..85ee8044 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -6,7 +6,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse; -use Docopt\Response as Opts; +use JKingWeb\Arsse\REST\Fever\User as Fever; class CLI { const USAGE = << [] arsse.php user remove - arsse.php user set-pass [--oldpass=] [] - arsse.php user auth + arsse.php user set-pass [] + [--oldpass=] [--fever] + arsse.php user unset-pass + [--oldpass=] [--fever] + arsse.php user auth [--fever] arsse.php --version arsse.php --help | -h @@ -106,16 +109,36 @@ USAGE_TEXT; return new Conf; } + /** @codeCoverageIgnore */ + protected function getFever(): Fever { + return new Fever; + } + protected function userManage($args): int { switch ($this->command(["add", "remove", "set-pass", "list", "auth"], $args)) { case "add": return $this->userAddOrSetPassword("add", $args[""], $args[""]); case "set-pass": - return $this->userAddOrSetPassword("passwordSet", $args[""], $args[""], $args["--oldpass"]); + if ($args['--fever']) { + $passwd = $this->getFever()->register($args[""], $args[""]); + if (is_null($args[""])) { + echo $passwd.\PHP_EOL; + } + return 0; + } else { + return $this->userAddOrSetPassword("passwordSet", $args[""], $args[""], $args["--oldpass"]); + } + case "unset-pass": + if ($args['--fever']) { + $this->getFever()->unegister($args[""]); + } else { + Arsse::$user->passwordUnset($args[""], $args["--oldpass"]); + } + return 0; case "remove": return (int) !Arsse::$user->remove($args[""]); case "auth": - return $this->userAuthenticate($args[""], $args[""]); + return $this->userAuthenticate($args[""], $args[""], $args["--fever"]); case "list": case "": return $this->userList(); @@ -138,8 +161,9 @@ USAGE_TEXT; return 0; } - protected function userAuthenticate(string $user, string $password): int { - if (Arsse::$user->auth($user, $password)) { + protected function userAuthenticate(string $user, string $password, bool $fever = false): int { + $result = $fever ? $this->getFever()->authenticate($user, $password) : Arsse::$user->auth($user, $password); + if ($result) { echo Arsse::$lang->msg("CLI.Auth.Success").\PHP_EOL; return 0; } else { diff --git a/lib/REST/Fever/User.php b/lib/REST/Fever/User.php index ac3cf696..b702ae40 100644 --- a/lib/REST/Fever/User.php +++ b/lib/REST/Fever/User.php @@ -10,7 +10,7 @@ use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Db\ExceptionInput; class User { - public static function register(string $user, string $password = null): string { + public function register(string $user, string $password = null): string { $password = $password ?? Arsse::$user->generatePassword(); $hash = md5("$user:$password"); $tr = Arsse::$db->begin(); @@ -20,11 +20,11 @@ class User { return $password; } - public static function unregister(string $user): bool { + public function unregister(string $user): bool { return (bool) Arsse::$db->tokenRevoke($user, "fever.login"); } - public static function authenticate(string $user, string $password): bool { + public function authenticate(string $user, string $password): bool { try { return (bool) Arsse::$db->tokenLookup("fever.login", md5("$user:$password")); } catch (ExceptionInput $e) { diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index 608eebce..2ca1ebe1 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -12,6 +12,7 @@ use JKingWeb\Arsse\User; use JKingWeb\Arsse\Database; use JKingWeb\Arsse\Service; use JKingWeb\Arsse\CLI; +use JKingWeb\Arsse\REST\Fever\User as FeverUser; use Phake; /** @covers \JKingWeb\Arsse\CLI */ @@ -174,16 +175,27 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { ($user === "jane.doe@example.com" && $pass === "superman") ); })); + $fever = \Phake::mock(FeverUser::class); + \Phake::when($fever)->authenticate->thenReturn(false); + \Phake::when($fever)->authenticate("john.doe@example.com", "ashalla")->thenReturn(true); + \Phake::when($fever)->authenticate("jane.doe@example.com", "thx1388")->thenReturn(true); + \Phake::when($this->cli)->getFever->thenReturn($fever); $this->assertConsole($this->cli, $cmd, $exitStatus, $output); } public function provideUserAuthentication() { $l = new \JKingWeb\Arsse\Lang; + $success = $l("CLI.Auth.Success"); + $failure = $l("CLI.Auth.Failure"); return [ - ["arsse.php user auth john.doe@example.com secret", 0, $l("CLI.Auth.Success")], - ["arsse.php user auth john.doe@example.com superman", 1, $l("CLI.Auth.Failure")], - ["arsse.php user auth jane.doe@example.com secret", 1, $l("CLI.Auth.Failure")], - ["arsse.php user auth jane.doe@example.com superman", 0, $l("CLI.Auth.Success")], + ["arsse.php user auth john.doe@example.com secret", 0, $success], + ["arsse.php user auth john.doe@example.com superman", 1, $failure], + ["arsse.php user auth jane.doe@example.com secret", 1, $failure], + ["arsse.php user auth jane.doe@example.com superman", 0, $success], + ["arsse.php user auth john.doe@example.com ashalla --fever", 0, $success], + ["arsse.php user auth john.doe@example.com thx1138 --fever", 1, $failure], + ["arsse.php user auth --fever jane.doe@example.com ashalla", 1, $failure], + ["arsse.php user auth --fever jane.doe@example.com thx1138", 0, $success], ]; } @@ -229,4 +241,8 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { ["arsse.php user set-pass jane.doe@example.com", 10402, ""], ]; } + + public function testChangeAFeverPassword() { + $this->markTestIncomplete(); + } }