Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ jobs:
- name: PHPUnit integration
working-directory: apps/${{ env.APP_NAME }}
env:
CI_CLIENT_ID: ${{ secrets.CI_CLIENT_ID }}
CI_CLIENT_SECRET: ${{ secrets.CI_CLIENT_SECRET }}
CI_USER_LOGIN: ${{ secrets.CI_USER_LOGIN }}
CI_USER_PASSWORD: ${{ secrets.CI_USER_PASSWORD }}
CI_TOTP_SECRET: ${{ secrets.CI_TOTP_SECRET }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: composer run test:integration

Expand Down
178 changes: 0 additions & 178 deletions tests/integration/GitHubHtml.php

This file was deleted.

112 changes: 76 additions & 36 deletions tests/integration/GitHubNotificationsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,60 @@

namespace OCA\Github\Tests\Integration;

require_once __DIR__ . '/MockedGithubApiTrait.php';

use OCA\Github\Service\GithubAPIService;
use OCA\Github\Service\SecretService;
use OCP\Server;
use PHPUnit\Framework\Attributes\DependsExternal;
use PHPUnit\Framework\Attributes\Group;
use Test\TestCase;

#[Group('DB')]
class GitHubNotificationsIntegrationTest extends TestCase {
use MockedGithubApiTrait;

private const ACCESS_TOKEN = 'gho_test_access_token';

private GithubAPIService $githubAPIService;
private SecretService $secretService;

protected function setUp(): void {
parent::setUp();

$this->githubAPIService = Server::get(GithubAPIService::class);
$this->secretService = Server::get(SecretService::class);
$this->useTestUser();
Server::get(SecretService::class)->setEncryptedUserValue(self::TEST_USER_ID, 'token', self::ACCESS_TOKEN);
$this->githubAPIService = $this->createGithubAPIService();
}

#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
public function testGetNotificationsStructure(array $oauthData): void {
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test, got: ' . gettype($oauthData));
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
$userId = $oauthData['userId'];

$token = $this->secretService->getEncryptedUserValue($userId, 'token');
$this->assertNotSame('', $token, 'Token should be stored after OAuth flow');
protected function tearDown(): void {
$this->resetTestUserConfig();
parent::tearDown();
}

$notifications = $this->githubAPIService->getNotifications($userId);
public function testGetNotificationsStructure(): void {
$this->client->expects($this->once())
->method('get')
->with(
$this->callback(function (string $url): bool {
$this->assertStringStartsWith('https://api.github.com/notifications?', $url);
parse_str((string)parse_url($url, PHP_URL_QUERY), $query);
$this->assertArrayHasKey('since', $query, 'The request should be limited to recent notifications');
$this->assertEqualsWithDelta(time() - 14 * 24 * 3600, strtotime((string)$query['since']), 300, 'since should default to two weeks ago');
return true;
}),
[
'timeout' => 30,
'headers' => [
'User-Agent' => self::USER_AGENT,
'Authorization' => 'token ' . self::ACCESS_TOKEN,
],
],
)
->willReturn($this->mockResponse(200, json_encode(self::notifications())));

$notifications = $this->githubAPIService->getNotifications(self::TEST_USER_ID);
$this->assertArrayNotHasKey('error', $notifications, 'GitHub API returned error: ' . ($notifications['error'] ?? 'unknown'));

$this->assertIsArray($notifications, 'Notifications should be an array');

$this->assertNotEmpty($notifications, 'The interesting notifications should be returned');
foreach ($notifications as $notification) {
$this->assertNotificationStructure($notification);
}
Expand Down Expand Up @@ -118,30 +139,49 @@ private function computeExpectedTargetUrl(string $subjectType, string $subjectUr
return 'https://github.com/' . $repoFullName . '/discussions';
}

#[DependsExternal(GithubOauthIntegrationTest::class, 'testOAuthLogin')]
public function testNotificationFilteringLogic(array $oauthData): void {
$this->assertIsArray($oauthData, 'oauthData should be an array from OAuth test, got: ' . gettype($oauthData));
$this->assertArrayHasKey('userId', $oauthData, 'oauthData must contain userId');
$userId = $oauthData['userId'];
public function testNotificationFilteringLogic(): void {
$this->client->method('get')
->willReturn($this->mockResponse(200, json_encode(self::notifications())));

$token = $this->secretService->getEncryptedUserValue($userId, 'token');
$this->assertNotSame('', $token, 'Token should be stored after OAuth flow');
$notifications = $this->githubAPIService->getNotifications(self::TEST_USER_ID);
$this->assertSame(['1', '3', '5'], array_column($notifications, 'id'),
'Only unread notifications with an interesting reason, or subscribed releases, should be kept');

$notifications = $this->githubAPIService->getNotifications($userId);
$this->assertArrayNotHasKey('error', $notifications, 'GitHub API returned error: ' . ($notifications['error'] ?? 'unknown'));

$validReasons = ['assign', 'mention', 'team_mention', 'review_requested', 'author', 'manual'];

foreach ($notifications as $notification) {
$reason = $notification['reason'] ?? '';
$subjectType = $notification['subject']['type'] ?? '';
$unread = $notification['unread'] ?? false;
$limited = $this->githubAPIService->getNotifications(self::TEST_USER_ID, null, null, 2);
$this->assertSame(['1', '3'], array_column($limited, 'id'), 'The result should be limited');
}

$isValidReason = in_array($reason, $validReasons, true)
|| ($reason === 'subscribed' && $subjectType === 'Release');
/**
* A kept and a dropped case for each branch of the filter in GithubAPIService::getNotifications().
*/
private static function notifications(): array {
return [
self::notification('1', 'mention', true, 'Issue', 'https://api.github.com/repos/nextcloud/server/issues/1'),
self::notification('2', 'assign', false, 'Issue', 'https://api.github.com/repos/nextcloud/server/issues/2'),
self::notification('3', 'subscribed', true, 'Release', 'https://api.github.com/repos/nextcloud/server/releases/3'),
self::notification('4', 'subscribed', true, 'Issue', 'https://api.github.com/repos/nextcloud/server/issues/4'),
self::notification('5', 'review_requested', true, 'PullRequest', 'https://api.github.com/repos/nextcloud/server/pulls/5'),
self::notification('6', 'ci_activity', true, 'CheckSuite', ''),
];
}

$this->assertTrue($isValidReason, "Notification has unexpected reason: $reason");
$this->assertTrue($unread, 'Notification should be unread');
}
private static function notification(string $id, string $reason, bool $unread, string $type, string $subjectUrl): array {
return [
'id' => $id,
'unread' => $unread,
'reason' => $reason,
'updated_at' => '2026-09-01T12:00:00Z',
'subject' => [
'title' => 'Notification ' . $id,
'type' => $type,
'url' => $subjectUrl,
],
'repository' => [
'name' => 'server',
'full_name' => 'nextcloud/server',
'html_url' => 'https://github.com/nextcloud/server',
'owner' => ['login' => 'nextcloud'],
],
];
}
}
Loading
Loading