2019-03-25 12:31:49 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
2019-03-25 12:31:49 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\REST\Fever;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\User;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\Db\ExceptionInput;
|
2020-11-15 21:24:26 +00:00
|
|
|
use JKingWeb\Arsse\User\ExceptionConflict as UserException;
|
2019-03-25 12:31:49 +00:00
|
|
|
use JKingWeb\Arsse\Db\Transaction;
|
|
|
|
use JKingWeb\Arsse\REST\Fever\User as FeverUser;
|
|
|
|
|
|
|
|
/** @covers \JKingWeb\Arsse\REST\Fever\User<extended> */
|
|
|
|
class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
|
|
|
|
protected $u;
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
parent::setUp();
|
2019-03-25 12:31:49 +00:00
|
|
|
self::setConf();
|
|
|
|
// create a mock user manager
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->userMock = $this->mock(User::class);
|
|
|
|
$this->userMock->auth->returns(true);
|
2019-03-25 12:31:49 +00:00
|
|
|
// create a mock database interface
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock = $this->mock(Database::class);
|
|
|
|
$this->dbMock->begin->returns($this->mock(Transaction::class));
|
2019-03-25 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 23:01:25 +00:00
|
|
|
protected function prepTest(): FeverUser {
|
|
|
|
Arsse::$user = $this->userMock->get();
|
|
|
|
Arsse::$db = $this->dbMock->get();
|
|
|
|
// instantiate the handler
|
|
|
|
return new FeverUser;
|
2021-04-14 15:17:01 +00:00
|
|
|
}
|
2021-03-01 23:01:25 +00:00
|
|
|
|
2019-03-25 12:31:49 +00:00
|
|
|
/** @dataProvider providePasswordCreations */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testRegisterAUserPassword(string $user, string $password = null, $exp): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->userMock->generatePassword->returns("RANDOM_PASSWORD");
|
|
|
|
$this->dbMock->tokenCreate->does(function($user, $class, $id = null) {
|
2019-03-25 12:31:49 +00:00
|
|
|
return $id ?? "RANDOM_TOKEN";
|
|
|
|
});
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->tokenCreate->with("john.doe@example.org", $this->anything(), $this->anything())->throws(new UserException("doesNotExist"));
|
2019-03-25 12:31:49 +00:00
|
|
|
try {
|
|
|
|
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
|
|
|
|
$this->assertException($exp);
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->prepTest()->register($user, $password);
|
2019-03-25 12:31:49 +00:00
|
|
|
} else {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->assertSame($exp, $this->prepTest()->register($user, $password));
|
2019-03-25 12:31:49 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->tokenRevoke->calledWith($user, "fever.login");
|
|
|
|
$this->dbMock->tokenCreate->calledWith($user, "fever.login", md5($user.":".($password ?? "RANDOM_PASSWORD")));
|
2019-03-25 12:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function providePasswordCreations(): iterable {
|
2019-03-25 12:31:49 +00:00
|
|
|
return [
|
|
|
|
["jane.doe@example.com", "secret", "secret"],
|
|
|
|
["jane.doe@example.com", "superman", "superman"],
|
|
|
|
["jane.doe@example.com", null, "RANDOM_PASSWORD"],
|
|
|
|
["john.doe@example.org", null, new UserException("doesNotExist")],
|
|
|
|
["john.doe@example.net", null, "RANDOM_PASSWORD"],
|
|
|
|
["john.doe@example.net", "secret", "secret"],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testUnregisterAUser(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->tokenRevoke->returns(3);
|
|
|
|
$this->assertTrue($this->prepTest()->unregister("jane.doe@example.com"));
|
|
|
|
$this->dbMock->tokenRevoke->calledWith("jane.doe@example.com", "fever.login");
|
|
|
|
$this->dbMock->tokenRevoke->returns(0);
|
|
|
|
$this->assertFalse($this->prepTest()->unregister("john.doe@example.com"));
|
|
|
|
$this->dbMock->tokenRevoke->calledWith("john.doe@example.com", "fever.login");
|
2019-03-25 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideUserAuthenticationRequests */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testAuthenticateAUserName(string $user, string $password, bool $exp): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->tokenLookup->throws(new ExceptionInput("constraintViolation"));
|
|
|
|
$this->dbMock->tokenLookup->with("fever.login", md5("jane.doe@example.com:secret"))->returns(['user' => "jane.doe@example.com"]);
|
|
|
|
$this->dbMock->tokenLookup->with("fever.login", md5("john.doe@example.com:superman"))->returns(['user' => "john.doe@example.com"]);
|
|
|
|
$this->assertSame($exp, $this->prepTest()->authenticate($user, $password));
|
2019-03-25 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function provideUserAuthenticationRequests(): iterable {
|
2019-03-25 12:31:49 +00:00
|
|
|
return [
|
|
|
|
["jane.doe@example.com", "secret", true],
|
|
|
|
["jane.doe@example.com", "superman", false],
|
|
|
|
["john.doe@example.com", "secret", false],
|
|
|
|
["john.doe@example.com", "superman", true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|