aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-18 17:50:11 +0100
committerArthurHoaro <arthur@hoa.ro>2020-01-26 11:34:14 +0100
commit6c50a6ccceecf54850e62c312ab2397b84d89ab4 (patch)
tree3205aa447930eab020ea04bf00082880abec5001 /tests/front
parent1410dce2db310e71b5e683b0871c2f28d8807844 (diff)
downloadShaarli-6c50a6ccceecf54850e62c312ab2397b84d89ab4.tar.gz
Shaarli-6c50a6ccceecf54850e62c312ab2397b84d89ab4.tar.zst
Shaarli-6c50a6ccceecf54850e62c312ab2397b84d89ab4.zip
Render login page through Slim controller
Diffstat (limited to 'tests/front')
-rw-r--r--tests/front/ShaarliMiddlewareTest.php70
-rw-r--r--tests/front/controller/LoginControllerTest.php173
2 files changed, 243 insertions, 0 deletions
diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php
new file mode 100644
index 00000000..80974f37
--- /dev/null
+++ b/tests/front/ShaarliMiddlewareTest.php
@@ -0,0 +1,70 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Container\ShaarliContainer;
10use Shaarli\Front\Exception\LoginBannedException;
11use Shaarli\Render\PageBuilder;
12use Slim\Http\Request;
13use Slim\Http\Response;
14
15class ShaarliMiddlewareTest extends TestCase
16{
17 /** @var ShaarliContainer */
18 protected $container;
19
20 /** @var ShaarliMiddleware */
21 protected $middleware;
22
23 public function setUp(): void
24 {
25 $this->container = $this->createMock(ShaarliContainer::class);
26 $this->middleware = new ShaarliMiddleware($this->container);
27 }
28
29 public function testMiddlewareExecution(): void
30 {
31 $request = $this->createMock(Request::class);
32 $response = new Response();
33 $controller = function (Request $request, Response $response): Response {
34 return $response->withStatus(418); // I'm a tea pot
35 };
36
37 /** @var Response $result */
38 $result = $this->middleware->__invoke($request, $response, $controller);
39
40 static::assertInstanceOf(Response::class, $result);
41 static::assertSame(418, $result->getStatusCode());
42 }
43
44 public function testMiddlewareExecutionWithException(): void
45 {
46 $request = $this->createMock(Request::class);
47 $response = new Response();
48 $controller = function (): void {
49 $exception = new LoginBannedException();
50
51 throw new $exception;
52 };
53
54 $pageBuilder = $this->createMock(PageBuilder::class);
55 $pageBuilder->method('render')->willReturnCallback(function (string $message): string {
56 return $message;
57 });
58 $this->container->pageBuilder = $pageBuilder;
59
60 $conf = $this->createMock(ConfigManager::class);
61 $this->container->conf = $conf;
62
63 /** @var Response $result */
64 $result = $this->middleware->__invoke($request, $response, $controller);
65
66 static::assertInstanceOf(Response::class, $result);
67 static::assertSame(401, $result->getStatusCode());
68 static::assertContains('error', (string) $result->getBody());
69 }
70}
diff --git a/tests/front/controller/LoginControllerTest.php b/tests/front/controller/LoginControllerTest.php
new file mode 100644
index 00000000..ddcfe154
--- /dev/null
+++ b/tests/front/controller/LoginControllerTest.php
@@ -0,0 +1,173 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Container\ShaarliContainer;
10use Shaarli\Front\Exception\LoginBannedException;
11use Shaarli\Render\PageBuilder;
12use Shaarli\Security\LoginManager;
13use Slim\Http\Request;
14use Slim\Http\Response;
15
16class LoginControllerTest extends TestCase
17{
18 /** @var ShaarliContainer */
19 protected $container;
20
21 /** @var LoginController */
22 protected $controller;
23
24 public function setUp(): void
25 {
26 $this->container = $this->createMock(ShaarliContainer::class);
27 $this->controller = new LoginController($this->container);
28 }
29
30 public function testValidControllerInvoke(): void
31 {
32 $this->createValidContainerMockSet();
33
34 $request = $this->createMock(Request::class);
35 $request->expects(static::once())->method('getServerParam')->willReturn('> referer');
36 $response = new Response();
37
38 $assignedVariables = [];
39 $this->container->pageBuilder
40 ->expects(static::exactly(3))
41 ->method('assign')
42 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
43 $assignedVariables[$key] = $value;
44
45 return $this;
46 })
47 ;
48
49 $result = $this->controller->index($request, $response);
50
51 static::assertInstanceOf(Response::class, $result);
52 static::assertSame(200, $result->getStatusCode());
53 static::assertSame('loginform', (string) $result->getBody());
54
55 static::assertSame('&gt; referer', $assignedVariables['returnurl']);
56 static::assertSame(true, $assignedVariables['remember_user_default']);
57 static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
58 }
59
60 public function testValidControllerInvokeWithUserName(): void
61 {
62 $this->createValidContainerMockSet();
63
64 $request = $this->createMock(Request::class);
65 $request->expects(static::once())->method('getServerParam')->willReturn('> referer');
66 $request->expects(static::exactly(2))->method('getParam')->willReturn('myUser>');
67 $response = new Response();
68
69 $assignedVariables = [];
70 $this->container->pageBuilder
71 ->expects(static::exactly(4))
72 ->method('assign')
73 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
74 $assignedVariables[$key] = $value;
75
76 return $this;
77 })
78 ;
79
80 $result = $this->controller->index($request, $response);
81
82 static::assertInstanceOf(Response::class, $result);
83 static::assertSame(200, $result->getStatusCode());
84 static::assertSame('loginform', (string) $result->getBody());
85
86 static::assertSame('myUser&gt;', $assignedVariables['username']);
87 static::assertSame('&gt; referer', $assignedVariables['returnurl']);
88 static::assertSame(true, $assignedVariables['remember_user_default']);
89 static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
90 }
91
92 public function testLoginControllerWhileLoggedIn(): void
93 {
94 $request = $this->createMock(Request::class);
95 $response = new Response();
96
97 $loginManager = $this->createMock(LoginManager::class);
98 $loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
99 $this->container->loginManager = $loginManager;
100
101 $result = $this->controller->index($request, $response);
102
103 static::assertInstanceOf(Response::class, $result);
104 static::assertSame(302, $result->getStatusCode());
105 static::assertSame(['./'], $result->getHeader('Location'));
106 }
107
108 public function testLoginControllerOpenShaarli(): void
109 {
110 $this->createValidContainerMockSet();
111
112 $request = $this->createMock(Request::class);
113 $response = new Response();
114
115 $conf = $this->createMock(ConfigManager::class);
116 $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
117 if ($parameter === 'security.open_shaarli') {
118 return true;
119 }
120 return $default;
121 });
122 $this->container->conf = $conf;
123
124 $result = $this->controller->index($request, $response);
125
126 static::assertInstanceOf(Response::class, $result);
127 static::assertSame(302, $result->getStatusCode());
128 static::assertSame(['./'], $result->getHeader('Location'));
129 }
130
131 public function testLoginControllerWhileBanned(): void
132 {
133 $this->createValidContainerMockSet();
134
135 $request = $this->createMock(Request::class);
136 $response = new Response();
137
138 $loginManager = $this->createMock(LoginManager::class);
139 $loginManager->method('isLoggedIn')->willReturn(false);
140 $loginManager->method('canLogin')->willReturn(false);
141 $this->container->loginManager = $loginManager;
142
143 $this->expectException(LoginBannedException::class);
144
145 $this->controller->index($request, $response);
146 }
147
148 protected function createValidContainerMockSet(): void
149 {
150 // User logged out
151 $loginManager = $this->createMock(LoginManager::class);
152 $loginManager->method('isLoggedIn')->willReturn(false);
153 $loginManager->method('canLogin')->willReturn(true);
154 $this->container->loginManager = $loginManager;
155
156 // Config
157 $conf = $this->createMock(ConfigManager::class);
158 $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
159 return $default;
160 });
161 $this->container->conf = $conf;
162
163 // PageBuilder
164 $pageBuilder = $this->createMock(PageBuilder::class);
165 $pageBuilder
166 ->method('render')
167 ->willReturnCallback(function (string $template): string {
168 return $template;
169 })
170 ;
171 $this->container->pageBuilder = $pageBuilder;
172 }
173}