Skip to content

Commit 01a6c91

Browse files
committed
test: add tests for ProfilePageController
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 82ee83a commit 01a6c91

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

core/Controller/ProfilePageController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929

3030
use OC\Profile\ProfileManager;
3131
use OCP\AppFramework\Controller;
32+
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
33+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
3234
use OCP\AppFramework\Http\Attribute\OpenAPI;
35+
use OCP\AppFramework\Http\Attribute\UserRateLimit;
3336
use OCP\AppFramework\Http\TemplateResponse;
3437
use OCP\AppFramework\Services\IInitialState;
3538
use OCP\EventDispatcher\IEventDispatcher;
3639
use OCP\INavigationManager;
3740
use OCP\IRequest;
38-
use OCP\IUser;
3941
use OCP\IUserManager;
4042
use OCP\IUserSession;
4143
use OCP\Profile\BeforeTemplateRenderedEvent;
@@ -65,6 +67,9 @@ public function __construct(
6567
* @NoAdminRequired
6668
* @NoSubAdminRequired
6769
*/
70+
#[BruteForceProtection(action: 'user')]
71+
#[UserRateLimit(limit: 30, period: 120)]
72+
#[AnonRateLimit(limit: 30, period: 120)]
6873
public function index(string $targetUserId): TemplateResponse {
6974
$profileNotFoundTemplate = new TemplateResponse(
7075
'core',
@@ -74,7 +79,11 @@ public function index(string $targetUserId): TemplateResponse {
7479
);
7580

7681
$targetUser = $this->userManager->get($targetUserId);
77-
if (!($targetUser instanceof IUser) || !$targetUser->isEnabled()) {
82+
if ($targetUser === null) {
83+
$profileNotFoundTemplate->throttle();
84+
return $profileNotFoundTemplate;
85+
}
86+
if (!$targetUser->isEnabled()) {
7887
return $profileNotFoundTemplate;
7988
}
8089
$visitingUser = $this->userSession->getUser();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace Core\Controller;
11+
12+
use OC\Core\Controller\ProfilePageController;
13+
use OC\Profile\ProfileManager;
14+
use OC\UserStatus\Manager;
15+
use OCP\AppFramework\Services\IInitialState;
16+
use OCP\EventDispatcher\IEventDispatcher;
17+
use OCP\INavigationManager;
18+
use OCP\IRequest;
19+
use OCP\IUser;
20+
use OCP\IUserManager;
21+
use OCP\IUserSession;
22+
use OCP\Share\IManager;
23+
use Test\TestCase;
24+
25+
class ProfilePageControllerTest extends TestCase {
26+
27+
private IUserManager $userManager;
28+
private ProfilePageController $controller;
29+
30+
protected function setUp(): void {
31+
parent::setUp();
32+
33+
$request = $this->createMock(IRequest::class);
34+
$initialStateService = $this->createMock(IInitialState::class);
35+
$profileManager = $this->createMock(ProfileManager::class);
36+
$shareManager = $this->createMock(IManager::class);
37+
$this->userManager = $this->createMock(IUserManager::class);
38+
$userSession = $this->createMock(IUserSession::class);
39+
$userStatusManager = $this->createMock(Manager::class);
40+
$navigationManager = $this->createMock(INavigationManager::class);
41+
$eventDispatcher = $this->createMock(IEventDispatcher::class);
42+
43+
$this->controller = new ProfilePageController(
44+
'core',
45+
$request,
46+
$initialStateService,
47+
$profileManager,
48+
$shareManager,
49+
$this->userManager,
50+
$userSession,
51+
$userStatusManager,
52+
$navigationManager,
53+
$eventDispatcher,
54+
);
55+
}
56+
57+
public function testUserNotFound(): void {
58+
$this->userManager->method('get')
59+
->willReturn(null);
60+
61+
$response = $this->controller->index('bob');
62+
63+
$this->assertTrue($response->isThrottled());
64+
}
65+
66+
public function testUserDisabled(): void {
67+
$user = $this->createMock(IUser::class);
68+
$user->method('isEnabled')
69+
->willReturn(false);
70+
71+
$this->userManager->method('get')
72+
->willReturn($user);
73+
74+
$response = $this->controller->index('bob');
75+
76+
$this->assertFalse($response->isThrottled());
77+
}
78+
}

0 commit comments

Comments
 (0)