appwrite/tests/e2e/Services/Projects/ProjectsDevKeys.php

266 lines
9.7 KiB
PHP
Raw Normal View History

2024-07-29 10:12:57 +00:00
<?php
namespace Tests\E2E\Services\Projects;
use Tests\E2E\Client;
use Utopia\Database\DateTime;
2024-11-22 04:21:03 +00:00
trait ProjectsDevKeys
2024-07-29 10:12:57 +00:00
{
/**
* @depends testCreateProject
2024-11-22 04:21:03 +00:00
* @group devKeys
2024-07-29 10:12:57 +00:00
*/
2024-11-22 04:21:03 +00:00
public function testCreateProjectDevKey($data): array
2024-07-29 10:12:57 +00:00
{
$id = $data['projectId'] ?? '';
$response = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2024-07-29 10:18:06 +00:00
'name' => 'Key Test',
'expire' => DateTime::addSeconds(new \DateTime(), 36000)
2024-07-29 10:12:57 +00:00
]);
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertEquals('Key Test', $response['body']['name']);
$this->assertNotEmpty($response['body']['secret']);
$this->assertArrayHasKey('sdks', $response['body']);
$this->assertEmpty($response['body']['sdks']);
$this->assertArrayHasKey('accessedAt', $response['body']);
$this->assertEmpty($response['body']['accessedAt']);
2024-07-29 10:18:06 +00:00
/** TEST expiry date is required */
$res = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Key Test'
]);
$this->assertEquals(400, $res['headers']['status-code']);
2024-07-29 10:12:57 +00:00
$data = array_merge($data, [
'keyId' => $response['body']['$id'],
'secret' => $response['body']['secret']
]);
return $data;
}
/**
2024-11-22 04:21:03 +00:00
* @depends testCreateProjectDevKey
* @group devKeys
2024-07-29 10:12:57 +00:00
*/
2024-11-22 04:21:03 +00:00
public function testListProjectDevKey($data): array
2024-07-29 10:12:57 +00:00
{
$id = $data['projectId'] ?? '';
$response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(1, $response['body']['total']);
return $data;
}
/**
2024-11-22 04:21:03 +00:00
* @depends testCreateProjectDevKey
* @group devKeys
2024-07-29 10:12:57 +00:00
*/
2024-11-22 04:21:03 +00:00
public function testGetProjectDevKey($data): array
2024-07-29 10:12:57 +00:00
{
$id = $data['projectId'] ?? '';
$keyId = $data['keyId'] ?? '';
$response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($keyId, $response['body']['$id']);
$this->assertEquals('Key Test', $response['body']['name']);
$this->assertNotEmpty($response['body']['secret']);
$this->assertArrayHasKey('sdks', $response['body']);
$this->assertEmpty($response['body']['sdks']);
$this->assertArrayHasKey('accessedAt', $response['body']);
$this->assertEmpty($response['body']['accessedAt']);
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/error', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(404, $response['headers']['status-code']);
return $data;
}
/**
* @depends testCreateProject
2024-11-22 04:21:03 +00:00
* @group devKeys
2024-07-29 10:12:57 +00:00
*/
2024-11-22 04:21:03 +00:00
public function testNoRateLimitWithDevKey($data): void
2024-07-29 10:12:57 +00:00
{
$id = $data['projectId'] ?? '';
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Key Test',
'expire' => DateTime::addSeconds(new \DateTime(), 3600),
]);
2024-11-22 04:21:03 +00:00
$devKey = $response['body']['secret'];
2024-08-12 08:39:03 +00:00
//
2024-11-17 07:01:20 +00:00
for ($i = 0; $i < 11; $i++) {
2024-08-12 08:39:03 +00:00
$res = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $id,
], [
'email' => 'user@appwrite.io',
'password' => 'password'
]);
}
$res = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [
2024-07-29 10:12:57 +00:00
'content-type' => 'application/json',
2024-08-12 08:39:03 +00:00
'x-appwrite-project' => $id,
], [
'email' => 'user@appwrite.io',
'password' => 'password'
2024-07-29 10:12:57 +00:00
]);
2024-08-12 08:39:03 +00:00
$this->assertEquals('429', $res['headers']['status-code']);
2024-07-29 10:12:57 +00:00
2024-08-12 08:39:03 +00:00
$res = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [
2024-07-29 10:12:57 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $id,
2024-11-22 04:21:03 +00:00
'x-appwrite-development-key' => $devKey
2024-08-12 08:39:03 +00:00
], [
'email' => 'user@appwrite.io',
'password' => 'password'
]);
$this->assertEquals(401, $res['headers']['status-code']);
2024-07-29 10:12:57 +00:00
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Key Test',
'expire' => DateTime::addSeconds(new \DateTime(), -3600),
]);
2024-08-12 08:39:03 +00:00
$res = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [
2024-07-29 10:12:57 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $id,
2024-08-12 08:39:03 +00:00
'x-appwrite-development-key' => $response['body']['secret']
], [
'email' => 'user@appwrite.io',
'password' => 'password'
]);
$this->assertEquals('429', $res['headers']['status-code']);
2024-07-29 10:12:57 +00:00
}
/**
2024-11-22 04:21:03 +00:00
* @depends testCreateProjectDevKey
* @group devKeys
2024-07-29 10:12:57 +00:00
*/
2024-11-22 04:21:03 +00:00
public function testUpdateProjectDevKey($data): array
2024-07-29 10:12:57 +00:00
{
$id = $data['projectId'] ?? '';
$keyId = $data['keyId'] ?? '';
$response = $this->client->call(Client::METHOD_PUT, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Key Test Update',
'expire' => DateTime::addSeconds(new \DateTime(), 360),
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($keyId, $response['body']['$id']);
$this->assertEquals('Key Test Update', $response['body']['name']);
$this->assertArrayHasKey('sdks', $response['body']);
$this->assertEmpty($response['body']['sdks']);
$this->assertArrayHasKey('accessedAt', $response['body']);
$this->assertEmpty($response['body']['accessedAt']);
$response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($keyId, $response['body']['$id']);
$this->assertEquals('Key Test Update', $response['body']['name']);
$this->assertArrayHasKey('sdks', $response['body']);
$this->assertEmpty($response['body']['sdks']);
$this->assertArrayHasKey('accessedAt', $response['body']);
$this->assertEmpty($response['body']['accessedAt']);
return $data;
}
/**
2024-11-22 04:21:03 +00:00
* @depends testCreateProjectDevKey
* @group devKeys
2024-07-29 10:12:57 +00:00
*/
2024-11-22 04:21:03 +00:00
public function testDeleteProjectDevKey($data): array
2024-07-29 10:12:57 +00:00
{
$id = $data['projectId'] ?? '';
$keyId = $data['keyId'] ?? '';
$response = $this->client->call(Client::METHOD_DELETE, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(204, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
$response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(404, $response['headers']['status-code']);
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_DELETE, '/projects/' . $id . '/development-keys/error', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);
$this->assertEquals(404, $response['headers']['status-code']);
return $data;
}
}