/* * Copyright 2011 gitblit.com. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gitblit.wicket.pages; import org.apache.wicket.protocol.http.WebRequest; import org.apache.wicket.protocol.http.WebResponse; import com.gitblit.models.UserModel; import com.gitblit.wicket.GitBlitWebSession; public class LogoutPage extends BasePage { public LogoutPage() { super(); GitBlitWebSession session = GitBlitWebSession.get(); UserModel user = session.getUser(); app().authentication().logout(((WebRequest) getRequest()).getHttpServletRequest(), ((WebResponse) getResponse()).getHttpServletResponse(), user); session.invalidate(); /* * Now check whether the authentication was realized via the Authorization in the header. * If so, it is likely to be cached by the browser, and cannot be undone. Effectively, this means * that you cannot log out... */ if ( ((WebRequest)getRequest()).getHttpServletRequest().getHeader("Authorization") != null ) { // authentication will be done via this route anyway, show a page to close the browser: // this will be done by Wicket. setupPage(null, getString("gb.logout")); } else { setRedirect(true); setResponsePage(getApplication().getHomePage()); } // not via WWW-Auth } // LogoutPage }ing'>Jerome-Herbinet-better-devices-wipe-action-wording Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php
blob: 1e0b13b575583ab82388f60ed7a98fb38365fadc (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace lib\AppFramework\Bootstrap;

use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\Core\Middleware\TwoFactorMiddleware;
use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class RegistrationContextTest extends TestCase {
	/** @var LoggerInterface|MockObject */
	private $logger;

	/** @var RegistrationContext */
	private $context;

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

		$this->logger = $this->createMock(LoggerInterface::class);

		$this->context = new RegistrationContext(
			$this->logger
		);
	}

	public function testRegisterCapability(): void {
		$app = $this->createMock(App::class);
		$name = 'abc';
		$container = $this->createMock(IAppContainer::class);
		$app->method('getContainer')
			->willReturn($container);
		$container->expects($this->once())
			->method('registerCapability')
			->with($name);
		$this->logger->expects($this->never())
			->method('error');

		$this->context->for('myapp')->registerCapability($name);
		$this->context->delegateCapabilityRegistrations([
			'myapp' => $app,
		]);
	}

	public function testRegisterEventListener(): void {
		$event = 'abc';
		$service = 'def';
		$dispatcher = $this->createMock(IEventDispatcher::class);
		$dispatcher->expects($this->once())
			->method('addServiceListener')
			->with($event, $service, 0);
		$this->logger->expects($this->never())
			->method('error');

		$this->context->for('myapp')->registerEventListener($event, $service);
		$this->context->delegateEventListenerRegistrations($dispatcher);
	}

	/**
	 * @dataProvider dataProvider_TrueFalse
	 */
	public function testRegisterService(bool $shared): void {
		$app = $this->createMock(App::class);
		$service = 'abc';
		$factory = function () {
			return 'def';
		};
		$container = $this->createMock(IAppContainer::class);
		$app->method('getContainer')
			->willReturn($container);
		$container->expects($this->once())
			->method('registerService')
			->with($service, $factory, $shared);
		$this->logger->expects($this->never())
			->method('error');

		$this->context->for('myapp')->registerService($service, $factory, $shared);
		$this->context->delegateContainerRegistrations([
			'myapp' => $app,
		]);
	}

	public function testRegisterServiceAlias(): void {
		$app = $this->createMock(App::class);
		$alias = 'abc';
		$target = 'def';
		$container = $this->createMock(IAppContainer::class);
		$app->method('getContainer')
			->willReturn($container);
		$container->expects($this->once())
			->method('registerAlias')
			->with($alias, $target);
		$this->logger->expects($this->never())
			->method('error');

		$this->context->for('myapp')->registerServiceAlias($alias, $target);
		$this->context->delegateContainerRegistrations([
			'myapp' => $app,
		]);
	}

	public function testRegisterParameter(): void {
		$app = $this->createMock(App::class);
		$name = 'abc';
		$value = 'def';
		$container = $this->createMock(IAppContainer::class);
		$app->method('getContainer')
			->willReturn($container);
		$container->expects($this->once())
			->method('registerParameter')
			->with($name, $value);
		$this->logger->expects($this->never())
			->method('error');

		$this->context->for('myapp')->registerParameter($name, $value);
		$this->context->delegateContainerRegistrations([
			'myapp' => $app,
		]);
	}

	public function testRegisterUserMigrator(): void {
		$appIdA = 'myapp';
		$migratorClassA = 'OCA\App\UserMigration\AppMigrator';

		$appIdB = 'otherapp';
		$migratorClassB = 'OCA\OtherApp\UserMigration\OtherAppMigrator';

		$serviceRegistrationA = new ServiceRegistration($appIdA, $migratorClassA);
		$serviceRegistrationB = new ServiceRegistration($appIdB, $migratorClassB);

		$this->context
			->for($appIdA)
			->registerUserMigrator($migratorClassA);
		$this->context
			->for($appIdB)
			->registerUserMigrator($migratorClassB);

		$this->assertEquals(
			[
				$serviceRegistrationA,
				$serviceRegistrationB,
			],
			$this->context->getUserMigrators(),
		);
	}

	public function dataProvider_TrueFalse() {
		return[
			[true],
			[false]
		];
	}

	public function testGetMiddlewareRegistrations(): void {
		$this->context->registerMiddleware('core', TwoFactorMiddleware::class, false);

		$registrations = $this->context->getMiddlewareRegistrations();

		self::assertNotEmpty($registrations);
		self::assertSame('core', $registrations[0]->getAppId());
		self::assertSame(TwoFactorMiddleware::class, $registrations[0]->getService());
	}
}