From 14dda66c031bdddf26e65f7f3aa2d9061adced09 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 3 Jul 2022 13:10:26 +0000 Subject: [PATCH] more work on tests --- tests/e2e/General/UsageTest.php | 113 +++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 10 deletions(-) diff --git a/tests/e2e/General/UsageTest.php b/tests/e2e/General/UsageTest.php index 652c9549ef..c706ee79bc 100644 --- a/tests/e2e/General/UsageTest.php +++ b/tests/e2e/General/UsageTest.php @@ -14,9 +14,6 @@ class UsageTest extends Scope protected array $headers = []; protected string $projectId; - protected int $usersCount = 0; - protected int $requestsCount = 0; - protected function setUp(): void { parent::setUp(); @@ -29,7 +26,8 @@ class UsageTest extends Scope public function testUsersStats(): void { - + $usersCount = 0; + $requestsCount = 0; for ($i = 0; $i < 10; $i++) { $email = uniqid() . 'user@usage.test'; $password = 'password'; @@ -42,6 +40,8 @@ class UsageTest extends Scope ]); $this->assertEquals($email, $res['body']['email']); $this->assertNotEmpty($res['body']['$id']); + $usersCount++; + $requestsCount++; if ($i < 5) { $userId = $res['body']['$id']; @@ -49,12 +49,10 @@ class UsageTest extends Scope $this->assertEquals($userId, $res['body']['$id']); $res = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, $this->headers); $this->assertEmpty($res['body']); - $this->requestsCount += 2; - $this->usersCount--; + $requestsCount += 2; + $usersCount--; } - $this->usersCount++; - $this->requestsCount++; } @@ -73,8 +71,103 @@ class UsageTest extends Scope $this->assertEquals(8, count($res)); $this->assertEquals(30, count($res['requests'])); $this->assertEquals(30, count($res['users'])); - $this->assertEquals($this->usersCount, $res['users'][array_key_last($res['users'])]['value']); - $this->assertEquals($this->requestsCount, $res['requests'][array_key_last($res['requests'])]['value']); + $this->assertEquals($usersCount, $res['users'][array_key_last($res['users'])]['value']); + $this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']); + + $res = $this->client->call(Client::METHOD_GET, '/users/usage?range=30d', array_merge($headers, [ + 'x-appwrite-project' => $this->projectId, + 'x-appwrite-mode' => 'admin' + ])); + $res = $res['body']; + $this->assertEquals(10, $res['usersCreate'][array_key_last($res['usersCreate'])]['value']); + $this->assertEquals(5, $res['usersRead'][array_key_last($res['usersRead'])]['value']); + $this->assertEquals(5, $res['usersDelete'][array_key_last($res['usersDelete'])]['value']); + } + + public function testStorageStats() { + $bucketId = ''; + $bucketsCount = 0; + $requestsCount = 0; + $storageTotal = 0; + $bucketsCreate = 0; + $bucketsDelete = 0; + $bucketsRead = 0; + $filesCount = 0; + + + for ($i = 0; $i < 10; $i++) { + $name = uniqid() . ' bucket'; + $res = $this->client->call(Client::METHOD_POST, '/storage/buckets', $this->headers, [ + 'bucketId' => 'unique()', + 'name' => $name, + 'permission' => 'bucket' + ]); + $this->assertEquals($name, $res['body']['name']); + $this->assertNotEmpty($res['body']['$id']); + $bucketId = $res['body']['$id']; + array_push($bucketIds, $bucketId); + + $bucketsCreate++; + $bucketsCount++; + $requestsCount++; + + if ($i < 5) { + $res = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId, $this->headers); + $this->assertEquals($bucketId, $res['body']['$id']); + $bucketsRead++; + + $res = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId, $this->headers); + $this->assertEmpty($res['body']); + $bucketsDelete++; + array_pop($bucketIds); + + $requestsCount += 2; + $bucketsCount--; + } + + } + + // upload some files + for ($i = 0; $i < 10; $i++) { + $res = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', $this->headers, [ + 'fileId' => 'unique()', + 'file' => new CURLFile(realpath(__DIR__ . '/../../resources/logo.png', '', 'logo.png')), + ]); + + $this->assertNotEmpty($res['body']['$id']); + + $storageTotal += $res['body']['size']; + $filesCount++; + $requestsCount++; + + + if ($i < 5) { + $res = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId, $this->headers); + $this->assertEquals($bucketId, $res['body']['$id']); + $res = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId, $this->headers); + $this->assertEmpty($res['body']); + array_pop($bucketIds); + $requestsCount += 2; + $filesCount--; + } + + } + + sleep(75); + + // console request + $headers = [ + 'origin' => 'http://localhost', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]; + + $res = $this->client->call(Client::METHOD_GET, '/projects/' . $this->projectId . '/usage?range=30d', $headers); + $res = $res['body']; + + $this->assertEquals(8, count($res)); + $this->assertEquals(30, count($res['requests'])); + $this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']); $res = $this->client->call(Client::METHOD_GET, '/users/usage?range=30d', array_merge($headers, [ 'x-appwrite-project' => $this->projectId,