blob: 802f9d28b146037d46bbc7f9ddaf844c29eb6c01 [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
122$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
123 ->status_is(302)
124 ->header_is('Location' => '/');
125
126$t->get_ok('/')
127 ->status_is(200)
128 ->element_exists('div.notify-error')
129 ->text_is('div.notify-error', 'Bad CSRF token')
130 ->element_exists('input[name=handle_or_email][value=test]')
131 ->element_exists_not('div.button.top a')
132 ;
133
134$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
135 ->status_is(302)
136 ->header_is('Location' => '/');
137
138my $csrf = $t->get_ok('/')
139 ->status_is(200)
140 ->element_exists('div.notify-error')
141 ->text_is('div.notify-error', 'Bad CSRF token')
142 ->element_exists_not('div.button.top a')
143 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
144 ;
145
146$t->post_ok('/user/login' => form => {
147 handle_or_email => 'test',
148 pwd => 'ldaperr',
149 csrf_token => $csrf
150})
151 ->status_is(302)
152 ->content_is('')
153 ->header_is('Location' => '/');
154
155$csrf = $t->get_ok('/')
156 ->status_is(200)
157 ->element_exists('div.notify-error')
158 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
159 ->element_exists('input[name=handle_or_email][value=test]')
160 ->element_exists_not('div.button.top a')
161 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
162 ;
163
164$t->post_ok('/user/login' => form => {
165 handle_or_email => 'test',
166 pwd => 'unknown',
167 csrf_token => $csrf
168})
169 ->status_is(302)
170 ->content_is('')
171 ->header_is('Location' => '/');
172
173$csrf = $t->get_ok('/')
174 ->status_is(200)
175 ->element_exists('div.notify-error')
Akron8bbbecf2019-07-01 18:57:30 +0200176 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
Akron33f5c672019-06-24 19:40:47 +0200177 ->element_exists('input[name=handle_or_email][value=test]')
178 ->element_exists_not('div.button.top a')
179 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
180 ;
181
182$t->post_ok('/user/login' => form => {
183 handle_or_email => 'test',
184 pwd => 'pass',
185 csrf_token => $csrf
186})
187 ->status_is(302)
188 ->content_is('')
189 ->header_is('Location' => '/');
190
191$t->get_ok('/')
192 ->status_is(200)
193 ->element_exists_not('div.notify-error')
194 ->element_exists('div.notify-success')
195 ->text_is('div.notify-success', 'Login successful')
196 ->element_exists('aside.off')
197 ->element_exists_not('aside.active')
198 ;
199
Akron33f5c672019-06-24 19:40:47 +0200200# Now the user is logged in and should be able to
201# search with authorization
202$t->get_ok('/?q=Baum')
203 ->status_is(200)
Akron4cefe1f2019-09-04 10:11:28 +0200204 ->session_has('/auth')
205 ->session_is('/auth', 'Bearer ' . $access_token)
206 ->session_is('/auth_r', $refresh_token)
207 ->session_is('/user', 'test')
Akron33f5c672019-06-24 19:40:47 +0200208 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
209 ->text_like('#total-results', qr/\d+$/)
210 ->element_exists_not('div.notify-error')
211 ->content_like(qr/\"authorized\"\:\"yes\"/)
212 ->element_exists('div.button.top a')
213 ->element_exists('div.button.top a.logout[title~="test"]')
214 ;
215
Akron27031aa2020-04-28 14:57:10 +0200216$t->get_ok('/?q=Paum')
217 ->status_is(200)
218 ->text_like('h1 span', qr/KorAP: Find .Paum./i)
219 ->text_is('#total-results', '')
220 ->content_like(qr/\"authorized\"\:\"yes\"/)
221 ->element_exists_not('p.hint')
222 ;
223
224
Akron33f5c672019-06-24 19:40:47 +0200225# Logout
226$t->get_ok('/user/logout')
227 ->status_is(302)
Akron4cefe1f2019-09-04 10:11:28 +0200228 ->session_hasnt('/auth')
229 ->session_hasnt('/auth_r')
230 ->session_hasnt('/user')
Akron33f5c672019-06-24 19:40:47 +0200231 ->header_is('Location' => '/');
232
233$t->get_ok('/')
234 ->status_is(200)
235 ->element_exists_not('div.notify-error')
236 ->element_exists('div.notify-success')
237 ->text_is('div.notify-success', 'Logout successful')
Akron4cefe1f2019-09-04 10:11:28 +0200238 ->element_exists("input[name=handle_or_email]")
239 ->element_exists("input[name=handle_or_email][value=test]")
Akron33f5c672019-06-24 19:40:47 +0200240 ;
241
242$t->get_ok('/?q=Baum')
243 ->status_is(200)
244 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
245 ->text_like('#total-results', qr/\d+$/)
246 ->content_like(qr/\"authorized\"\:null/)
247 ;
248
Akron27031aa2020-04-28 14:57:10 +0200249$t->get_ok('/?q=Paum')
250 ->status_is(200)
251 ->text_like('h1 span', qr/KorAP: Find .Paum./i)
252 ->text_is('#total-results', '')
253 ->content_like(qr/\"authorized\"\:null/)
254 ->text_is('p.hint', 'Maybe you need to log in first?')
255 ;
256
257
Akron33f5c672019-06-24 19:40:47 +0200258# Get redirect
259my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
260 ->status_is(200)
261 ->element_exists_not('div.notify-error')
262 ->tx->res->dom->at('input[name=fwd]')->attr('value')
263 ;
264
265is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
266
267$t->post_ok('/user/login' => form => {
268 handle_or_email => 'test',
269 pwd => 'pass',
270 csrf_token => $csrf,
271 fwd => 'http://bad.example.com/test'
272})
273 ->status_is(302)
274 ->header_is('Location' => '/');
275
276$t->get_ok('/')
277 ->status_is(200)
278 ->element_exists('div.notify-error')
279 ->element_exists_not('div.notify-success')
280 ->text_is('div.notify-error', 'Redirect failure')
281 ;
282
283$t->post_ok('/user/login' => form => {
284 handle_or_email => 'test',
285 pwd => 'pass',
286 csrf_token => $csrf,
287 fwd => $fwd
288})
289 ->status_is(302)
290 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
291
Akron8bbbecf2019-07-01 18:57:30 +0200292$t->get_ok('/?q=Baum&ql=poliqarp')
293 ->status_is(200)
294 ->element_exists_not('div.notify-error')
295 ->element_exists('div.notify-success')
296 ->text_is('div.notify-success', 'Login successful')
Akroncdfd9d52019-07-23 11:35:00 +0200297 ->session_has('/auth')
298 ->session_is('/auth', 'Bearer ' . $access_token)
299 ->session_is('/auth_r', $refresh_token)
300 ->header_isnt('X-Kalamar-Cache', 'true')
Akron8bbbecf2019-07-01 18:57:30 +0200301 ;
302
Akroncdfd9d52019-07-23 11:35:00 +0200303# Expire the session
304# (makes the token be marked as expired - though it isn't serverside)
305$t->get_ok('/x/expire')
Akron8bbbecf2019-07-01 18:57:30 +0200306 ->status_is(200)
Akroncdfd9d52019-07-23 11:35:00 +0200307 ->content_is('okay')
308 ;
309
310## It may be a problem, but the cache is still valid
311$t->get_ok('/?q=Baum')
312 ->status_is(200)
313 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
314 ->text_like('#total-results', qr/\d+$/)
315 ->content_like(qr/\"authorized\"\:\"yes\"/)
316 ->header_is('X-Kalamar-Cache', 'true')
317 ;
318
319# Query without partial cache (unfortunately) (but no total results)
320$t->get_ok('/?q=baum&cutoff=true')
321 ->status_is(200)
322 ->session_is('/auth', 'Bearer ' . $access_token_2)
323 ->session_is('/auth_r', $refresh_token_2)
324 ->text_is('#error','')
325 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
326 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
327 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
328 ->content_like(qr/\"authorized\"\:\"yes\"/)
329 ->header_isnt('X-Kalamar-Cache', 'true')
330 ->content_like(qr!\"cutOff":true!)
331 ->element_exists_not('#total-results')
332 ;
333
334# Expire the session and remove the refresh option
335$t->get_ok('/x/expire-no-refresh')
336 ->status_is(200)
337 ->content_is('okay')
338 ;
339
340$t->app->defaults(no_cache => 1);
341
342
343$t->get_ok('/x/invalid-no-refresh')
344 ->status_is(200)
345 ->content_is('okay')
346 ;
347
348# Query without cache
349# The token is invalid and can't be refreshed!
350$t->get_ok('/?q=baum&cutoff=true')
Akron3c390c42020-03-30 09:06:21 +0200351 ->status_is(400)
Akroncdfd9d52019-07-23 11:35:00 +0200352 ->session_hasnt('/auth')
353 ->session_hasnt('/auth_r')
354 ->text_is('#error','')
355 ->text_is('div.notify-error','Access token invalid')
356 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
357 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
358 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
359 ->content_unlike(qr/\"authorized\"\:\"yes\"/)
360 ->header_isnt('X-Kalamar-Cache', 'true')
361 ->element_exists('p.no-results')
362 ;
363
364$t->get_ok('/x/invalid')
365 ->status_is(200)
366 ->content_is('okay')
367 ;
368
369# Query without cache
370# The token is invalid and can't be refreshed!
371$t->get_ok('/?q=baum&cutoff=true')
372 ->status_is(200)
373 ->session_is('/auth', 'Bearer ' . $access_token_2)
374 ->session_is('/auth_r', $refresh_token_2)
375 ->text_is('#error','')
376 ->element_exists_not('div.notify-error','Access token invalid')
377 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
378 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
379 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
380 ->content_like(qr/\"authorized\"\:\"yes\"/)
381 ->header_isnt('X-Kalamar-Cache', 'true')
382 ->element_exists_not('p.no-results')
Akron8bbbecf2019-07-01 18:57:30 +0200383 ;
384
Akron33f5c672019-06-24 19:40:47 +0200385
Akroncdfd9d52019-07-23 11:35:00 +0200386$t->get_ok('/x/expired-with-wrong-refresh')
387 ->status_is(200)
388 ->content_is('okay')
389 ;
Akron4796e002019-07-05 10:13:15 +0200390
Akron4796e002019-07-05 10:13:15 +0200391
Akroncdfd9d52019-07-23 11:35:00 +0200392# The token is invalid and can't be refreshed!
Akron59992122019-10-29 11:28:45 +0100393$csrf = $t->get_ok('/?q=baum&cutoff=true')
Akron3c390c42020-03-30 09:06:21 +0200394 ->status_is(400)
Akroncdfd9d52019-07-23 11:35:00 +0200395 ->session_hasnt('/auth')
396 ->session_hasnt('/auth_r')
397 ->text_is('#error','')
398 ->text_is('div.notify-error','Refresh token is expired')
399 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
400 ->content_unlike(qr/\"authorized\"\:\"yes\"/)
401 ->element_exists('p.no-results')
Akron59992122019-10-29 11:28:45 +0100402 ->tx->res->dom->at('input[name="csrf_token"]')
403 ->attr('value')
Akroncdfd9d52019-07-23 11:35:00 +0200404 ;
Akron4796e002019-07-05 10:13:15 +0200405
Akron59992122019-10-29 11:28:45 +0100406# Login:
407$t->post_ok('/user/login' => form => {
408 handle_or_email => 'test',
409 pwd => 'pass',
410 csrf_token => $csrf
411})
412 ->status_is(302)
413 ->content_is('')
414 ->header_is('Location' => '/');
415
416$t->get_ok('/')
417 ->status_is(200)
418 ->element_exists_not('div.notify-error')
419 ->element_exists('div.notify-success')
420 ->text_is('div.notify-success', 'Login successful')
421 ->element_exists('aside.off')
422 ->element_exists_not('aside.active')
423 ;
424
425$t->get_ok('/settings/oauth')
426 ->text_is('form.form-table legend', 'Register new client application')
427 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
Akron1a9d5be2020-03-19 17:28:33 +0100428 ->element_exists('ul.client-list')
429 ->element_exists_not('ul.client-list > li')
430# ->text_is('ul.client-list > li > span.client-name', 'R statistical computing tool ')
431# ->text_is('ul.client-list > li > span.client-desc', 'R is a free software environment for statistical computing and graphics.')
432# ->text_is('ul.client-list > li > span.client-url a', 'https://www.r-project.org/')
433# ->text_is('ul.client-list > li a.client-unregister', 'Unregister')
434# ->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 +0100435 ;
Akron59992122019-10-29 11:28:45 +0100436$csrf = $t->post_ok('/settings/oauth/register' => form => {
437 name => 'MyApp',
438 type => 'PUBLIC',
439 desc => 'This is my application'
440})
441 ->text_is('div.notify-error', 'Bad CSRF token')
442 ->tx->res->dom->at('input[name="csrf_token"]')
443 ->attr('value')
444 ;
445
446$t->post_ok('/settings/oauth/register' => form => {
447 name => 'MyApp',
448 type => 'CONFIDENTIAL',
449 desc => 'This is my application',
450 csrf_token => $csrf
451})
452 ->status_is(200)
453 ->element_exists('div.notify-success')
454 ->text_is('legend', 'Client Credentials')
455 ->text_is('label[for=client_id]', 'ID of the client application')
456 ->element_exists('input[name=client_id][readonly][value]')
457 ->element_exists('input[name=client_secret][readonly][value]')
458 ;
Akron4796e002019-07-05 10:13:15 +0200459
Akron1a9d5be2020-03-19 17:28:33 +0100460$t->get_ok('/settings/oauth')
461 ->text_is('form.form-table legend', 'Register new client application')
462 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
Akron17de86e2020-04-16 16:03:40 +0200463 ->text_is('ul.client-list > li > span.client-name a', 'MyApp')
Akron1a9d5be2020-03-19 17:28:33 +0100464 ->text_is('ul.client-list > li > span.client-desc', 'This is my application')
465 ->text_is('ul.client-list > li > span.client-url a', '')
Akron17de86e2020-04-16 16:03:40 +0200466 ;
467
468$t->get_ok('/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==')
469 ->status_is(200)
470 ->text_is('form ul.client-list > li.client > span.client-name', 'MyApp')
471 ->text_is('form ul.client-list > li.client > span.client-desc', 'This is my application')
472 ->text_is('a.client-unregister', 'Unregister')
473 ->attr_is('a.client-unregister', 'href', '/settings/oauth/unregister/fCBbQkA2NDA3MzM1Yw==?name=MyApp')
Akron1a9d5be2020-03-19 17:28:33 +0100474 ;
475
476$csrf = $t->get_ok('/settings/oauth/unregister/fCBbQkA2NDA3MzM1Yw==?name=MyApp')
477 ->content_like(qr!Do you really want to unregister \<span class="client-name"\>MyApp\<\/span\>?!)
478 ->attr_is('form.form-table input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==')
479 ->attr_is('form.form-table input[name=client-name]', 'value', 'MyApp')
480 ->tx->res->dom->at('input[name="csrf_token"]')
481 ->attr('value')
482 ;
483
484$t->post_ok('/settings/oauth/unregister' => form => {
485 'client-name' => 'MyApp',
486 'client-id' => 'xxxx==',
487 'csrf_token' => $csrf
488})->status_is(302)
489 ->content_is('')
490 ->header_is('Location' => '/settings/oauth')
491 ;
492
493$t->get_ok('/settings/oauth')
494 ->text_is('form.form-table legend', 'Register new client application')
495 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
496 ->element_exists('ul.client-list > li')
497 ->text_is('div.notify', 'Unknown client with xxxx==.')
498 ;
499
500$t->post_ok('/settings/oauth/unregister' => form => {
501 'client-name' => 'MyApp',
502 'client-id' => 'fCBbQkA2NDA3MzM1Yw==',
503 'csrf_token' => $csrf
504})->status_is(302)
505 ->content_is('')
506 ->header_is('Location' => '/settings/oauth')
507 ;
508
509$t->get_ok('/settings/oauth')
510 ->text_is('form.form-table legend', 'Register new client application')
511 ->attr_is('form.oauth-register','action', '/settings/oauth/register')
512 ->element_exists_not('ul.client-list > li')
513 ->text_is('div.notify-success', 'Successfully deleted MyApp')
514 ;
515
Akron33f5c672019-06-24 19:40:47 +0200516done_testing;
517__END__