1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\Middleware; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\GuestUser; 24use Fisharebest\Webtrees\TestCase; 25use Fisharebest\Webtrees\Tree; 26use Fisharebest\Webtrees\User; 27use Psr\Http\Server\RequestHandlerInterface; 28use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 29 30use function response; 31 32/** 33 * Test the AuthManager middleware. 34 * 35 * @covers \Fisharebest\Webtrees\Http\Middleware\AuthManager 36 */ 37class AuthManagerTest extends TestCase 38{ 39 /** 40 * @return void 41 */ 42 public function testAllowed(): void 43 { 44 $handler = $this->createMock(RequestHandlerInterface::class); 45 $handler->method('handle')->willReturn(response('lorem ipsum')); 46 47 $user = $this->createMock(User::class); 48 $user->method('getPreference')->with('canadmin')->willReturn('0'); 49 50 $tree = $this->createMock(Tree::class); 51 $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('admin'); 52 53 $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 54 $middleware = new AuthManager(); 55 $response = $middleware->process($request, $handler); 56 57 $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 58 $this->assertSame('lorem ipsum', (string) $response->getBody()); 59 } 60 61 /** 62 * @return void 63 */ 64 public function testNotAllowed(): void 65 { 66 $this->expectException(AccessDeniedHttpException::class); 67 $this->expectExceptionMessage('You do not have permission to view this page.'); 68 69 $handler = $this->createMock(RequestHandlerInterface::class); 70 $handler->method('handle')->willReturn(response('lorem ipsum')); 71 72 $user = $this->createMock(User::class); 73 $user->method('getPreference')->with('canadmin')->willReturn('0'); 74 75 $tree = $this->createMock(Tree::class); 76 $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('accept'); 77 78 $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 79 $middleware = new AuthManager(); 80 81 $middleware->process($request, $handler); 82 } 83 84 /** 85 * @return void 86 */ 87 public function testNotLoggedIn(): void 88 { 89 $handler = $this->createMock(RequestHandlerInterface::class); 90 $handler->method('handle')->willReturn(response('lorem ipsum')); 91 92 $tree = $this->createMock(Tree::class); 93 94 $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser()); 95 $middleware = new AuthManager(); 96 $response = $middleware->process($request, $handler); 97 98 $this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode()); 99 } 100} 101