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