request = $this->createMock(IRequest::class); $this->tokenProvider = $this->createMock(IProvider::class); $this->session = $this->createMock(ISession::class); $this->userSession = $this->createMock(IUserSession::class); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->activityManager = $this->createMock(IManager::class); $this->remoteWipe = $this->createMock(RemoteWipe::class); /** @var LoggerInterface|MockObject $logger */ $logger = $this->createMock(LoggerInterface::class); $this->controller = new AuthSettingsController( 'core', $this->request, $this->tokenProvider, $this->session, $this->secureRandom, $this->uid, $this->userSession, $this->activityManager, $this->remoteWipe, $logger ); } public function testCreate(): void { $name = 'Nexus 4'; $sessionToken = $this->createMock(IToken::class); $deviceToken = $this->createMock(IToken::class); $password = '123456'; $this->session->expects($this->once()) ->method('getId') ->willReturn('sessionid'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sessionid') ->willReturn($sessionToken); $this->tokenProvider->expects($this->once()) ->method('getPassword') ->with($sessionToken, 'sessionid') ->willReturn($password); $sessionToken->expects($this->once()) ->method('getLoginName') ->willReturn('User13'); $this->secureRandom->expects($this->exactly(5)) ->method('generate') ->with(5, ISecureRandom::CHAR_HUMAN_READABLE) ->willReturn('XXXXX'); $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX'; $this->tokenProvider->expects($this->once()) ->method('generateToken') ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN) ->willReturn($deviceToken); $deviceToken->expects($this->once()) ->method('jsonSerialize') ->willReturn(['dummy' => 'dummy', 'canDelete' => true]); $this->mockActivityManager(); $expected = [ 'token' => $newToken, 'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true, 'canRename' => true], 'loginName' => 'User13', ]; $response = $this->controller->create($name); $this->assertInstanceOf(JSONResponse::class, $response); $this->assertEquals($expected, $response->getData()); } public function testCreateSessionNotAvailable(): void { $name = 'personal phone'; $this->session->expects($this->once()) ->method('getId') ->will($this->throwException(new SessionNotAvailableException())); $expected = new JSONResponse(); $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); $this->assertEquals($expected, $this->controller->create($name)); } public function testCreateInvalidToken(): void { $name = 'Company IPhone'; $this->session->expects($this->once()) ->method('getId') ->willReturn('sessionid'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sessionid') ->will($this->throwException(new InvalidTokenException())); $expected = new JSONResponse(); $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); $this->assertEquals($expected, $this->controller->create($name)); } public function testDestroy(): void { $tokenId = 124; $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $this->mockActivityManager(); $token->expects($this->exactly(2)) ->method('getId') ->willReturn($tokenId); $token->expects($this->once()) ->method('getUID') ->willReturn('jane'); $this->tokenProvider->expects($this->once()) ->method('invalidateTokenById') ->with($this->uid, $tokenId); $this->assertEquals([], $this->controller->destroy($tokenId)); } public function testDestroyExpired(): void { $tokenId = 124; $token = $this->createMock(PublicKeyToken::class); $token->expects($this->exactly(2)) ->method('getId') ->willReturn($tokenId); $token->expects($this->once()) ->method('getUID') ->willReturn($this->uid); $this->tokenProvider->expects($this->once()) ->method('getTokenById') ->with($this->equalTo($tokenId)) ->willThrowException(new ExpiredTokenException($token)); $this->tokenProvider->expects($this->once()) ->method('invalidateTokenById') ->with($this->uid, $tokenId); $this->assertSame([], $this->controller->destroy($tokenId)); } public function testDestroyWrongUser(): void { $tokenId = 124; $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $token->expects($this->once()) ->method('getUID') ->willReturn('foobar'); $response = $this->controller->destroy($tokenId); $this->assertSame([], $response->getData()); $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } public function dataRenameToken(): array { return [ 'App password => Other token name' => ['App password', 'Other token name'], 'Other token name => App password' => ['Other token name', 'App password'], ]; } /** * @dataProvider dataRenameToken * * @param string $name * @param string $newName */ public function testUpdateRename(string $name, string $newName): void { $tokenId = 42; $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $this->mockActivityManager(); $token->expects($this->once()) ->method('getUID') ->willReturn('jane'); $token->expects($this->once()) ->method('getName') ->willReturn($name); $token->expects($this->once()) ->method('getScopeAsArray') ->willReturn([IToken::SCOPE_FILESYSTEM => true]); $token->expects($this->once()) ->method('setName') ->with($this->equalTo($newName)); $this->tokenProvider->expects($this->once()) ->method('updateToken') ->with($this->equalTo($token)); $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], $newName)); } public function dataUpdateFilesystemScope(): array { return [ 'Grant filesystem access' => [false, true], 'Revoke filesystem access' => [true, false], ]; } /** * @dataProvider dataUpdateFilesystemScope * * @param bool $filesystem * @param bool $newFilesystem */ public function testUpdateFilesystemScope(bool $filesystem, bool $newFilesystem): void { $tokenId = 42; $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $this->mockActivityManager(); $token->expects($this->once()) ->method('getUID') ->willReturn('jane'); $token->expects($this->once()) ->method('getName') ->willReturn('App password'); $token->expects($this->once()) ->method('getScopeAsArray') ->willReturn([IToken::SCOPE_FILESYSTEM => $filesystem]); $token->expects($this->once()) ->method('setScope') ->with($this->equalTo([IToken::SCOPE_FILESYSTEM => $newFilesystem])); $this->tokenProvider->expects($this->once()) ->method('updateToken') ->with($this->equalTo($token)); $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => $newFilesystem], 'App password')); } public function testUpdateNoChange(): void { $tokenId = 42; $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $token->expects($this->once()) ->method('getUID') ->willReturn('jane'); $token->expects($this->once()) ->method('getName') ->willReturn('App password'); $token->expects($this->once()) ->method('getScopeAsArray') ->willReturn([IToken::SCOPE_FILESYSTEM => true]); $token->expects($this->never()) ->method('setName'); $token->expects($this->never()) ->method('setScope'); $this->tokenProvider->expects($this->once()) ->method('updateToken') ->with($this->equalTo($token)); $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password')); } public function testUpdateExpired(): void { $tokenId = 42; $token = $this->createMock(PublicKeyToken::class); $token->expects($this->once()) ->method('getUID') ->willReturn($this->uid); $this->tokenProvider->expects($this->once()) ->method('getTokenById') ->with($this->equalTo($tokenId)) ->willThrowException(new ExpiredTokenException($token)); $this->tokenProvider->expects($this->once()) ->method('updateToken') ->with($this->equalTo($token)); $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password')); } public function testUpdateTokenWrongUser(): void { $tokenId = 42; $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $token->expects($this->once()) ->method('getUID') ->willReturn('foobar'); $token->expects($this->never()) ->method('setScope'); $this->tokenProvider->expects($this->never()) ->method('updateToken'); $response = $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'); $this->assertSame([], $response->getData()); $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } public function testUpdateTokenNonExisting(): void { $this->tokenProvider->expects($this->once()) ->method('getTokenById') ->with($this->equalTo(42)) ->willThrowException(new InvalidTokenException('Token does not exist')); $this->tokenProvider->expects($this->never()) ->method('updateToken'); $response = $this->controller->update(42, [IToken::SCOPE_FILESYSTEM => true], 'App password'); $this->assertSame([], $response->getData()); $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } private function mockActivityManager(): void { $this->activityManager->expects($this->once()) ->method('generateEvent') ->willReturn($this->createMock(IEvent::class)); $this->activityManager->expects($this->once()) ->method('publish'); } /** * @param int $tokenId * @param $token */ private function mockGetTokenById(int $tokenId, $token): void { $this->tokenProvider->expects($this->once()) ->method('getTokenById') ->with($this->equalTo($tokenId)) ->willReturn($token); } public function testRemoteWipeNotSuccessful(): void { $token = $this->createMock(IToken::class); $token->expects($this->once()) ->method('getUID') ->willReturn($this->uid); $this->mockGetTokenById(123, $token); $this->remoteWipe->expects($this->once()) ->method('markTokenForWipe') ->with($token) ->willReturn(false); $response = $this->controller->wipe(123); $expected = new JSONResponse([], Http::STATUS_BAD_REQUEST); $this->assertEquals($expected, $response); } public function testRemoteWipeWrongUser(): void { $token = $this->createMock(IToken::class); $token->expects($this->once()) ->method('getUID') ->willReturn('definetly-not-' . $this->uid); $this->mockGetTokenById(123, $token); $this->remoteWipe->expects($this->never()) ->method('markTokenForWipe'); $response = $this->controller->wipe(123); $expected = new JSONResponse([], Http::STATUS_NOT_FOUND); $this->assertEquals($expected, $response); } public function testRemoteWipeSuccessful(): void { $token = $this->createMock(IWipeableToken::class); $token->expects($this->once()) ->method('getUID') ->willReturn($this->uid); $this->mockGetTokenById(123, $token); $this->remoteWipe->expects($this->once()) ->method('markTokenForWipe') ->with($token) ->willReturn(true); $response = $this->controller->wipe(123); $expected = new JSONResponse([]); $this->assertEquals($expected, $response); } } ted/noid/stable27-fix-npm-audit'>automated/noid/stable27-fix-npm-audit Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/models/Tab.js
blob: b67d51f277fc643af32b3185da354956ca733690 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import DOMPurify from 'dompurify'

export default class Tab {

	_id
	_name
	_icon
	_iconSvgSanitized
	_mount
	_setIsActive
	_update
	_destroy
	_enabled
	_scrollBottomReached

	/**
	 * Create a new tab instance
	 *
	 * @param {object} options destructuring object
	 * @param {string} options.id the unique id of this tab
	 * @param {string} options.name the translated tab name
	 * @param {?string} options.icon the icon css class
	 * @param {?string} options.iconSvg the icon in svg format
	 * @param {Function} options.mount function to mount the tab
	 * @param {Function} [options.setIsActive] function to forward the active state of the tab
	 * @param {Function} options.update function to update the tab
	 * @param {Function} options.destroy function to destroy the tab
	 * @param {Function} [options.enabled] define conditions whether this tab is active. Must returns a boolean
	 * @param {Function} [options.scrollBottomReached] executed when the tab is scrolled to the bottom
	 */
	constructor({ id, name, icon, iconSvg, mount, setIsActive, update, destroy, enabled, scrollBottomReached } = {}) {
		if (enabled === undefined) {
			enabled = () => true
		}
		if (scrollBottomReached === undefined) {
			scrollBottomReached = () => { }
		}

		// Sanity checks
		if (typeof id !== 'string' || id.trim() === '') {
			throw new Error('The id argument is not a valid string')
		}
		if (typeof name !== 'string' || name.trim() === '') {
			throw new Error('The name argument is not a valid string')
		}
		if ((typeof icon !== 'string' || icon.trim() === '') && typeof iconSvg !== 'string') {
			throw new Error('Missing valid string for icon or iconSvg argument')
		}
		if (typeof mount !== 'function') {
			throw new Error('The mount argument should be a function')
		}
		if (setIsActive !== undefined && typeof setIsActive !== 'function') {
			throw new Error('The setIsActive argument should be a function')
		}
		if (typeof update !== 'function') {
			throw new Error('The update argument should be a function')
		}
		if (typeof destroy !== 'function') {
			throw new Error('The destroy argument should be a function')
		}
		if (typeof enabled !== 'function') {
			throw new Error('The enabled argument should be a function')
		}
		if (typeof scrollBottomReached !== 'function') {
			throw new Error('The scrollBottomReached argument should be a function')
		}

		this._id = id
		this._name = name
		this._icon = icon
		this._mount = mount
		this._setIsActive = setIsActive
		this._update = update
		this._destroy = destroy
		this._enabled = enabled
		this._scrollBottomReached = scrollBottomReached

		if (typeof iconSvg === 'string') {
			this._iconSvgSanitized = DOMPurify.sanitize(iconSvg)
		}

	}

	get id() {
		return this._id
	}

	get name() {
		return this._name
	}

	get icon() {
		return this._icon
	}

	get iconSvg() {
		return this._iconSvgSanitized
	}

	get mount() {
		return this._mount
	}

	get setIsActive() {
		return this._setIsActive || (() => undefined)
	}

	get update() {
		return this._update
	}

	get destroy() {
		return this._destroy
	}

	get enabled() {
		return this._enabled
	}

	get scrollBottomReached() {
		return this._scrollBottomReached
	}

}