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