11<?php
22
3- namespace Utopia \Tests \VCS \ Adapter ;
3+ namespace Utopia \Tests \Adapter ;
44
55use Utopia \Cache \Adapter \None ;
66use Utopia \Cache \Cache ;
@@ -15,6 +15,10 @@ class GiteaTest extends Base
1515 protected static string $ owner = '' ;
1616 protected static string $ defaultBranch = 'main ' ;
1717
18+ protected string $ webhookEventHeader = 'X-Gitea-Event ' ;
19+ protected string $ webhookSignatureHeader = 'X-Gitea-Signature ' ;
20+ protected string $ avatarDomain = 'gravatar.com ' ;
21+
1822 protected function createVCSAdapter (): Git
1923 {
2024 return new Gitea (new Cache (new None ()));
@@ -45,7 +49,7 @@ public function setUp(): void
4549 $ this ->vcsAdapter = $ adapter ;
4650 }
4751
48- private function setupGitea (): void
52+ protected function setupGitea (): void
4953 {
5054 $ tokenFile = '/data/gitea/token.txt ' ;
5155
@@ -778,7 +782,7 @@ public function testGetCommit(): void
778782 $ this ->assertSame ($ commitHash , $ result ['commitHash ' ]);
779783 $ this ->assertSame ('utopia ' , $ result ['commitAuthor ' ]);
780784 $ this ->assertStringStartsWith ($ customMessage , $ result ['commitMessage ' ]);
781- $ this ->assertStringContainsString (' gravatar.com ' , $ result ['commitAuthorAvatar ' ]);
785+ $ this ->assertStringContainsString ($ this -> avatarDomain , $ result ['commitAuthorAvatar ' ]);
782786 $ this ->assertNotEmpty ($ result ['commitUrl ' ]);
783787
784788 $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
@@ -806,7 +810,7 @@ public function testGetLatestCommit(): void
806810 $ this ->assertNotEmpty ($ commit1 ['commitHash ' ]);
807811 $ this ->assertSame ('utopia ' , $ commit1 ['commitAuthor ' ]);
808812 $ this ->assertStringStartsWith ($ firstMessage , $ commit1 ['commitMessage ' ]);
809- $ this ->assertStringContainsString (' gravatar.com ' , $ commit1 ['commitAuthorAvatar ' ]);
813+ $ this ->assertStringContainsString ($ this -> avatarDomain , $ commit1 ['commitAuthorAvatar ' ]);
810814 $ this ->assertNotEmpty ($ commit1 ['commitUrl ' ]);
811815
812816 $ commit1Hash = $ commit1 ['commitHash ' ];
@@ -1530,14 +1534,14 @@ public function testWebhookPushEvent(): void
15301534 $ webhookData = $ this ->getLastWebhookRequest ();
15311535 $ this ->assertNotEmpty ($ webhookData , 'No webhook received ' );
15321536 $ this ->assertNotEmpty ($ webhookData ['data ' ] ?? '' , 'Webhook payload is empty ' );
1533- $ this ->assertSame ('push ' , $ webhookData ['headers ' ][' X-Gitea-Event ' ] ?? '' , 'Expected push event ' );
1537+ $ this ->assertSame ('push ' , $ webhookData ['headers ' ][$ this -> webhookEventHeader ] ?? '' , 'Expected push event ' );
15341538 }, 15000 , 500 );
15351539
15361540 $ payload = $ webhookData ['data ' ];
15371541 $ headers = $ webhookData ['headers ' ] ?? [];
1538- $ signature = $ headers [' X-Gitea-Signature ' ] ?? '' ;
1542+ $ signature = $ headers [$ this -> webhookSignatureHeader ] ?? '' ;
15391543
1540- $ this ->assertNotEmpty ($ signature , 'Missing X-Gitea-Signature header ' );
1544+ $ this ->assertNotEmpty ($ signature , 'Missing ' . $ this -> webhookSignatureHeader . ' header ' );
15411545 $ this ->assertTrue (
15421546 $ this ->vcsAdapter ->validateWebhookEvent ($ payload , $ signature , $ secret ),
15431547 'Webhook signature validation failed '
@@ -1569,7 +1573,7 @@ public function testWebhookPullRequestEvent(): void
15691573 $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'feature.txt ' , 'content ' , 'Add feature ' , 'feature-branch ' );
15701574
15711575 $ catcherUrl = System::getEnv ('TESTS_GITEA_REQUEST_CATCHER_URL ' , 'http://request-catcher:5000 ' );
1572- $ this ->vcsAdapter ->createWebhook (static ::$ owner , $ repositoryName , $ catcherUrl . '/webhook ' , $ secret );
1576+ $ this ->vcsAdapter ->createWebhook (static ::$ owner , $ repositoryName , $ catcherUrl . '/webhook ' , $ secret, [ ' pull_request ' ] );
15731577
15741578 // Clear after setup so only PR event will arrive
15751579 $ this ->deleteLastWebhookRequest ();
@@ -1589,14 +1593,14 @@ public function testWebhookPullRequestEvent(): void
15891593 $ webhookData = $ this ->getLastWebhookRequest ();
15901594 $ this ->assertNotEmpty ($ webhookData , 'No webhook received ' );
15911595 $ this ->assertNotEmpty ($ webhookData ['data ' ] ?? '' , 'Webhook payload is empty ' );
1592- $ this ->assertSame ('pull_request ' , $ webhookData ['headers ' ][' X-Gitea-Event ' ] ?? '' , 'Expected pull_request event ' );
1596+ $ this ->assertSame ('pull_request ' , $ webhookData ['headers ' ][$ this -> webhookEventHeader ] ?? '' , 'Expected pull_request event ' );
15931597 }, 15000 , 500 );
15941598
15951599 $ payload = $ webhookData ['data ' ];
15961600 $ headers = $ webhookData ['headers ' ] ?? [];
1597- $ signature = $ headers [' X-Gitea-Signature ' ] ?? '' ;
1601+ $ signature = $ headers [$ this -> webhookSignatureHeader ] ?? '' ;
15981602
1599- $ this ->assertNotEmpty ($ signature , 'Missing X-Gitea-Signature header ' );
1603+ $ this ->assertNotEmpty ($ signature , 'Missing ' . $ this -> webhookSignatureHeader . ' header ' );
16001604 $ this ->assertTrue (
16011605 $ this ->vcsAdapter ->validateWebhookEvent ($ payload , $ signature , $ secret ),
16021606 'Webhook signature validation failed '
0 commit comments