Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::More; |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 3 | use Mojo::ByteStream 'b'; |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 4 | use Test::Mojo::WithRoles 'Session'; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 5 | use Mojo::File qw/path/; |
| 6 | use Data::Dumper; |
| 7 | |
| 8 | |
| 9 | ##################### |
| 10 | # Start Fake server # |
| 11 | ##################### |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 12 | my $mount_point = '/realapi/'; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 13 | $ENV{KALAMAR_API} = $mount_point; |
| 14 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 15 | my $t = Test::Mojo::WithRoles->new('Kalamar' => { |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 16 | Kalamar => { |
| 17 | plugins => ['Auth'] |
| 18 | }, |
| 19 | 'Kalamar-Auth' => { |
| 20 | client_id => 2, |
| 21 | client_secret => 'k414m4r-s3cr3t', |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 22 | oauth2 => 1, |
| 23 | experimental_client_registration => 1 |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 24 | } |
| 25 | }); |
| 26 | |
| 27 | # Mount fake backend |
| 28 | # Get the fixture path |
| 29 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server'); |
| 30 | my $fake_backend = $t->app->plugin( |
| 31 | Mount => { |
| 32 | $mount_point => |
| 33 | $fixtures_path->child('mock.pl') |
| 34 | } |
| 35 | ); |
| 36 | # Configure fake backend |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 37 | my $fake_backend_app = $fake_backend->pattern->defaults->{app}; |
| 38 | |
| 39 | # Set general app logger for simplicity |
| 40 | $fake_backend_app->log($t->app->log); |
| 41 | |
| 42 | my $access_token = $fake_backend_app->get_token('access_token'); |
| 43 | my $refresh_token = $fake_backend_app->get_token('refresh_token'); |
| 44 | my $access_token_2 = $fake_backend_app->get_token('access_token_2'); |
| 45 | my $refresh_token_2 = $fake_backend_app->get_token('refresh_token_2'); |
| 46 | |
| 47 | # Some routes to modify the session |
| 48 | # This expires the session |
| 49 | $t->app->routes->get('/x/expire')->to( |
| 50 | cb => sub { |
| 51 | my $c = shift; |
| 52 | $c->session(auth_exp => 0); |
| 53 | return $c->render(text => 'okay') |
| 54 | } |
| 55 | ); |
| 56 | |
| 57 | # This expires the session and removes the refresh token |
| 58 | $t->app->routes->get('/x/expire-no-refresh')->to( |
| 59 | cb => sub { |
| 60 | my $c = shift; |
| 61 | $c->session(auth_exp => 0); |
| 62 | delete $c->session->{auth_r}; |
| 63 | return $c->render(text => 'okay') |
| 64 | } |
| 65 | ); |
| 66 | |
| 67 | # This sets an invalid token |
| 68 | $t->app->routes->get('/x/invalid')->to( |
| 69 | cb => sub { |
| 70 | my $c = shift; |
| 71 | $c->session(auth_exp => time + 1000); |
| 72 | $c->session(auth_r => $refresh_token_2); |
| 73 | $c->session(auth => 'Bearer inv4lid'); |
| 74 | return $c->render(text => 'okay') |
| 75 | } |
| 76 | ); |
| 77 | |
| 78 | |
| 79 | # This sets an invalid token |
| 80 | $t->app->routes->get('/x/invalid-no-refresh')->to( |
| 81 | cb => sub { |
| 82 | my $c = shift; |
| 83 | $c->session(auth_exp => time + 1000); |
| 84 | delete $c->session->{auth_r}; |
| 85 | $c->session(auth => 'Bearer inv4lid'); |
| 86 | return $c->render(text => 'okay') |
| 87 | } |
| 88 | ); |
| 89 | |
| 90 | # This sets an invalid refresh token |
| 91 | $t->app->routes->get('/x/expired-with-wrong-refresh')->to( |
| 92 | cb => sub { |
| 93 | my $c = shift; |
| 94 | $c->session(auth_exp => 0); |
| 95 | $c->session(auth => 'Bearer inv4lid'); |
| 96 | $c->session(auth_r => 'inv4lid'); |
| 97 | return $c->render(text => 'okay') |
| 98 | } |
| 99 | ); |
| 100 | |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 101 | my $q = qr!(?:\"|")!; |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 102 | |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 103 | $t->get_ok('/realapi/v1.0') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 104 | ->status_is(200) |
| 105 | ->content_is('Fake server available'); |
| 106 | |
| 107 | $t->get_ok('/?q=Baum') |
| 108 | ->status_is(200) |
| 109 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 110 | ->text_like('#total-results', qr/\d+$/) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 111 | ->content_like(qr/${q}authorized${q}:null/) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 112 | ->element_exists_not('div.button.top a') |
| 113 | ->element_exists_not('aside.active') |
| 114 | ->element_exists_not('aside.off') |
| 115 | ; |
| 116 | |
| 117 | $t->get_ok('/') |
| 118 | ->status_is(200) |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 119 | ->element_exists('form[action=/user/login] input[name=handle]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 120 | ->element_exists('aside.active') |
| 121 | ->element_exists_not('aside.off') |
| 122 | ; |
| 123 | |
Akron | ff08811 | 2021-06-15 15:26:04 +0200 | [diff] [blame] | 124 | $t->get_ok('/settings/oauth') |
| 125 | ->status_is(401) |
| 126 | ->text_is('p.no-results', 'Not authenticated') |
| 127 | ; |
| 128 | |
Akron | 3e0fdc1 | 2020-05-15 16:17:21 +0200 | [diff] [blame] | 129 | # Test for bug with long password |
| 130 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 131 | handle => 'test', |
Akron | 3e0fdc1 | 2020-05-15 16:17:21 +0200 | [diff] [blame] | 132 | pwd => 'kjskjhndkjndqknaskjnakjdnkjdankajdnkjdsankjdsakjdfkjahzroiuqzriudjoijdmlamdlkmdsalkmdl' }) |
| 133 | ->status_is(302) |
| 134 | ->header_is('Location' => '/'); |
| 135 | |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 136 | $t->post_ok('/user/login' => form => { handle => 'test', pwd => 'fail' }) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 137 | ->status_is(302) |
| 138 | ->header_is('Location' => '/'); |
| 139 | |
| 140 | $t->get_ok('/') |
| 141 | ->status_is(200) |
| 142 | ->element_exists('div.notify-error') |
| 143 | ->text_is('div.notify-error', 'Bad CSRF token') |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 144 | ->element_exists('input[name=handle][value=test]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 145 | ->element_exists_not('div.button.top a') |
| 146 | ; |
| 147 | |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 148 | $t->post_ok('/user/login' => form => { handle => 'test', pwd => 'pass' }) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 149 | ->status_is(302) |
| 150 | ->header_is('Location' => '/'); |
| 151 | |
| 152 | my $csrf = $t->get_ok('/') |
| 153 | ->status_is(200) |
| 154 | ->element_exists('div.notify-error') |
| 155 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 156 | ->element_exists_not('div.button.top a') |
| 157 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 158 | ; |
| 159 | |
| 160 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 161 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 162 | pwd => 'ldaperr', |
| 163 | csrf_token => $csrf |
| 164 | }) |
| 165 | ->status_is(302) |
| 166 | ->content_is('') |
| 167 | ->header_is('Location' => '/'); |
| 168 | |
| 169 | $csrf = $t->get_ok('/') |
| 170 | ->status_is(200) |
| 171 | ->element_exists('div.notify-error') |
| 172 | ->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] | 173 | ->element_exists('input[name=handle][value=test]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 174 | ->element_exists_not('div.button.top a') |
Akron | 3b3c7af | 2020-05-15 16:23:55 +0200 | [diff] [blame] | 175 | ->element_exists_not('div.notify-success') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 176 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 177 | ; |
| 178 | |
| 179 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 180 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 181 | pwd => 'unknown', |
| 182 | csrf_token => $csrf |
| 183 | }) |
| 184 | ->status_is(302) |
| 185 | ->content_is('') |
| 186 | ->header_is('Location' => '/'); |
| 187 | |
| 188 | $csrf = $t->get_ok('/') |
| 189 | ->status_is(200) |
| 190 | ->element_exists('div.notify-error') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 191 | ->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] | 192 | ->element_exists('input[name=handle][value=test]') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 193 | ->element_exists_not('div.button.top a') |
| 194 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 195 | ; |
| 196 | |
| 197 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 198 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 199 | pwd => 'pass', |
| 200 | csrf_token => $csrf |
| 201 | }) |
| 202 | ->status_is(302) |
| 203 | ->content_is('') |
| 204 | ->header_is('Location' => '/'); |
| 205 | |
| 206 | $t->get_ok('/') |
| 207 | ->status_is(200) |
| 208 | ->element_exists_not('div.notify-error') |
| 209 | ->element_exists('div.notify-success') |
| 210 | ->text_is('div.notify-success', 'Login successful') |
Akron | 1d09b53 | 2021-06-15 18:18:25 +0200 | [diff] [blame] | 211 | ->element_exists_not('aside.off') |
Akron | dc0b3ab | 2021-06-18 11:52:43 +0200 | [diff] [blame] | 212 | ->element_exists_not('aside.active') |
Akron | 1d09b53 | 2021-06-15 18:18:25 +0200 | [diff] [blame] | 213 | ->element_exists('aside.settings') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 214 | ; |
| 215 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 216 | # Now the user is logged in and should be able to |
| 217 | # search with authorization |
| 218 | $t->get_ok('/?q=Baum') |
| 219 | ->status_is(200) |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 220 | ->session_has('/auth') |
| 221 | ->session_is('/auth', 'Bearer ' . $access_token) |
| 222 | ->session_is('/auth_r', $refresh_token) |
| 223 | ->session_is('/user', 'test') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 224 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 225 | ->text_like('#total-results', qr/\d+$/) |
| 226 | ->element_exists_not('div.notify-error') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 227 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 228 | ->element_exists('div.button.top a') |
| 229 | ->element_exists('div.button.top a.logout[title~="test"]') |
| 230 | ; |
| 231 | |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 232 | $t->get_ok('/?q=Paum') |
| 233 | ->status_is(200) |
| 234 | ->text_like('h1 span', qr/KorAP: Find .Paum./i) |
| 235 | ->text_is('#total-results', '') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 236 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 237 | ->element_exists_not('p.hint') |
| 238 | ; |
| 239 | |
Akron | cce055c | 2021-07-02 12:18:03 +0200 | [diff] [blame] | 240 | # Query with error |
| 241 | $t->get_ok('/?q=error') |
| 242 | ->status_is(400) |
| 243 | ->text_is('#notifications .notify-error','500: Internal Server Error') |
| 244 | ; |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 245 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 246 | # Logout |
| 247 | $t->get_ok('/user/logout') |
| 248 | ->status_is(302) |
Akron | 4cefe1f | 2019-09-04 10:11:28 +0200 | [diff] [blame] | 249 | ->session_hasnt('/auth') |
| 250 | ->session_hasnt('/auth_r') |
| 251 | ->session_hasnt('/user') |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 252 | ->header_is('Location' => '/'); |
| 253 | |
| 254 | $t->get_ok('/') |
| 255 | ->status_is(200) |
| 256 | ->element_exists_not('div.notify-error') |
| 257 | ->element_exists('div.notify-success') |
| 258 | ->text_is('div.notify-success', 'Logout successful') |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 259 | ->element_exists("input[name=handle]") |
| 260 | ->element_exists("input[name=handle][value=test]") |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 261 | ; |
| 262 | |
| 263 | $t->get_ok('/?q=Baum') |
| 264 | ->status_is(200) |
| 265 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 266 | ->text_like('#total-results', qr/\d+$/) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 267 | ->content_like(qr/${q}authorized${q}:null/) |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 268 | ; |
| 269 | |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 270 | $t->get_ok('/?q=Paum') |
| 271 | ->status_is(200) |
| 272 | ->text_like('h1 span', qr/KorAP: Find .Paum./i) |
| 273 | ->text_is('#total-results', '') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 274 | ->content_like(qr/${q}authorized${q}:null/) |
Akron | 27031aa | 2020-04-28 14:57:10 +0200 | [diff] [blame] | 275 | ->text_is('p.hint', 'Maybe you need to log in first?') |
| 276 | ; |
| 277 | |
| 278 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 279 | # Get redirect |
| 280 | my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp') |
| 281 | ->status_is(200) |
| 282 | ->element_exists_not('div.notify-error') |
| 283 | ->tx->res->dom->at('input[name=fwd]')->attr('value') |
| 284 | ; |
| 285 | |
| 286 | is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid'); |
| 287 | |
| 288 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 289 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 290 | pwd => 'pass', |
| 291 | csrf_token => $csrf, |
| 292 | fwd => 'http://bad.example.com/test' |
| 293 | }) |
| 294 | ->status_is(302) |
| 295 | ->header_is('Location' => '/'); |
| 296 | |
| 297 | $t->get_ok('/') |
| 298 | ->status_is(200) |
| 299 | ->element_exists('div.notify-error') |
| 300 | ->element_exists_not('div.notify-success') |
| 301 | ->text_is('div.notify-error', 'Redirect failure') |
| 302 | ; |
| 303 | |
| 304 | $t->post_ok('/user/login' => form => { |
Akron | e208d30 | 2020-11-28 11:14:50 +0100 | [diff] [blame] | 305 | handle => 'test', |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 306 | pwd => 'pass', |
| 307 | csrf_token => $csrf, |
| 308 | fwd => $fwd |
| 309 | }) |
| 310 | ->status_is(302) |
| 311 | ->header_is('Location' => '/?q=Baum&ql=poliqarp'); |
| 312 | |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 313 | $t->get_ok('/?q=Baum&ql=poliqarp') |
| 314 | ->status_is(200) |
| 315 | ->element_exists_not('div.notify-error') |
| 316 | ->element_exists('div.notify-success') |
| 317 | ->text_is('div.notify-success', 'Login successful') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 318 | ->session_has('/auth') |
| 319 | ->session_is('/auth', 'Bearer ' . $access_token) |
| 320 | ->session_is('/auth_r', $refresh_token) |
| 321 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 322 | ; |
| 323 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 324 | # Expire the session |
| 325 | # (makes the token be marked as expired - though it isn't serverside) |
| 326 | $t->get_ok('/x/expire') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 327 | ->status_is(200) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 328 | ->content_is('okay') |
| 329 | ; |
| 330 | |
| 331 | ## It may be a problem, but the cache is still valid |
| 332 | $t->get_ok('/?q=Baum') |
| 333 | ->status_is(200) |
| 334 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 335 | ->text_like('#total-results', qr/\d+$/) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 336 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 337 | ->header_is('X-Kalamar-Cache', 'true') |
| 338 | ; |
| 339 | |
| 340 | # Query without partial cache (unfortunately) (but no total results) |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 341 | my $err = $t->get_ok('/?q=baum&cutoff=true') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 342 | ->status_is(200) |
| 343 | ->session_is('/auth', 'Bearer ' . $access_token_2) |
| 344 | ->session_is('/auth_r', $refresh_token_2) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 345 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 346 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 347 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 348 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 349 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 350 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 351 | ->element_exists_not('#total-results') |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 352 | ->tx->res->dom->at('#error') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 353 | ; |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 354 | is(defined $err ? $err->text : '', ''); |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 355 | |
| 356 | # Expire the session and remove the refresh option |
| 357 | $t->get_ok('/x/expire-no-refresh') |
| 358 | ->status_is(200) |
| 359 | ->content_is('okay') |
| 360 | ; |
| 361 | |
| 362 | $t->app->defaults(no_cache => 1); |
| 363 | |
| 364 | |
| 365 | $t->get_ok('/x/invalid-no-refresh') |
| 366 | ->status_is(200) |
| 367 | ->content_is('okay') |
| 368 | ; |
| 369 | |
| 370 | # Query without cache |
| 371 | # The token is invalid and can't be refreshed! |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 372 | $err = $t->get_ok('/?q=baum&cutoff=true') |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 373 | ->status_is(400) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 374 | ->session_hasnt('/auth') |
| 375 | ->session_hasnt('/auth_r') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 376 | ->text_is('div.notify-error','Access token invalid') |
| 377 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 378 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 379 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 380 | ->content_unlike(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 381 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 382 | ->element_exists('p.no-results') |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 383 | ->tx->res->dom->at('#error') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 384 | ; |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 385 | is(defined $err ? $err->text : '', ''); |
| 386 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 387 | |
| 388 | $t->get_ok('/x/invalid') |
| 389 | ->status_is(200) |
| 390 | ->content_is('okay') |
| 391 | ; |
| 392 | |
| 393 | # Query without cache |
| 394 | # The token is invalid and can't be refreshed! |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 395 | $err = $t->get_ok('/?q=baum&cutoff=true') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 396 | ->status_is(200) |
| 397 | ->session_is('/auth', 'Bearer ' . $access_token_2) |
| 398 | ->session_is('/auth_r', $refresh_token_2) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 399 | ->element_exists_not('div.notify-error','Access token invalid') |
| 400 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 401 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 402 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 403 | ->content_like(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 404 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 405 | ->element_exists_not('p.no-results') |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 406 | ->tx->res->dom->at('#error') |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 407 | ; |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 408 | is(defined $err ? $err->text : '', ''); |
Akron | 8bbbecf | 2019-07-01 18:57:30 +0200 | [diff] [blame] | 409 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 410 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 411 | $t->get_ok('/x/expired-with-wrong-refresh') |
| 412 | ->status_is(200) |
| 413 | ->content_is('okay') |
| 414 | ; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 415 | |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 416 | |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 417 | # The token is invalid and can't be refreshed! |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 418 | my $dom = $t->get_ok('/?q=baum&cutoff=true') |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 419 | ->status_is(400) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 420 | ->session_hasnt('/auth') |
| 421 | ->session_hasnt('/auth_r') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 422 | ->text_is('div.notify-error','Refresh token is expired') |
| 423 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 424 | ->content_unlike(qr/${q}authorized${q}:${q}yes${q}/) |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 425 | ->element_exists('p.no-results') |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 426 | ->tx->res->dom; |
| 427 | |
| 428 | $csrf = $dom->at('input[name="csrf_token"]') |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 429 | ->attr('value') |
Akron | cdfd9d5 | 2019-07-23 11:35:00 +0200 | [diff] [blame] | 430 | ; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 431 | |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 432 | $err = $dom->at('#error'); |
| 433 | is(defined $err ? $err->text : '', ''); |
| 434 | |
| 435 | |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 436 | # This should fail |
| 437 | my $wide_char_login = "\x{61}\x{E5}\x{61}"; # "\x{443}\x{434}"; |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 438 | $t->post_ok('/user/login' => form => { |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 439 | handle => $wide_char_login, |
| 440 | pwd => 'pass', |
| 441 | csrf_token => $csrf, |
| 442 | fwd => $fwd |
| 443 | }) |
| 444 | ->status_is(302) |
| 445 | ->header_is('Location' => '/'); |
| 446 | |
| 447 | $t->get_ok('/') |
| 448 | ->status_is(200) |
| 449 | ->element_exists('div.notify-error') |
| 450 | ->text_is('div.notify-error', 'Invalid character in request') |
| 451 | ->element_exists('input[name=handle]:not([value])') |
| 452 | ->element_exists_not('div.button.top a') |
| 453 | ; |
| 454 | |
| 455 | # Login: |
| 456 | # UTF8 request |
| 457 | my $username = b('täst')->encode; |
| 458 | $t->post_ok('/user/login' => form => { |
| 459 | handle => $username, |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 460 | pwd => 'pass', |
| 461 | csrf_token => $csrf |
| 462 | }) |
| 463 | ->status_is(302) |
| 464 | ->content_is('') |
| 465 | ->header_is('Location' => '/'); |
| 466 | |
| 467 | $t->get_ok('/') |
| 468 | ->status_is(200) |
| 469 | ->element_exists_not('div.notify-error') |
| 470 | ->element_exists('div.notify-success') |
| 471 | ->text_is('div.notify-success', 'Login successful') |
Akron | 6a228db | 2021-10-14 15:57:00 +0200 | [diff] [blame] | 472 | ->attr_is('a.logout', 'title', "Logout: $username") |
Akron | 1d09b53 | 2021-06-15 18:18:25 +0200 | [diff] [blame] | 473 | ->element_exists_not('aside.off') |
Akron | dc0b3ab | 2021-06-18 11:52:43 +0200 | [diff] [blame] | 474 | ->element_exists_not('aside.active') |
Akron | 1d09b53 | 2021-06-15 18:18:25 +0200 | [diff] [blame] | 475 | ->element_exists('aside.settings') |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 476 | ; |
| 477 | |
| 478 | $t->get_ok('/settings/oauth') |
| 479 | ->text_is('form.form-table legend', 'Register new client application') |
| 480 | ->attr_is('form.oauth-register','action', '/settings/oauth/register') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 481 | ->element_exists('ul.client-list') |
| 482 | ->element_exists_not('ul.client-list > li') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 483 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 484 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 485 | ->header_is('Pragma','no-cache') |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 486 | ; |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 487 | |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 488 | $csrf = $t->post_ok('/settings/oauth/register' => form => { |
| 489 | name => 'MyApp', |
| 490 | type => 'PUBLIC', |
| 491 | desc => 'This is my application' |
| 492 | }) |
| 493 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 494 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 495 | ->attr('value') |
| 496 | ; |
| 497 | |
| 498 | $t->post_ok('/settings/oauth/register' => form => { |
| 499 | name => 'MyApp', |
| 500 | type => 'CONFIDENTIAL', |
| 501 | desc => 'This is my application', |
| 502 | csrf_token => $csrf |
| 503 | }) |
| 504 | ->status_is(200) |
| 505 | ->element_exists('div.notify-success') |
| 506 | ->text_is('legend', 'Client Credentials') |
| 507 | ->text_is('label[for=client_id]', 'ID of the client application') |
| 508 | ->element_exists('input[name=client_id][readonly][value]') |
| 509 | ->element_exists('input[name=client_secret][readonly][value]') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 510 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 511 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 512 | ->header_is('Pragma','no-cache') |
Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame] | 513 | ; |
Akron | 4796e00 | 2019-07-05 10:13:15 +0200 | [diff] [blame] | 514 | |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 515 | my $anchor = $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 | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 518 | ->text_is('ul.client-list > li > span.client-name a', 'MyApp') |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 519 | ->text_is('ul.client-list > li > p.client-desc', 'This is my application') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 520 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 521 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 522 | ->header_is('Pragma','no-cache') |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 523 | ->tx->res->dom->at('ul.client-list > li > p.client-url a') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 524 | ; |
Akron | 58c6099 | 2021-09-07 13:11:43 +0200 | [diff] [blame] | 525 | is(defined $anchor ? $anchor->text : '', ''); |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 526 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 527 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 528 | ->status_is(200) |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 529 | ->text_is('ul.client-list > li.client > span.client-name', 'MyApp') |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 530 | ->text_is('ul.client-list > li.client > p.client-desc', 'This is my application') |
Akron | 17de86e | 2020-04-16 16:03:40 +0200 | [diff] [blame] | 531 | ->text_is('a.client-unregister', 'Unregister') |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 532 | ->attr_is('a.client-unregister', 'href', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister?name=MyApp') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 533 | ; |
| 534 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 535 | $csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister?name=MyApp') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 536 | ->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] | 537 | ->attr_is('.form-table input[name=client-name]', 'value', 'MyApp') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 538 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 539 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 540 | ->header_is('Pragma','no-cache') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 541 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 542 | ->attr('value') |
| 543 | ; |
| 544 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 545 | $t->post_ok('/settings/oauth/xxxx==/unregister' => form => { |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 546 | 'client-name' => 'MyApp', |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 547 | 'csrf_token' => $csrf |
| 548 | })->status_is(302) |
| 549 | ->content_is('') |
| 550 | ->header_is('Location' => '/settings/oauth') |
| 551 | ; |
| 552 | |
| 553 | $t->get_ok('/settings/oauth') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 554 | ->text_is('.form-table legend', 'Register new client application') |
| 555 | ->attr_is('.oauth-register','action', '/settings/oauth/register') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 556 | ->element_exists('ul.client-list > li') |
| 557 | ->text_is('div.notify', 'Unknown client with xxxx==.') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 558 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 559 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 560 | ->header_is('Pragma','no-cache') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 561 | ; |
| 562 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 563 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/unregister' => form => { |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 564 | 'client-name' => 'MyApp', |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 565 | 'csrf_token' => $csrf |
| 566 | })->status_is(302) |
| 567 | ->content_is('') |
| 568 | ->header_is('Location' => '/settings/oauth') |
| 569 | ; |
| 570 | |
| 571 | $t->get_ok('/settings/oauth') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 572 | ->text_is('.form-table legend', 'Register new client application') |
| 573 | ->attr_is('.oauth-register','action', '/settings/oauth/register') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 574 | ->element_exists_not('ul.client-list > li') |
| 575 | ->text_is('div.notify-success', 'Successfully deleted MyApp') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 576 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 577 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 578 | ->header_is('Pragma','no-cache') |
Akron | 1a9d5be | 2020-03-19 17:28:33 +0100 | [diff] [blame] | 579 | ; |
| 580 | |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 581 | $t->post_ok('/settings/oauth/register' => form => { |
| 582 | name => 'MyApp2', |
| 583 | type => 'PUBLIC', |
| 584 | desc => 'This is my application', |
| 585 | csrf_token => $csrf |
| 586 | })->status_is(200) |
| 587 | ->element_exists('div.notify-success') |
| 588 | ->text_is('legend', 'Client Credentials') |
| 589 | ->text_is('label[for=client_id]', 'ID of the client application') |
| 590 | ->element_exists('input[name=client_id][readonly][value]') |
| 591 | ->element_exists_not('input[name=client_secret][readonly][value]') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 592 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 593 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 594 | ->header_is('Pragma','no-cache') |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 595 | ->element_exists('.client-issue-token') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 596 | ; |
| 597 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 598 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 599 | ->text_is('.client-name', 'MyApp2') |
| 600 | ->text_is('.client-desc', 'This is my application') |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 601 | ->text_is('.client-issue-token', 'Issue new token') |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 602 | ->attr_is('.client-issue-token', 'href', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 603 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 604 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 605 | ->header_is('Pragma','no-cache') |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 606 | ->text_is('ul.token-list label[for=token]', 'Access Token') |
| 607 | ->text_is('p[name=created]', 'Created at ') |
| 608 | ->text_is('p[name=expires]', 'Expires in 31533851 seconds.') |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 609 | ->element_exists('.client-issue-token') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 610 | ; |
| 611 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 612 | $csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 613 | ->status_is(200) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 614 | ->attr_is('#issue-token','action', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 615 | ->attr_is('input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==') |
| 616 | ->attr_is('input[name=name]', 'value', 'MyApp2') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 617 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 618 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 619 | ->header_is('Pragma','no-cache') |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 620 | ->text_is('a.button-abort', 'Abort') |
| 621 | ->attr_is('#issue-token input[type=submit]', 'value', 'Issue new token') |
| 622 | ->content_like(qr!Issue a new token for!) |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 623 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 624 | ->attr('value') |
| 625 | ; |
| 626 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 627 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token' => form => { |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 628 | csrf_token => $csrf, |
| 629 | name => 'MyApp2', |
| 630 | 'client-id' => 'fCBbQkA2NDA3MzM1Yw==' |
| 631 | }) |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 632 | ->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 633 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 634 | ; |
| 635 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 636 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
| 637 | ->status_is(200) |
Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 638 | ->text_is('div.notify-success', 'New access token created') |
Akron | ad011bb | 2021-06-10 12:16:36 +0200 | [diff] [blame] | 639 | ->status_is(200) |
| 640 | ->header_is('Cache-Control','max-age=0, no-cache, no-store, must-revalidate') |
| 641 | ->header_is('Expires','Thu, 01 Jan 1970 00:00:00 GMT') |
| 642 | ->header_is('Pragma','no-cache') |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 643 | ; |
| 644 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 645 | $csrf = $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 646 | ->status_is(200) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 647 | ->attr_is('form.token-revoke', 'action', '/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token/revoke') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 648 | ->attr_is('form.token-revoke input[name=token]', 'value', 'jhkhkjhk_hjgjsfz67i') |
| 649 | ->attr_is('form.token-revoke input[name=name]', 'value', 'MyApp2') |
| 650 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 651 | ->attr('value') |
| 652 | ; |
| 653 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 654 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token/revoke' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 655 | csrf_token => $csrf, |
| 656 | name => 'MyApp2', |
| 657 | token => 'jhkhkjhk_hjgjsfz67i' |
| 658 | }) |
| 659 | ->status_is(200) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 660 | ->attr_is('form#revoke-token','action','/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 661 | ->attr_is('form#revoke-token','method','POST') |
| 662 | ->attr_is('form#revoke-token input[name=token]','value','jhkhkjhk_hjgjsfz67i') |
Akron | e997bb5 | 2021-06-11 16:44:06 +0200 | [diff] [blame] | 663 | ->text_is('a.button-abort', 'Abort') |
| 664 | ->attr_is('#revoke-token input[type=submit]', 'value', 'Revoke') |
| 665 | ->content_like(qr!Revoke a token for!) |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 666 | ; |
| 667 | |
| 668 | |
| 669 | # CSRF missing |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 670 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 671 | name => 'MyApp2', |
| 672 | token => 'jhkhkjhk_hjgjsfz67i' |
| 673 | })->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 674 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 675 | ; |
| 676 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 677 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 678 | ->element_exists_not('div.notify-success') |
| 679 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 680 | ; |
| 681 | |
| 682 | # Token missing |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 683 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 684 | name => 'MyApp2', |
| 685 | csrf_token => $csrf, |
| 686 | })->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 687 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 688 | ; |
| 689 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 690 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 691 | ->element_exists_not('div.notify-success') |
| 692 | ->text_is('div.notify-error', 'Some fields are invalid') |
| 693 | ; |
| 694 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 695 | $t->post_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==/token?_method=DELETE' => form => { |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 696 | name => 'MyApp2', |
| 697 | csrf_token => $csrf, |
| 698 | token => 'jhkhkjhk_hjgjsfz67i' |
| 699 | })->status_is(302) |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 700 | ->header_is('Location','/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 701 | ; |
| 702 | |
Akron | 041ca4d | 2021-06-10 11:52:51 +0200 | [diff] [blame] | 703 | $t->get_ok('/settings/oauth/fCBbQkA2NDA3MzM1Yw==') |
Akron | c1aaf93 | 2021-06-09 12:19:15 +0200 | [diff] [blame] | 704 | ->element_exists_not('div.notify-error') |
| 705 | ->text_is('div.notify-success', 'Token was revoked successfully') |
| 706 | ; |
Akron | 83209f7 | 2021-01-29 17:54:15 +0100 | [diff] [blame] | 707 | |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 708 | $t->app->routes->get('/x/redirect-target')->to( |
| 709 | cb => sub { |
| 710 | my $c = shift; |
| 711 | return $c->render(text => 'redirected'); |
| 712 | } |
| 713 | ); |
| 714 | |
| 715 | $csrf = $t->post_ok('/settings/oauth/register' => form => { |
| 716 | name => 'MyConfApp', |
| 717 | type => 'CONFIDENTIAL', |
| 718 | desc => 'This is my application', |
| 719 | }) |
| 720 | ->text_is('div.notify-error', 'Bad CSRF token') |
| 721 | ->tx->res->dom->at('input[name="csrf_token"]') |
| 722 | ->attr('value') |
| 723 | ; |
| 724 | |
| 725 | $t->post_ok('/settings/oauth/register' => form => { |
| 726 | name => 'MyConfApp', |
| 727 | type => 'CONFIDENTIAL', |
| 728 | desc => 'This is my confidential application', |
| 729 | csrf_token => $csrf, |
| 730 | redirect_uri => 'http://localhost/redirect-target' |
| 731 | }) |
| 732 | ->text_is('div.notify-error', undef) |
| 733 | ->text_is('li.client span.client-name', 'MyConfApp') |
| 734 | ->text_is('li.client p.client-desc', 'This is my confidential application') |
| 735 | ->text_is('li.client .client-redirect-uri tt', 'http://localhost/redirect-target') |
| 736 | ->text_is('li.client .client-type tt', 'CONFIDENTIAL') |
| 737 | ->element_exists_not('.client-issue-token') |
| 738 | ; |
| 739 | |
| 740 | $t->post_ok('/settings/oauth/register' => form => { |
| 741 | name => 'MyConfApp2', |
| 742 | type => 'CONFIDENTIAL', |
| 743 | desc => 'This is my second confidential application', |
| 744 | csrf_token => $csrf, |
| 745 | redirect_uri => 'http://localhost/FAIL' |
| 746 | }) |
| 747 | ->status_is(302) |
| 748 | ->header_is('location','/settings/oauth/') |
| 749 | ; |
| 750 | |
| 751 | $t->get_ok('/settings/oauth/') |
| 752 | ->text_is('div.notify-error', 'invalid_request: http://localhost/FAIL is invalid.') |
| 753 | ; |
| 754 | |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame^] | 755 | # OAuth client authorization flow |
| 756 | $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')) |
| 757 | ->status_is(302) |
| 758 | ->header_is('location','/settings/oauth/authorize') |
| 759 | ; |
| 760 | |
| 761 | # Logout |
| 762 | $t->get_ok('/x/expired-with-wrong-refresh'); |
| 763 | |
| 764 | $t->get_ok('/user/logout') |
| 765 | ->status_is(302) |
| 766 | ->session_hasnt('/auth') |
| 767 | ->session_hasnt('/auth_r') |
| 768 | ->session_hasnt('/user') |
| 769 | ->header_is('Location' => '/'); |
| 770 | |
| 771 | $csrf = $t->get_ok('/') |
| 772 | ->status_is(200) |
| 773 | ->element_exists_not('div.notify-error') |
| 774 | ->element_exists('div.notify-success') |
| 775 | ->text_is('div.notify-success', 'Logout successful') |
| 776 | ->element_exists("input[name=handle]") |
| 777 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 778 | ; |
| 779 | |
| 780 | $fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({ |
| 781 | client_id => 'xyz', |
| 782 | state => 'abcde', |
| 783 | scope => 'search match', |
| 784 | redirect_uri => 'http://test.com/', |
| 785 | })) |
| 786 | ->status_is(200) |
| 787 | ->attr_is('input[name=client_id]','value','xyz') |
| 788 | ->attr_is('input[name=state]','value','abcde') |
| 789 | ->attr_is('input[name=name]','value','xyz') |
| 790 | ->attr_like('input[name=fwd]','value',qr!test\.com!) |
| 791 | ->text_is('span.client-name','xyz') |
| 792 | ->text_is('div.intro p:nth-child(2)', 'Please log in!') |
| 793 | ->tx->res->dom->at('input[name=fwd]')->attr('value') |
| 794 | ; |
| 795 | |
| 796 | $fwd = $t->post_ok(Mojo::URL->new('/user/login')->query({ |
| 797 | csrf_token => $csrf, |
| 798 | client_id => 'xyz', |
| 799 | state => 'abcde', |
| 800 | scope => 'search match', |
| 801 | redirect_uri => 'http://test.com/', |
| 802 | handle => 'test', |
| 803 | pwd => 'pass', |
| 804 | fwd => $fwd |
| 805 | })) |
| 806 | ->status_is(302) |
| 807 | ->header_like('location', qr!/settings/oauth/authorize!) |
| 808 | ->tx->res->headers->header('location') |
| 809 | ; |
| 810 | |
| 811 | $t->get_ok($fwd) |
| 812 | ->status_is(200) |
| 813 | ->attr_is('input[name=client_id]','value','xyz') |
| 814 | ->attr_is('input[name=state]','value','abcde') |
| 815 | ->attr_is('input[name=name]','value','xyz') |
| 816 | ->text_is('ul#scopes li:nth-child(1)','search') |
| 817 | ->text_is('ul#scopes li:nth-child(2)','match') |
| 818 | ->text_is('span.client-name','xyz') |
| 819 | ->attr_is('a.form-button','href','http://test.com/') |
| 820 | ->attr_is('a.embedded-link', 'href', '/doc/korap/kalamar') |
| 821 | ; |
| 822 | |
| 823 | $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({ |
| 824 | client_id => 'xyz', |
| 825 | state => 'abcde', |
| 826 | scope => 'search match', |
| 827 | redirect_uri => 'http://test.com/' |
| 828 | })) |
| 829 | ->status_is(200) |
| 830 | ->attr_is('input[name=client_id]','value','xyz') |
| 831 | ->attr_is('input[name=state]','value','abcde') |
| 832 | ->attr_is('input[name=name]','value','xyz') |
| 833 | ->text_is('ul#scopes li:nth-child(1)','search') |
| 834 | ->text_is('ul#scopes li:nth-child(2)','match') |
| 835 | ->text_is('span.client-name','xyz') |
| 836 | ->attr_is('a.form-button','href','http://test.com/') |
| 837 | ->attr_is('a.embedded-link', 'href', '/doc/korap/kalamar') |
| 838 | ; |
| 839 | |
| 840 | $t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({ |
| 841 | client_id => 'xyz', |
| 842 | state => 'abcde', |
| 843 | scope => 'search match', |
| 844 | redirect_uri => 'http://test.com/' |
| 845 | })) |
| 846 | ->status_is(302) |
| 847 | ->header_is('location', '/?error_description=Bad+CSRF+token') |
| 848 | ; |
| 849 | |
| 850 | $fwd = $t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({ |
| 851 | client_id => 'xyz', |
| 852 | state => 'abcde', |
| 853 | scope => 'search match', |
| 854 | redirect_uri_server => 'http://example.com/', |
| 855 | redirect_uri => $fake_backend_app->url_for('return_uri')->to_abs, |
| 856 | csrf_token => $csrf, |
| 857 | })) |
| 858 | ->status_is(302) |
| 859 | ->header_like('location', qr!/realapi/fakeclient/return!) |
| 860 | ->tx->res->headers->header('location') |
| 861 | ; |
| 862 | |
| 863 | $t->get_ok($fwd) |
| 864 | ->status_is(200) |
| 865 | ->content_like(qr'welcome back! \[(.+?)\]') |
| 866 | ; |
| 867 | |
| 868 | $t->post_ok(Mojo::URL->new('/settings/oauth/authorize')->query({ |
| 869 | client_id => 'xyz', |
| 870 | state => 'fail', |
| 871 | scope => 'search match', |
| 872 | redirect_uri_server => 'http://example.com/', |
| 873 | redirect_uri => $fake_backend_app->url_for('return_uri')->to_abs, |
| 874 | csrf_token => $csrf, |
| 875 | })) |
| 876 | ->status_is(302) |
| 877 | ->header_is('location', 'http://example.com/?error_description=FAIL') |
| 878 | ; |
| 879 | |
Akron | b6b156e | 2022-03-31 14:57:49 +0200 | [diff] [blame] | 880 | |
Akron | 33f5c67 | 2019-06-24 19:40:47 +0200 | [diff] [blame] | 881 | done_testing; |
| 882 | __END__ |
Akron | a8efaa9 | 2022-04-09 14:45:43 +0200 | [diff] [blame^] | 883 | |
| 884 | |