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

Tests for new user functionality in Database

This commit is contained in:
J. King 2020-11-13 19:30:23 -05:00
parent dde9d7a28a
commit 4baf5fa2f9
2 changed files with 60 additions and 6 deletions

View file

@ -300,10 +300,11 @@ class Database {
} }
public function userPropertiesGet(string $user): array { public function userPropertiesGet(string $user): array {
if (!$this->userExists($user)) { $out = $this->db->prepare("SELECT num, admin, lang, tz, sort_asc from arsse_users where id = ?", "str")->run($user)->getRow();
if (!$out) {
throw new User\Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]); throw new User\Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
} }
$out = $this->db->prepare("SELECT num, admin, lang, tz, sort_asc from arsse_users where id = ?", "str")->run($user)->getRow(); settype($out['num'], "int");
settype($out['admin'], "bool"); settype($out['admin'], "bool");
settype($out['sort_asc'], "bool"); settype($out['sort_asc'], "bool");
return $out; return $out;
@ -320,7 +321,10 @@ class Database {
'sort_asc' => "strict bool", 'sort_asc' => "strict bool",
]; ];
[$setClause, $setTypes, $setValues] = $this->generateSet($data, $allowed); [$setClause, $setTypes, $setValues] = $this->generateSet($data, $allowed);
return (bool) $this->db->prepare("UPDATE arsse_users set $setClause where user = ?", $setTypes, "str")->run($setValues, $user)->changes(); if (!$setClause) {
return false;
}
return (bool) $this->db->prepare("UPDATE arsse_users set $setClause where id = ?", $setTypes, "str")->run($setValues, $user)->changes();
} }

View file

@ -16,11 +16,15 @@ trait SeriesUser {
'id' => 'str', 'id' => 'str',
'password' => 'str', 'password' => 'str',
'num' => 'int', 'num' => 'int',
'admin' => 'bool',
'lang' => 'str',
'tz' => 'str',
'sort_asc' => 'bool',
], ],
'rows' => [ 'rows' => [
["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW',1], // password is hash of "secret" ["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW',1, 1, "en", "America/Toronto", 0], // password is hash of "secret"
["jane.doe@example.com", "",2], ["jane.doe@example.com", "",2, 0, "fr", "Asia/Kuala_Lumpur", 1],
["john.doe@example.com", "",3], ["john.doe@example.com", "",3, 0, null, "Etc/UTC", 0],
], ],
], ],
]; ];
@ -100,4 +104,50 @@ trait SeriesUser {
$this->assertException("doesNotExist", "User"); $this->assertException("doesNotExist", "User");
Arsse::$db->userPasswordSet("john.doe@example.org", "secret"); Arsse::$db->userPasswordSet("john.doe@example.org", "secret");
} }
/** @dataProvider provideMetaData */
public function testGetMetadata(string $user, array $exp): void {
$this->assertSame($exp, Arsse::$db->userPropertiesGet($user));
}
public function provideMetadata() {
return [
["admin@example.net", ['num' => 1, 'admin' => true, 'lang' => "en", 'tz' => "America/Toronto", 'sort_asc' => false]],
["jane.doe@example.com", ['num' => 2, 'admin' => false, 'lang' => "fr", 'tz' => "Asia/Kuala_Lumpur", 'sort_asc' => true]],
["john.doe@example.com", ['num' => 3, 'admin' => false, 'lang' => null, 'tz' => "Etc/UTC", 'sort_asc' => false]],
];
}
public function testGetTheMetadataOfAMissingUser(): void {
$this->assertException("doesNotExist", "User");
Arsse::$db->userPropertiesGet("john.doe@example.org");
}
public function testSetMetadata(): void {
$in = [
'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', 'lang', 'tz', 'sort_asc']]);
$state['arsse_users']['rows'][2] = ["john.doe@example.com", 3, 1, "en-ca", "Atlantic/Reykjavik", 1];
$this->compareExpectations(static::$drv, $state);
}
public function testSetNoMetadata(): void {
$in = [
'num' => 2112,
'blah' => "bloo"
];
$this->assertFalse(Arsse::$db->userPropertiesSet("john.doe@example.com", $in));
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id', 'num', 'admin', 'lang', 'tz', 'sort_asc']]);
$this->compareExpectations(static::$drv, $state);
}
public function testSetTheMetadataOfAMissingUser(): void {
$this->assertException("doesNotExist", "User");
Arsse::$db->userPropertiesSet("john.doe@example.org", ['admin' => true]);
}
} }