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