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