1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-26 18:42:40 +00:00
Arsse/tests/cases/REST/Fever/TestUser.php

96 lines
4.1 KiB
PHP
Raw Normal View History

<?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\Fever;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\User;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Db\ExceptionInput;
use JKingWeb\Arsse\User\ExceptionConflict as UserException;
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();
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);
// 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));
}
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-07-06 01:47:44 +00:00
}
2021-03-01 23:01:25 +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) {
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"));
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
2021-03-01 23:01:25 +00:00
$this->prepTest()->register($user, $password);
} else {
2021-03-01 23:01:25 +00:00
$this->assertSame($exp, $this->prepTest()->register($user, $password));
}
} 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-10-16 18:42:43 +00:00
public function providePasswordCreations(): iterable {
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");
}
/** @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-10-16 18:42:43 +00:00
public function provideUserAuthenticationRequests(): iterable {
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],
];
}
}