blob: f1349a6169957b39c44ae58c2fc5dfa13ee9c984 [file] [log] [blame]
Akron33f5c672019-06-24 19:40:47 +02001use Mojo::Base -strict;
2use Test::More;
Akron6a228db2021-10-14 15:57:00 +02003use Mojo::ByteStream 'b';
Akroncdfd9d52019-07-23 11:35:00 +02004use Test::Mojo::WithRoles 'Session';
Akron66ef3b52022-11-22 14:25:15 +01005use Mojo::File qw/path tempfile/;
Akron33f5c672019-06-24 19:40:47 +02006use Data::Dumper;
7
Akron33f5c672019-06-24 19:40:47 +02008#####################
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
Akronbc6b3f22021-01-13 14:53:12 +0100100my $q = qr!(?:\"|")!;
Akron33f5c672019-06-24 19:40:47 +0200101
Akron63d963b2019-07-05 15:35:51 +0200102$t->get_ok('/realapi/v1.0')
Akron33f5c672019-06-24 19:40:47 +0200103 ->status_is(200)
104 ->content_is('Fake server available');
105
106$t->get_ok('/?q=Baum')
107 ->status_is(200)
108 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
109 ->text_like('#total-results', qr/\d+$/)
Akronbc6b3f22021-01-13 14:53:12 +0100110 ->content_like(qr/${q}authorized${q}:null/)
Akron33f5c672019-06-24 19:40:47 +0200111 ->element_exists_not('div.button.top a')
112 ->element_exists_not('aside.active')
Akron4d17d0f2025-03-05 20:58:44 +0100113 ->element_exists('aside.off')
Akron33f5c672019-06-24 19:40:47 +0200114 ;
115
116$t->get_ok('/')
117 ->status_is(200)
Akron9fa7cc52022-05-12 11:17:20 +0200118 ->element_exists('form[action=/user/login] input[name=handle_or_email]')
Akronbb3da4d2025-12-05 22:47:35 +0100119 ->element_exists('*[data-testid=handle-or-email]')
120 ->element_exists('*[data-testid=pwd]')
121 ->element_exists('*[data-testid=login-submit]')
122 ->element_exists_not('*[data-testid=logout]')
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200123 ->element_exists('aside')
124 ->element_exists('aside.invisible')
Akron33f5c672019-06-24 19:40:47 +0200125 ;
126
Akronff088112021-06-15 15:26:04 +0200127$t->get_ok('/settings/oauth')
128 ->status_is(401)
129 ->text_is('p.no-results', 'Not authenticated')
Akronbb3da4d2025-12-05 22:47:35 +0100130 ->text_is('*[data-testid=error-message]','Not authenticated')
Akronff088112021-06-15 15:26:04 +0200131 ;
132
Helge278fbca2022-11-29 18:49:15 +0100133$t->get_ok('/settings/marketplace')
134 ->status_is(401)
135 ->text_is('p.no-results', 'Not authenticated')
136 ;
137
Akron3e0fdc12020-05-15 16:17:21 +0200138# Test for bug with long password
139$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200140 handle_or_email => 'test',
Akron3e0fdc12020-05-15 16:17:21 +0200141 pwd => 'kjskjhndkjndqknaskjnakjdnkjdankajdnkjdsankjdsakjdfkjahzroiuqzriudjoijdmlamdlkmdsalkmdl' })
142 ->status_is(302)
143 ->header_is('Location' => '/');
144
Akron9fa7cc52022-05-12 11:17:20 +0200145$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
Akron33f5c672019-06-24 19:40:47 +0200146 ->status_is(302)
147 ->header_is('Location' => '/');
148
149$t->get_ok('/')
150 ->status_is(200)
151 ->element_exists('div.notify-error')
152 ->text_is('div.notify-error', 'Bad CSRF token')
Akron9fa7cc52022-05-12 11:17:20 +0200153 ->element_exists('input[name=handle_or_email][value=test]')
Akron33f5c672019-06-24 19:40:47 +0200154 ->element_exists_not('div.button.top a')
155 ;
156
Akron9fa7cc52022-05-12 11:17:20 +0200157$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
Akron33f5c672019-06-24 19:40:47 +0200158 ->status_is(302)
159 ->header_is('Location' => '/');
160
161my $csrf = $t->get_ok('/')
162 ->status_is(200)
163 ->element_exists('div.notify-error')
164 ->text_is('div.notify-error', 'Bad CSRF token')
165 ->element_exists_not('div.button.top a')
166 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
167 ;
168
169$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200170 handle_or_email => 'test',
Akron33f5c672019-06-24 19:40:47 +0200171 pwd => 'ldaperr',
172 csrf_token => $csrf
173})
174 ->status_is(302)
175 ->content_is('')
176 ->header_is('Location' => '/');
177
178$csrf = $t->get_ok('/')
179 ->status_is(200)
180 ->element_exists('div.notify-error')
181 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
Akron9fa7cc52022-05-12 11:17:20 +0200182 ->element_exists('input[name=handle_or_email][value=test]')
Akron33f5c672019-06-24 19:40:47 +0200183 ->element_exists_not('div.button.top a')
Akron3b3c7af2020-05-15 16:23:55 +0200184 ->element_exists_not('div.notify-success')
Akron33f5c672019-06-24 19:40:47 +0200185 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
186 ;
187
188$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200189 handle_or_email => 'test',
Akron33f5c672019-06-24 19:40:47 +0200190 pwd => 'unknown',
191 csrf_token => $csrf
192})
193 ->status_is(302)
194 ->content_is('')
195 ->header_is('Location' => '/');
196
197$csrf = $t->get_ok('/')
198 ->status_is(200)
199 ->element_exists('div.notify-error')
Akron8bbbecf2019-07-01 18:57:30 +0200200 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
Akron9fa7cc52022-05-12 11:17:20 +0200201 ->element_exists('input[name=handle_or_email][value=test]')
Akron33f5c672019-06-24 19:40:47 +0200202 ->element_exists_not('div.button.top a')
203 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
204 ;
205
206$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200207 handle_or_email => 'test',
Akron33f5c672019-06-24 19:40:47 +0200208 pwd => 'pass',
209 csrf_token => $csrf
210})
211 ->status_is(302)
212 ->content_is('')
213 ->header_is('Location' => '/');
214
215$t->get_ok('/')
216 ->status_is(200)
217 ->element_exists_not('div.notify-error')
218 ->element_exists('div.notify-success')
219 ->text_is('div.notify-success', 'Login successful')
Akron1d09b532021-06-15 18:18:25 +0200220 ->element_exists_not('aside.off')
Akrondc0b3ab2021-06-18 11:52:43 +0200221 ->element_exists_not('aside.active')
Akron1d09b532021-06-15 18:18:25 +0200222 ->element_exists('aside.settings')
Akron33f5c672019-06-24 19:40:47 +0200223 ;
224
Akron33f5c672019-06-24 19:40:47 +0200225# Now the user is logged in and should be able to
226# search with authorization
227$t->get_ok('/?q=Baum')
228 ->status_is(200)
Akron4cefe1f2019-09-04 10:11:28 +0200229 ->session_has('/auth')
230 ->session_is('/auth', 'Bearer ' . $access_token)
231 ->session_is('/auth_r', $refresh_token)
232 ->session_is('/user', 'test')
Akron33f5c672019-06-24 19:40:47 +0200233 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
234 ->text_like('#total-results', qr/\d+$/)
235 ->element_exists_not('div.notify-error')
Akronbc6b3f22021-01-13 14:53:12 +0100236 ->content_like(qr/${q}authorized${q}:${q}yes${q}/)
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200237 ->element_exists('nav.dropdown')
Akronbb3da4d2025-12-05 22:47:35 +0100238 ->element_exists_not('*[data-testid=handle-or-email]')
239 ->element_exists('*[data-testid=logout]')
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200240 ->element_exists('a.dropdown-item.logout[title~="test"]')
Akron33f5c672019-06-24 19:40:47 +0200241 ;
242
Akron27031aa2020-04-28 14:57:10 +0200243$t->get_ok('/?q=Paum')
244 ->status_is(200)
245 ->text_like('h1 span', qr/KorAP: Find .Paum./i)
246 ->text_is('#total-results', '')
Akronbc6b3f22021-01-13 14:53:12 +0100247 ->content_like(qr/${q}authorized${q}:${q}yes${q}/)
Akron27031aa2020-04-28 14:57:10 +0200248 ->element_exists_not('p.hint')
249 ;
250
Akroncce055c2021-07-02 12:18:03 +0200251# Query with error
252$t->get_ok('/?q=error')
253 ->status_is(400)
254 ->text_is('#notifications .notify-error','500: Internal Server Error')
255;
Akron27031aa2020-04-28 14:57:10 +0200256
Akron33f5c672019-06-24 19:40:47 +0200257# Logout
258$t->get_ok('/user/logout')
259 ->status_is(302)
Akron4cefe1f2019-09-04 10:11:28 +0200260 ->session_hasnt('/auth')
261 ->session_hasnt('/auth_r')
262 ->session_hasnt('/user')
Akron33f5c672019-06-24 19:40:47 +0200263 ->header_is('Location' => '/');
264
265$t->get_ok('/')
266 ->status_is(200)
267 ->element_exists_not('div.notify-error')
268 ->element_exists('div.notify-success')
269 ->text_is('div.notify-success', 'Logout successful')
Akron9fa7cc52022-05-12 11:17:20 +0200270 ->element_exists("input[name=handle_or_email]")
271 ->element_exists("input[name=handle_or_email][value=test]")
Akron33f5c672019-06-24 19:40:47 +0200272 ;
273
274$t->get_ok('/?q=Baum')
275 ->status_is(200)
276 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
277 ->text_like('#total-results', qr/\d+$/)
Akronbc6b3f22021-01-13 14:53:12 +0100278 ->content_like(qr/${q}authorized${q}:null/)
Akron33f5c672019-06-24 19:40:47 +0200279 ;
280
Akron27031aa2020-04-28 14:57:10 +0200281$t->get_ok('/?q=Paum')
282 ->status_is(200)
283 ->text_like('h1 span', qr/KorAP: Find .Paum./i)
284 ->text_is('#total-results', '')
Akronbc6b3f22021-01-13 14:53:12 +0100285 ->content_like(qr/${q}authorized${q}:null/)
Akron27031aa2020-04-28 14:57:10 +0200286 ->text_is('p.hint', 'Maybe you need to log in first?')
287 ;
288
289
Akron33f5c672019-06-24 19:40:47 +0200290# Get redirect
291my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
292 ->status_is(200)
293 ->element_exists_not('div.notify-error')
294 ->tx->res->dom->at('input[name=fwd]')->attr('value')
295 ;
296
297is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
298
299$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200300 handle_or_email => 'test',
Akron33f5c672019-06-24 19:40:47 +0200301 pwd => 'pass',
302 csrf_token => $csrf,
303 fwd => 'http://bad.example.com/test'
304})
305 ->status_is(302)
306 ->header_is('Location' => '/');
307
308$t->get_ok('/')
309 ->status_is(200)
310 ->element_exists('div.notify-error')
311 ->element_exists_not('div.notify-success')
312 ->text_is('div.notify-error', 'Redirect failure')
313 ;
314
315$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200316 handle_or_email => 'test',
Akron33f5c672019-06-24 19:40:47 +0200317 pwd => 'pass',
318 csrf_token => $csrf,
319 fwd => $fwd
320})
321 ->status_is(302)
322 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
323
Akron8bbbecf2019-07-01 18:57:30 +0200324$t->get_ok('/?q=Baum&ql=poliqarp')
325 ->status_is(200)
326 ->element_exists_not('div.notify-error')
327 ->element_exists('div.notify-success')
328 ->text_is('div.notify-success', 'Login successful')
Akroncdfd9d52019-07-23 11:35:00 +0200329 ->session_has('/auth')
330 ->session_is('/auth', 'Bearer ' . $access_token)
331 ->session_is('/auth_r', $refresh_token)
332 ->header_isnt('X-Kalamar-Cache', 'true')
Akron8bbbecf2019-07-01 18:57:30 +0200333 ;
334
Akroncdfd9d52019-07-23 11:35:00 +0200335# Expire the session
336# (makes the token be marked as expired - though it isn't serverside)
337$t->get_ok('/x/expire')
Akron8bbbecf2019-07-01 18:57:30 +0200338 ->status_is(200)
Akroncdfd9d52019-07-23 11:35:00 +0200339 ->content_is('okay')
340 ;
341
342## It may be a problem, but the cache is still valid
343$t->get_ok('/?q=Baum')
344 ->status_is(200)
345 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
346 ->text_like('#total-results', qr/\d+$/)
Akronbc6b3f22021-01-13 14:53:12 +0100347 ->content_like(qr/${q}authorized${q}:${q}yes${q}/)
Akroncdfd9d52019-07-23 11:35:00 +0200348 ->header_is('X-Kalamar-Cache', 'true')
349 ;
350
351# Query without partial cache (unfortunately) (but no total results)
Akron58c60992021-09-07 13:11:43 +0200352my $err = $t->get_ok('/?q=baum&cutoff=true')
Akroncdfd9d52019-07-23 11:35:00 +0200353 ->status_is(200)
354 ->session_is('/auth', 'Bearer ' . $access_token_2)
355 ->session_is('/auth_r', $refresh_token_2)
Akroncdfd9d52019-07-23 11:35:00 +0200356 ->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"]')
Akronbc6b3f22021-01-13 14:53:12 +0100359 ->content_like(qr/${q}authorized${q}:${q}yes${q}/)
Akroncdfd9d52019-07-23 11:35:00 +0200360 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100361 ->content_like(qr!${q}cutOff${q}:true!)
Akroncdfd9d52019-07-23 11:35:00 +0200362 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200363 ->tx->res->dom->at('#error')
Akroncdfd9d52019-07-23 11:35:00 +0200364 ;
Akron58c60992021-09-07 13:11:43 +0200365is(defined $err ? $err->text : '', '');
Akroncdfd9d52019-07-23 11:35:00 +0200366
367# Expire the session and remove the refresh option
368$t->get_ok('/x/expire-no-refresh')
369 ->status_is(200)
370 ->content_is('okay')
371 ;
372
373$t->app->defaults(no_cache => 1);
374
375
376$t->get_ok('/x/invalid-no-refresh')
377 ->status_is(200)
378 ->content_is('okay')
379 ;
380
381# Query without cache
382# The token is invalid and can't be refreshed!
Akron58c60992021-09-07 13:11:43 +0200383$err = $t->get_ok('/?q=baum&cutoff=true')
Akron3c390c42020-03-30 09:06:21 +0200384 ->status_is(400)
Akroncdfd9d52019-07-23 11:35:00 +0200385 ->session_hasnt('/auth')
386 ->session_hasnt('/auth_r')
Akroncdfd9d52019-07-23 11:35:00 +0200387 ->text_is('div.notify-error','Access token invalid')
388 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
389 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
390 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
Akronbc6b3f22021-01-13 14:53:12 +0100391 ->content_unlike(qr/${q}authorized${q}:${q}yes${q}/)
Akroncdfd9d52019-07-23 11:35:00 +0200392 ->header_isnt('X-Kalamar-Cache', 'true')
393 ->element_exists('p.no-results')
Akron58c60992021-09-07 13:11:43 +0200394 ->tx->res->dom->at('#error')
Akroncdfd9d52019-07-23 11:35:00 +0200395 ;
Akron58c60992021-09-07 13:11:43 +0200396is(defined $err ? $err->text : '', '');
397
Akroncdfd9d52019-07-23 11:35:00 +0200398
399$t->get_ok('/x/invalid')
400 ->status_is(200)
401 ->content_is('okay')
402 ;
403
404# Query without cache
405# The token is invalid and can't be refreshed!
Akron58c60992021-09-07 13:11:43 +0200406$err = $t->get_ok('/?q=baum&cutoff=true')
Akroncdfd9d52019-07-23 11:35:00 +0200407 ->status_is(200)
408 ->session_is('/auth', 'Bearer ' . $access_token_2)
409 ->session_is('/auth_r', $refresh_token_2)
Akroncdfd9d52019-07-23 11:35:00 +0200410 ->element_exists_not('div.notify-error','Access token invalid')
411 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
412 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
413 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
Akronbc6b3f22021-01-13 14:53:12 +0100414 ->content_like(qr/${q}authorized${q}:${q}yes${q}/)
Akroncdfd9d52019-07-23 11:35:00 +0200415 ->header_isnt('X-Kalamar-Cache', 'true')
416 ->element_exists_not('p.no-results')
Akron58c60992021-09-07 13:11:43 +0200417 ->tx->res->dom->at('#error')
Akron8bbbecf2019-07-01 18:57:30 +0200418 ;
Akron58c60992021-09-07 13:11:43 +0200419is(defined $err ? $err->text : '', '');
Akron8bbbecf2019-07-01 18:57:30 +0200420
Akron33f5c672019-06-24 19:40:47 +0200421
Akroncdfd9d52019-07-23 11:35:00 +0200422$t->get_ok('/x/expired-with-wrong-refresh')
423 ->status_is(200)
424 ->content_is('okay')
425 ;
Akron4796e002019-07-05 10:13:15 +0200426
Akron4796e002019-07-05 10:13:15 +0200427
Akroncdfd9d52019-07-23 11:35:00 +0200428# The token is invalid and can't be refreshed!
Akron58c60992021-09-07 13:11:43 +0200429my $dom = $t->get_ok('/?q=baum&cutoff=true')
Akron3c390c42020-03-30 09:06:21 +0200430 ->status_is(400)
Akroncdfd9d52019-07-23 11:35:00 +0200431 ->session_hasnt('/auth')
432 ->session_hasnt('/auth_r')
Akroncdfd9d52019-07-23 11:35:00 +0200433 ->text_is('div.notify-error','Refresh token is expired')
434 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
Akronbc6b3f22021-01-13 14:53:12 +0100435 ->content_unlike(qr/${q}authorized${q}:${q}yes${q}/)
Akroncdfd9d52019-07-23 11:35:00 +0200436 ->element_exists('p.no-results')
Akron58c60992021-09-07 13:11:43 +0200437 ->tx->res->dom;
438
439$csrf = $dom->at('input[name="csrf_token"]')
Akron59992122019-10-29 11:28:45 +0100440 ->attr('value')
Akroncdfd9d52019-07-23 11:35:00 +0200441 ;
Akron4796e002019-07-05 10:13:15 +0200442
Akron58c60992021-09-07 13:11:43 +0200443$err = $dom->at('#error');
444is(defined $err ? $err->text : '', '');
445
446
Akron6a228db2021-10-14 15:57:00 +0200447# This should fail
448my $wide_char_login = "\x{61}\x{E5}\x{61}"; # "\x{443}\x{434}";
Akron59992122019-10-29 11:28:45 +0100449$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200450 handle_or_email => $wide_char_login,
Akron6a228db2021-10-14 15:57:00 +0200451 pwd => 'pass',
452 csrf_token => $csrf,
453 fwd => $fwd
454})
455 ->status_is(302)
456 ->header_is('Location' => '/');
457
458$t->get_ok('/')
459 ->status_is(200)
460 ->element_exists('div.notify-error')
461 ->text_is('div.notify-error', 'Invalid character in request')
Akron9fa7cc52022-05-12 11:17:20 +0200462 ->element_exists('input[name=handle_or_email]:not([value])')
Akron6a228db2021-10-14 15:57:00 +0200463 ->element_exists_not('div.button.top a')
464 ;
465
466# Login:
467# UTF8 request
468my $username = b('täst')->encode;
469$t->post_ok('/user/login' => form => {
Akron9fa7cc52022-05-12 11:17:20 +0200470 handle_or_email => $username,
Akron59992122019-10-29 11:28:45 +0100471 pwd => 'pass',
472 csrf_token => $csrf
473})
474 ->status_is(302)
475 ->content_is('')
476 ->header_is('Location' => '/');
477
478$t->get_ok('/')
479 ->status_is(200)
Akron78e0b6f2023-04-12 12:50:29 +0200480 ->content_type_is('text/html;charset=UTF-8')
Akron59992122019-10-29 11:28:45 +0100481 ->element_exists_not('div.notify-error')
482 ->element_exists('div.notify-success')
483 ->text_is('div.notify-success', 'Login successful')
Akron78e0b6f2023-04-12 12:50:29 +0200484 # Weird error in certain environments otherwise
485 ->attr_like('a.logout', 'title', qr!^Logout: t.+st$!)
Akron1d09b532021-06-15 18:18:25 +0200486 ->element_exists_not('aside.off')
Akrondc0b3ab2021-06-18 11:52:43 +0200487 ->element_exists_not('aside.active')
Akron1d09b532021-06-15 18:18:25 +0200488 ->element_exists('aside.settings')
Akronb0a7a0f2025-02-27 09:51:06 +0100489 ->text_is('nav.dropdown a:first-child span','API tokens')
Akron59992122019-10-29 11:28:45 +0100490 ;
491
Uyen-Nhu Tran94f93b02024-11-20 20:17:57 +0100492
Akron59992122019-10-29 11:28:45 +0100493$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')
Akron9f2ad342022-05-04 16:16:40 +0200496 ->text_is('label[for=name]','Name of the client application')
497 ->text_is('label[for=type]','Type of the client application')
498 ->text_is('label[for=desc]','Short description')
Helge9f9d4852024-12-09 16:06:34 +0100499 ->text_like('label[for=url]'=> '/Homepage/')
500 ->element_exists('label[for=url] > span.field-required')
Akron9f2ad342022-05-04 16:16:40 +0200501 ->text_is('label[for=redirect_uri]','Redirect URI')
502 ->text_is('label[for=src]','Declaration of the plugin (*.json file)')
Akron1a9d5be2020-03-19 17:28:33 +0100503 ->element_exists('ul.client-list')
504 ->element_exists_not('ul.client-list > li')
Akronad011bb2021-06-10 12:16:36 +0200505 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
506 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
507 ->header_is('Pragma','no-cache')
Akron59992122019-10-29 11:28:45 +0100508 ;
Akronad011bb2021-06-10 12:16:36 +0200509
Helgedb720ea2023-03-20 09:39:36 +0100510my $loglines = '';
511$t->app->log->on(
512 message => sub {
513 my ($log, $level, @lines) = @_;
514 if ($level eq 'warn') {
515 $loglines = join ',', @lines;
516 };
517 });
Helge278fbca2022-11-29 18:49:15 +0100518
519$t->get_ok('/settings/marketplace')
520 ->status_is(200)
521 ->text_is('html head title' => 'Marketplace')
Helgedb720ea2023-03-20 09:39:36 +0100522 ->element_exists_not('ul.plugin_list')
523 ->element_exists_not('ul.plugin_in-list')
Helge278fbca2022-11-29 18:49:15 +0100524 ;
525
Helgedb720ea2023-03-20 09:39:36 +0100526is($loglines, '', 'Check log is fine');
527
Akron59992122019-10-29 11:28:45 +0100528$csrf = $t->post_ok('/settings/oauth/register' => form => {
529 name => 'MyApp',
530 type => 'PUBLIC',
Akron6b75d122022-05-12 17:39:05 +0200531 desc => 'This is my plugin application'
Akron59992122019-10-29 11:28:45 +0100532})
533 ->text_is('div.notify-error', 'Bad CSRF token')
534 ->tx->res->dom->at('input[name="csrf_token"]')
535 ->attr('value')
536 ;
537
538$t->post_ok('/settings/oauth/register' => form => {
539 name => 'MyApp',
Akron99968a92022-06-03 12:32:07 +0200540 type => 'PUBLIC',
Akron6b75d122022-05-12 17:39:05 +0200541 desc => 'This is my plugin application',
Akron9f2ad342022-05-04 16:16:40 +0200542 csrf_token => $csrf,
543 src => {
544 filename => '',
545 content => ''
546 }
Akron59992122019-10-29 11:28:45 +0100547})
548 ->status_is(200)
549 ->element_exists('div.notify-success')
550 ->text_is('legend', 'Client Credentials')
551 ->text_is('label[for=client_id]', 'ID of the client application')
552 ->element_exists('input[name=client_id][readonly][value]')
Akron99968a92022-06-03 12:32:07 +0200553 ->element_exists_not('input[name=client_secret][readonly][value]')
Akronad011bb2021-06-10 12:16:36 +0200554 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
555 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
556 ->header_is('Pragma','no-cache')
Akron59992122019-10-29 11:28:45 +0100557 ;
Akron4796e002019-07-05 10:13:15 +0200558
Akron58c60992021-09-07 13:11:43 +0200559my $anchor = $t->get_ok('/settings/oauth')
Akronc1aaf932021-06-09 12:19:15 +0200560 ->text_is('.form-table legend', 'Register new client application')
561 ->attr_is('.oauth-register','action', '/settings/oauth/register')
Akron17de86e2020-04-16 16:03:40 +0200562 ->text_is('ul.client-list > li > span.client-name a', 'MyApp')
Akron6b75d122022-05-12 17:39:05 +0200563 ->text_is('ul.client-list > li > p.client-desc', 'This is my plugin application')
Akronad011bb2021-06-10 12:16:36 +0200564 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
565 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
566 ->header_is('Pragma','no-cache')
Akronb6b156e2022-03-31 14:57:49 +0200567 ->tx->res->dom->at('ul.client-list > li > p.client-url a')
Akron17de86e2020-04-16 16:03:40 +0200568 ;
Akron58c60992021-09-07 13:11:43 +0200569is(defined $anchor ? $anchor->text : '', '');
Akron17de86e2020-04-16 16:03:40 +0200570
Akron041ca4d2021-06-10 11:52:51 +0200571$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akron17de86e2020-04-16 16:03:40 +0200572 ->status_is(200)
Akronc1aaf932021-06-09 12:19:15 +0200573 ->text_is('ul.client-list > li.client > span.client-name', 'MyApp')
Akron6b75d122022-05-12 17:39:05 +0200574 ->text_is('ul.client-list > li.client > p.client-desc', 'This is my plugin application')
Akron17de86e2020-04-16 16:03:40 +0200575 ->text_is('a.client-unregister', 'Unregister')
Akron041ca4d2021-06-10 11:52:51 +0200576 ->attr_is('a.client-unregister', 'href', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister?name=MyApp')
Akron1a9d5be2020-03-19 17:28:33 +0100577 ;
578
Akron041ca4d2021-06-10 11:52:51 +0200579$csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister?name=MyApp')
Akron1a9d5be2020-03-19 17:28:33 +0100580 ->content_like(qr!Do you really want to unregister \<span class="client-name"\>MyApp\<\/span\>?!)
Akronc1aaf932021-06-09 12:19:15 +0200581 ->attr_is('.form-table input[name=client-name]', 'value', 'MyApp')
Akronad011bb2021-06-10 12:16:36 +0200582 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
583 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
584 ->header_is('Pragma','no-cache')
Akron1a9d5be2020-03-19 17:28:33 +0100585 ->tx->res->dom->at('input[name="csrf_token"]')
586 ->attr('value')
587 ;
588
Akron041ca4d2021-06-10 11:52:51 +0200589$t->post_ok('/settings/oauth/xxxx==/unregister' => form => {
Akron1a9d5be2020-03-19 17:28:33 +0100590 'client-name' => 'MyApp',
Akron1a9d5be2020-03-19 17:28:33 +0100591 'csrf_token' => $csrf
592})->status_is(302)
593 ->content_is('')
594 ->header_is('Location' => '/settings/oauth')
595 ;
596
597$t->get_ok('/settings/oauth')
Akronc1aaf932021-06-09 12:19:15 +0200598 ->text_is('.form-table legend', 'Register new client application')
599 ->attr_is('.oauth-register','action', '/settings/oauth/register')
Akron1a9d5be2020-03-19 17:28:33 +0100600 ->element_exists('ul.client-list > li')
601 ->text_is('div.notify', 'Unknown client with xxxx==.')
Akronad011bb2021-06-10 12:16:36 +0200602 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
603 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
604 ->header_is('Pragma','no-cache')
Akron1a9d5be2020-03-19 17:28:33 +0100605 ;
606
Akron041ca4d2021-06-10 11:52:51 +0200607$t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister' => form => {
Akron1a9d5be2020-03-19 17:28:33 +0100608 'client-name' => 'MyApp',
Akron1a9d5be2020-03-19 17:28:33 +0100609 'csrf_token' => $csrf
610})->status_is(302)
611 ->content_is('')
612 ->header_is('Location' => '/settings/oauth')
613 ;
614
615$t->get_ok('/settings/oauth')
Akronc1aaf932021-06-09 12:19:15 +0200616 ->text_is('.form-table legend', 'Register new client application')
617 ->attr_is('.oauth-register','action', '/settings/oauth/register')
Akron1a9d5be2020-03-19 17:28:33 +0100618 ->element_exists_not('ul.client-list > li')
619 ->text_is('div.notify-success', 'Successfully deleted MyApp')
Akronad011bb2021-06-10 12:16:36 +0200620 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
621 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
622 ->header_is('Pragma','no-cache')
Akron1a9d5be2020-03-19 17:28:33 +0100623 ;
624
Akron83209f72021-01-29 17:54:15 +0100625$t->post_ok('/settings/oauth/register' => form => {
626 name => 'MyApp2',
627 type => 'PUBLIC',
Akron6b75d122022-05-12 17:39:05 +0200628 desc => 'This is my plugin application',
Akron83209f72021-01-29 17:54:15 +0100629 csrf_token => $csrf
630})->status_is(200)
631 ->element_exists('div.notify-success')
632 ->text_is('legend', 'Client Credentials')
633 ->text_is('label[for=client_id]', 'ID of the client application')
634 ->element_exists('input[name=client_id][readonly][value]')
635 ->element_exists_not('input[name=client_secret][readonly][value]')
Akronad011bb2021-06-10 12:16:36 +0200636 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
637 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
638 ->header_is('Pragma','no-cache')
Akronb6b156e2022-03-31 14:57:49 +0200639 ->element_exists('.client-issue-token')
Akron83209f72021-01-29 17:54:15 +0100640 ;
641
Akron041ca4d2021-06-10 11:52:51 +0200642$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akron83209f72021-01-29 17:54:15 +0100643 ->text_is('.client-name', 'MyApp2')
Akron6b75d122022-05-12 17:39:05 +0200644 ->text_is('.client-desc', 'This is my plugin application')
Akrone997bb52021-06-11 16:44:06 +0200645 ->text_is('.client-issue-token', 'Issue new token')
Akron041ca4d2021-06-10 11:52:51 +0200646 ->attr_is('.client-issue-token', 'href', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2')
Akronad011bb2021-06-10 12:16:36 +0200647 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
648 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
649 ->header_is('Pragma','no-cache')
Akrone997bb52021-06-11 16:44:06 +0200650 ->text_is('ul.token-list label[for=token]', 'Access Token')
651 ->text_is('p[name=created]', 'Created at ')
652 ->text_is('p[name=expires]', 'Expires in 31533851 seconds.')
Akronb6b156e2022-03-31 14:57:49 +0200653 ->element_exists('.client-issue-token')
Akron83209f72021-01-29 17:54:15 +0100654 ;
655
Akron041ca4d2021-06-10 11:52:51 +0200656$csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2')
Akron83209f72021-01-29 17:54:15 +0100657 ->status_is(200)
Akron041ca4d2021-06-10 11:52:51 +0200658 ->attr_is('#issue-token','action', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token')
Akron83209f72021-01-29 17:54:15 +0100659 ->attr_is('input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==')
660 ->attr_is('input[name=name]', 'value', 'MyApp2')
Akronad011bb2021-06-10 12:16:36 +0200661 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
662 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
663 ->header_is('Pragma','no-cache')
Akrone997bb52021-06-11 16:44:06 +0200664 ->text_is('a.button-abort', 'Abort')
665 ->attr_is('#issue-token input[type=submit]', 'value', 'Issue new token')
666 ->content_like(qr!Issue a new token for!)
Akron83209f72021-01-29 17:54:15 +0100667 ->tx->res->dom->at('input[name="csrf_token"]')
668 ->attr('value')
669 ;
670
Akron041ca4d2021-06-10 11:52:51 +0200671$t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token' => form => {
Akron83209f72021-01-29 17:54:15 +0100672 csrf_token => $csrf,
673 name => 'MyApp2',
674 'client-id' => 'fCBbQkA2NDA3MzM1Yw=='
675})
Akronbc94a9c2021-04-15 00:07:35 +0200676 ->status_is(302)
Akron041ca4d2021-06-10 11:52:51 +0200677 ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronbc94a9c2021-04-15 00:07:35 +0200678 ;
679
Akron041ca4d2021-06-10 11:52:51 +0200680$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
681 ->status_is(200)
Akronbc94a9c2021-04-15 00:07:35 +0200682 ->text_is('div.notify-success', 'New access token created')
Akronad011bb2021-06-10 12:16:36 +0200683 ->status_is(200)
684 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
685 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
686 ->header_is('Pragma','no-cache')
Akron83209f72021-01-29 17:54:15 +0100687 ;
688
Akrone3daaeb2023-05-08 09:44:18 +0200689$t->post_ok('/settings/oauth/307/token' => form => {
690 csrf_token => $csrf,
691 name => 'MyApp2',
692})
693 ->status_is(302)
694 ->header_is('Location','/settings/oauth/307')
695 ;
696
697$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
698 ->status_is(200)
699 ->text_is('div.notify-success', 'New access token created')
700 ->status_is(200)
701 ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate')
702 ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT')
703 ->header_is('Pragma','no-cache')
704 ;
705
706
Akron041ca4d2021-06-10 11:52:51 +0200707$csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200708 ->status_is(200)
Akron041ca4d2021-06-10 11:52:51 +0200709 ->attr_is('form.token-revoke', 'action', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token/revoke')
Akronc1aaf932021-06-09 12:19:15 +0200710 ->attr_is('form.token-revoke input[name=token]', 'value', 'jhkhkjhk_hjgjsfz67i')
711 ->attr_is('form.token-revoke input[name=name]', 'value', 'MyApp2')
712 ->tx->res->dom->at('input[name="csrf_token"]')
713 ->attr('value')
714 ;
715
Akron041ca4d2021-06-10 11:52:51 +0200716$t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token/revoke' => form => {
Akronc1aaf932021-06-09 12:19:15 +0200717 csrf_token => $csrf,
718 name => 'MyApp2',
719 token => 'jhkhkjhk_hjgjsfz67i'
720})
721 ->status_is(200)
Akron041ca4d2021-06-10 11:52:51 +0200722 ->attr_is('form#revoke-token','action','/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE')
Akronc1aaf932021-06-09 12:19:15 +0200723 ->attr_is('form#revoke-token','method','POST')
724 ->attr_is('form#revoke-token input[name=token]','value','jhkhkjhk_hjgjsfz67i')
Akrone997bb52021-06-11 16:44:06 +0200725 ->text_is('a.button-abort', 'Abort')
726 ->attr_is('#revoke-token input[type=submit]', 'value', 'Revoke')
727 ->content_like(qr!Revoke a token for!)
Akronc1aaf932021-06-09 12:19:15 +0200728;
729
730
731# CSRF missing
Akron041ca4d2021-06-10 11:52:51 +0200732$t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => {
Akronc1aaf932021-06-09 12:19:15 +0200733 name => 'MyApp2',
734 token => 'jhkhkjhk_hjgjsfz67i'
735})->status_is(302)
Akron041ca4d2021-06-10 11:52:51 +0200736 ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200737 ;
738
Akron041ca4d2021-06-10 11:52:51 +0200739$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200740 ->element_exists_not('div.notify-success')
741 ->text_is('div.notify-error', 'Bad CSRF token')
742 ;
743
744# Token missing
Akron041ca4d2021-06-10 11:52:51 +0200745$t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => {
Akronc1aaf932021-06-09 12:19:15 +0200746 name => 'MyApp2',
747 csrf_token => $csrf,
748})->status_is(302)
Akron041ca4d2021-06-10 11:52:51 +0200749 ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200750 ;
751
Akron041ca4d2021-06-10 11:52:51 +0200752$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200753 ->element_exists_not('div.notify-success')
754 ->text_is('div.notify-error', 'Some fields are invalid')
755 ;
756
Akron041ca4d2021-06-10 11:52:51 +0200757$t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => {
Akronc1aaf932021-06-09 12:19:15 +0200758 name => 'MyApp2',
759 csrf_token => $csrf,
760 token => 'jhkhkjhk_hjgjsfz67i'
761})->status_is(302)
Akron041ca4d2021-06-10 11:52:51 +0200762 ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200763 ;
764
Akron041ca4d2021-06-10 11:52:51 +0200765$t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==')
Akronc1aaf932021-06-09 12:19:15 +0200766 ->element_exists_not('div.notify-error')
767 ->text_is('div.notify-success', 'Token was revoked successfully')
768 ;
Akron83209f72021-01-29 17:54:15 +0100769
Akronb6b156e2022-03-31 14:57:49 +0200770$t->app->routes->get('/x/redirect-target')->to(
771 cb => sub {
772 my $c = shift;
773 return $c->render(text => 'redirected');
774 }
775);
776
777$csrf = $t->post_ok('/settings/oauth/register' => form => {
778 name => 'MyConfApp',
779 type => 'CONFIDENTIAL',
Akron6b75d122022-05-12 17:39:05 +0200780 desc => 'This is my plugin application',
Akronb6b156e2022-03-31 14:57:49 +0200781})
782 ->text_is('div.notify-error', 'Bad CSRF token')
783 ->tx->res->dom->at('input[name="csrf_token"]')
784 ->attr('value')
785 ;
786
787$t->post_ok('/settings/oauth/register' => form => {
788 name => 'MyConfApp',
789 type => 'CONFIDENTIAL',
790 desc => 'This is my confidential application',
791 csrf_token => $csrf,
792 redirect_uri => 'http://localhost/redirect-target'
793})
794 ->text_is('div.notify-error', undef)
795 ->text_is('li.client span.client-name', 'MyConfApp')
796 ->text_is('li.client p.client-desc', 'This is my confidential application')
797 ->text_is('li.client .client-redirect-uri tt', 'http://localhost/redirect-target')
798 ->text_is('li.client .client-type tt', 'CONFIDENTIAL')
799 ->element_exists_not('.client-issue-token')
800 ;
801
802$t->post_ok('/settings/oauth/register' => form => {
803 name => 'MyConfApp2',
804 type => 'CONFIDENTIAL',
805 desc => 'This is my second confidential application',
806 csrf_token => $csrf,
807 redirect_uri => 'http://localhost/FAIL'
808})
809 ->status_is(302)
810 ->header_is('location','/settings/oauth/')
811 ;
812
813$t->get_ok('/settings/oauth/')
814 ->text_is('div.notify-error', 'invalid_request: http://localhost/FAIL is invalid.')
815 ;
816
Akrona8efaa92022-04-09 14:45:43 +0200817# OAuth client authorization flow
818$t->get_ok(Mojo::URL->new('/settings/oauth/authorize'))
819 ->status_is(302)
Akron5ea0f5d2023-01-20 11:51:43 +0100820 ->header_is('location','/settings/oauth')
821 ;
822
823$t->get_ok('/settings/oauth/')
Akron2c142ab2023-01-30 13:21:57 +0100824 ->text_is('div.notify-error', 'Client ID required')
825 ;
826
827$t->get_ok(Mojo::URL->new('/settings/oauth/authorize?client_id=xyz'))
828 ->status_is(302)
829 ->header_is('location','/settings/oauth')
830 ;
831
832$t->get_ok('/settings/oauth/')
833 ->text_is('div.notify-error', 'Scope required')
Akrona8efaa92022-04-09 14:45:43 +0200834 ;
835
Akrondb1f4672023-01-24 12:05:07 +0100836# OAuth client authorization flow
837$t->get_ok(Mojo::URL->new('/settings/oauth/authorize?client_id=abc'))
838 ->status_is(302)
839 ->header_is('location','/settings/oauth')
840 ;
841
842$t->get_ok('/settings/oauth/')
Akron2c142ab2023-01-30 13:21:57 +0100843 ->text_is('div.notify-error', 'Scope required')
844 ;
845
846# OAuth client authorization flow
847$t->get_ok(Mojo::URL->new('/settings/oauth/authorize?client_id=abc&scope=match'))
848 ->status_is(302)
849 ->header_is('location','/settings/oauth')
850 ;
851
852$t->get_ok('/settings/oauth/')
Akrondb1f4672023-01-24 12:05:07 +0100853 ->text_is('div.notify-error', 'Unknown client with abc.')
854 ;
855
Akrona8efaa92022-04-09 14:45:43 +0200856# Logout
857$t->get_ok('/x/expired-with-wrong-refresh');
858
859$t->get_ok('/user/logout')
860 ->status_is(302)
861 ->session_hasnt('/auth')
862 ->session_hasnt('/auth_r')
863 ->session_hasnt('/user')
864 ->header_is('Location' => '/');
865
Akron001dcd22023-02-07 08:38:11 +0100866$t->get_ok('/')
Akrona8efaa92022-04-09 14:45:43 +0200867 ->status_is(200)
868 ->element_exists_not('div.notify-error')
869 ->element_exists('div.notify-success')
870 ->text_is('div.notify-success', 'Logout successful')
Akron9fa7cc52022-05-12 11:17:20 +0200871 ->element_exists("input[name=handle_or_email]")
Akron001dcd22023-02-07 08:38:11 +0100872 ;
873
874# OAuth client authorization flow - but user not logged in
875$t->get_ok(Mojo::URL->new('/settings/oauth/authorize'))
876 ->status_is(302)
877 ->header_is('location','/')
878 ;
879
880$csrf = $t->get_ok('/')
881 ->status_is(200)
882 ->element_exists('div.notify-error')
883 ->element_exists_not('div.notify-success')
884 ->text_is('div.notify-error', 'Client ID required')
885 ->element_exists("input[name=handle_or_email]")
Akrona8efaa92022-04-09 14:45:43 +0200886 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
887 ;
888
Akron408bc7c2022-04-28 15:46:43 +0200889$fake_backend_app->add_client({
890 "client_id" => 'xyz',
891 "client_name" => 'New added client',
892 "client_description" => 'This is a new client',
893 "client_url" => 'http://example.com',
894 "client_type" => 'CONFIDENTIAL'
895# "client_redirect_uri" => $redirect_uri
896});
897
Akron0cbcc072024-10-08 14:04:42 +0200898$fake_backend_app->add_client({
899 "client_id" => 'xyz-public',
900 "client_name" => 'New added public client',
901 "client_description" => 'This is a new public client',
902 "client_url" => 'http://example.com',
903 "client_type" => 'PUBLIC'
904# "client_redirect_uri" => $redirect_uri
905});
906
Helge278fbca2022-11-29 18:49:15 +0100907
Akron9d826902023-01-25 10:20:52 +0100908$fake_backend_app->add_client({
909 "client_id" => 'xyz2',
910 "client_name" => 'New added client',
911 "client_description" => 'This is a new client',
912 "client_url" => 'http://example.com',
913 "client_type" => 'CONFIDENTIAL',
914 "client_redirect_uri" => 'http://redirect.url/'
915});
916
Akroneb39cf32023-04-03 14:40:48 +0200917$t->get_ok('/settings/oauth/xyz2')
918 ->status_is(200)
919 ->text_is('li.client span.client-name','New added client')
920 ->attr_is('li.client p.client-url a','href','http://example.com')
921 ->attr_is('li.client input[name=client_id]','value','xyz2')
922 ->element_exists('li.client p.client-type-confidential')
923 ->text_is('li.client p.client-redirect-uri tt','http://redirect.url/')
924 ;
925
Akron9d826902023-01-25 10:20:52 +0100926$fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
927 client_id => 'xyz2',
928 scope => 'search match',
929 redirect_uri => 'http://test.com/',
930}))
931 ->status_is(200)
932 ->text_is('div.notify-warn', 'redirect_uri host differs from registered host')
933 ->element_exists_not('div.notify-error')
934 ->element_exists_not('div.notify-success')
935 ->element_exists("input[name=handle_or_email]")
936 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
937 ;
938
939$fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
940 client_id => 'xyz2',
941 scope => 'search match',
942 redirect_uri => 'http://redirect.url:9000/',
943}))
944 ->status_is(200)
945 ->text_is('div.notify-warn', 'redirect_uri port differs from registered port')
946 ->element_exists_not('div.notify-error')
947 ->element_exists_not('div.notify-success')
948 ->element_exists("input[name=handle_or_email]")
949 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
950 ;
951
Helge278fbca2022-11-29 18:49:15 +0100952
953$fake_backend_app->add_plugin({
954"source" => {"key1" => 'wert1', "key2" => 'wert2'},
Helge278fbca2022-11-29 18:49:15 +0100955"client_id" => '52abc',
Helgedb720ea2023-03-20 09:39:36 +0100956"permitted" => 'true',
Helge278fbca2022-11-29 18:49:15 +0100957"client_name" => 'Plugin 1',
958"client_type" => 'CONFIDENTIAL',
Helgedb720ea2023-03-20 09:39:36 +0100959"client_description" => 'Description Plugin 1',
960"client_url" => 'http://example.client.de',
961"registration_date" => '2022-05-31T14:30:09+02:00[Europe/Berlin]',
Helge216a4822024-06-17 12:02:34 +0200962"registered_by" => 'testuser',
963"refresh_token_expiry" => '7776000',
Helge278fbca2022-11-29 18:49:15 +0100964});
965
966
Akrona8efaa92022-04-09 14:45:43 +0200967$fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
968 client_id => 'xyz',
969 state => 'abcde',
970 scope => 'search match',
971 redirect_uri => 'http://test.com/',
972}))
973 ->status_is(200)
974 ->attr_is('input[name=client_id]','value','xyz')
975 ->attr_is('input[name=state]','value','abcde')
Akrona8efaa92022-04-09 14:45:43 +0200976 ->attr_like('input[name=fwd]','value',qr!test\.com!)
Akron408bc7c2022-04-28 15:46:43 +0200977 ->text_is('span.client-name','New added client')
Akrona8efaa92022-04-09 14:45:43 +0200978 ->text_is('div.intro p:nth-child(2)', 'Please log in!')
979 ->tx->res->dom->at('input[name=fwd]')->attr('value')
980 ;
981
982$fwd = $t->post_ok(Mojo::URL->new('/user/login')->query({
983 csrf_token => $csrf,
984 client_id => 'xyz',
985 state => 'abcde',
986 scope => 'search match',
987 redirect_uri => 'http://test.com/',
Akron9fa7cc52022-05-12 11:17:20 +0200988 handle_or_email => 'test',
Akrona8efaa92022-04-09 14:45:43 +0200989 pwd => 'pass',
990 fwd => $fwd
991}))
992 ->status_is(302)
993 ->header_like('location', qr!/settings/oauth/authorize!)
994 ->tx->res->headers->header('location')
995 ;
996
997$t->get_ok($fwd)
998 ->status_is(200)
999 ->attr_is('input[name=client_id]','value','xyz')
1000 ->attr_is('input[name=state]','value','abcde')
Akron9d826902023-01-25 10:20:52 +01001001 ->attr_like('input[name=redirect_uri]','value', qr!^http://test\.com\/\?crto=.{3,}!)
Akrona8efaa92022-04-09 14:45:43 +02001002 ->text_is('ul#scopes li:nth-child(1)','search')
1003 ->text_is('ul#scopes li:nth-child(2)','match')
Akron408bc7c2022-04-28 15:46:43 +02001004 ->text_is('span.client-name','New added client')
Akrona8efaa92022-04-09 14:45:43 +02001005 ->attr_is('a.form-button','href','http://test.com/')
1006 ->attr_is('a.embedded-link', 'href', '/doc/korap/kalamar')
Akron9d826902023-01-25 10:20:52 +01001007 ->element_exists_not('div.notify-error')
1008 ->element_exists_not('div.notify-warn')
1009 ->element_exists_not('blockquote.warning')
Akron0cbcc072024-10-08 14:04:42 +02001010 ->text_is('h2 + p', ' wants to have access')
Akrona8efaa92022-04-09 14:45:43 +02001011 ;
1012
Akron0cbcc072024-10-08 14:04:42 +02001013$fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1014 client_id => 'xyz-public',
1015 state => 'abcde',
1016 scope => 'search match',
1017 redirect_uri => 'http://test.com/',
1018}))
1019 ->status_is(200)
1020 ->attr_is('input[name=client_id]','value','xyz-public')
1021 ->attr_is('input[name=state]','value','abcde')
1022 ->attr_like('input[name=redirect_uri]','value', qr!^http://test\.com\/\?crto=.{3,}!)
1023 ->text_is('ul#scopes li:nth-child(1)','search')
1024 ->text_is('ul#scopes li:nth-child(2)','match')
1025 ->text_is('span.client-name','New added public client')
1026 ->attr_is('a.form-button','href','http://test.com/')
1027 ->attr_is('a.embedded-link', 'href', '/doc/korap/kalamar')
1028 ->element_exists_not('div.notify-error')
1029 ->element_exists_not('div.notify-warn')
1030 ->text_is('blockquote.warning','Warning - this is a public client!')
1031 ->text_is('h2 + p', ' wants to have access')
1032 ;
1033
1034
Helge278fbca2022-11-29 18:49:15 +01001035$t->get_ok('/settings/marketplace')
1036 ->status_is(200)
1037 ->text_is('html head title' => 'Marketplace')
1038 ->element_exists('ul.plugin-list')
1039 ->element_exists('ul.plugin-list > li')
Helge278fbca2022-11-29 18:49:15 +01001040 ->text_is('span.client-name','Plugin 1')
1041 ->text_is('p.plugin-desc','Description Plugin 1')
Helge216a4822024-06-17 12:02:34 +02001042 ->text_is('p.registration_date', 'Date of registration: 2022-05-31T14:30:09+02:00[Europe/Berlin]')
1043 ->text_is('p.registered_by', 'Registered by: testuser')
Helge278fbca2022-11-29 18:49:15 +01001044 ;
1045
Helgedb720ea2023-03-20 09:39:36 +01001046
1047
Helge278fbca2022-11-29 18:49:15 +01001048$fake_backend_app->add_plugin({
1049"source" => {"one" => '1', "two" => '2'},
1050"permitted" => 'false',
1051"client_id" => '53abc',
1052"client_name" => 'Plugin 2',
1053"client_type" => 'CONFIDENTIAL',
1054"client_description" =>'Description Plugin 2'
1055});
1056
Helge278fbca2022-11-29 18:49:15 +01001057$t->get_ok('/settings/marketplace')
1058 ->status_is(200)
1059 ->element_exists('ul.plugin-list')
1060 ->element_exists('ul.plugin-list > li')
1061 ->text_is('span.client-name','Plugin 1')
1062 ->text_is('p.plugin-desc','Description Plugin 1')
1063 ->element_exists('ul.plugin-list > li + li')
Helgedb720ea2023-03-20 09:39:36 +01001064 ->text_is('ul.plugin-list > li + li >span.client-name','Plugin 2')
1065 ->text_is('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 2')
Helge278fbca2022-11-29 18:49:15 +01001066 ;
1067
Helged36478d2023-06-08 17:43:01 +02001068$t->post_ok('/settings/marketplace/install', form => {'client-id' => '52abc'})
Helgedb720ea2023-03-20 09:39:36 +01001069 ->status_is(302)
1070 ->header_is(location => '/settings/marketplace')
1071 ;
Helgedb720ea2023-03-20 09:39:36 +01001072$t->ua->max_redirects(1);
1073
Helged36478d2023-06-08 17:43:01 +02001074$t->post_ok('/settings/marketplace/install', form => {'client-id' => '52abc'})
Helgedb720ea2023-03-20 09:39:36 +01001075 ->status_is(200)
1076 ->element_exists('ul.plugin-list')
1077 ->element_exists('ul.plugin-list > li')
1078 ->text_is('ul.plugin-list > li > span.client-name','Plugin 2')
1079 ->text_is('ul.plugin-list > li > p.plugin-desc','Description Plugin 2')
1080 ->element_exists_not('ul.plugin-list > li + li')
1081 ->element_exists('ul.plugin_in-list')
1082 ->element_exists('ul.plugin_in-list > li')
1083 ->text_is('ul.plugin_in-list > li > span.client-name','Plugin 1')
Helge216a4822024-06-17 12:02:34 +02001084 ->text_is('ul.plugin_in-list > li > p.inst_date','Installation date: 2022-12-13T16:33:27.621+01:00[Europe/Berlin]')
Helgedb720ea2023-03-20 09:39:36 +01001085 ;
1086
1087$t->ua->max_redirects(0);
1088
Helged36478d2023-06-08 17:43:01 +02001089 $t->post_ok('/settings/marketplace/install', form => {'client-id' => 'unsinn31'})
Helgedb720ea2023-03-20 09:39:36 +01001090 ->status_is(302)
1091 ->header_is(location => '/settings/marketplace')
1092 ;
1093
1094$t->ua->max_redirects(1);
1095
Helged36478d2023-06-08 17:43:01 +02001096$t->post_ok('/settings/marketplace/install', form => {'client-id' => 'unsinn31'})
Helgedb720ea2023-03-20 09:39:36 +01001097 ->status_is(200)
1098 ->text_is('div.notify-error', 'Plugin could not be installed')
1099 ;
1100
Helged36478d2023-06-08 17:43:01 +02001101$t->post_ok('/settings/marketplace/uninstall', form => {'client-id' => '52abc'})
1102 ->status_is(200)
1103 ->element_exists('ul.plugin-list')
1104 ->element_exists('ul.plugin-list > li')
1105 ->text_is('span.client-name','Plugin 1')
1106 ->text_is('p.plugin-desc','Description Plugin 1')
1107 ->element_exists('ul.plugin-list > li + li')
1108 ->text_is('ul.plugin-list > li + li >span.client-name','Plugin 2')
1109 ->text_is('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 2')
1110 ->element_exists_not('ul.plugin_in-list')
1111 ;
1112
1113$t->post_ok('/settings/marketplace/uninstall', form => {'client-id' => 'quatsch12'})
1114 ->status_is(200)
1115 ->text_is('div.notify-error', 'Plugin could not be uninstalled')
1116 ;
1117
Helgedb720ea2023-03-20 09:39:36 +01001118$t->ua->max_redirects(0);
Helge278fbca2022-11-29 18:49:15 +01001119
Akrona8efaa92022-04-09 14:45:43 +02001120$t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1121 client_id => 'xyz',
1122 state => 'abcde',
1123 scope => 'search match',
1124 redirect_uri => 'http://test.com/'
1125}))
1126 ->status_is(200)
1127 ->attr_is('input[name=client_id]','value','xyz')
1128 ->attr_is('input[name=state]','value','abcde')
Akrona8efaa92022-04-09 14:45:43 +02001129 ->text_is('ul#scopes li:nth-child(1)','search')
1130 ->text_is('ul#scopes li:nth-child(2)','match')
Akron408bc7c2022-04-28 15:46:43 +02001131 ->text_is('span.client-name','New added client')
Akrona8efaa92022-04-09 14:45:43 +02001132 ->attr_is('a.form-button','href','http://test.com/')
1133 ->attr_is('a.embedded-link', 'href', '/doc/korap/kalamar')
1134 ;
1135
1136$t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1137 client_id => 'xyz',
1138 state => 'abcde',
1139 scope => 'search match',
1140 redirect_uri => 'http://test.com/'
1141}))
1142 ->status_is(302)
Akron9d826902023-01-25 10:20:52 +01001143 ->header_is('location', '/settings/oauth?error_description=Bad+CSRF+token')
Akrona8efaa92022-04-09 14:45:43 +02001144 ;
1145
Akrona8f87cc2023-02-23 12:21:30 +01001146
1147my $local_port = $t->get_ok('/')->tx->local_port;
1148my $remote_port = $t->get_ok('/')->tx->remote_port;
1149
1150like($local_port, qr!^\d+$!);
1151like($remote_port, qr!^\d+$!);
1152
1153my $port = $remote_port;
1154
1155my $redirect_url_fakeapi = $t->app->close_redirect_to(Mojo::URL->new('http://localhost:' . $port)->path($fake_backend_app->url_for('return_uri'))->to_abs->to_string);
1156
Akrona8efaa92022-04-09 14:45:43 +02001157$fwd = $t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1158 client_id => 'xyz',
1159 state => 'abcde',
1160 scope => 'search match',
Akrona8f87cc2023-02-23 12:21:30 +01001161 redirect_uri_server => 'http://localhost:'.$port,
1162 redirect_uri => "$redirect_url_fakeapi",
Akrona8efaa92022-04-09 14:45:43 +02001163 csrf_token => $csrf,
1164}))
1165 ->status_is(302)
Akrona8f87cc2023-02-23 12:21:30 +01001166 ->header_like('location', qr!^http://localhost:\d+/realapi/fakeclient/return\?code=.+$!)
Akrona8efaa92022-04-09 14:45:43 +02001167 ->tx->res->headers->header('location')
1168 ;
1169
Akronf47813c2023-05-08 16:24:03 +02001170unless ($^O eq 'MSWin32') {
1171 $t->get_ok($fwd)
1172 ->status_is(200)
1173 ->content_like(qr'welcome back! \[(.+?)\]')
1174 ;
1175};
Akrona8efaa92022-04-09 14:45:43 +02001176
Akrona8f87cc2023-02-23 12:21:30 +01001177my $fake_port = $port;
1178
1179while ($fake_port == $remote_port || $fake_port == $local_port) {
1180 $fake_port++;
1181};
1182
1183$redirect_url_fakeapi = $t->app->close_redirect_to(Mojo::URL->new('http://localhost:' . $fake_port)->path($fake_backend_app->url_for('return_uri'))->to_abs->to_string);
1184
1185$fwd = $t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1186 client_id => 'xyz',
1187 state => 'abcde',
1188 scope => 'search match',
1189 redirect_uri_server => 'http://localhost:'.$port,
1190 redirect_uri => "$redirect_url_fakeapi",
1191 csrf_token => $csrf,
1192}))
1193 ->status_is(302)
1194 ->header_unlike('location', qr!^http://localhost:\d+/realapi/fakeclient/return\?error_description=Connection\+refused$!)
1195 ->header_like('location', qr!^http://localhost:\d+/realapi/fakeclient/return\?code=.+?$!)
1196 ->tx->res->headers->header('location')
1197 ;
1198
Akrona8efaa92022-04-09 14:45:43 +02001199$t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1200 client_id => 'xyz',
Akron9ccf69a2023-01-31 14:21:37 +01001201 state => 'abcde',
1202 scope => 'search match',
1203 redirect_uri_server => 'http://example.com/',
1204 redirect_uri => $t->app->close_redirect_to('http://wrong'),
1205 csrf_token => $csrf,
1206}))
1207 ->status_is(302)
1208 ->header_is('location', '/settings/oauth')
1209 ->tx->res->headers->header('location')
1210 ;
1211
1212$t->get_ok('/settings/oauth')
1213 ->text_is('div.notify-error', 'Invalid redirect URI')
1214 ;
1215
1216$t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
1217 client_id => 'xyz',
Akrona8efaa92022-04-09 14:45:43 +02001218 state => 'fail',
1219 scope => 'search match',
Akron9d826902023-01-25 10:20:52 +01001220# redirect_uri_server => 'http://example.com/',
Akrona8efaa92022-04-09 14:45:43 +02001221 redirect_uri => $fake_backend_app->url_for('return_uri')->to_abs,
1222 csrf_token => $csrf,
1223}))
1224 ->status_is(302)
Akron9d826902023-01-25 10:20:52 +01001225 ->header_is('location', '/realapi/fakeclient/return?error_description=FAIL')
Akrona8efaa92022-04-09 14:45:43 +02001226 ;
1227
Akron9f2ad342022-05-04 16:16:40 +02001228my $json_post = {
1229 name => 'Funny',
1230 type => 'PUBLIC',
Akron6b75d122022-05-12 17:39:05 +02001231 desc => 'This is my plugin application 2',
Helge9f9d4852024-12-09 16:06:34 +01001232 url => 'https://xyz/123',
Akron9f2ad342022-05-04 16:16:40 +02001233 csrf_token => $csrf,
1234 src => 'hMMM'
1235};
1236
Helge9f9d4852024-12-09 16:06:34 +01001237
Akron9f2ad342022-05-04 16:16:40 +02001238$t->post_ok('/settings/oauth/register' => form => $json_post)
1239 ->status_is(200)
1240 ->element_exists('div.notify-error')
Akron99968a92022-06-03 12:32:07 +02001241 ->text_is('div.notify-error', 'Plugin declarations need to be json files')
1242 ;
1243
1244$json_post->{src} = {
1245 content => 'jjjjjj',
1246 filename => 'fun.txt'
1247};
1248
1249$t->post_ok('/settings/oauth/register' => form => $json_post)
1250 ->status_is(200)
1251 ->element_exists('div.notify-error')
Akron9f2ad342022-05-04 16:16:40 +02001252 ->text_is('div.notify-error', 'Plugins need to be confidential')
1253 ;
1254
1255$json_post->{type} = 'CONFIDENTIAL';
1256
Akron99968a92022-06-03 12:32:07 +02001257# This somehow gets removed in the last form send ...
Akron9f2ad342022-05-04 16:16:40 +02001258$json_post->{src} = {
1259 content => 'jjjjjj',
1260 filename => 'fun.txt'
1261};
1262
1263$t->post_ok('/settings/oauth/register' => form => $json_post)
1264 ->status_is(200)
1265 ->element_exists('div.notify-error')
1266 ->text_is('div.notify-error', 'Plugin declarations need to be json files')
1267 ;
1268
1269$json_post->{src} = {
1270 content => '{"name":"example"}',
1271 filename => 'fun.txt'
1272};
1273
1274$t->post_ok('/settings/oauth/register' => form => $json_post)
1275 ->status_is(200)
1276 ->element_exists_not('div.notify-error')
1277 ->element_exists('div.notify-success')
1278 ->text_is('div.notify-success', 'Registration successful')
1279 ;
1280
Akron6b75d122022-05-12 17:39:05 +02001281$t->get_ok('/settings/oauth/jh0gfjhjbfdsgzjghj==')
1282 ->status_is(200)
1283 ->text_is('div.notify-error', undef)
1284 ->text_is('li.client #client_source', '{"name":"example"}')
1285 ->text_is('li.client span.client-name', 'Funny')
1286 ->text_is('li.client p.client-desc', 'This is my plugin application 2')
1287 ->element_exists_not('li.client .client-redirect-uri tt')
1288 ->text_is('li.client .client-type tt', 'CONFIDENTIAL')
1289 ;
Akron9f2ad342022-05-04 16:16:40 +02001290
Akronb6b156e2022-03-31 14:57:49 +02001291
Akron66ef3b52022-11-22 14:25:15 +01001292# Retest client with super_client_file
1293my $client_file = tempfile;
1294
Akron889bc202024-03-15 17:16:55 +01001295$client_file->spew(
Akron66ef3b52022-11-22 14:25:15 +01001296 '{"client_id":"2","client_secret":"k414m4r-s3cr3t"}'
1297);
1298
1299$t = Test::Mojo::WithRoles->new('Kalamar' => {
1300 Kalamar => {
1301 plugins => ['Auth']
1302 },
1303 'Kalamar-Auth' => {
1304 client_file => $client_file,
1305 oauth2 => 1
1306 }
1307});
1308
1309$t->app->plugin(
1310 Mount => {
1311 $mount_point =>
1312 $fixtures_path->child('mock.pl')
1313 }
1314);
1315
1316$csrf = $t->get_ok('/')
1317 ->status_is(200)
1318 ->element_exists_not('div.button.top a')
1319 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
1320 ;
1321
1322$t->post_ok('/user/login' => form => {
1323 handle_or_email => 'test',
1324 pwd => 'pass',
1325 csrf_token => $csrf
1326})
1327 ->status_is(302)
1328 ->header_is('Location' => '/')
1329 ->content_is('');
1330
1331$t->get_ok('/')
1332 ->status_is(200)
1333 ->element_exists_not('div.notify-error')
1334 ->element_exists('div.notify-success')
1335 ->text_is('div.notify-success', 'Login successful')
1336 ->element_exists_not('aside.off')
1337 ->element_exists_not('aside.active')
1338 ->element_exists('aside.settings')
1339 ;
1340
Akron53a171e2022-12-05 18:22:58 +01001341$main::ENV{KALAMAR_CLIENT_FILE} = $client_file;
1342
1343$t = Test::Mojo::WithRoles->new('Kalamar' => {
1344 Kalamar => {
1345 plugins => ['Auth']
1346 },
1347 'Kalamar-Auth' => {
1348 oauth2 => 1,
1349# client_file => $client_file,
1350 }
1351});
1352
1353$t->app->plugin(
1354 Mount => {
1355 $mount_point =>
1356 $fixtures_path->child('mock.pl')
1357 }
1358);
1359
1360$csrf = $t->get_ok('/')
1361 ->status_is(200)
1362 ->element_exists_not('div.button.top a')
1363 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
1364 ;
1365
1366$t->post_ok('/user/login' => form => {
1367 handle_or_email => 'test',
1368 pwd => 'pass',
1369 csrf_token => $csrf
1370})
1371 ->status_is(302)
1372 ->header_is('Location' => '/')
1373 ->content_is('');
1374
1375$t->get_ok('/')
1376 ->status_is(200)
1377 ->element_exists_not('div.notify-error')
1378 ->element_exists('div.notify-success')
1379 ->text_is('div.notify-success', 'Login successful')
1380 ->element_exists_not('aside.off')
1381 ->element_exists_not('aside.active')
1382 ->element_exists('aside.settings')
1383 ;
1384
Akron66ef3b52022-11-22 14:25:15 +01001385
Akron33f5c672019-06-24 19:40:47 +02001386done_testing;
1387__END__
Akrona8efaa92022-04-09 14:45:43 +02001388
1389