1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 08:04:54 +00:00

Partial tests for new CLI features

This commit is contained in:
J. King 2019-03-25 10:45:05 -04:00
parent f4d4feb69c
commit 22c2629078
3 changed files with 54 additions and 14 deletions

View file

@ -6,7 +6,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace JKingWeb\Arsse; namespace JKingWeb\Arsse;
use Docopt\Response as Opts; use JKingWeb\Arsse\REST\Fever\User as Fever;
class CLI { class CLI {
const USAGE = <<<USAGE_TEXT const USAGE = <<<USAGE_TEXT
@ -18,8 +18,11 @@ Usage:
arsse.php user [list] arsse.php user [list]
arsse.php user add <username> [<password>] arsse.php user add <username> [<password>]
arsse.php user remove <username> arsse.php user remove <username>
arsse.php user set-pass [--oldpass=<pass>] <username> [<password>] arsse.php user set-pass <username> [<password>]
arsse.php user auth <username> <password> [--oldpass=<pass>] [--fever]
arsse.php user unset-pass <username>
[--oldpass=<pass>] [--fever]
arsse.php user auth <username> <password> [--fever]
arsse.php --version arsse.php --version
arsse.php --help | -h arsse.php --help | -h
@ -106,16 +109,36 @@ USAGE_TEXT;
return new Conf; return new Conf;
} }
/** @codeCoverageIgnore */
protected function getFever(): Fever {
return new Fever;
}
protected function userManage($args): int { protected function userManage($args): int {
switch ($this->command(["add", "remove", "set-pass", "list", "auth"], $args)) { switch ($this->command(["add", "remove", "set-pass", "list", "auth"], $args)) {
case "add": case "add":
return $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]); return $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]);
case "set-pass": case "set-pass":
if ($args['--fever']) {
$passwd = $this->getFever()->register($args["<username>"], $args["<password>"]);
if (is_null($args["<password>"])) {
echo $passwd.\PHP_EOL;
}
return 0;
} else {
return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"], $args["--oldpass"]); return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"], $args["--oldpass"]);
}
case "unset-pass":
if ($args['--fever']) {
$this->getFever()->unegister($args["<username>"]);
} else {
Arsse::$user->passwordUnset($args["<username>"], $args["--oldpass"]);
}
return 0;
case "remove": case "remove":
return (int) !Arsse::$user->remove($args["<username>"]); return (int) !Arsse::$user->remove($args["<username>"]);
case "auth": case "auth":
return $this->userAuthenticate($args["<username>"], $args["<password>"]); return $this->userAuthenticate($args["<username>"], $args["<password>"], $args["--fever"]);
case "list": case "list":
case "": case "":
return $this->userList(); return $this->userList();
@ -138,8 +161,9 @@ USAGE_TEXT;
return 0; return 0;
} }
protected function userAuthenticate(string $user, string $password): int { protected function userAuthenticate(string $user, string $password, bool $fever = false): int {
if (Arsse::$user->auth($user, $password)) { $result = $fever ? $this->getFever()->authenticate($user, $password) : Arsse::$user->auth($user, $password);
if ($result) {
echo Arsse::$lang->msg("CLI.Auth.Success").\PHP_EOL; echo Arsse::$lang->msg("CLI.Auth.Success").\PHP_EOL;
return 0; return 0;
} else { } else {

View file

@ -10,7 +10,7 @@ use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\ExceptionInput; use JKingWeb\Arsse\Db\ExceptionInput;
class User { 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(); $password = $password ?? Arsse::$user->generatePassword();
$hash = md5("$user:$password"); $hash = md5("$user:$password");
$tr = Arsse::$db->begin(); $tr = Arsse::$db->begin();
@ -20,11 +20,11 @@ class User {
return $password; return $password;
} }
public static function unregister(string $user): bool { public function unregister(string $user): bool {
return (bool) Arsse::$db->tokenRevoke($user, "fever.login"); 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 { try {
return (bool) Arsse::$db->tokenLookup("fever.login", md5("$user:$password")); return (bool) Arsse::$db->tokenLookup("fever.login", md5("$user:$password"));
} catch (ExceptionInput $e) { } catch (ExceptionInput $e) {

View file

@ -12,6 +12,7 @@ use JKingWeb\Arsse\User;
use JKingWeb\Arsse\Database; use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Service; use JKingWeb\Arsse\Service;
use JKingWeb\Arsse\CLI; use JKingWeb\Arsse\CLI;
use JKingWeb\Arsse\REST\Fever\User as FeverUser;
use Phake; use Phake;
/** @covers \JKingWeb\Arsse\CLI */ /** @covers \JKingWeb\Arsse\CLI */
@ -174,16 +175,27 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
($user === "jane.doe@example.com" && $pass === "superman") ($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); $this->assertConsole($this->cli, $cmd, $exitStatus, $output);
} }
public function provideUserAuthentication() { public function provideUserAuthentication() {
$l = new \JKingWeb\Arsse\Lang; $l = new \JKingWeb\Arsse\Lang;
$success = $l("CLI.Auth.Success");
$failure = $l("CLI.Auth.Failure");
return [ return [
["arsse.php user auth john.doe@example.com secret", 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, $l("CLI.Auth.Failure")], ["arsse.php user auth john.doe@example.com superman", 1, $failure],
["arsse.php user auth jane.doe@example.com secret", 1, $l("CLI.Auth.Failure")], ["arsse.php user auth jane.doe@example.com secret", 1, $failure],
["arsse.php user auth jane.doe@example.com superman", 0, $l("CLI.Auth.Success")], ["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, ""], ["arsse.php user set-pass jane.doe@example.com", 10402, ""],
]; ];
} }
public function testChangeAFeverPassword() {
$this->markTestIncomplete();
}
} }