1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/tests/cases/Database/SeriesUser.php

207 lines
9.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);
2018-11-23 15:01:17 +00:00
namespace JKingWeb\Arsse\TestCase\Database;
2017-08-29 14:50:31 +00:00
use JKingWeb\Arsse\Arsse;
trait SeriesUser {
2020-01-20 18:52:48 +00:00
protected function setUpSeriesUser(): void {
2018-11-25 05:03:56 +00:00
$this->data = [
'arsse_users' => [
'columns' => ["id", "password", "num", "admin"],
2022-09-15 14:12:04 +00:00
'rows' => [
2020-12-05 16:01:44 +00:00
["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW', 1, 1], // password is hash of "secret"
["jane.doe@example.com", "", 2, 0],
["john.doe@example.com", "", 3, 0],
],
],
'arsse_user_meta' => [
'columns' => ["owner", "key", "value"],
2022-09-15 14:12:04 +00:00
'rows' => [
2020-12-05 16:01:44 +00:00
["admin@example.net", "lang", "en"],
["admin@example.net", "tz", "America/Toronto"],
["admin@example.net", "sort_asc", "0"],
2020-12-05 16:01:44 +00:00
["jane.doe@example.com", "lang", "fr"],
["jane.doe@example.com", "tz", "Asia/Kuala_Lumpur"],
["jane.doe@example.com", "sort_asc", "1"],
["john.doe@example.com", "stylesheet", "body {background:lightgray}"],
2018-11-25 05:03:56 +00:00
],
],
2018-11-25 05:03:56 +00:00
];
}
2020-01-20 18:52:48 +00:00
protected function tearDownSeriesUser(): void {
2018-11-25 05:03:56 +00:00
unset($this->data);
}
2020-01-20 18:52:48 +00:00
public function testCheckThatAUserExists(): void {
$this->assertTrue(Arsse::$db->userExists("jane.doe@example.com"));
$this->assertFalse(Arsse::$db->userExists("jane.doe@example.org"));
2019-06-21 22:52:27 +00:00
$this->compareExpectations(static::$drv, $this->data);
}
2020-01-20 18:52:48 +00:00
public function testGetAPassword(): void {
$hash = Arsse::$db->userPasswordGet("admin@example.net");
$this->assertSame('$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW', $hash);
$this->assertTrue(password_verify("secret", $hash));
}
2020-01-20 18:52:48 +00:00
public function testGetThePasswordOfAMissingUser(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userPasswordGet("john.doe@example.org");
}
2020-01-20 18:52:48 +00:00
public function testAddANewUser(): void {
$this->assertTrue(Arsse::$db->userAdd("john.doe@example.org", ""));
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id']]);
$state['arsse_users']['rows'][] = ["john.doe@example.org"];
2019-06-21 22:52:27 +00:00
$this->compareExpectations(static::$drv, $state);
}
2020-01-20 18:52:48 +00:00
public function testAddAnExistingUser(): void {
$this->assertException("alreadyExists", "User", "ExceptionConflict");
Arsse::$db->userAdd("john.doe@example.com", "");
}
2020-01-20 18:52:48 +00:00
public function testRemoveAUser(): void {
$this->assertTrue(Arsse::$db->userRemove("admin@example.net"));
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id']]);
array_shift($state['arsse_users']['rows']);
2019-06-21 22:52:27 +00:00
$this->compareExpectations(static::$drv, $state);
}
2020-01-20 18:52:48 +00:00
public function testRemoveAMissingUser(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userRemove("john.doe@example.org");
}
2018-10-26 18:58:04 +00:00
2020-01-20 18:52:48 +00:00
public function testListAllUsers(): void {
$users = ["admin@example.net", "jane.doe@example.com", "john.doe@example.com"];
$this->assertSame($users, Arsse::$db->userList());
}
2018-10-26 18:58:04 +00:00
/**
* @depends testGetAPassword
*/
2020-01-20 18:52:48 +00:00
public function testSetAPassword(): void {
$user = "john.doe@example.com";
$pass = "secret";
$this->assertEquals("", Arsse::$db->userPasswordGet($user));
$this->assertTrue(Arsse::$db->userPasswordSet($user, $pass));
$hash = Arsse::$db->userPasswordGet($user);
$this->assertNotEquals("", $hash);
$this->assertTrue(password_verify($pass, $hash), "Failed verifying password of $user '$pass' against hash '$hash'.");
}
2020-01-20 18:52:48 +00:00
public function testUnsetAPassword(): void {
$user = "john.doe@example.com";
$this->assertEquals("", Arsse::$db->userPasswordGet($user));
$this->assertTrue(Arsse::$db->userPasswordSet($user, null));
$this->assertNull(Arsse::$db->userPasswordGet($user));
}
2020-01-20 18:52:48 +00:00
public function testSetThePasswordOfAMissingUser(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userPasswordSet("john.doe@example.org", "secret");
}
2020-11-16 15:26:14 +00:00
/** @dataProvider provideMetaData */
public function testGetMetadata(string $user, bool $includeLarge, array $exp): void {
$this->assertSame($exp, Arsse::$db->userPropertiesGet($user, $includeLarge));
}
2020-11-16 15:26:14 +00:00
public function provideMetadata(): iterable {
return [
2020-12-06 18:17:19 +00:00
["admin@example.net", true, ['num' => 1, 'admin' => 1, 'lang' => "en", 'sort_asc' => "0", 'tz' => "America/Toronto"]],
["jane.doe@example.com", true, ['num' => 2, 'admin' => 0, 'lang' => "fr", 'sort_asc' => "1", 'tz' => "Asia/Kuala_Lumpur"]],
["john.doe@example.com", true, ['num' => 3, 'admin' => 0, 'stylesheet' => "body {background:lightgray}"]],
["admin@example.net", false, ['num' => 1, 'admin' => 1, 'lang' => "en", 'sort_asc' => "0", 'tz' => "America/Toronto"]],
["jane.doe@example.com", false, ['num' => 2, 'admin' => 0, 'lang' => "fr", 'sort_asc' => "1", 'tz' => "Asia/Kuala_Lumpur"]],
["john.doe@example.com", false, ['num' => 3, 'admin' => 0]],
];
}
public function testGetTheMetadataOfAMissingUser(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userPropertiesGet("john.doe@example.org");
}
2020-11-16 15:26:14 +00:00
public function testSetMetadata(): void {
$in = [
2020-11-16 15:26:14 +00:00
'admin' => true,
'lang' => "en-ca",
'tz' => "Atlantic/Reykjavik",
'sort_asc' => true,
];
$this->assertTrue(Arsse::$db->userPropertiesSet("john.doe@example.com", $in));
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id', 'num', 'admin'], 'arsse_user_meta' => ["owner", "key", "value"]]);
$state['arsse_users']['rows'][2][2] = 1;
$state['arsse_user_meta']['rows'][] = ["john.doe@example.com", "lang", "en-ca"];
$state['arsse_user_meta']['rows'][] = ["john.doe@example.com", "tz", "Atlantic/Reykjavik"];
$state['arsse_user_meta']['rows'][] = ["john.doe@example.com", "sort_asc", "1"];
$this->compareExpectations(static::$drv, $state);
}
2020-11-16 15:26:14 +00:00
public function testSetNoMetadata(): void {
$in = [
2020-12-22 21:13:12 +00:00
'num' => 2112,
'stylesheet' => "body {background:lightgray}",
];
$this->assertTrue(Arsse::$db->userPropertiesSet("john.doe@example.com", $in));
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id', 'num', 'admin'], 'arsse_user_meta' => ["owner", "key", "value"]]);
$this->compareExpectations(static::$drv, $state);
}
public function testSetTheMetadataOfAMissingUser(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userPropertiesSet("john.doe@example.org", ['admin' => true]);
}
public function testLookUpAUserByNumber(): void {
$this->assertSame("admin@example.net", Arsse::$db->userLookup(1));
$this->assertSame("jane.doe@example.com", Arsse::$db->userLookup(2));
$this->assertSame("john.doe@example.com", Arsse::$db->userLookup(3));
}
public function testLookUpAMissingUserByNumber(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userLookup(2112);
}
public function testRenameAUser(): void {
$this->assertTrue(Arsse::$db->userRename("john.doe@example.com", "juan.doe@example.com"));
$state = $this->primeExpectations($this->data, [
2021-01-08 20:47:19 +00:00
'arsse_users' => ['id', 'num'],
'arsse_user_meta' => ["owner", "key", "value"],
]);
$state['arsse_users']['rows'][2][0] = "juan.doe@example.com";
$state['arsse_user_meta']['rows'][6][0] = "juan.doe@example.com";
$this->compareExpectations(static::$drv, $state);
}
public function testRenameAUserToTheSameName(): void {
$this->assertFalse(Arsse::$db->userRename("john.doe@example.com", "john.doe@example.com"));
}
public function testRenameAMissingUser(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userRename("juan.doe@example.com", "john.doe@example.com");
}
public function testRenameAUserToADuplicateName(): void {
$this->assertException("alreadyExists", "User", "ExceptionConflict");
Arsse::$db->userRename("john.doe@example.com", "jane.doe@example.com");
}
2021-05-17 19:46:46 +00:00
public function testAddFirstUser(): void {
// first truncate the users table
static::$drv->exec("DELETE FROM arsse_users");
// add a user; if the max of the num column is not properly coalesced, this will result in a constraint violation
$this->assertTrue(Arsse::$db->userAdd("john.doe@example.com", ""));
}
2017-08-29 14:50:31 +00:00
}