Skip to content

Commit 1de7ef2

Browse files
committed
fix(integration-test): run the search and reference tests without the GitHub login
All integration tests depended on testOAuthLogin, which signs in to github.com with the CI account's password and a TOTP code. GitHub often answers that login with a two-factor prompt or a checkup page, and the 24 dependent tests were then skipped. The 21 search, issue/PR reference and code reference tests only read public data, so they now store the workflow's GITHUB_TOKEN as the test user's personal token, restoring the previous token afterwards so the OAuth-dependent tests are unaffected. The integration workflow requests a read-only token and passes it to PHPUnit. Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent 56c1460 commit 1de7ef2

5 files changed

Lines changed: 143 additions & 112 deletions

File tree

‎.github/workflows/integration.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ on:
2525
- templates/**
2626
- tests/**
2727

28+
permissions:
29+
contents: read
30+
2831
env:
2932
APP_NAME: integration_github
3033

@@ -86,6 +89,7 @@ jobs:
8689
CI_USER_LOGIN: ${{ secrets.CI_USER_LOGIN }}
8790
CI_USER_PASSWORD: ${{ secrets.CI_USER_PASSWORD }}
8891
CI_TOTP_SECRET: ${{ secrets.CI_TOTP_SECRET }}
92+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8993
run: composer run test:integration
9094

9195
- name: Upload Nextcloud log on failure

‎tests/integration/GitHubCodeReferenceIntegrationTest.php‎

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,40 @@
99

1010
namespace OCA\Github\Tests\Integration;
1111

12+
require_once __DIR__ . '/WorkflowTokenTrait.php';
13+
1214
use OCA\Github\Reference\GithubCodeReferenceProvider;
1315
use OCA\Github\Service\SecretService;
1416
use OCP\Collaboration\Reference\IReference;
1517
use OCP\Server;
16-
use PHPUnit\Framework\Attributes\DependsExternal;
1718
use PHPUnit\Framework\Attributes\Group;
1819
use Test\TestCase;
1920

2021
#[Group('DB')]
2122
class GitHubCodeReferenceIntegrationTest extends TestCase {
23+
use WorkflowTokenTrait;
24+
2225
private GithubCodeReferenceProvider $referenceProvider;
2326
private SecretService $secretService;
2427

2528
protected function setUp(): void {
2629
parent::setUp();
2730

31+
$this->useWorkflowToken();
2832
$this->referenceProvider = Server::get(GithubCodeReferenceProvider::class);
2933
$this->secretService = Server::get(SecretService::class);
3034
}
3135

32-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
33-
public function testResolveSingleLineCodeReference(array $oauthData): void {
34-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
35-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
36-
$userId = $oauthData['userId'];
36+
protected function tearDown(): void {
37+
$this->restorePreviousToken();
38+
parent::tearDown();
39+
}
40+
41+
public function testResolveSingleLineCodeReference(): void {
42+
$userId = $this->userId;
3743

3844
$token = $this->secretService->getEncryptedUserValue($userId, 'token');
39-
$this->assertNotSame('', $token, 'Token should be stored after OAuth flow');
45+
$this->assertNotSame('', $token, 'The workflow token should be stored for the test user');
4046

4147
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
4248
$reference = $this->referenceProvider->resolveReference($referenceUrl);
@@ -79,11 +85,7 @@ private function assertCodeRichObjectStructure(array $richObject): void {
7985
$this->assertStringContainsString('github.com', $richObject['link'], 'link should contain github.com');
8086
}
8187

82-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
83-
public function testResolveMultiLineCodeReference(array $oauthData): void {
84-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
85-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
86-
88+
public function testResolveMultiLineCodeReference(): void {
8789
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1-L5';
8890
$reference = $this->referenceProvider->resolveReference($referenceUrl);
8991

@@ -103,10 +105,7 @@ public function testResolveMultiLineCodeReference(array $oauthData): void {
103105
$this->assertCount($expectedLineCount, $richObject['lines'], 'lines array should have correct count');
104106
}
105107

106-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
107-
public function testMatchReference(array $oauthData): void {
108-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
109-
108+
public function testMatchReference(): void {
110109
$validSingleLine = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
111110
$validMultiLine = 'https://github.com/nextcloud/server/blob/abc123/lib/base.php#L1-L10';
112111
$invalidNoLine = 'https://github.com/nextcloud/server/blob/master/lib/base.php';
@@ -118,11 +117,7 @@ public function testMatchReference(array $oauthData): void {
118117
$this->assertFalse($this->referenceProvider->matchReference($invalidWrongUrl), 'Should not match non-blob URL');
119118
}
120119

121-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
122-
public function testReferenceTitle(array $oauthData): void {
123-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
124-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
125-
120+
public function testReferenceTitle(): void {
126121
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
127122
$reference = $this->referenceProvider->resolveReference($referenceUrl);
128123

@@ -133,11 +128,7 @@ public function testReferenceTitle(array $oauthData): void {
133128
$this->assertStringContainsString('permalink', strtolower($title), 'Title should mention permalink');
134129
}
135130

136-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
137-
public function testReferenceDescription(array $oauthData): void {
138-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
139-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
140-
131+
public function testReferenceDescription(): void {
141132
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
142133
$reference = $this->referenceProvider->resolveReference($referenceUrl);
143134

@@ -151,11 +142,7 @@ public function testReferenceDescription(array $oauthData): void {
151142
}
152143
}
153144

154-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
155-
public function testShortRefFormat(array $oauthData): void {
156-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
157-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
158-
145+
public function testShortRefFormat(): void {
159146
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
160147
$reference = $this->referenceProvider->resolveReference($referenceUrl);
161148

@@ -174,11 +161,7 @@ public function testShortRefFormat(array $oauthData): void {
174161
}
175162
}
176163

177-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
178-
public function testVcsCodePermalinkStructure(array $oauthData): void {
179-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
180-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
181-
164+
public function testVcsCodePermalinkStructure(): void {
182165
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
183166
$reference = $this->referenceProvider->resolveReference($referenceUrl);
184167

@@ -204,11 +187,7 @@ public function testVcsCodePermalinkStructure(array $oauthData): void {
204187
}
205188
}
206189

207-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
208-
public function testCodeLinesContent(array $oauthData): void {
209-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
210-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
211-
190+
public function testCodeLinesContent(): void {
212191
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1-L3';
213192
$reference = $this->referenceProvider->resolveReference($referenceUrl);
214193

@@ -226,11 +205,7 @@ public function testCodeLinesContent(array $oauthData): void {
226205
}
227206
}
228207

229-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
230-
public function testFilePathExtraction(array $oauthData): void {
231-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
232-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
233-
208+
public function testFilePathExtraction(): void {
234209
$referenceUrl = 'https://github.com/nextcloud/server/blob/master/lib/base.php#L1';
235210
$reference = $this->referenceProvider->resolveReference($referenceUrl);
236211

‎tests/integration/GitHubIssuePrReferenceIntegrationTest.php‎

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,40 @@
99

1010
namespace OCA\Github\Tests\Integration;
1111

12+
require_once __DIR__ . '/WorkflowTokenTrait.php';
13+
1214
use OCA\Github\Reference\GithubIssuePrReferenceProvider;
1315
use OCA\Github\Service\SecretService;
1416
use OCP\Collaboration\Reference\IReference;
1517
use OCP\Server;
16-
use PHPUnit\Framework\Attributes\DependsExternal;
1718
use PHPUnit\Framework\Attributes\Group;
1819
use Test\TestCase;
1920

2021
#[Group('DB')]
2122
class GitHubIssuePrReferenceIntegrationTest extends TestCase {
23+
use WorkflowTokenTrait;
24+
2225
private GithubIssuePrReferenceProvider $referenceProvider;
2326
private SecretService $secretService;
2427

2528
protected function setUp(): void {
2629
parent::setUp();
2730

31+
$this->useWorkflowToken();
2832
$this->referenceProvider = Server::get(GithubIssuePrReferenceProvider::class);
2933
$this->secretService = Server::get(SecretService::class);
3034
}
3135

32-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
33-
public function testResolveIssueReference(array $oauthData): void {
34-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
35-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
36-
$userId = $oauthData['userId'];
36+
protected function tearDown(): void {
37+
$this->restorePreviousToken();
38+
parent::tearDown();
39+
}
40+
41+
public function testResolveIssueReference(): void {
42+
$userId = $this->userId;
3743

3844
$token = $this->secretService->getEncryptedUserValue($userId, 'token');
39-
$this->assertNotSame('', $token, 'Token should be stored after OAuth flow');
45+
$this->assertNotSame('', $token, 'The workflow token should be stored for the test user');
4046

4147
$referenceUrl = 'https://github.com/nextcloud/server/issues/1';
4248
$reference = $this->referenceProvider->resolveReference($referenceUrl);
@@ -96,14 +102,11 @@ private function assertIssueRichObjectStructure(array $richObject): void {
96102
}
97103
}
98104

99-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
100-
public function testResolvePullRequestReference(array $oauthData): void {
101-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
102-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
103-
$userId = $oauthData['userId'];
105+
public function testResolvePullRequestReference(): void {
106+
$userId = $this->userId;
104107

105108
$token = $this->secretService->getEncryptedUserValue($userId, 'token');
106-
$this->assertNotSame('', $token, 'Token should be stored after OAuth flow');
109+
$this->assertNotSame('', $token, 'The workflow token should be stored for the test user');
107110

108111
$referenceUrl = 'https://github.com/nextcloud/server/pull/1';
109112
$reference = $this->referenceProvider->resolveReference($referenceUrl);
@@ -156,14 +159,11 @@ private function assertPullRequestRichObjectStructure(array $richObject): void {
156159
$this->assertValidDateString($richObject['created_at'], 'created_at should be a valid date string');
157160
}
158161

159-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
160-
public function testResolveIssueWithCommentReference(array $oauthData): void {
161-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
162-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
163-
$userId = $oauthData['userId'];
162+
public function testResolveIssueWithCommentReference(): void {
163+
$userId = $this->userId;
164164

165165
$token = $this->secretService->getEncryptedUserValue($userId, 'token');
166-
$this->assertNotSame('', $token, 'Token should be stored after OAuth flow');
166+
$this->assertNotSame('', $token, 'The workflow token should be stored for the test user');
167167

168168
$referenceUrl = 'https://github.com/nextcloud/server/issues/1#issuecomment-223229268';
169169
$reference = $this->referenceProvider->resolveReference($referenceUrl);
@@ -193,10 +193,7 @@ private function assertCommentStructure(array $comment): void {
193193
$this->assertValidDateString($comment['updated_at'], 'Comment updated_at should be a valid date string');
194194
}
195195

196-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
197-
public function testMatchReference(array $oauthData): void {
198-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
199-
196+
public function testMatchReference(): void {
200197
$validIssueUrl = 'https://github.com/nextcloud/server/issues/123';
201198
$validPrUrl = 'https://github.com/nextcloud/server/pull/456';
202199
$invalidUrl = 'https://github.com/nextcloud/server';
@@ -206,11 +203,7 @@ public function testMatchReference(array $oauthData): void {
206203
$this->assertFalse($this->referenceProvider->matchReference($invalidUrl), 'Should not match repo URL');
207204
}
208205

209-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
210-
public function testReferenceTitle(array $oauthData): void {
211-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
212-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
213-
206+
public function testReferenceTitle(): void {
214207
$referenceUrl = 'https://github.com/nextcloud/server/issues/1';
215208
$reference = $this->referenceProvider->resolveReference($referenceUrl);
216209

@@ -222,11 +215,7 @@ public function testReferenceTitle(array $oauthData): void {
222215
$this->assertStringContainsString('nextcloud/server', $title, 'Title should contain repo name');
223216
}
224217

225-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
226-
public function testReferenceMilestone(array $oauthData): void {
227-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
228-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
229-
218+
public function testReferenceMilestone(): void {
230219
$referenceUrl = 'https://github.com/nextcloud/server/issues/1';
231220
$reference = $this->referenceProvider->resolveReference($referenceUrl);
232221

@@ -240,11 +229,7 @@ public function testReferenceMilestone(array $oauthData): void {
240229
}
241230
}
242231

243-
#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
244-
public function testReferenceReactions(array $oauthData): void {
245-
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test');
246-
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
247-
232+
public function testReferenceReactions(): void {
248233
$referenceUrl = 'https://github.com/nextcloud/server/issues/1';
249234
$reference = $this->referenceProvider->resolveReference($referenceUrl);
250235

0 commit comments

Comments
 (0)