Merge pull request #45822 from nextcloud/backport/45811/stable26

[stable26] test: add tests for ProfilePageController
This commit is contained in:
Arthur Schiwon 2024-06-18 18:38:19 +02:00 committed by GitHub
commit 1d9e1665a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 2 deletions

View file

@ -32,7 +32,6 @@ use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager as IShareManager;
@ -74,6 +73,9 @@ class ProfilePageController extends Controller {
* @NoCSRFRequired
* @NoAdminRequired
* @NoSubAdminRequired
* @BruteForceProtection(action=user)
* @UserRateThrottle(limit=30, period=120)
* @AnonRateThrottle(limit=30, period=120)
*/
public function index(string $targetUserId): TemplateResponse {
$profileNotFoundTemplate = new TemplateResponse(
@ -84,7 +86,11 @@ class ProfilePageController extends Controller {
);
$targetUser = $this->userManager->get($targetUserId);
if (!($targetUser instanceof IUser) || !$targetUser->isEnabled()) {
if ($targetUser === null) {
$profileNotFoundTemplate->throttle();
return $profileNotFoundTemplate;
}
if (!$targetUser->isEnabled()) {
return $profileNotFoundTemplate;
}
$visitingUser = $this->userSession->getUser();

View file

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Core\Controller;
use OC\Core\Controller\ProfilePageController;
use OC\Profile\ProfileManager;
use OC\UserStatus\Manager;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager;
use Test\TestCase;
class ProfilePageControllerTest extends TestCase {
private IUserManager $userManager;
private ProfilePageController $controller;
protected function setUp(): void {
parent::setUp();
$request = $this->createMock(IRequest::class);
$initialStateService = $this->createMock(IInitialState::class);
$profileManager = $this->createMock(ProfileManager::class);
$shareManager = $this->createMock(IManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$userSession = $this->createMock(IUserSession::class);
$userStatusManager = $this->createMock(Manager::class);
$eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->controller = new ProfilePageController(
'core',
$request,
$initialStateService,
$profileManager,
$shareManager,
$this->userManager,
$userSession,
$userStatusManager,
$eventDispatcher,
);
}
public function testUserNotFound(): void {
$this->userManager->method('get')
->willReturn(null);
$response = $this->controller->index('bob');
$this->assertTrue($response->isThrottled());
}
public function testUserDisabled(): void {
$user = $this->createMock(IUser::class);
$user->method('isEnabled')
->willReturn(false);
$this->userManager->method('get')
->willReturn($user);
$response = $this->controller->index('bob');
$this->assertFalse($response->isThrottled());
}
}