1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +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);
namespace JKingWeb\Arsse;
use Docopt\Response as Opts;
use JKingWeb\Arsse\REST\Fever\User as Fever;
class CLI {
const USAGE = <<<USAGE_TEXT
@ -18,8 +18,11 @@ Usage:
arsse.php user [list]
arsse.php user add <username> [<password>]
arsse.php user remove <username>
arsse.php user set-pass [--oldpass=<pass>] <username> [<password>]
arsse.php user auth <username> <password>
arsse.php user set-pass <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 --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["<username>"], $args["<password>"]);
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"]);
}
case "unset-pass":
if ($args['--fever']) {
$this->getFever()->unegister($args["<username>"]);
} else {
Arsse::$user->passwordUnset($args["<username>"], $args["--oldpass"]);
}
return 0;
case "remove":
return (int) !Arsse::$user->remove($args["<username>"]);
case "auth":
return $this->userAuthenticate($args["<username>"], $args["<password>"]);
return $this->userAuthenticate($args["<username>"], $args["<password>"], $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 {

View file

@ -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) {

View file

@ -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();
}
}