fix apitest

This commit is contained in:
Andrew Dolgov 2023-10-29 09:42:53 +03:00
parent adf3985afa
commit 7cd2c5cac8
No known key found for this signature in database
GPG Key ID: 1A56B4FA25D4AF2A
1 changed files with 22 additions and 13 deletions

View File

@ -7,6 +7,9 @@ final class ApiTest extends TestCase {
/** @var string */
private $api_url;
/** @var string */
private $sid;
function __construct() {
$this->api_url = getenv('API_URL');
@ -25,34 +28,40 @@ final class ApiTest extends TestCase {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$response = curl_exec($ch);
$resp = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status != 200) {
print("error: failed with HTTP status: $status");
return null;
}
curl_close($ch);
return json_decode($response, true);
return json_decode($resp, true);
}
/** @param array<mixed> $response */
public function common_assertions(array $response) : void {
$this->assertArrayHasKey("content", $response);
$this->assertArrayNotHasKey("error", $response['content'], $response['content']['error']);
/** @param array<mixed> $resp */
public function common_assertions(array $resp) : void {
$this->assertArrayHasKey("content", $resp);
$this->assertArrayNotHasKey("error", $resp['content'], $resp['content']['error'] ?? '');
}
public function test_login() : void {
$response = $this->api(["op" => "login", "user" => "test", "password" => "test"]);
$resp = $this->api(["op" => "login", "user" => "test", "password" => "test"]);
$this->common_assertions($resp);
$this->common_assertions($response);
$this->assertArrayHasKey("session_id", $resp['content']);
$this->sid = $resp['content']['session_id'];
}
public function test_getVersion() : void {
$response = $this->api(["op" => "getVersion"]);
$this->common_assertions($response);
$this->test_login();
$resp = $this->api(["op" => "getVersion", "sid" => $this->sid]);
$this->common_assertions($resp);
$this->assertArrayHasKey("version", $resp['content']);
}
}