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 | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 43 | ; |
| 44 | |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 45 | $t->get_ok('/') |
Akron | a9c8b0e | 2018-11-16 20:20:28 +0100 | [diff] [blame] | 46 | ->status_is(200) |
| 47 | ->element_exists('form[action=/user/login] input[name=handle_or_email]') |
| 48 | ; |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 49 | |
Akron | 741b2b1 | 2017-04-13 22:15:59 +0200 | [diff] [blame] | 50 | $t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' }) |
| 51 | ->status_is(302) |
| 52 | ->header_is('Location' => '/'); |
| 53 | |
| 54 | $t->get_ok('/') |
| 55 | ->status_is(200) |
| 56 | ->element_exists('div.notify-error') |
Akron | 2e3d377 | 2017-04-14 16:20:40 +0200 | [diff] [blame] | 57 | ->element_exists('input[name=handle_or_email][value=test]') |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 58 | ->element_exists_not('div.button.top a') |
Akron | 741b2b1 | 2017-04-13 22:15:59 +0200 | [diff] [blame] | 59 | ; |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 60 | |
Akron | e5ef4e0 | 2017-04-19 17:07:52 +0200 | [diff] [blame] | 61 | $t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' }) |
| 62 | ->status_is(302) |
| 63 | ->header_is('Location' => '/'); |
| 64 | |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 65 | my $csrf = $t->get_ok('/') |
| 66 | ->status_is(200) |
| 67 | ->element_exists('div.notify-error') |
| 68 | ->text_is('div.notify-error', 'Bad CSRF token') |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 69 | ->element_exists_not('div.button.top a') |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 70 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 71 | ; |
| 72 | |
Akron | d7ed34b | 2018-10-22 18:42:28 +0200 | [diff] [blame] | 73 | $t->post_ok('/user/login' => form => { |
| 74 | handle_or_email => 'test', |
| 75 | pwd => 'pass', |
| 76 | csrf_token => $csrf |
| 77 | }) |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 78 | ->status_is(302) |
Akron | 864c293 | 2018-11-16 17:18:55 +0100 | [diff] [blame] | 79 | ->content_is('') |
Akron | 15158e0 | 2018-03-19 12:42:46 +0100 | [diff] [blame] | 80 | ->header_is('Location' => '/'); |
| 81 | |
Akron | e5ef4e0 | 2017-04-19 17:07:52 +0200 | [diff] [blame] | 82 | $t->get_ok('/') |
| 83 | ->status_is(200) |
| 84 | ->element_exists_not('div.notify-error') |
| 85 | ->element_exists('div.notify-success') |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 86 | ->text_is('div.notify-success', 'Login successful') |
| 87 | ; |
| 88 | |
| 89 | # Now the user is logged in and should be able to |
| 90 | # search with authorization |
| 91 | $t->get_ok('/?q=Baum') |
| 92 | ->status_is(200) |
| 93 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 94 | ->text_like('#total-results', qr/\d+$/) |
| 95 | ->element_exists_not('div.notify-error') |
| 96 | ->content_like(qr/\"authorized\"\:\"test\"/) |
Akron | c82b1bc | 2018-11-18 18:06:14 +0100 | [diff] [blame] | 97 | ->element_exists('div.button.top a') |
| 98 | ->element_exists('div.button.top a.logout[title~="test"]') |
Akron | e5ef4e0 | 2017-04-19 17:07:52 +0200 | [diff] [blame] | 99 | ; |
| 100 | |
Akron | bc213c0 | 2017-04-20 16:45:55 +0200 | [diff] [blame] | 101 | # Logout |
| 102 | $t->get_ok('/user/logout') |
| 103 | ->status_is(302) |
| 104 | ->header_is('Location' => '/'); |
| 105 | |
| 106 | $t->get_ok('/') |
| 107 | ->status_is(200) |
| 108 | ->element_exists_not('div.notify-error') |
| 109 | ->element_exists('div.notify-success') |
| 110 | ->text_is('div.notify-success', 'Logout successful') |
| 111 | ; |
| 112 | |
| 113 | $t->get_ok('/?q=Baum') |
| 114 | ->status_is(200) |
| 115 | ->text_like('h1 span', qr/KorAP: Find .Baum./i) |
| 116 | ->text_like('#total-results', qr/\d+$/) |
| 117 | ->content_like(qr/\"authorized\"\:null/) |
| 118 | ; |
| 119 | |
Akron | 429aeda | 2018-03-19 16:02:29 +0100 | [diff] [blame] | 120 | # Get redirect |
| 121 | my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp') |
| 122 | ->status_is(200) |
| 123 | ->element_exists_not('div.notify-error') |
| 124 | ->tx->res->dom->at('input[name=fwd]')->attr('value') |
| 125 | ; |
| 126 | |
| 127 | is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid'); |
| 128 | |
| 129 | $t->post_ok('/user/login' => form => { |
| 130 | handle_or_email => 'test', |
| 131 | pwd => 'pass', |
| 132 | csrf_token => $csrf, |
| 133 | fwd => 'http://bad.example.com/test' |
| 134 | }) |
| 135 | ->status_is(302) |
| 136 | ->header_is('Location' => '/'); |
| 137 | |
| 138 | $t->get_ok('/') |
| 139 | ->status_is(200) |
| 140 | ->element_exists('div.notify-error') |
| 141 | ->element_exists_not('div.notify-success') |
| 142 | ->text_is('div.notify-error', 'Redirect failure') |
| 143 | ; |
| 144 | |
| 145 | $t->post_ok('/user/login' => form => { |
| 146 | handle_or_email => 'test', |
| 147 | pwd => 'pass', |
| 148 | csrf_token => $csrf, |
| 149 | fwd => $fwd |
| 150 | }) |
| 151 | ->status_is(302) |
| 152 | ->header_is('Location' => '/?q=Baum&ql=poliqarp'); |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
Akron | be9d5b3 | 2017-04-05 20:48:24 +0200 | [diff] [blame] | 157 | done_testing; |
| 158 | __END__ |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 159 | |
| 160 | |
Akron | 1b0c265 | 2017-04-27 15:28:49 +0200 | [diff] [blame] | 161 | # Login mit falschem Nutzernamen: |
| 162 | # 400 und: |
| 163 | {"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]} |
| 164 | |
Akron | 741b2b1 | 2017-04-13 22:15:59 +0200 | [diff] [blame] | 165 | |
| 166 | |
Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 167 | ok(!$c->user->get('details'), 'User not logged in'); |
| 168 | |
| 169 | # Login with user credentials |
| 170 | ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user'); |
| 171 | is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in'); |
| 172 | like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in'); |
| 173 | |
| 174 | my $details = $c->user->get('details'); |
| 175 | is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email'); |
| 176 | is($details->{firstName}, 'Kustvakt', 'Firstname'); |
| 177 | is($details->{lastName}, 'KorAP', 'Lastname'); |
| 178 | is($details->{country}, 'Germany', 'Country'); |
| 179 | is($details->{address}, 'Mannheim', 'Address'); |
| 180 | is($details->{username}, 'kustvakt', 'Username'); |
| 181 | is($details->{institution}, 'IDS Mannheim', 'Institution'); |
| 182 | |
| 183 | my $settings = $c->user->get('settings'); |
| 184 | is($settings->{username}, 'kustvakt', 'Username'); |
| 185 | |
| 186 | # ok($c->user->set(details => { firstName => 'Me' }), 'Set first name'); |
| 187 | #ok($c->user->set(details => { |
| 188 | # firstName => 'Akron', |
| 189 | # lastName => 'Fuxfell' |
| 190 | #}), 'Set first name'); |
| 191 | |
| 192 | # diag Dumper $c->user->get('info'); |
| 193 | |
| 194 | ok(1,'Fine'); |
| 195 | |
| 196 | done_testing; |
| 197 | __END__ |