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