Merge pull request #10656 from appwrite/feat-replace-sleep-in-webhooks-service

Replace sleep in webhook tests with assertEventually
This commit is contained in:
Luke B. Silver 2025-10-16 10:45:32 +01:00 committed by GitHub
commit 5ce9b8eaeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 125 additions and 123 deletions

View file

@ -147,11 +147,10 @@ trait WebhooksBase
$this->assertEquals($extra['body']['key'], 'extra'); $this->assertEquals($extra['body']['key'], 'extra');
// wait for database worker to kick in // wait for database worker to kick in
sleep(10); $this->assertEventually(function () use ($databaseId, $actorsId) {
$webhook = $this->getLastRequest(); $webhook = $this->getLastRequest();
$this->assertNotEmpty($webhook);
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']); $signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
$this->assertEquals($webhook['method'], 'POST'); $this->assertEquals($webhook['method'], 'POST');
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json'); $this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io'); $this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
@ -166,6 +165,7 @@ trait WebhooksBase
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
$this->assertNotEmpty($webhook['data']['key']); $this->assertNotEmpty($webhook['data']['key']);
$this->assertEquals($webhook['data']['key'], 'extra'); $this->assertEquals($webhook['data']['key'], 'extra');
}, 15000, 500);
$removed = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $data['actorsId'] . '/attributes/' . $extra['body']['key'], array_merge([ $removed = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $data['actorsId'] . '/attributes/' . $extra['body']['key'], array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
@ -498,11 +498,10 @@ trait WebhooksBase
$this->assertEquals($extra['body']['key'], 'extra'); $this->assertEquals($extra['body']['key'], 'extra');
// wait for database worker to kick in // wait for database worker to kick in
sleep(10); $this->assertEventually(function () use ($databaseId, $actorsId) {
$webhook = $this->getLastRequest(); $webhook = $this->getLastRequest();
$this->assertNotEmpty($webhook);
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']); $signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
$this->assertEquals($webhook['method'], 'POST'); $this->assertEquals($webhook['method'], 'POST');
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json'); $this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io'); $this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
@ -517,6 +516,7 @@ trait WebhooksBase
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
$this->assertNotEmpty($webhook['data']['key']); $this->assertNotEmpty($webhook['data']['key']);
$this->assertEquals($webhook['data']['key'], 'extra'); $this->assertEquals($webhook['data']['key'], 'extra');
}, 15000, 500);
$removed = $this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/' . $data['actorsId'] . '/columns/' . $extra['body']['key'], array_merge([ $removed = $this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/' . $data['actorsId'] . '/columns/' . $extra['body']['key'], array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
@ -1487,8 +1487,7 @@ trait WebhooksBase
$this->assertNotEmpty($newCollection['body']['$id']); $this->assertNotEmpty($newCollection['body']['$id']);
} }
sleep(10); $this->assertEventually(function () use ($projectId, $webhookId) {
$webhook = $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/webhooks/' . $webhookId, array_merge([ $webhook = $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/webhooks/' . $webhookId, array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
'content-type' => 'application/json', 'content-type' => 'application/json',
@ -1499,5 +1498,6 @@ trait WebhooksBase
// assert that the webhook is now disabled after 10 consecutive failures // assert that the webhook is now disabled after 10 consecutive failures
$this->assertEquals($webhook['body']['enabled'], false); $this->assertEquals($webhook['body']['enabled'], false);
$this->assertEquals($webhook['body']['attempts'], 10); $this->assertEquals($webhook['body']['attempts'], 10);
}, 15000, 500);
} }
} }

View file

@ -2,6 +2,7 @@
namespace Tests\E2E\Services\Webhooks; namespace Tests\E2E\Services\Webhooks;
use Appwrite\Tests\Async;
use CURLFile; use CURLFile;
use Tests\E2E\Client; use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\ProjectCustom;
@ -15,6 +16,7 @@ use Utopia\Database\Validator\Datetime as DatetimeValidator;
class WebhooksCustomServerTest extends Scope class WebhooksCustomServerTest extends Scope
{ {
use Async;
use WebhooksBase; use WebhooksBase;
use ProjectCustom; use ProjectCustom;
use SideServer; use SideServer;
@ -89,8 +91,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals('fullname', $index['body']['key']); $this->assertEquals('fullname', $index['body']['key']);
// wait for database worker to create index // wait for database worker to create index
sleep(5); $this->assertEventually(function () use ($databaseId, $actorsId) {
$webhook = $this->getLastRequest(); $webhook = $this->getLastRequest();
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']); $signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
@ -107,6 +108,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
$this->assertTrue(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? '')); $this->assertTrue(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''));
}, 10000, 500);
// Remove index // Remove index
$this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $data['actorsId'] . '/indexes/' . $index['body']['key'], array_merge([ $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $data['actorsId'] . '/indexes/' . $index['body']['key'], array_merge([
@ -275,8 +277,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals('fullname', $index['body']['key']); $this->assertEquals('fullname', $index['body']['key']);
// wait for database worker to create index // wait for database worker to create index
sleep(5); $this->assertEventually(function () use ($databaseId, $actorsId) {
$webhook = $this->getLastRequest(); $webhook = $this->getLastRequest();
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']); $signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
@ -293,6 +294,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
$this->assertTrue(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? '')); $this->assertTrue(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''));
}, 10000, 500);
// Remove index // Remove index
$this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/' . $data['actorsId'] . '/indexes/' . $index['body']['key'], array_merge([ $this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/' . $data['actorsId'] . '/indexes/' . $index['body']['key'], array_merge([
@ -735,8 +737,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertNotEmpty($response['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
// Wait for deployment to be built. // Wait for deployment to be built.
sleep(5); $this->assertEventually(function () use ($deploymentId, $id) {
$webhook = $this->getLastRequest(); $webhook = $this->getLastRequest();
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']); $signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
@ -756,6 +757,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
}, 10000, 500);
/** /**
* Test for FAILURE * Test for FAILURE
@ -806,8 +808,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
// wait for timeout function to complete // wait for timeout function to complete
sleep(20); $this->assertEventually(function () use ($executionId, $id) {
$webhook = $this->getLastRequest(); $webhook = $this->getLastRequest();
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']); $signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
@ -827,6 +828,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']); $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
}, 30000, 1000);
/** /**
* Test for FAILURE * Test for FAILURE