diff --git a/src/Appwrite/Utopia/Response/Model/MigrationReport.php b/src/Appwrite/Utopia/Response/Model/MigrationReport.php index 9de39d75bf..f767a28d9a 100644 --- a/src/Appwrite/Utopia/Response/Model/MigrationReport.php +++ b/src/Appwrite/Utopia/Response/Model/MigrationReport.php @@ -71,6 +71,12 @@ class MigrationReport extends Model 'default' => 0, 'example' => 5, ]) + ->addRule(Resource::TYPE_WEBHOOK, [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Number of webhooks to be migrated.', + 'default' => 0, + 'example' => 5, + ]) ->addRule(Resource::TYPE_SITE, [ 'type' => self::TYPE_INTEGER, 'description' => 'Number of sites to be migrated.', diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 0cafd64975..d4a84db811 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -226,10 +226,19 @@ trait MigrationsBase $this->assertEquals('Appwrite', $response['source']); $this->assertEquals('Appwrite', $response['destination']); + // ProjectCustom provisions an api-key and a webhook on each project, so both show up + // here. Other resources stay empty and getStatusCounters strips them. + // Webhook is name-deduped on the destination — source ProjectCustom's 'Webhook Test' + // collides with destination ProjectCustom's, so it lands in 'skip', not 'success'. $this->assertArrayHasKey(Resource::TYPE_API_KEY, $response['statusCounters']); - $counts = $response['statusCounters'][Resource::TYPE_API_KEY]; - $this->assertEquals(0, $counts['error']); - $this->assertGreaterThan(0, $counts['success']); + $this->assertArrayHasKey(Resource::TYPE_WEBHOOK, $response['statusCounters']); + + $apiKeyCounts = $response['statusCounters'][Resource::TYPE_API_KEY]; + $this->assertEquals(0, $apiKeyCounts['error']); + $this->assertGreaterThan(0, $apiKeyCounts['success']); + + $webhookCounts = $response['statusCounters'][Resource::TYPE_WEBHOOK]; + $this->assertEquals(0, $webhookCounts['error']); } /** @@ -2628,6 +2637,87 @@ trait MigrationsBase $this->client->call(Client::METHOD_DELETE, '/project/keys/' . $apiKey['$id'], $sourceHeaders); } + public function testAppwriteMigrationWebhook(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + + $destinationHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + // Unique name so re-runs and parallel suites can't match the wrong webhook + // on the destination list. + $webhookName = 'Test Webhook ' . ID::unique(); + + $createResp = $this->client->call(Client::METHOD_POST, '/webhooks', $sourceHeaders, [ + 'webhookId' => ID::unique(), + 'url' => 'https://appwrite.io/hook', + 'name' => $webhookName, + 'events' => ['users.*.create', 'users.*.delete'], + 'enabled' => true, + 'tls' => true, + 'authUsername' => 'hook-user', + 'authPassword' => 'hook-pass', + ]); + $this->assertEquals(201, $createResp['headers']['status-code']); + $sourceWebhook = $createResp['body']; + + $result = $this->performMigrationSync([ + 'resources' => [ + Resource::TYPE_WEBHOOK, + ], + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + + $this->assertEquals('completed', $result['status']); + $this->assertEquals([Resource::TYPE_WEBHOOK], $result['resources']); + $this->assertArrayHasKey(Resource::TYPE_WEBHOOK, $result['statusCounters']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_WEBHOOK]['error']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_WEBHOOK]['pending']); + $this->assertGreaterThanOrEqual(1, $result['statusCounters'][Resource::TYPE_WEBHOOK]['success']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_WEBHOOK]['processing']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_WEBHOOK]['warning']); + + $listResp = $this->client->call(Client::METHOD_GET, '/webhooks', $destinationHeaders); + $this->assertEquals(200, $listResp['headers']['status-code']); + + $foundWebhook = null; + foreach ($listResp['body']['webhooks'] as $w) { + if ($w['name'] === $webhookName) { + $foundWebhook = $w; + break; + } + } + + $this->assertNotNull($foundWebhook, 'Migrated webhook not found on destination'); + $this->assertEquals($webhookName, $foundWebhook['name']); + $this->assertEquals('https://appwrite.io/hook', $foundWebhook['url']); + $this->assertEqualsCanonicalizing(['users.*.create', 'users.*.delete'], $foundWebhook['events']); + $this->assertTrue($foundWebhook['enabled']); + $this->assertTrue($foundWebhook['tls']); + $this->assertEquals('hook-user', $foundWebhook['authUsername']); + $this->assertEquals('hook-pass', $foundWebhook['authPassword']); + // secret is regenerated on the destination because the SDK strips it from list + // responses on read — same caveat as api keys. + if (!empty($sourceWebhook['secret'])) { + $this->assertNotEquals($sourceWebhook['secret'], $foundWebhook['secret'] ?? ''); + } + + // Cleanup on destination + $this->client->call(Client::METHOD_DELETE, '/webhooks/' . $foundWebhook['$id'], $destinationHeaders); + + // Cleanup on source + $this->client->call(Client::METHOD_DELETE, '/webhooks/' . $sourceWebhook['$id'], $sourceHeaders); + } + public function testAppwriteMigrationProjectVariable(): void { $sourceHeaders = [