refactoring collection a little bit, adjust token from collection manual,write some test for the simple logic, fixing superusers authAdmin

This commit is contained in:
Jonathan Martz
2025-01-21 23:52:11 +01:00
parent 28df3f28e7
commit f9911ffc29
14 changed files with 383 additions and 62 deletions

46
src/AuthTest.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
namespace Pb;
use PHPUnit\Framework\TestCase;
final class AuthTest extends TestCase
{
private Collection $collection;
private ?string $url;
protected function setUp(): void
{
$this->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']);
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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);

8
src/SuperUser.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
namespace Pb;
class SuperUser
{
}

18
src/User.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace Pb;
use DateTime;
class User
{
public string $avatar;
public string $collectionName;
public string $collectionId;
public bool $emailVisibility;
public DateTime $created;
public string $id;
public string $name;
public DateTime $updated;
public bool $verified;
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Pb\viewModels;
use DateTime;
class CollectionItem
{
public string $collectionName;
public string $collectionId;
public DateTime $created;
public string $id;
public DateTime $updated;
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Pb\viewModels;
class CollectionResponse
{
public array $items;
public int $page;
public int $perPage;
public int $totalItems;
public int $totalPages;
}