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