blob: e2ff229565d4be76ec1da0cc7f19462a965ec33e [file] [log] [blame]
Akron33f5c672019-06-24 19:40:47 +02001use Mojo::Base -strict;
2use Test::More;
Akroncdfd9d52019-07-23 11:35:00 +02003use Test::Mojo::WithRoles 'Session';
Akron33f5c672019-06-24 19:40:47 +02004use Mojo::File qw/path/;
5use Data::Dumper;
6
7
8#####################
9# Start Fake server #
10#####################
Akron63d963b2019-07-05 15:35:51 +020011my $mount_point = '/realapi/';
Akron33f5c672019-06-24 19:40:47 +020012$ENV{KALAMAR_API} = $mount_point;
13
Akroncdfd9d52019-07-23 11:35:00 +020014my $t = Test::Mojo::WithRoles->new('Kalamar' => {
Akron33f5c672019-06-24 19:40:47 +020015 Kalamar => {
16 plugins => ['Auth']
17 },
18 'Kalamar-Auth' => {
19 client_id => 2,
20 client_secret => 'k414m4r-s3cr3t',
Akron59992122019-10-29 11:28:45 +010021 oauth2 => 1,
22 experimental_client_registration => 1
Akron33f5c672019-06-24 19:40:47 +020023 }
24});
25
26# Mount fake backend
27# Get the fixture path
28my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server');
29my $fake_backend = $t->app->plugin(
30 Mount => {
31 $mount_point =>
32 $fixtures_path->child('mock.pl')
33 }
34);
35# Configure fake backend
Akroncdfd9d52019-07-23 11:35:00 +020036my $fake_backend_app = $fake_backend->pattern->defaults->{app};
37
38# Set general app logger for simplicity
39$fake_backend_app->log($t->app->log);
40
41my $access_token = $fake_backend_app->get_token('access_token');
42my $refresh_token = $fake_backend_app->get_token('refresh_token');
43my $access_token_2 = $fake_backend_app->get_token('access_token_2');
44my $refresh_token_2 = $fake_backend_app->get_token('refresh_token_2');
45
46# Some routes to modify the session
47# This expires the session
48$t->app->routes->get('/x/expire')->to(
49 cb => sub {
50 my $c = shift;
51 $c->session(auth_exp => 0);
52 return $c->render(text => 'okay')
53 }
54);
55
56# This expires the session and removes the refresh token
57$t->app->routes->get('/x/expire-no-refresh')->to(
58 cb => sub {
59 my $c = shift;
60 $c->session(auth_exp => 0);
61 delete $c->session->{auth_r};
62 return $c->render(text => 'okay')
63 }
64);
65
66# This sets an invalid token
67$t->app->routes->get('/x/invalid')->to(
68 cb => sub {
69 my $c = shift;
70 $c->session(auth_exp => time + 1000);
71 $c->session(auth_r => $refresh_token_2);
72 $c->session(auth => 'Bearer inv4lid');
73 return $c->render(text => 'okay')
74 }
75);
76
77
78# This sets an invalid token
79$t->app->routes->get('/x/invalid-no-refresh')->to(
80 cb => sub {
81 my $c = shift;
82 $c->session(auth_exp => time + 1000);
83 delete $c->session->{auth_r};
84 $c->session(auth => 'Bearer inv4lid');
85 return $c->render(text => 'okay')
86 }
87);
88
89# This sets an invalid refresh token
90$t->app->routes->get('/x/expired-with-wrong-refresh')->to(
91 cb => sub {
92 my $c = shift;
93 $c->session(auth_exp => 0);
94 $c->session(auth => 'Bearer inv4lid');
95 $c->session(auth_r => 'inv4lid');
96 return $c->render(text => 'okay')
97 }
98);
99
Akron33f5c672019-06-24 19:40:47 +0200100
Akron63d963b2019-07-05 15:35:51 +0200101$t->get_ok('/realapi/v1.0')
Akron33f5c672019-06-24 19:40:47 +0200102 ->status_is(200)
103 ->content_is('Fake server available');
104
105$t->get_ok('/?q=Baum')
106 ->status_is(200)
107 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
108 ->text_like('#total-results', qr/\d+$/)
109 ->content_like(qr/\"authorized\"\:null/)
110 ->element_exists_not('div.button.top a')
111 ->element_exists_not('aside.active')
112 ->element_exists_not('aside.off')
113 ;
114
115$t->get_ok('/')
116 ->status_is(200)
117 ->element_exists('form[action=/user/login] input[name=handle_or_email]')
118 ->element_exists('aside.active')
119 ->element_exists_not('aside.off')
120 ;
121
Akron3e0fdc12020-05-15 16:17:21 +0200122# Test for bug with long password
123$t->post_ok('/user/login' => form => {
124 handle_or_email => 'test',
125 pwd => 'kjskjhndkjndqknaskjnakjdnkjdankajdnkjdsankjdsakjdfkjahzroiuqzriudjoijdmlamdlkmdsalkmdl' })
126 ->status_is(302)
127 ->header_is('Location' => '/');
128
Akron33f5c672019-06-24 19:40:47 +0200129$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
130 ->status_is(302)
131 ->header_is('Location' => '/');
132
133$t->get_ok('/')
134 ->status_is(200)
135 ->element_exists('div.notify-error')
136 ->text_is('div.notify-error', 'Bad CSRF token')
137 ->element_exists('input[name=handle_or_email][value=test]')
138 ->element_exists_not('div.button.top a')
139 ;
140
141$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
142 ->status_is(302)
143 ->header_is('Location' => '/');
144
145my $csrf = $t->get_ok('/')
146 ->status_is(200)
147 ->element_exists('div.notify-error')
148 ->text_is('div.notify-error', 'Bad CSRF token')
149 ->element_exists_not('div.button.top a')
150 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
151 ;
152
153$t->post_ok('/user/login' => form => {
154 handle_or_email => 'test',
155 pwd => 'ldaperr',
156 csrf_token => $csrf
157})
158 ->status_is(302)
159 ->content_is('')
160 ->header_is('Location' => '/');
161
162$csrf = $t->get_ok('/')
163 ->status_is(200)
164 ->element_exists('div.notify-error')
165 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
166 ->element_exists('input[name=handle_or_email][value=test]')
167 ->element_exists_not('div.button.top a')
168 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
169 ;
170
171$t->post_ok('/user/login' => form => {
172 handle_or_email => 'test',
173 pwd => 'unknown',
174 csrf_token => $csrf
175})
176 ->status_is(302)
177 ->content_is('')
178 ->header_is('Location' => '/');
179
180$csrf = $t->get_ok('/')
181 ->status_is(200)
182 ->element_exists('div.notify-error')
Akron8bbbecf2019-07-01 18:57:30 +0200183 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
Akron33f5c672019-06-24 19:40:47 +0200184 ->element_exists('input[name=handle_or_email][value=test]')
185 ->element_exists_not('div.button.top a')
186 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
187 ;
188
189$t->post_ok('/user/login' => form => {
190 handle_or_email => 'test',
191 pwd => 'pass',
192 csrf_token => $csrf
193})
194 ->status_is(302)
195 ->content_is('')
196 ->header_is('Location' => '/');
197
198$t->get_ok('/')
199 ->status_is(200)
200 ->element_exists_not('div.notify-error')
201 ->element_exists('div.notify-success')
202 ->text_is('div.notify-success', 'Login successful')
203 ->element_exists('aside.off')
204 ->element_exists_not('aside.active')
205 ;
206
Akron33f5c672019-06-24 19:40:47 +0200207# Now the user is logged in and should be able to
208# search with authorization
209$t->get_ok('/?q=Baum')
210 ->status_is(200)
Akron4cefe1f2019-09-04 10:11:28 +0200211 ->session_has('/auth')
212 ->session_is('/auth', 'Bearer ' . $access_token)
213 ->session_is('/auth_r', $refresh_token)
214 ->session_is('/user', 'test')
Akron33f5c672019-06-24 19:40:47 +0200215 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
216 ->text_like('#total-results', qr/\d+$/)
217 ->element_exists_not('div.notify-error')
218 ->content_like(qr/\"authorized\"\:\"yes\"/)
219 ->element_exists('div.button.top a')
220 ->element_exists('div.button.top a.logout[title~="test"]')
221 ;
222
Akron27031aa2020-04-28 14:57:10 +0200223$t->get_ok('/?q=Paum')
224 ->status_is(200)
225 ->text_like('h1 span', qr/KorAP: Find .Paum./i)
226 ->text_is('#total-results', '')
227 ->content_like(qr/\"authorized\"\:\"yes\"/)
228 ->element_exists_not('p.hint')
229 ;
230
231
Akron33f5c672019-06-24 19:40:47 +0200232# Logout
233$t->get_ok('/user/logout')
234 ->status_is(302)
Akron4cefe1f2019-09-04 10:11:28 +0200235 ->session_hasnt('/auth')
236 ->session_hasnt('/auth_r')
237 ->session_hasnt('/user')
Akron33f5c672019-06-24 19:40:47 +0200238 ->header_is('Location' => '/');
239
240$t->get_ok('/')
241 ->status_is(200)
242 ->element_exists_not('div.notify-error')
243 ->element_exists('div.notify-success')
244 ->text_is('div.notify-success', 'Logout successful')
Akron4cefe1f2019-09-04 10:11:28 +0200245 ->element_exists("input[name=handle_or_email]")
246 ->element_exists("input[name=handle_or_email][value=test]")
Akron33f5c672019-06-24 19:40:47 +0200247 ;
248
249$t->get_ok('/?q=Baum')
250 ->status_is(200)
251 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
252 ->text_like('#total-results', qr/\d+$/)
253 ->content_like(qr/\"authorized\"\:null/)
254 ;
255
Akron27031aa2020-04-28 14:57:10 +0200256$t->get_ok('/?q=Paum')
257 ->status_is(200)
258 ->text_like('h1 span', qr/KorAP: Find .Paum./i)
259 ->text_is('#total-results', '')
260 ->content_like(qr/\"authorized\"\:null/)
261 ->text_is('p.hint', 'Maybe you need to log in first?')
262 ;
263
264
Akron33f5c672019-06-24 19:40:47 +0200265# Get redirect
266my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
267 ->status_is(200)
268 ->element_exists_not('div.notify-error')
269 ->tx->res->dom->at('input[name=fwd]')->attr('value')
270 ;
271
272is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
273
274$t->post_ok('/user/login' => form => {
275 handle_or_email => 'test',
276 pwd => 'pass',
277 csrf_token => $csrf,
278 fwd => 'http://bad.example.com/test'
279})
280 ->status_is(302)
281 ->header_is('Location' => '/');
282
283$t->get_ok('/')
284 ->status_is(200)
285 ->element_exists('div.notify-error')
286 ->element_exists_not('div.notify-success')
287 ->text_is('div.notify-error', 'Redirect failure')
288 ;
289
290$t->post_ok('/user/login' => form => {
291 handle_or_email => 'test',
292 pwd => 'pass',
293 csrf_token => $csrf,
294 fwd => $fwd
295})
296 ->status_is(302)
297 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
298
Akron8bbbecf2019-07-01 18:57:30 +0200299$t->get_ok('/?q=Baum&ql=poliqarp')
300 ->status_is(200)
301 ->element_exists_not('div.notify-error')
302 ->element_exists('div.notify-success')
303 ->text_is('div.notify-success', 'Login successful')
Akroncdfd9d52019-07-23 11:35:00 +0200304 ->session_has('/auth')
305 ->session_is('/auth', 'Bearer ' . $access_token)
306 ->session_is('/auth_r', $refresh_token)
307 ->header_isnt('X-Kalamar-Cache', 'true')
Akron8bbbecf2019-07-01 18:57:30 +0200308 ;
309
Akroncdfd9d52019-07-23 11:35:00 +0200310# Expire the session
311# (makes the token be marked as expired - though it isn't serverside)
312$t->get_ok('/x/expire')
Akron8bbbecf2019-07-01 18:57:30 +0200313 ->status_is(200)
Akroncdfd9d52019-07-23 11:35:00 +0200314 ->content_is('okay')
315 ;
316
317## It may be a problem, but the cache is still valid
318$t->get_ok('/?q=Baum')
319 ->status_is(200)
320 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
321 ->text_like('#total-results', qr/\d+$/)
322 ->content_like(qr/\"authorized\"\:\"yes\"/)
323 ->header_is('X-Kalamar-Cache', 'true')
324 ;
325
326# Query without partial cache (unfortunately) (but no total results)
327$t->get_ok('/?q=baum&cutoff=true')
328 ->status_is(200)
329 ->session_is('/auth', 'Bearer ' . $access_token_2)
330 ->session_is('/auth_r', $refresh_token_2)
331 ->text_is('#error','')
332 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
333 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
334 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
335 ->content_like(qr/\"authorized\"\:\"yes\"/)
336 ->header_isnt('X-Kalamar-Cache', 'true')
337 ->content_like(qr!\"cutOff":true!)
338 ->element_exists_not('#total-results')
339 ;
340
341# Expire the session and remove the refresh option
342$t->get_ok('/x/expire-no-refresh')
343 ->status_is(200)
344 ->content_is('okay')
345 ;
346
347$t->app->defaults(no_cache => 1);
348
349
350$t->get_ok('/x/invalid-no-refresh')
351 ->status_is(200)
352 ->content_is('okay')
353 ;
354
355# Query without cache
356# The token is invalid and can't be refreshed!
357$t->get_ok('/?q=baum&cutoff=true')
Akron3c390c42020-03-30 09:06:21 +0200358 ->status_is(400)
Akroncdfd9d52019-07-23 11:35:00 +0200359 ->session_hasnt('/auth')
360 ->session_hasnt('/auth_r')
361 ->text_is('#error','')
362 ->text_is('div.notify-error','Access token invalid')
363 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
364 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
365 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
366 ->content_unlike(qr/\"authorized\"\:\"yes\"/)
367 ->header_isnt('X-Kalamar-Cache', 'true')
368 ->element_exists('p.no-results')
369 ;
370
371$t->get_ok('/x/invalid')
372 ->status_is(200)
373 ->content_is('okay')
374 ;
375
376# Query without cache
377# The token is invalid and can't be refreshed!
378$t->get_ok('/?q=baum&cutoff=true')
379 ->status_is(200)
380 ->session_is('/auth', 'Bearer ' . $access_token_2)
381 ->session_is('/auth_r', $refresh_token_2)
382 ->text_is('#error','')
383 ->element_exists_not('div.notify-error','Access token invalid')
384 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
385 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
386 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
387 ->content_like(qr/\"authorized\"\:\"yes\"/)
388 ->header_isnt('X-Kalamar-Cache', 'true')
389 ->element_exists_not('p.no-results')
Akron8bbbecf2019-07-01 18:57:30 +0200390 ;
391
Akron33f5c672019-06-24 19:40:47 +0200392
Akroncdfd9d52019-07-23 11:35:00 +0200393$t->get_ok('/x/expired-with-wrong-refresh')
394 ->status_is(200)
395 ->content_is('okay')
396 ;
Akron4796e002019-07-05 10:13:15 +0200397
Akron4796e002019-07-05 10:13:15 +0200398
Akroncdfd9d52019-07-23 11:35:00 +0200399# The token is invalid and can't be refreshed!
Akron59992122019-10-29 11:28:45 +0100400$csrf = $t->get_ok('/?q=baum&cutoff=true')
Akron3c390c42020-03-30 09:06:21 +0200401 ->status_is(400)
Akroncdfd9d52019-07-23 11:35:00 +0200402 ->session_hasnt('/auth')
403 ->session_hasnt('/auth_r')
404 ->text_is('#error','')
405 ->text_is('div.notify-error','Refresh token is expired')
406 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
407 ->content_unlike(qr/\"authorized\"\:\"yes\"/)
408 ->element_exists('p.no-results')
Akron59992122019-10-29 11:28:45 +0100409 ->tx->res->dom->at('input[name="csrf_token"]')
410 ->attr('value')
Akroncdfd9d52019-07-23 11:35:00 +0200411 ;
Akron4796e002019-07-05 10:13:15 +0200412
Akron59992122019-10-29 11:28:45 +0100413# Login:
414$t->post_ok('/user/login' => form => {
415 handle_or_email => 'test',
416 pwd => 'pass',
417 csrf_token => $csrf
418})
419 ->status_is(302)
420 ->content_is('')
421 ->header_is('Location' => '/');
422
423$t->get_ok('/')
424 ->status_is(200)
425 ->element_exists_not('div.notify-error')
426 ->element_exists('div.notify-success')
427 ->text_is('div.notify-success', 'Login successful')
428 ->element_exists('aside.off')
429 ->element_exists_not('aside.active')
430 ;
431
432$t->get_ok('/settings/oauth')
433 ->text_is('form.form-table legend', 'Register new client application')
434 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
Akron1a9d5be2020-03-19 17:28:33 +0100435 ->element_exists('ul.client-list')
436 ->element_exists_not('ul.client-list > li')
437# ->text_is('ul.client-list > li > span.client-name', 'R statistical computing tool ')
438# ->text_is('ul.client-list > li > span.client-desc', 'R is a free software environment for statistical computing and graphics.')
439# ->text_is('ul.client-list > li > span.client-url a', 'https://www.r-project.org/')
440# ->text_is('ul.client-list > li a.client-unregister', 'Unregister')
441# ->attr_is('ul.client-list > li a.client-unregister', 'href', '/settings/oauth/unregister/9aHsGW6QflV13ixNpez?name=R+statistical+computing+tool')
Akron59992122019-10-29 11:28:45 +0100442 ;
Akron59992122019-10-29 11:28:45 +0100443$csrf = $t->post_ok('/settings/oauth/register' => form => {
444 name => 'MyApp',
445 type => 'PUBLIC',
446 desc => 'This is my application'
447})
448 ->text_is('div.notify-error', 'Bad CSRF token')
449 ->tx->res->dom->at('input[name="csrf_token"]')
450 ->attr('value')
451 ;
452
453$t->post_ok('/settings/oauth/register' => form => {
454 name => 'MyApp',
455 type => 'CONFIDENTIAL',
456 desc => 'This is my application',
457 csrf_token => $csrf
458})
459 ->status_is(200)
460 ->element_exists('div.notify-success')
461 ->text_is('legend', 'Client Credentials')
462 ->text_is('label[for=client_id]', 'ID of the client application')
463 ->element_exists('input[name=client_id][readonly][value]')
464 ->element_exists('input[name=client_secret][readonly][value]')
465 ;
Akron4796e002019-07-05 10:13:15 +0200466
Akron1a9d5be2020-03-19 17:28:33 +0100467$t->get_ok('/settings/oauth')
468 ->text_is('form.form-table legend', 'Register new client application')
469 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
Akron17de86e2020-04-16 16:03:40 +0200470 ->text_is('ul.client-list > li > span.client-name a', 'MyApp')
Akron1a9d5be2020-03-19 17:28:33 +0100471 ->text_is('ul.client-list > li > span.client-desc', 'This is my application')
472 ->text_is('ul.client-list > li > span.client-url a', '')
Akron17de86e2020-04-16 16:03:40 +0200473 ;
474
475$t->get_ok('/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==')
476 ->status_is(200)
477 ->text_is('form ul.client-list > li.client > span.client-name', 'MyApp')
478 ->text_is('form ul.client-list > li.client > span.client-desc', 'This is my application')
479 ->text_is('a.client-unregister', 'Unregister')
480 ->attr_is('a.client-unregister', 'href', '/settings/oauth/unregister/fCBbQkA2NDA3MzM1Yw==?name=MyApp')
Akron1a9d5be2020-03-19 17:28:33 +0100481 ;
482
483$csrf = $t->get_ok('/settings/oauth/unregister/fCBbQkA2NDA3MzM1Yw==?name=MyApp')
484 ->content_like(qr!Do you really want to unregister \<span class="client-name"\>MyApp\<\/span\>?!)
485 ->attr_is('form.form-table input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==')
486 ->attr_is('form.form-table input[name=client-name]', 'value', 'MyApp')
487 ->tx->res->dom->at('input[name="csrf_token"]')
488 ->attr('value')
489 ;
490
491$t->post_ok('/settings/oauth/unregister' => form => {
492 'client-name' => 'MyApp',
493 'client-id' => 'xxxx==',
494 'csrf_token' => $csrf
495})->status_is(302)
496 ->content_is('')
497 ->header_is('Location' => '/settings/oauth')
498 ;
499
500$t->get_ok('/settings/oauth')
501 ->text_is('form.form-table legend', 'Register new client application')
502 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
503 ->element_exists('ul.client-list > li')
504 ->text_is('div.notify', 'Unknown client with xxxx==.')
505 ;
506
507$t->post_ok('/settings/oauth/unregister' => form => {
508 'client-name' => 'MyApp',
509 'client-id' => 'fCBbQkA2NDA3MzM1Yw==',
510 'csrf_token' => $csrf
511})->status_is(302)
512 ->content_is('')
513 ->header_is('Location' => '/settings/oauth')
514 ;
515
516$t->get_ok('/settings/oauth')
517 ->text_is('form.form-table legend', 'Register new client application')
518 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
519 ->element_exists_not('ul.client-list > li')
520 ->text_is('div.notify-success', 'Successfully deleted MyApp')
521 ;
522
Akron33f5c672019-06-24 19:40:47 +0200523done_testing;
524__END__