From 80838cc46595ef93e1a7b25635c1e6532ab70a85 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Tue, 8 Sep 2020 23:09:09 +0300 Subject: [PATCH] Added tests --- tests/e2e/Scopes/Scope.php | 11 ++ tests/e2e/Services/Workers/WebhooksTest.php | 152 ++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 tests/e2e/Services/Workers/WebhooksTest.php diff --git a/tests/e2e/Scopes/Scope.php b/tests/e2e/Scopes/Scope.php index 61920877b7..07e9ec67e5 100644 --- a/tests/e2e/Scopes/Scope.php +++ b/tests/e2e/Scopes/Scope.php @@ -34,6 +34,7 @@ abstract class Scope extends TestCase protected function getLastEmail():array { sleep(10); + $emails = json_decode(file_get_contents('http://maildev/email'), true); if($emails && is_array($emails)) { @@ -43,6 +44,16 @@ abstract class Scope extends TestCase return []; } + protected function getLastRequest():array + { + sleep(10); + + $resquest = json_decode(file_get_contents('http://request-catcher:5000/__last_request__'), true); + $resquest['data'] = json_decode($resquest['data'], true); + + return $resquest; + } + /** * @return array */ diff --git a/tests/e2e/Services/Workers/WebhooksTest.php b/tests/e2e/Services/Workers/WebhooksTest.php new file mode 100644 index 0000000000..67c51785cd --- /dev/null +++ b/tests/e2e/Services/Workers/WebhooksTest.php @@ -0,0 +1,152 @@ +client->call(Client::METHOD_POST, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Project Test', + ]); + + $this->assertEquals(201, $team['headers']['status-code']); + $this->assertEquals('Project Test', $team['body']['name']); + $this->assertNotEmpty($team['body']['$id']); + + $response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Project Test', + 'teamId' => $team['body']['$id'], + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertEquals('Project Test', $response['body']['name']); + $this->assertEquals($team['body']['$id'], $response['body']['teamId']); + $this->assertArrayHasKey('platforms', $response['body']); + $this->assertArrayHasKey('webhooks', $response['body']); + $this->assertArrayHasKey('keys', $response['body']); + $this->assertArrayHasKey('tasks', $response['body']); + + $projectId = $response['body']['$id']; + + /** + * Test for FAILURE + */ + $response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => '', + 'teamId' => $team['body']['$id'], + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Project Test', + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + + return ['projectId' => $projectId]; + } + + /** + * @depends testCreateProject + */ + public function testCreateWebhook($data): array + { + $id = (isset($data['projectId'])) ? $data['projectId'] : ''; + + $response = $this->client->call(Client::METHOD_POST, '/projects/'.$id.'/webhooks', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Webhook Worker Test', + 'events' => ['account.create', 'account.update.email'], + 'url' => 'http://request-catcher:5000/webhook', + 'security' => true, + 'httpUser' => 'username', + 'httpPass' => 'password', + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertContains('account.create', $response['body']['events']); + $this->assertContains('account.update.email', $response['body']['events']); + $this->assertCount(2, $response['body']['events']); + $this->assertEquals('http://request-catcher:5000/webhook', $response['body']['url']); + $this->assertIsBool($response['body']['security']); + $this->assertEquals(true, $response['body']['security']); + $this->assertEquals('username', $response['body']['httpUser']); + + $data = array_merge($data, ['webhookId' => $response['body']['$id']]); + + /** + * Test for FAILURE + */ + + return $data; + } + + /** + * @depends testCreateWebhook + */ + public function testCreateAccount($data) + { + $projectId = (isset($data['projectId'])) ? $data['projectId'] : ''; + $email = uniqid().'webhook.user@localhost.test'; + $password = 'password'; + $name = 'User Name'; + + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_POST, '/account', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ]), [ + 'email' => $email, + 'password' => $password, + 'name' => $name, + ]); + + $this->assertEquals($response['headers']['status-code'], 201); + + $webhook = $this->getLastRequest(); + + $this->assertNotEmpty($webhook['data']); + $this->assertNotEmpty($webhook['data']['$id']); + $this->assertIsNumeric($webhook['data']['status']); + $this->assertIsNumeric($webhook['data']['registration']); + $this->assertEquals($webhook['data']['email'], $email); + $this->assertEquals($webhook['data']['name'], $name); + $this->assertIsBool($webhook['data']['emailVerification']); + $this->assertIsArray($webhook['data']['prefs']); + $this->assertIsArray($webhook['data']['roles']); + } +} \ No newline at end of file