From 4baf5fa2f95fb79a03db58946f73947e02303c4e Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 13 Nov 2020 19:30:23 -0500 Subject: [PATCH] Tests for new user functionality in Database --- lib/Database.php | 10 ++++-- tests/cases/Database/SeriesUser.php | 56 +++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index eff40a5..a531f23 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -300,10 +300,11 @@ class Database { } 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]); } - $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['sort_asc'], "bool"); return $out; @@ -320,7 +321,10 @@ class Database { 'sort_asc' => "strict bool", ]; [$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(); } diff --git a/tests/cases/Database/SeriesUser.php b/tests/cases/Database/SeriesUser.php index 3211cc9..10bd258 100644 --- a/tests/cases/Database/SeriesUser.php +++ b/tests/cases/Database/SeriesUser.php @@ -16,11 +16,15 @@ trait SeriesUser { 'id' => 'str', 'password' => 'str', 'num' => 'int', + 'admin' => 'bool', + 'lang' => 'str', + 'tz' => 'str', + 'sort_asc' => 'bool', ], 'rows' => [ - ["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW',1], // password is hash of "secret" - ["jane.doe@example.com", "",2], - ["john.doe@example.com", "",3], + ["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW',1, 1, "en", "America/Toronto", 0], // password is hash of "secret" + ["jane.doe@example.com", "",2, 0, "fr", "Asia/Kuala_Lumpur", 1], + ["john.doe@example.com", "",3, 0, null, "Etc/UTC", 0], ], ], ]; @@ -100,4 +104,50 @@ trait SeriesUser { $this->assertException("doesNotExist", "User"); 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]); + } }