Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::More; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 3 | use Test::Mojo::WithRoles 'Session'; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 4 | use Mojo::File qw/path/; |
| 5 | use Data::Dumper; |
| 6 | |
| 7 | |
| 8 | ##################### |
| 9 | # Start Fake server # |
| 10 | ##################### |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 11 | my $mount_point = '/realapi/'; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 12 | $ENV{KALAMAR_API} = $mount_point; |
| 13 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 14 | my $t = Test::Mojo::WithRoles->new('Kalamar' => { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 15 | Kalamar => { |
| 16 | plugins => ['Auth'] |
| 17 | }, |
| 18 | 'Kalamar-Auth' => { |
| 19 | client_id => 2, |
| 20 | client_secret => 'k414m4r-s3cr3t', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 21 | oauth2 => 1, |
| 22 | experimental_client_registration => 1 |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 23 | } |
| 24 | }); |
| 25 | |
| 26 | # Mount fake backend |
| 27 | # Get the fixture path |
| 28 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server'); |
| 29 | my $fake_backend = $t->app->plugin( |
| 30 | Mount => { |
| 31 | $mount_point => |
| 32 | $fixtures_path->child('mock.pl') |
| 33 | } |
| 34 | ); |
| 35 | # Configure fake backend |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 36 | my $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 | |
| 41 | my $access_token = $fake_backend_app->get_token('access_token'); |
| 42 | my $refresh_token = $fake_backend_app->get_token('refresh_token'); |
| 43 | my $access_token_2 = $fake_backend_app->get_token('access_token_2'); |
| 44 | my $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 | |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 100 | my $q = qr!(?:\"|")!; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 101 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 102 | $t->get_ok('/realapi/v1.0') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 103 | ->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+$/) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 110 | ->content_like(qr/${q}authorized${q}:null/) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 111 | ->element_exists_not('div.button.top a') |
| 112 | ->element_exists_not('aside.active') |
| 113 | ->element_exists_not('aside.off') |
| 114 | ; |
| 115 | |
| 116 | $t->get_ok('/') |
| 117 | ->status_is(200) |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 118 | ->element_exists('form[action=/user/login] input[name=handle]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 119 | ->element_exists('aside.active') |
| 120 | ->element_exists_not('aside.off') |
| 121 | ; |
| 122 | |
Akron | 3e0fdc1 | 2020-05-15 16:17:21 +0200 | [diff] [blame] | 123 | # Test for bug with long password |
| 124 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 125 | handle => 'test', |
Akron | 3e0fdc1 | 2020-05-15 16:17:21 +0200 | [diff] [blame] | 126 | pwd => 'kjskjhndkjndqknaskjnakjdnkjdankajdnkjdsankjdsakjdfkjahzroiuqzriudjoijdmlamdlkmdsalkmdl' }) |
| 127 | ->status_is(302) |
| 128 | ->header_is('Location' => '/'); |
| 129 | |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 130 | $t->post_ok('/user/login' => form => { handle => 'test', pwd => 'fail' }) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 131 | ->status_is(302) |
| 132 | ->header_is('Location' => '/'); |
| 133 | |
| 134 | $t->get_ok('/') |
| 135 | ->status_is(200) |
| 136 | ->element_exists('div.notify-error') |
| 137 | ->text_is('div.notify-error', 'Bad CSRF token') |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 138 | ->element_exists('input[name=handle][value=test]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 139 | ->element_exists_not('div.button.top a') |
| 140 | ; |
| 141 | |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 142 | $t->post_ok('/user/login' => form => { handle => 'test', pwd => 'pass' }) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 143 | ->status_is(302) |
| 144 | ->header_is('Location' => '/'); |
| 145 | |
| 146 | my $csrf = $t->get_ok('/') |
| 147 | ->status_is(200) |
| 148 | ->element_exists('div.notify-error') |
| 149 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 150 | ->element_exists_not('div.button.top a') |
| 151 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 152 | ; |
| 153 | |
| 154 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 155 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 156 | pwd => 'ldaperr', |
| 157 | csrf_token => $csrf |
| 158 | }) |
| 159 | ->status_is(302) |
| 160 | ->content_is('') |
| 161 | ->header_is('Location' => '/'); |
| 162 | |
| 163 | $csrf = $t->get_ok('/') |
| 164 | ->status_is(200) |
| 165 | ->element_exists('div.notify-error') |
| 166 | ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!') |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 167 | ->element_exists('input[name=handle][value=test]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 168 | ->element_exists_not('div.button.top a') |
Akron | 3b3c7af | 2020-05-15 16:23:55 +0200 | [diff] [blame] | 169 | ->element_exists_not('div.notify-success') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 170 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 171 | ; |
| 172 | |
| 173 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 174 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 175 | pwd => 'unknown', |
| 176 | csrf_token => $csrf |
| 177 | }) |
| 178 | ->status_is(302) |
| 179 | ->content_is('') |
| 180 | ->header_is('Location' => '/'); |
| 181 | |
| 182 | $csrf = $t->get_ok('/') |
| 183 | ->status_is(200) |
| 184 | ->element_exists('div.notify-error') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 185 | ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!') |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 186 | ->element_exists('input[name=handle][value=test]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 187 | ->element_exists_not('div.button.top a') |
| 188 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 189 | ; |
| 190 | |
| 191 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 192 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 193 | pwd => 'pass', |
| 194 | csrf_token => $csrf |
| 195 | }) |
| 196 | ->status_is(302) |
| 197 | ->content_is('') |
| 198 | ->header_is('Location' => '/'); |
| 199 | |
| 200 | $t->get_ok('/') |
| 201 | ->status_is(200) |
| 202 | ->element_exists_not('div.notify-error') |
| 203 | ->element_exists('div.notify-success') |
| 204 | ->text_is('div.notify-success', 'Login successful') |
| 205 | ->element_exists('aside.off') |
| 206 | ->element_exists_not('aside.active') |
| 207 | ; |
| 208 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 209 | # Now the user is logged in and should be able to |
| 210 | # search with authorization |
| 211 | $t->get_ok('/?q=Baum') |
| 212 | ->status_is(200) |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 213 | ->session_has('/auth') |
| 214 | ->session_is('/auth', 'Bearer ' . $access_token) |
| 215 | ->session_is('/auth_r', $refresh_token) |
| 216 | ->session_is('/user', 'test') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 217 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 218 | ->text_like('#total-results', qr/\d+$/) |
| 219 | ->element_exists_not('div.notify-error') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 220 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 221 | ->element_exists('div.button.top a') |
| 222 | ->element_exists('div.button.top a.logout[title~="test"]') |
| 223 | ; |
| 224 | |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 225 | $t->get_ok('/?q=Paum') |
| 226 | ->status_is(200) |
| 227 | ->text_like('h1 span', qr/KorAP: Find .Paum./i) |
| 228 | ->text_is('#total-results', '') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 229 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 230 | ->element_exists_not('p.hint') |
| 231 | ; |
| 232 | |
| 233 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 234 | # Logout |
| 235 | $t->get_ok('/user/logout') |
| 236 | ->status_is(302) |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 237 | ->session_hasnt('/auth') |
| 238 | ->session_hasnt('/auth_r') |
| 239 | ->session_hasnt('/user') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 240 | ->header_is('Location' => '/'); |
| 241 | |
| 242 | $t->get_ok('/') |
| 243 | ->status_is(200) |
| 244 | ->element_exists_not('div.notify-error') |
| 245 | ->element_exists('div.notify-success') |
| 246 | ->text_is('div.notify-success', 'Logout successful') |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 247 | ->element_exists("input[name=handle]") |
| 248 | ->element_exists("input[name=handle][value=test]") |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 249 | ; |
| 250 | |
| 251 | $t->get_ok('/?q=Baum') |
| 252 | ->status_is(200) |
| 253 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 254 | ->text_like('#total-results', qr/\d+$/) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 255 | ->content_like(qr/${q}authorized${q}:null/) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 256 | ; |
| 257 | |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 258 | $t->get_ok('/?q=Paum') |
| 259 | ->status_is(200) |
| 260 | ->text_like('h1 span', qr/KorAP: Find .Paum./i) |
| 261 | ->text_is('#total-results', '') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 262 | ->content_like(qr/${q}authorized${q}:null/) |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 263 | ->text_is('p.hint', 'Maybe you need to log in first?') |
| 264 | ; |
| 265 | |
| 266 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 267 | # Get redirect |
| 268 | my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp') |
| 269 | ->status_is(200) |
| 270 | ->element_exists_not('div.notify-error') |
| 271 | ->tx->res->dom->at('input[name=fwd]')->attr('value') |
| 272 | ; |
| 273 | |
| 274 | is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid'); |
| 275 | |
| 276 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 277 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 278 | pwd => 'pass', |
| 279 | csrf_token => $csrf, |
| 280 | fwd => 'http://bad.example.com/test' |
| 281 | }) |
| 282 | ->status_is(302) |
| 283 | ->header_is('Location' => '/'); |
| 284 | |
| 285 | $t->get_ok('/') |
| 286 | ->status_is(200) |
| 287 | ->element_exists('div.notify-error') |
| 288 | ->element_exists_not('div.notify-success') |
| 289 | ->text_is('div.notify-error', 'Redirect failure') |
| 290 | ; |
| 291 | |
| 292 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 293 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 294 | pwd => 'pass', |
| 295 | csrf_token => $csrf, |
| 296 | fwd => $fwd |
| 297 | }) |
| 298 | ->status_is(302) |
| 299 | ->header_is('Location' => '/?q=Baum&ql=poliqarp'); |
| 300 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 301 | $t->get_ok('/?q=Baum&ql=poliqarp') |
| 302 | ->status_is(200) |
| 303 | ->element_exists_not('div.notify-error') |
| 304 | ->element_exists('div.notify-success') |
| 305 | ->text_is('div.notify-success', 'Login successful') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 306 | ->session_has('/auth') |
| 307 | ->session_is('/auth', 'Bearer ' . $access_token) |
| 308 | ->session_is('/auth_r', $refresh_token) |
| 309 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 310 | ; |
| 311 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 312 | # Expire the session |
| 313 | # (makes the token be marked as expired - though it isn't serverside) |
| 314 | $t->get_ok('/x/expire') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 315 | ->status_is(200) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 316 | ->content_is('okay') |
| 317 | ; |
| 318 | |
| 319 | ## It may be a problem, but the cache is still valid |
| 320 | $t->get_ok('/?q=Baum') |
| 321 | ->status_is(200) |
| 322 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 323 | ->text_like('#total-results', qr/\d+$/) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 324 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 325 | ->header_is('X-Kalamar-Cache', 'true') |
| 326 | ; |
| 327 | |
| 328 | # Query without partial cache (unfortunately) (but no total results) |
| 329 | $t->get_ok('/?q=baum&cutoff=true') |
| 330 | ->status_is(200) |
| 331 | ->session_is('/auth', 'Bearer ' . $access_token_2) |
| 332 | ->session_is('/auth_r', $refresh_token_2) |
| 333 | ->text_is('#error','') |
| 334 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 335 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 336 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 337 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 338 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 339 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 340 | ->element_exists_not('#total-results') |
| 341 | ; |
| 342 | |
| 343 | # Expire the session and remove the refresh option |
| 344 | $t->get_ok('/x/expire-no-refresh') |
| 345 | ->status_is(200) |
| 346 | ->content_is('okay') |
| 347 | ; |
| 348 | |
| 349 | $t->app->defaults(no_cache => 1); |
| 350 | |
| 351 | |
| 352 | $t->get_ok('/x/invalid-no-refresh') |
| 353 | ->status_is(200) |
| 354 | ->content_is('okay') |
| 355 | ; |
| 356 | |
| 357 | # Query without cache |
| 358 | # The token is invalid and can't be refreshed! |
| 359 | $t->get_ok('/?q=baum&cutoff=true') |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 360 | ->status_is(400) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 361 | ->session_hasnt('/auth') |
| 362 | ->session_hasnt('/auth_r') |
| 363 | ->text_is('#error','') |
| 364 | ->text_is('div.notify-error','Access token invalid') |
| 365 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 366 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 367 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 368 | ->content_unlike(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 369 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 370 | ->element_exists('p.no-results') |
| 371 | ; |
| 372 | |
| 373 | $t->get_ok('/x/invalid') |
| 374 | ->status_is(200) |
| 375 | ->content_is('okay') |
| 376 | ; |
| 377 | |
| 378 | # Query without cache |
| 379 | # The token is invalid and can't be refreshed! |
| 380 | $t->get_ok('/?q=baum&cutoff=true') |
| 381 | ->status_is(200) |
| 382 | ->session_is('/auth', 'Bearer ' . $access_token_2) |
| 383 | ->session_is('/auth_r', $refresh_token_2) |
| 384 | ->text_is('#error','') |
| 385 | ->element_exists_not('div.notify-error','Access token invalid') |
| 386 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 387 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 388 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 389 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 390 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 391 | ->element_exists_not('p.no-results') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 392 | ; |
| 393 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 394 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 395 | $t->get_ok('/x/expired-with-wrong-refresh') |
| 396 | ->status_is(200) |
| 397 | ->content_is('okay') |
| 398 | ; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 399 | |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 400 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 401 | # The token is invalid and can't be refreshed! |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 402 | $csrf = $t->get_ok('/?q=baum&cutoff=true') |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 403 | ->status_is(400) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 404 | ->session_hasnt('/auth') |
| 405 | ->session_hasnt('/auth_r') |
| 406 | ->text_is('#error','') |
| 407 | ->text_is('div.notify-error','Refresh token is expired') |
| 408 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 409 | ->content_unlike(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 410 | ->element_exists('p.no-results') |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 411 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 412 | ->attr('value') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 413 | ; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 414 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 415 | # Login: |
| 416 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 417 | handle => 'test', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 418 | pwd => 'pass', |
| 419 | csrf_token => $csrf |
| 420 | }) |
| 421 | ->status_is(302) |
| 422 | ->content_is('') |
| 423 | ->header_is('Location' => '/'); |
| 424 | |
| 425 | $t->get_ok('/') |
| 426 | ->status_is(200) |
| 427 | ->element_exists_not('div.notify-error') |
| 428 | ->element_exists('div.notify-success') |
| 429 | ->text_is('div.notify-success', 'Login successful') |
| 430 | ->element_exists('aside.off') |
| 431 | ->element_exists_not('aside.active') |
| 432 | ; |
| 433 | |
| 434 | $t->get_ok('/settings/oauth') |
| 435 | ->text_is('form.form-table legend', 'Register new client application') |
| 436 | ->attr_is('form.oauth-register','action', '/settings/oauth/register') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 437 | ->element_exists('ul.client-list') |
| 438 | ->element_exists_not('ul.client-list > li') |
| 439 | # ->text_is('ul.client-list > li > span.client-name', 'R statistical computing tool ') |
| 440 | # ->text_is('ul.client-list > li > span.client-desc', 'R is a free software environment for statistical computing and graphics.') |
| 441 | # ->text_is('ul.client-list > li > span.client-url a', 'https://www.r-project.org/') |
| 442 | # ->text_is('ul.client-list > li a.client-unregister', 'Unregister') |
| 443 | # ->attr_is('ul.client-list > li a.client-unregister', 'href', '/settings/oauth/unregister/9aHsGW6QflV13ixNpez?name=R+statistical+computing+tool') |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 444 | ; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 445 | $csrf = $t->post_ok('/settings/oauth/register' => form => { |
| 446 | name => 'MyApp', |
| 447 | type => 'PUBLIC', |
| 448 | desc => 'This is my application' |
| 449 | }) |
| 450 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 451 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 452 | ->attr('value') |
| 453 | ; |
| 454 | |
| 455 | $t->post_ok('/settings/oauth/register' => form => { |
| 456 | name => 'MyApp', |
| 457 | type => 'CONFIDENTIAL', |
| 458 | desc => 'This is my application', |
| 459 | csrf_token => $csrf |
| 460 | }) |
| 461 | ->status_is(200) |
| 462 | ->element_exists('div.notify-success') |
| 463 | ->text_is('legend', 'Client Credentials') |
| 464 | ->text_is('label[for=client_id]', 'ID of the client application') |
| 465 | ->element_exists('input[name=client_id][readonly][value]') |
| 466 | ->element_exists('input[name=client_secret][readonly][value]') |
| 467 | ; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 468 | |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 469 | $t->get_ok('/settings/oauth') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 470 | ->text_is('.form-table legend', 'Register new client application') |
| 471 | ->attr_is('.oauth-register','action', '/settings/oauth/register') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 472 | ->text_is('ul.client-list > li > span.client-name a', 'MyApp') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 473 | ->text_is('ul.client-list > li > span.client-desc', 'This is my application') |
| 474 | ->text_is('ul.client-list > li > span.client-url a', '') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 475 | ; |
| 476 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 477 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 478 | ->status_is(200) |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 479 | ->text_is('ul.client-list > li.client > span.client-name', 'MyApp') |
| 480 | ->text_is('ul.client-list > li.client > span.client-desc', 'This is my application') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 481 | ->text_is('a.client-unregister', 'Unregister') |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 482 | ->attr_is('a.client-unregister', 'href', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister?name=MyApp') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 483 | ; |
| 484 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 485 | $csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister?name=MyApp') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 486 | ->content_like(qr!Do you really want to unregister \<span class="client-name"\>MyApp\<\/span\>?!) |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 487 | ->attr_is('.form-table input[name=client-name]', 'value', 'MyApp') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 488 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 489 | ->attr('value') |
| 490 | ; |
| 491 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 492 | $t->post_ok('/settings/oauth/xxxx==/unregister' => form => { |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 493 | 'client-name' => 'MyApp', |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 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') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 501 | ->text_is('.form-table legend', 'Register new client application') |
| 502 | ->attr_is('.oauth-register','action', '/settings/oauth/register') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 503 | ->element_exists('ul.client-list > li') |
| 504 | ->text_is('div.notify', 'Unknown client with xxxx==.') |
| 505 | ; |
| 506 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 507 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister' => form => { |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 508 | 'client-name' => 'MyApp', |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 509 | 'csrf_token' => $csrf |
| 510 | })->status_is(302) |
| 511 | ->content_is('') |
| 512 | ->header_is('Location' => '/settings/oauth') |
| 513 | ; |
| 514 | |
| 515 | $t->get_ok('/settings/oauth') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 516 | ->text_is('.form-table legend', 'Register new client application') |
| 517 | ->attr_is('.oauth-register','action', '/settings/oauth/register') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 518 | ->element_exists_not('ul.client-list > li') |
| 519 | ->text_is('div.notify-success', 'Successfully deleted MyApp') |
| 520 | ; |
| 521 | |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 522 | $t->post_ok('/settings/oauth/register' => form => { |
| 523 | name => 'MyApp2', |
| 524 | type => 'PUBLIC', |
| 525 | desc => 'This is my application', |
| 526 | csrf_token => $csrf |
| 527 | })->status_is(200) |
| 528 | ->element_exists('div.notify-success') |
| 529 | ->text_is('legend', 'Client Credentials') |
| 530 | ->text_is('label[for=client_id]', 'ID of the client application') |
| 531 | ->element_exists('input[name=client_id][readonly][value]') |
| 532 | ->element_exists_not('input[name=client_secret][readonly][value]') |
| 533 | ; |
| 534 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 535 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 536 | ->text_is('.client-name', 'MyApp2') |
| 537 | ->text_is('.client-desc', 'This is my application') |
| 538 | ->text_is('.client-issue-token', 'IssueToken') |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 539 | ->attr_is('.client-issue-token', 'href', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 540 | ; |
| 541 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 542 | $csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 543 | ->status_is(200) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 544 | ->attr_is('#issue-token','action', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 545 | ->attr_is('input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==') |
| 546 | ->attr_is('input[name=name]', 'value', 'MyApp2') |
| 547 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 548 | ->attr('value') |
| 549 | ; |
| 550 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 551 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token' => form => { |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 552 | csrf_token => $csrf, |
| 553 | name => 'MyApp2', |
| 554 | 'client-id' => 'fCBbQkA2NDA3MzM1Yw==' |
| 555 | }) |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 556 | ->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 557 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 558 | ; |
| 559 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 560 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
| 561 | ->status_is(200) |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 562 | ->text_is('div.notify-success', 'New access token created') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 563 | ; |
| 564 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 565 | $csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 566 | ->status_is(200) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 567 | ->attr_is('form.token-revoke', 'action', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token/revoke') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 568 | ->attr_is('form.token-revoke input[name=token]', 'value', 'jhkhkjhk_hjgjsfz67i') |
| 569 | ->attr_is('form.token-revoke input[name=name]', 'value', 'MyApp2') |
| 570 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 571 | ->attr('value') |
| 572 | ; |
| 573 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 574 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token/revoke' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 575 | csrf_token => $csrf, |
| 576 | name => 'MyApp2', |
| 577 | token => 'jhkhkjhk_hjgjsfz67i' |
| 578 | }) |
| 579 | ->status_is(200) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 580 | ->attr_is('form#revoke-token','action','/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 581 | ->attr_is('form#revoke-token','method','POST') |
| 582 | ->attr_is('form#revoke-token input[name=token]','value','jhkhkjhk_hjgjsfz67i') |
| 583 | ; |
| 584 | |
| 585 | |
| 586 | # CSRF missing |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 587 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 588 | name => 'MyApp2', |
| 589 | token => 'jhkhkjhk_hjgjsfz67i' |
| 590 | })->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 591 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 592 | ; |
| 593 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 594 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 595 | ->element_exists_not('div.notify-success') |
| 596 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 597 | ; |
| 598 | |
| 599 | # Token missing |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 600 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 601 | name => 'MyApp2', |
| 602 | csrf_token => $csrf, |
| 603 | })->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 604 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 605 | ; |
| 606 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 607 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 608 | ->element_exists_not('div.notify-success') |
| 609 | ->text_is('div.notify-error', 'Some fields are invalid') |
| 610 | ; |
| 611 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 612 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 613 | name => 'MyApp2', |
| 614 | csrf_token => $csrf, |
| 615 | token => 'jhkhkjhk_hjgjsfz67i' |
| 616 | })->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 617 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 618 | ; |
| 619 | |
| 620 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame^] | 621 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 622 | ->element_exists_not('div.notify-error') |
| 623 | ->text_is('div.notify-success', 'Token was revoked successfully') |
| 624 | ; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 625 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 626 | done_testing; |
| 627 | __END__ |