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