Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 2 | use Test::More; |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 3 | use Test::Mojo; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 4 | use Mojo::File qw/path/; |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 5 | use Data::Dumper; |
| 6 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 7 | |
| 8 | ##################### |
| 9 | # Start Fake server # |
| 10 | ##################### |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 11 | my $mount_point = '/api/'; |
| 12 | $ENV{KALAMAR_API} = $mount_point; |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 13 | |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 14 | my $t = Test::Mojo->new('Kalamar' => { |
| 15 | Kalamar => { |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 16 | plugins => ['Auth'] |
| 17 | } |
| 18 | }); |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 19 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 20 | # Mount fake backend |
| 21 | # Get the fixture path |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 22 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 23 | my $fake_backend = $t->app->plugin( |
| 24 | Mount => { |
| 25 | $mount_point => |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 26 | $fixtures_path->child('mock.pl') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 27 | } |
| 28 | ); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 29 | # Configure fake backend |
| 30 | $fake_backend->pattern->defaults->{app}->log($t->app->log); |
| 31 | |
| 32 | $t->get_ok('/api') |
| 33 | ->status_is(200) |
| 34 | ->content_is('Fake server available'); |
Akron | 7d75ee3 | 2017-05-02 13:42:41 +0200 | [diff] [blame] | 35 | |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 36 | $t->get_ok('/?q=Baum') |
| 37 | ->status_is(200) |
| 38 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 39 | ->text_like('#total-results', qr/\d+$/) |
| 40 | ->content_like(qr/\"authorized\"\:null/) |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 41 | ->element_exists_not('div.button.top a') |
Akron | 2d01d29 | 2018-11-23 11:17:35 +0100 | [diff] [blame] | 42 | ->element_exists_not('aside.active') |
| 43 | ->element_exists_not('aside.off') |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 44 | ; |
| 45 | |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 46 | $t->get_ok('/') |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 47 | ->status_is(200) |
| 48 | ->element_exists('form[action=/user/login] input[name=handle_or_email]') |
Akron | 2d01d29 | 2018-11-23 11:17:35 +0100 | [diff] [blame] | 49 | ->element_exists('aside.active') |
| 50 | ->element_exists_not('aside.off') |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 51 | ; |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 52 | |
Akron | 741b2b1 | 2017-04-13 22:15:59 +0200 | [diff] [blame] | 53 | $t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' }) |
| 54 | ->status_is(302) |
| 55 | ->header_is('Location' => '/'); |
| 56 | |
| 57 | $t->get_ok('/') |
| 58 | ->status_is(200) |
| 59 | ->element_exists('div.notify-error') |
Akron | 3d67306 | 2019-01-29 15:54:16 +0100 | [diff] [blame] | 60 | ->text_is('div.notify-error', 'Bad CSRF token') |
Akron | 2e3d377 | 2017-04-14 16:20:40 +0200 | [diff] [blame] | 61 | ->element_exists('input[name=handle_or_email][value=test]') |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 62 | ->element_exists_not('div.button.top a') |
Akron | 741b2b1 | 2017-04-13 22:15:59 +0200 | [diff] [blame] | 63 | ; |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 64 | |
Akron | e5ef4e0 | 2017-04-19 17:07:52 +0200 | [diff] [blame] | 65 | $t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' }) |
| 66 | ->status_is(302) |
| 67 | ->header_is('Location' => '/'); |
| 68 | |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 69 | my $csrf = $t->get_ok('/') |
| 70 | ->status_is(200) |
| 71 | ->element_exists('div.notify-error') |
| 72 | ->text_is('div.notify-error', 'Bad CSRF token') |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 73 | ->element_exists_not('div.button.top a') |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 74 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 75 | ; |
| 76 | |
Akron | d7ed34b | 2018-10-22 18:42:28 +0200 | [diff] [blame] | 77 | $t->post_ok('/user/login' => form => { |
| 78 | handle_or_email => 'test', |
Akron | 3d67306 | 2019-01-29 15:54:16 +0100 | [diff] [blame] | 79 | pwd => 'ldaperr', |
| 80 | csrf_token => $csrf |
| 81 | }) |
| 82 | ->status_is(302) |
| 83 | ->content_is('') |
| 84 | ->header_is('Location' => '/'); |
| 85 | |
| 86 | $csrf = $t->get_ok('/') |
| 87 | ->status_is(200) |
| 88 | ->element_exists('div.notify-error') |
| 89 | ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!') |
| 90 | ->element_exists('input[name=handle_or_email][value=test]') |
| 91 | ->element_exists_not('div.button.top a') |
| 92 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 93 | ; |
| 94 | |
| 95 | $t->post_ok('/user/login' => form => { |
| 96 | handle_or_email => 'test', |
| 97 | pwd => 'unknown', |
| 98 | csrf_token => $csrf |
| 99 | }) |
| 100 | ->status_is(302) |
| 101 | ->content_is('') |
| 102 | ->header_is('Location' => '/'); |
| 103 | |
| 104 | $csrf = $t->get_ok('/') |
| 105 | ->status_is(200) |
| 106 | ->element_exists('div.notify-error') |
| 107 | ->text_is('div.notify-error', 'Access denied') |
| 108 | ->element_exists('input[name=handle_or_email][value=test]') |
| 109 | ->element_exists_not('div.button.top a') |
| 110 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 111 | ; |
| 112 | |
| 113 | $t->post_ok('/user/login' => form => { |
| 114 | handle_or_email => 'test', |
Akron | d7ed34b | 2018-10-22 18:42:28 +0200 | [diff] [blame] | 115 | pwd => 'pass', |
| 116 | csrf_token => $csrf |
| 117 | }) |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 118 | ->status_is(302) |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 119 | ->content_is('') |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 120 | ->header_is('Location' => '/'); |
| 121 | |
Akron | e5ef4e0 | 2017-04-19 17:07:52 +0200 | [diff] [blame] | 122 | $t->get_ok('/') |
| 123 | ->status_is(200) |
| 124 | ->element_exists_not('div.notify-error') |
| 125 | ->element_exists('div.notify-success') |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 126 | ->text_is('div.notify-success', 'Login successful') |
Akron | 2d01d29 | 2018-11-23 11:17:35 +0100 | [diff] [blame] | 127 | ->element_exists('aside.off') |
| 128 | ->element_exists_not('aside.active') |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 129 | ; |
| 130 | |
| 131 | # Now the user is logged in and should be able to |
| 132 | # search with authorization |
| 133 | $t->get_ok('/?q=Baum') |
| 134 | ->status_is(200) |
| 135 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 136 | ->text_like('#total-results', qr/\d+$/) |
| 137 | ->element_exists_not('div.notify-error') |
| 138 | ->content_like(qr/\"authorized\"\:\"test\"/) |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 139 | ->element_exists('div.button.top a') |
| 140 | ->element_exists('div.button.top a.logout[title~="test"]') |
Akron | e5ef4e0 | 2017-04-19 17:07:52 +0200 | [diff] [blame] | 141 | ; |
| 142 | |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 143 | # Logout |
| 144 | $t->get_ok('/user/logout') |
| 145 | ->status_is(302) |
| 146 | ->header_is('Location' => '/'); |
| 147 | |
| 148 | $t->get_ok('/') |
| 149 | ->status_is(200) |
| 150 | ->element_exists_not('div.notify-error') |
| 151 | ->element_exists('div.notify-success') |
| 152 | ->text_is('div.notify-success', 'Logout successful') |
| 153 | ; |
| 154 | |
| 155 | $t->get_ok('/?q=Baum') |
| 156 | ->status_is(200) |
| 157 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 158 | ->text_like('#total-results', qr/\d+$/) |
| 159 | ->content_like(qr/\"authorized\"\:null/) |
| 160 | ; |
| 161 | |
Akron | 429aeda | 2018-03-19 16:02:29 +0100 | [diff] [blame] | 162 | # Get redirect |
| 163 | my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp') |
| 164 | ->status_is(200) |
| 165 | ->element_exists_not('div.notify-error') |
| 166 | ->tx->res->dom->at('input[name=fwd]')->attr('value') |
| 167 | ; |
| 168 | |
| 169 | is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid'); |
| 170 | |
| 171 | $t->post_ok('/user/login' => form => { |
| 172 | handle_or_email => 'test', |
| 173 | pwd => 'pass', |
| 174 | csrf_token => $csrf, |
| 175 | fwd => 'http://bad.example.com/test' |
| 176 | }) |
| 177 | ->status_is(302) |
| 178 | ->header_is('Location' => '/'); |
| 179 | |
| 180 | $t->get_ok('/') |
| 181 | ->status_is(200) |
| 182 | ->element_exists('div.notify-error') |
| 183 | ->element_exists_not('div.notify-success') |
| 184 | ->text_is('div.notify-error', 'Redirect failure') |
| 185 | ; |
| 186 | |
| 187 | $t->post_ok('/user/login' => form => { |
| 188 | handle_or_email => 'test', |
| 189 | pwd => 'pass', |
| 190 | csrf_token => $csrf, |
| 191 | fwd => $fwd |
| 192 | }) |
| 193 | ->status_is(302) |
| 194 | ->header_is('Location' => '/?q=Baum&ql=poliqarp'); |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 199 | done_testing; |
| 200 | __END__ |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 201 | |
| 202 | |
Akron | 1b0c265 | 2017-04-27 15:28:49 +0200 | [diff] [blame] | 203 | # Login mit falschem Nutzernamen: |
| 204 | # 400 und: |
| 205 | {"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]} |
| 206 | |
Akron | 741b2b1 | 2017-04-13 22:15:59 +0200 | [diff] [blame] | 207 | |
| 208 | |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 209 | ok(!$c->user->get('details'), 'User not logged in'); |
| 210 | |
| 211 | # Login with user credentials |
| 212 | ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user'); |
| 213 | is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in'); |
| 214 | like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in'); |
| 215 | |
| 216 | my $details = $c->user->get('details'); |
| 217 | is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email'); |
| 218 | is($details->{firstName}, 'Kustvakt', 'Firstname'); |
| 219 | is($details->{lastName}, 'KorAP', 'Lastname'); |
| 220 | is($details->{country}, 'Germany', 'Country'); |
| 221 | is($details->{address}, 'Mannheim', 'Address'); |
| 222 | is($details->{username}, 'kustvakt', 'Username'); |
| 223 | is($details->{institution}, 'IDS Mannheim', 'Institution'); |
| 224 | |
| 225 | my $settings = $c->user->get('settings'); |
| 226 | is($settings->{username}, 'kustvakt', 'Username'); |
| 227 | |
| 228 | # ok($c->user->set(details => { firstName => 'Me' }), 'Set first name'); |
| 229 | #ok($c->user->set(details => { |
| 230 | # firstName => 'Akron', |
| 231 | # lastName => 'Fuxfell' |
| 232 | #}), 'Set first name'); |
| 233 | |
| 234 | # diag Dumper $c->user->get('info'); |
| 235 | |
| 236 | ok(1,'Fine'); |
| 237 | |
| 238 | done_testing; |
| 239 | __END__ |