appManager->cleanAppId($app); if ($cleanId !== $app) { throw new \InvalidArgumentException('Invalid app id given'); } return $cleanId; } /** * Get a list of installed apps * * @param ?string $filter Filter for enabled or disabled apps * @return DataResponse}, array{}> * @throws OCSException * * 200: Installed apps returned */ public function getApps(?string $filter = null): DataResponse { $apps = (new OC_App())->listAllApps(); /** @var list $list */ $list = []; foreach ($apps as $app) { $list[] = $app['id']; } if ($filter) { switch ($filter) { case 'enabled': return new DataResponse(['apps' => \OC_App::getEnabledApps()]); break; case 'disabled': $enabled = OC_App::getEnabledApps(); return new DataResponse(['apps' => array_values(array_diff($list, $enabled))]); break; default: // Invalid filter variable throw new OCSException('', 101); } } else { return new DataResponse(['apps' => $list]); } } /** * Get the app info for an app * * @param string $app ID of the app * @return DataResponse, array{}> * @throws OCSException * * 200: App info returned */ public function getAppInfo(string $app): DataResponse { try { $app = $this->verifyAppId($app); } catch (\InvalidArgumentException $e) { throw new OCSException($e->getMessage(), OCSController::RESPOND_UNAUTHORISED); } $info = $this->appManager->getAppInfo($app); if (!is_null($info)) { return new DataResponse($info); } throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND); } /** * Enable an app * * @param string $app ID of the app * @return DataResponse, array{}> * @throws OCSException * * 200: App enabled successfully */ #[PasswordConfirmationRequired] public function enable(string $app): DataResponse { try { $app = $this->verifyAppId($app); $this->appManager->enableApp($app); } catch (\InvalidArgumentException $e) { throw new OCSException($e->getMessage(), OCSController::RESPOND_UNAUTHORISED); } catch (AppPathNotFoundException $e) { throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND); } return new DataResponse(); } /** * Disable an app * * @param string $app ID of the app * @return DataResponse, array{}> * @throws OCSException * * 200: App disabled successfully */ #[PasswordConfirmationRequired] public function disable(string $app): DataResponse { try { $app = $this->verifyAppId($app); $this->appManager->disableApp($app); } catch (\InvalidArgumentException $e) { throw new OCSException($e->getMessage(), OCSController::RESPOND_UNAUTHORISED); } return new DataResponse(); } } tion> Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
blob: 9f9afed4b7f7e8cefc9297179a73a50c318988a3 (plain)
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
<?php

declare(strict_types=1);
/**
 * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Test\AppFramework\Http;

use OC\AppFramework\Http\RequestId;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;

/**
 * Class RequestIdTest
 *
 * @package OC\AppFramework\Http
 */
class RequestIdTest extends \Test\TestCase {
	/** @var ISecureRandom|MockObject */
	protected $secureRandom;

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

		$this->secureRandom = $this->createMock(ISecureRandom::class);
	}

	public function testGetIdWithModUnique(): void {
		$requestId = new RequestId(
			'GeneratedUniqueIdByModUnique',
			$this->secureRandom
		);

		$this->secureRandom->expects($this->never())
			->method('generate');

		$this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId());
		$this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId());
	}

	public function testGetIdWithoutModUnique(): void {
		$requestId = new RequestId(
			'',
			$this->secureRandom
		);

		$this->secureRandom->expects($this->once())
			->method('generate')
			->with('20')
			->willReturnOnConsecutiveCalls(
				'GeneratedByNextcloudItself1',
				'GeneratedByNextcloudItself2',
				'GeneratedByNextcloudItself3'
			);

		$this->assertSame('GeneratedByNextcloudItself1', $requestId->getId());
		$this->assertSame('GeneratedByNextcloudItself1', $requestId->getId());
	}
}