1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00
Arsse/tests/cases/User/TestInternal.php

184 lines
7.4 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\User;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\User\Driver as DriverInterface;
use JKingWeb\Arsse\User\Internal\Driver;
/** @covers \JKingWeb\Arsse\User\Internal\Driver */
class TestInternal extends \JKingWeb\Arsse\Test\AbstractTest {
2019-10-16 18:42:43 +00:00
public function setUp(): void {
2021-02-27 20:24:02 +00:00
parent::setUp();
2018-11-23 00:55:54 +00:00
self::setConf();
// 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(\JKingWeb\Arsse\Db\Transaction::class));
}
protected function prepTest(): Driver {
Arsse::$db = $this->dbMock->get();
return new Driver;
}
2020-01-20 18:52:48 +00:00
public function testConstruct(): void {
$this->assertInstanceOf(DriverInterface::class, new Driver);
}
2020-01-20 18:52:48 +00:00
public function testFetchDriverName(): void {
$this->assertTrue(strlen(Driver::driverName()) > 0);
}
2018-12-05 22:28:11 +00:00
/**
* @dataProvider provideAuthentication
* @group slow
2020-03-01 20:16:50 +00:00
*/
2020-11-09 21:51:30 +00:00
public function testAuthenticateAUser(string $user, $password, bool $exp): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userPasswordGet->with("john.doe@example.com")->returns('$2y$10$1zbqRJhxM8uUjeSBPp4IhO90xrqK0XjEh9Z16iIYEFRV4U.zeAFom'); // hash of "secret"
$this->dbMock->userPasswordGet->with("jane.doe@example.com")->returns('$2y$10$bK1ljXfTSyc2D.NYvT.Eq..OpehLRXVbglW.23ihVuyhgwJCd.7Im'); // hash of "superman"
$this->dbMock->userPasswordGet->with("owen.hardy@example.com")->returns("");
$this->dbMock->userPasswordGet->with("kira.nerys@example.com")->throws(new \JKingWeb\Arsse\User\ExceptionConflict("doesNotExist"));
$this->dbMock->userPasswordGet->with("007@example.com")->returns(null);
$this->assertSame($exp, $this->prepTest()->auth($user, $password));
}
2019-10-16 18:42:43 +00:00
public function provideAuthentication(): iterable {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
$owen = "owen.hardy@example.com";
$kira = "kira.nerys@example.com";
$bond = "007@example.com";
return [
2020-11-09 21:51:30 +00:00
[$john, "secret", true],
[$jane, "superman", true],
[$owen, "", true],
[$kira, "ashalla", false],
[$john, "top secret", false],
[$jane, "clark kent", false],
[$owen, "watchmaker", false],
[$kira, "singha", false],
[$john, "", false],
[$jane, "", false],
[$kira, "", false],
[$bond, "for England", false],
[$bond, "", false],
];
}
2020-01-20 18:52:48 +00:00
public function testListUsers(): void {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
2021-03-01 23:01:25 +00:00
$this->dbMock->userList->returns([$john, $jane])->returns([$jane, $john]);
$driver = $this->prepTest();
$this->assertSame([$john, $jane], $driver->userList());
$this->assertSame([$jane, $john], $driver->userList());
2021-03-01 23:01:25 +00:00
$this->dbMock->userList->times(2)->called();
}
2020-01-20 18:52:48 +00:00
public function testAddAUser(): void {
$john = "john.doe@example.com";
2021-03-01 23:01:25 +00:00
$this->dbMock->userAdd->does(function($user, $pass) {
return $pass;
});
2021-03-01 23:01:25 +00:00
$driver = $this->prepTest();
$this->assertNull($driver->userAdd($john));
$this->assertNull($driver->userAdd($john, null));
$this->assertSame("secret", $driver->userAdd($john, "secret"));
2021-03-01 23:01:25 +00:00
$this->dbMock->userAdd->calledWith($john, "secret");
$this->dbMock->userAdd->called();
}
public function testRenameAUser(): void {
$john = "john.doe@example.com";
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(true);
$this->assertTrue($this->prepTest()->userRename($john, "jane.doe@example.com"));
$this->assertFalse($this->prepTest()->userRename($john, $john));
$this->dbMock->userExists->times(2)->calledWith($john);
}
public function testRenameAMissingUser(): void {
$john = "john.doe@example.com";
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(false);
$this->assertException("doesNotExist", "User", "ExceptionConflict");
2021-03-01 23:01:25 +00:00
$this->prepTest()->userRename($john, "jane.doe@example.com");
}
2020-01-20 18:52:48 +00:00
public function testRemoveAUser(): void {
$john = "john.doe@example.com";
2021-03-01 23:01:25 +00:00
$this->dbMock->userRemove->returns(true)->throws(new \JKingWeb\Arsse\User\ExceptionConflict("doesNotExist"));
$driver = $this->prepTest();
$this->assertTrue($driver->userRemove($john));
2021-03-01 23:01:25 +00:00
$this->dbMock->userRemove->calledWith($john);
$this->assertException("doesNotExist", "User", "ExceptionConflict");
try {
$this->assertFalse($driver->userRemove($john));
} finally {
2021-03-01 23:01:25 +00:00
$this->dbMock->userRemove->times(2)->calledWith($john);
}
}
2020-01-20 18:52:48 +00:00
public function testSetAPassword(): void {
$john = "john.doe@example.com";
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(true);
$this->assertSame("superman", $this->prepTest()->userPasswordSet($john, "superman"));
$this->assertSame(null, $this->prepTest()->userPasswordSet($john, null));
$this->dbMock->userPasswordSet->never()->called();
}
public function testSetAPasswordForAMssingUser(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(false);
$this->assertException("doesNotExist", "User", "ExceptionConflict");
2021-03-01 23:01:25 +00:00
$this->prepTest()->userPasswordSet("john.doe@example.com", "secret");
}
2020-01-20 18:52:48 +00:00
public function testUnsetAPassword(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(true);
$this->assertTrue($this->prepTest()->userPasswordUnset("john.doe@example.com"));
$this->dbMock->userPasswordSet->never()->called();
}
2020-01-20 18:52:48 +00:00
public function testUnsetAPasswordForAMssingUser(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(false);
$this->assertException("doesNotExist", "User", "ExceptionConflict");
2021-03-01 23:01:25 +00:00
$this->prepTest()->userPasswordUnset("john.doe@example.com");
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testGetUserProperties(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(true);
$this->assertSame([], $this->prepTest()->userPropertiesGet("john.doe@example.com"));
$this->dbMock->userExists->calledWith("john.doe@example.com");
2020-11-14 02:41:27 +00:00
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testGetPropertiesForAMissingUser(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(false);
$this->assertException("doesNotExist", "User", "ExceptionConflict");
2020-11-14 02:41:27 +00:00
try {
2021-03-01 23:01:25 +00:00
$this->prepTest()->userPropertiesGet("john.doe@example.com");
2020-11-14 02:41:27 +00:00
} finally {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->calledWith("john.doe@example.com");
2020-11-14 02:41:27 +00:00
}
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testSetUserProperties(): void {
$in = ['admin' => true];
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(true);
$this->assertSame($in, $this->prepTest()->userPropertiesSet("john.doe@example.com", $in));
$this->dbMock->userExists->calledWith("john.doe@example.com");
2020-11-14 02:41:27 +00:00
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testSetPropertiesForAMissingUser(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->returns(false);
$this->assertException("doesNotExist", "User", "ExceptionConflict");
2020-11-14 02:41:27 +00:00
try {
2021-03-01 23:01:25 +00:00
$this->prepTest()->userPropertiesSet("john.doe@example.com", ['admin' => true]);
2020-11-14 02:41:27 +00:00
} finally {
2021-03-01 23:01:25 +00:00
$this->dbMock->userExists->calledWith("john.doe@example.com");
2020-11-14 02:41:27 +00:00
}
}
}