-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathForgejoTest.php
More file actions
169 lines (140 loc) · 6.54 KB
/
ForgejoTest.php
File metadata and controls
169 lines (140 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Utopia\Tests\VCS\Adapter;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\System\System;
use Utopia\VCS\Adapter\Git;
use Utopia\VCS\Adapter\Git\Forgejo;
class ForgejoTest extends GiteaTest
{
protected static string $accessToken = '';
protected static string $owner = '';
protected function createVCSAdapter(): Git
{
return new Forgejo(new Cache(new None()));
}
public function setUp(): void
{
if (empty(static::$accessToken)) {
$this->setupForgejo();
}
$adapter = new Forgejo(new Cache(new None()));
$forgejoUrl = System::getEnv('TESTS_FORGEJO_URL', 'http://forgejo:3000') ?? '';
$adapter->initializeVariables(
installationId: '',
privateKey: '',
appId: '',
accessToken: static::$accessToken,
refreshToken: ''
);
$adapter->setEndpoint($forgejoUrl);
if (empty(static::$owner)) {
$orgName = 'test-org-' . \uniqid();
static::$owner = $adapter->createOrganization($orgName);
}
$this->vcsAdapter = $adapter;
}
protected function setupForgejo(): void
{
$tokenFile = '/forgejo-data/gitea/token.txt';
if (file_exists($tokenFile)) {
$contents = file_get_contents($tokenFile);
if ($contents !== false) {
static::$accessToken = trim($contents);
}
}
}
public function testWebhookPushEvent(): void
{
$repositoryName = 'test-webhook-push-' . \uniqid();
$secret = 'test-webhook-secret-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
$this->deleteLastWebhookRequest();
$this->vcsAdapter->createWebhook(static::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
// Trigger a real push by creating a file
$this->vcsAdapter->createFile(
static::$owner,
$repositoryName,
'README.md',
'# Webhook Test',
'Initial commit'
);
// Wait for push webhook to arrive automatically
$webhookData = [];
$this->assertEventually(function () use (&$webhookData) {
$webhookData = $this->getLastWebhookRequest();
$this->assertNotEmpty($webhookData, 'No webhook received');
$this->assertNotEmpty($webhookData['data'] ?? '', 'Webhook payload is empty');
$this->assertSame('push', $webhookData['headers']['X-Forgejo-Event'] ?? '', 'Expected push event');
}, 15000, 500);
$payload = $webhookData['data'];
$headers = $webhookData['headers'] ?? [];
$signature = $headers['X-Forgejo-Signature'] ?? '';
$this->assertNotEmpty($signature, 'Missing X-Forgejo-Signature header');
$this->assertTrue(
$this->vcsAdapter->validateWebhookEvent($payload, $signature, $secret),
'Webhook signature validation failed'
);
$event = $this->vcsAdapter->getEvent('push', $payload);
$this->assertIsArray($event);
$this->assertSame('main', $event['branch']);
$this->assertSame($repositoryName, $event['repositoryName']);
$this->assertSame(static::$owner, $event['owner']);
$this->assertNotEmpty($event['commitHash']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testWebhookPullRequestEvent(): void
{
$repositoryName = 'test-webhook-pr-' . \uniqid();
$secret = 'test-webhook-secret-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
// Create all files BEFORE configuring webhook
// so those push events don't pollute the catcher
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', 'main');
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'feature.txt', 'content', 'Add feature', 'feature-branch');
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
$this->vcsAdapter->createWebhook(static::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
// Clear after setup so only PR event will arrive
$this->deleteLastWebhookRequest();
// Trigger real PR event
$this->vcsAdapter->createPullRequest(
static::$owner,
$repositoryName,
'Test Webhook PR',
'feature-branch',
'main'
);
// Wait for pull_request webhook to arrive automatically
$webhookData = [];
$this->assertEventually(function () use (&$webhookData) {
$webhookData = $this->getLastWebhookRequest();
$this->assertNotEmpty($webhookData, 'No webhook received');
$this->assertNotEmpty($webhookData['data'] ?? '', 'Webhook payload is empty');
$this->assertSame('pull_request', $webhookData['headers']['X-Forgejo-Event'] ?? '', 'Expected pull_request event');
}, 15000, 500);
$payload = $webhookData['data'];
$headers = $webhookData['headers'] ?? [];
$signature = $headers['X-Forgejo-Signature'] ?? '';
$this->assertNotEmpty($signature, 'Missing X-Forgejo-Signature header');
$this->assertTrue(
$this->vcsAdapter->validateWebhookEvent($payload, $signature, $secret),
'Webhook signature validation failed'
);
$event = $this->vcsAdapter->getEvent('pull_request', $payload);
$this->assertIsArray($event);
$this->assertSame('feature-branch', $event['branch']);
$this->assertSame($repositoryName, $event['repositoryName']);
$this->assertSame(static::$owner, $event['owner']);
$this->assertContains($event['action'], ['opened', 'synchronized']);
$this->assertGreaterThan(0, $event['pullRequestNumber']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
}