From f9911ffc29717641b970748416363972a1e8fa4a Mon Sep 17 00:00:00 2001 From: Jonathan Martz Date: Tue, 21 Jan 2025 23:52:11 +0100 Subject: [PATCH] refactoring collection a little bit, adjust token from collection manual,write some test for the simple logic, fixing superusers authAdmin --- src/AuthTest.php | 46 ++++++++++++++ src/Client.php | 7 ++- src/Collection.php | 30 ++++++---- src/Settings.php | 13 +--- src/SuperUser.php | 8 +++ src/User.php | 18 ++++++ src/viewModels/CollectionItem.php | 14 +++++ src/viewModels/CollectionResponse.php | 12 ++++ tests/ClientTest.php | 48 +++++++++++++++ tests/CollectionGetFirstListItemTest.php | 55 +++++++++++++++++ tests/CollectionGetFullListTest.php | 28 +++++++++ tests/CollectionGetListTest.php | 76 ++++++++++++++++++++++++ tests/CollectionGetOneTest.php | 50 ++++++++++++++++ tests/CollectionTest.php | 40 ------------- 14 files changed, 383 insertions(+), 62 deletions(-) create mode 100644 src/AuthTest.php create mode 100644 src/SuperUser.php create mode 100644 src/User.php create mode 100644 src/viewModels/CollectionItem.php create mode 100644 src/viewModels/CollectionResponse.php create mode 100644 tests/ClientTest.php create mode 100644 tests/CollectionGetFirstListItemTest.php create mode 100644 tests/CollectionGetFullListTest.php create mode 100644 tests/CollectionGetListTest.php create mode 100644 tests/CollectionGetOneTest.php delete mode 100644 tests/CollectionTest.php diff --git a/src/AuthTest.php b/src/AuthTest.php new file mode 100644 index 0000000..5acaff2 --- /dev/null +++ b/src/AuthTest.php @@ -0,0 +1,46 @@ +url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev'; + $this->collection = new Collection($this->url, 'users'); + } + public function testAuthUser(): void + { + $actual = $this->collection->authAsUser('support@jonathan-martz.de', 'rockt'); + $expected = '{"data":{},"message":"Failed to authenticate.","status":400}'; + + $this->assertEquals($expected, trim($actual, PHP_EOL)); + } + + public function testAuthSuperUser(): void + { + $this->collection = new Collection($this->url, '_superusers'); + $actual = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt'); + $expected = '{"data":{},"message":"Failed to authenticate.","status":400}'; + + $this->assertEquals($expected, trim($actual, PHP_EOL)); + } + + public function testAuthSuperUser2(): void + { + $this->collection = new Collection($this->url, '_superusers'); + + $actual = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt'); + + $data = json_decode($actual,true); + $this->assertArrayHasKey('record',$data); + $this->assertArrayHasKey('token',$data); + $this->assertCount(8,$data['record']); + } +} \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index c379513..baebd31 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,7 +5,7 @@ namespace Pb; class Client { private string $url; - private string $token = ''; + public string $token = ''; public function __construct(string $url) { @@ -26,4 +26,9 @@ class Client { $this->token = $token; } + + public function getAuthToken(): string + { + return $this->token; + } } diff --git a/src/Collection.php b/src/Collection.php index d875a46..8e411a4 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -27,11 +27,13 @@ class Collection * @param string $collection * @param string $token */ - public function __construct(string $url, string $collection, string $token) + public function __construct(string $url, string $collection, string $token = '') { $this->url = $url; $this->collection = $collection; - self::$token = $token; + if (!empty($token)) { + self::$token = $token; + } } /** @@ -57,7 +59,7 @@ class Collection */ public function upload(string $recordId, string $field, string $filepath): void { - $ch = curl_init($this->url . "/api/collections/".$this->collection."/records/" . $recordId); + $ch = curl_init($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId); curl_setopt_array($ch, array( CURLOPT_CUSTOMREQUEST => 'PATCH', CURLOPT_POSTFIELDS => array( @@ -79,12 +81,13 @@ class Collection * @param string $password * @return void */ - public function authAsUser(string $email, string $password) + public function authAsUser(string $email, string $password): string { $result = $this->doRequest($this->url . "/api/collections/users/auth-with-password", 'POST', ['identity' => $email, 'password' => $password]); if (!empty($result['token'])) { self::$token = $result['token']; } + return $result; } /** @@ -94,7 +97,7 @@ class Collection */ public function getFullList(array $queryParams, int $batch = 200): array { - $queryParams = [... $queryParams, 'perPage'=> $batch]; + $queryParams = [... $queryParams, 'perPage' => $batch]; $getParams = !empty($queryParams) ? http_build_query($queryParams) : ""; $response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET'); @@ -121,7 +124,7 @@ class Collection */ public function create(array $bodyParams = [], array $queryParams = []): string { - return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', json_encode($bodyParams)); + return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', $bodyParams); } /** @@ -133,7 +136,7 @@ class Collection public function update(string $recordId, array $bodyParams = [], array $queryParams = []): void { // Todo bodyParams equals json, currently workaround - $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', json_encode($bodyParams)); + $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', $bodyParams); } /** @@ -154,6 +157,8 @@ class Collection */ public function doRequest(string $url, string $method, $bodyParams = []): string { + // TODO move doRequestIntoService ? + // TODO replace curl with HttpClient $ch = curl_init(); if (self::$token != '') { @@ -193,11 +198,16 @@ class Collection * @param string $password * @return void */ - public function authAsAdmin(string $email, string $password): void + public function authAsAdmin(string $email, string $password): string { $bodyParams['identity'] = $email; $bodyParams['password'] = $password; - $output = $this->doRequest($this->url . "/api/admins/auth-with-password", 'POST', $bodyParams); - self::$token = json_decode($output, true)['token']; + $output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams); + $token = json_decode($output, true)['token']; + if ($token) { + self::$token = $token; + } + + return $output; } } diff --git a/src/Settings.php b/src/Settings.php index 878c36a..f9304ac 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -21,7 +21,7 @@ class Settings * @param string $url * @param string $token */ - public function __construct(string $url, string $token) + public function __construct(string $url, string $token = '') { $this->url = $url; self::$token = $token; @@ -31,16 +31,10 @@ class Settings { $bodyParams['identity'] = $email; $bodyParams['password'] = $password; - $output = $this->doRequest($this->url . "/api/admins/auth-with-password", 'POST', $bodyParams); + $output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams); self::$token = json_decode($output, true)['token']; } - /** - * @param string $recordId - * @param string $url - * @param string $method - * @return bool|string - */ public function doRequest(string $url, string $method, $bodyParams = []): string { $ch = curl_init(); @@ -66,9 +60,6 @@ class Settings return $output; } - /** - * @return void - */ public function getAll():array { return json_decode($this->doRequest($this->url . '/api/settings', 'GET', []), true); diff --git a/src/SuperUser.php b/src/SuperUser.php new file mode 100644 index 0000000..6da5bfe --- /dev/null +++ b/src/SuperUser.php @@ -0,0 +1,8 @@ +url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev'; + $this->collection = new Collection($this->url, 'users'); + } + + public function test_collection(): void + { + $pb = new Client($this->url); + + $actual = $pb->collection('users'); + + $this->assertEquals($actual, $this->collection); + } + + public function test_settings(): void + { + $pb = new Client($this->url); + + $actual = $pb->settings(); + + $this->assertEquals($actual, new Settings($this->url)); + } + + public function test_setToken(){ + $token = 'test123'; + + $pb = new Client($this->url); + $pb->setAuthToken($token); + + $this->assertEquals($token, $pb->getAuthToken()); + $this->assertEquals($token, $pb->token); + $pb->token = 'test456'; + $this->assertEquals('test456', $pb->getAuthToken()); + } +} \ No newline at end of file diff --git a/tests/CollectionGetFirstListItemTest.php b/tests/CollectionGetFirstListItemTest.php new file mode 100644 index 0000000..7b77875 --- /dev/null +++ b/tests/CollectionGetFirstListItemTest.php @@ -0,0 +1,55 @@ +collection = new Collection($url, 'users'); + } + + public function test_getOne(): void + { + $id = '6588yk36406qqv1'; + $actual = $this->collection->getFirstListItem('id="'.$id.'"'); + $expected = [ + 'avatar' => '', + 'collectionId' => '_pb_users_auth_', + 'collectionName' => 'users', + 'created' => '2025-01-21 21:22:47.002Z', + 'emailVisibility' => false, + 'id' => '6588yk36406qqv1', + 'name' => 'Jonathan Martz', + 'updated' => '2025-01-21 21:22:47.002Z', + 'verified' => true + ]; + + $this->assertEquals($expected, $actual); + $this->assertCount(9, $actual); + } + + public function test_getOneWrongId(): void + { + $id = '6588yk36406qqva'; + $actual = $this->collection->getFirstListItem('id="'.$id.'"'); + $expected = [ + 'avatar' => '', + 'collectionId' => '_pb_users_auth_', + 'collectionName' => 'users', + 'created' => '2025-01-21 21:22:47.002Z', + 'emailVisibility' => false, + 'id' => '6588yk36406qqv1', + 'name' => 'Jonathan Martz', + 'updated' => '2025-01-21 21:22:47.002Z', + 'verified' => true + ]; + + $this->assertEquals($expected, $actual); + $this->assertCount(9,$actual); + } +} \ No newline at end of file diff --git a/tests/CollectionGetFullListTest.php b/tests/CollectionGetFullListTest.php new file mode 100644 index 0000000..70bc6de --- /dev/null +++ b/tests/CollectionGetFullListTest.php @@ -0,0 +1,28 @@ +url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev'; + $this->collection = new Collection($this->url, 'users'); + } + + public function test_getFullList_gettingArrayWithOneUser(): void + { + $actual = $this->collection->getFullList([],100); + + $this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.'); + $this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.'); + $this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.'); + $this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.'); + $this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.'); + $this->assertCount(1, $actual['items'], 'Expected no items in the response.'); + } +} \ No newline at end of file diff --git a/tests/CollectionGetListTest.php b/tests/CollectionGetListTest.php new file mode 100644 index 0000000..a25cbd8 --- /dev/null +++ b/tests/CollectionGetListTest.php @@ -0,0 +1,76 @@ +url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev'; + $this->collection = new Collection($this->url, 'users'); + } + + public function test_getList_gettingArrayWithOneItem(): void + { + $actual = $this->collection->getList(1, 10); + + $this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.'); + $this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.'); + $this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.'); + $this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.'); + $this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.'); + $this->assertCount(1, $actual['items'], 'Expected no items in the response.'); + } + + public function test_getListWithContainsMartz(): void + { + $actual = $this->collection->getList(1, 10, ['filter'=>'name~"%Martz%"']); + + $this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.'); + $this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.'); + $this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.'); + $this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.'); + $this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.'); + $this->assertCount(1, $actual['items'], 'Expected no items in the response.'); + } + + public function test_getListStartsWithMartz(): void + { + $actual = $this->collection->getList(1, 10, ['filter'=>'name~"Martz%"']); + + $this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.'); + $this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.'); + $this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.'); + $this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.'); + $this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.'); + $this->assertCount(0, $actual['items'], 'Expected no items in the response.'); + } + + public function test_getListStartWithJonathan(): void + { + $actual = $this->collection->getList(1, 10, ['filter'=>'name~"Jonathan%"']); + + $this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.'); + $this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.'); + $this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.'); + $this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.'); + $this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.'); + $this->assertCount(1, $actual['items'], 'Expected no items in the response.'); + } + + public function test_getListCheckingDifferentName(): void + { + $actual = $this->collection->getList(1, 10, ['filter'=>'name~"%Sibylle%"']); + + $this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.'); + $this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.'); + $this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.'); + $this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.'); + $this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.'); + $this->assertCount(0, $actual['items'], 'Expected no items in the response.'); + } +} \ No newline at end of file diff --git a/tests/CollectionGetOneTest.php b/tests/CollectionGetOneTest.php new file mode 100644 index 0000000..f92d2ed --- /dev/null +++ b/tests/CollectionGetOneTest.php @@ -0,0 +1,50 @@ +url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev'; + $this->collection = new Collection($this->url, 'users'); + } + + public function test_getOne(): void + { + $id = '6588yk36406qqv1'; + $actual = $this->collection->getOne($id); + $expected = [ + 'avatar' => '', + 'collectionId' => '_pb_users_auth_', + 'collectionName' => 'users', + 'created' => '2025-01-21 21:22:47.002Z', + 'emailVisibility' => false, + 'id' => '6588yk36406qqv1', + 'name' => 'Jonathan Martz', + 'updated' => '2025-01-21 21:22:47.002Z', + 'verified' => true + ]; + + $this->assertEquals($expected, $actual); + $this->assertCount(9, $actual); + } + + public function test_getOneWrongId(): void + { + $id = '6588yk36406qqva'; + $actual = $this->collection->getOne($id); + $expected = [ + 'data' => [], + 'message' => "The requested resource wasn't found.", + 'status' => 404, + ]; + + $this->assertEquals($expected, $actual); + $this->assertCount(3,$actual); + } +} \ No newline at end of file diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php deleted file mode 100644 index 1e3939e..0000000 --- a/tests/CollectionTest.php +++ /dev/null @@ -1,40 +0,0 @@ -url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev'; - } - - public function test_givenUrlAndCollectionNameUsers_thenCheckEmptyResult(): void - { - $collection = new \Pb\Collection($this->url, 'users'); - $actual = $collection->getList(1, 10); - - $this->assertTrue(array_key_exists('items', $actual), 'Key "items" does not exist in the response.'); - $this->assertTrue(array_key_exists('page', $actual), 'Key "page" does not exist in the response.'); - $this->assertTrue(array_key_exists('perPage', $actual), 'Key "perPage" does not exist in the response.'); - $this->assertTrue(array_key_exists('totalItems', $actual), 'Key "totalItems" does not exist in the response.'); - $this->assertTrue(array_key_exists('totalPages', $actual), 'Key "totalPages" does not exist in the response.'); - $this->assertCount(0, $actual['items'], 'Expected no items in the response.'); - } - - public function test_givenFilterByName_thenCheckEmptyResult(): void - { - $collection = new \Pb\Collection($this->url, 'users', [ - 'filter' => 'name ~ "%Jonathan%"' - ]); - $actual = $collection->getList(1, 10); - - $this->assertTrue(array_key_exists('items', $actual), 'Key "items" does not exist in the response.'); - $this->assertTrue(array_key_exists('page', $actual), 'Key "page" does not exist in the response.'); - $this->assertTrue(array_key_exists('perPage', $actual), 'Key "perPage" does not exist in the response.'); - $this->assertTrue(array_key_exists('totalItems', $actual), 'Key "totalItems" does not exist in the response.'); - $this->assertTrue(array_key_exists('totalPages', $actual), 'Key "totalPages" does not exist in the response.'); - $this->assertCount(1, $actual['items'], 'Expected no items in the response.'); - } -} \ No newline at end of file