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