blob: 9828bafc236458756edde00e733a1d7b0427b4e4 [file] [log] [blame]
Akrone8235be2016-06-27 11:02:18 +02001use Mojo::Base -strict;
Akronbe9d5b32017-04-05 20:48:24 +02002use Test::More;
Akrone8235be2016-06-27 11:02:18 +02003use Test::Mojo;
Akron0e1ed242018-10-11 13:22:00 +02004use Mojo::File qw/path/;
Akrone8235be2016-06-27 11:02:18 +02005use Data::Dumper;
6
Akron32396632018-10-11 17:08:37 +02007
8#####################
9# Start Fake server #
10#####################
Akron0e1ed242018-10-11 13:22:00 +020011my $mount_point = '/api/';
12$ENV{KALAMAR_API} = $mount_point;
Akrone8235be2016-06-27 11:02:18 +020013
Akron864c2932018-11-16 17:18:55 +010014my $t = Test::Mojo->new('Kalamar' => {
15 Kalamar => {
16 auth_support => 1,
17 plugins => ['Auth']
18 }
19});
Akrone8235be2016-06-27 11:02:18 +020020
Akron0e1ed242018-10-11 13:22:00 +020021# Mount fake backend
22# Get the fixture path
Akron864c2932018-11-16 17:18:55 +010023my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server');
Akron0e1ed242018-10-11 13:22:00 +020024my $fake_backend = $t->app->plugin(
25 Mount => {
26 $mount_point =>
Akron73f36082018-10-25 15:34:59 +020027 $fixtures_path->child('mock.pl')
Akron0e1ed242018-10-11 13:22:00 +020028 }
29);
Akron0e1ed242018-10-11 13:22:00 +020030# 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');
Akron7d75ee32017-05-02 13:42:41 +020036
Akronbc213c02017-04-20 16:45:55 +020037$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/)
Akronc82b1bc2018-11-18 18:06:14 +010042 ->element_exists_not('div.button.top a')
Akron2d01d292018-11-23 11:17:35 +010043 ->element_exists_not('aside.active')
44 ->element_exists_not('aside.off')
Akronbc213c02017-04-20 16:45:55 +020045 ;
46
Akronbe9d5b32017-04-05 20:48:24 +020047$t->get_ok('/')
Akrona9c8b0e2018-11-16 20:20:28 +010048 ->status_is(200)
49 ->element_exists('form[action=/user/login] input[name=handle_or_email]')
Akron2d01d292018-11-23 11:17:35 +010050 ->element_exists('aside.active')
51 ->element_exists_not('aside.off')
Akrona9c8b0e2018-11-16 20:20:28 +010052 ;
Akronbe9d5b32017-04-05 20:48:24 +020053
Akron741b2b12017-04-13 22:15:59 +020054$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
55 ->status_is(302)
56 ->header_is('Location' => '/');
57
58$t->get_ok('/')
59 ->status_is(200)
60 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020061 ->element_exists('input[name=handle_or_email][value=test]')
Akronc82b1bc2018-11-18 18:06:14 +010062 ->element_exists_not('div.button.top a')
Akron741b2b12017-04-13 22:15:59 +020063 ;
Akronbe9d5b32017-04-05 20:48:24 +020064
Akrone5ef4e02017-04-19 17:07:52 +020065$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
66 ->status_is(302)
67 ->header_is('Location' => '/');
68
Akron15158e02018-03-19 12:42:46 +010069my $csrf = $t->get_ok('/')
70 ->status_is(200)
71 ->element_exists('div.notify-error')
72 ->text_is('div.notify-error', 'Bad CSRF token')
Akronc82b1bc2018-11-18 18:06:14 +010073 ->element_exists_not('div.button.top a')
Akron15158e02018-03-19 12:42:46 +010074 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
75 ;
76
Akrond7ed34b2018-10-22 18:42:28 +020077$t->post_ok('/user/login' => form => {
78 handle_or_email => 'test',
79 pwd => 'pass',
80 csrf_token => $csrf
81})
Akron15158e02018-03-19 12:42:46 +010082 ->status_is(302)
Akron864c2932018-11-16 17:18:55 +010083 ->content_is('')
Akron15158e02018-03-19 12:42:46 +010084 ->header_is('Location' => '/');
85
Akrone5ef4e02017-04-19 17:07:52 +020086$t->get_ok('/')
87 ->status_is(200)
88 ->element_exists_not('div.notify-error')
89 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020090 ->text_is('div.notify-success', 'Login successful')
Akron2d01d292018-11-23 11:17:35 +010091 ->element_exists('aside.off')
92 ->element_exists_not('aside.active')
Akronbc213c02017-04-20 16:45:55 +020093 ;
94
95# Now the user is logged in and should be able to
96# search with authorization
97$t->get_ok('/?q=Baum')
98 ->status_is(200)
99 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
100 ->text_like('#total-results', qr/\d+$/)
101 ->element_exists_not('div.notify-error')
102 ->content_like(qr/\"authorized\"\:\"test\"/)
Akronc82b1bc2018-11-18 18:06:14 +0100103 ->element_exists('div.button.top a')
104 ->element_exists('div.button.top a.logout[title~="test"]')
Akrone5ef4e02017-04-19 17:07:52 +0200105 ;
106
Akronbc213c02017-04-20 16:45:55 +0200107# Logout
108$t->get_ok('/user/logout')
109 ->status_is(302)
110 ->header_is('Location' => '/');
111
112$t->get_ok('/')
113 ->status_is(200)
114 ->element_exists_not('div.notify-error')
115 ->element_exists('div.notify-success')
116 ->text_is('div.notify-success', 'Logout successful')
117 ;
118
119$t->get_ok('/?q=Baum')
120 ->status_is(200)
121 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
122 ->text_like('#total-results', qr/\d+$/)
123 ->content_like(qr/\"authorized\"\:null/)
124 ;
125
Akron429aeda2018-03-19 16:02:29 +0100126# Get redirect
127my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
128 ->status_is(200)
129 ->element_exists_not('div.notify-error')
130 ->tx->res->dom->at('input[name=fwd]')->attr('value')
131 ;
132
133is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
134
135$t->post_ok('/user/login' => form => {
136 handle_or_email => 'test',
137 pwd => 'pass',
138 csrf_token => $csrf,
139 fwd => 'http://bad.example.com/test'
140})
141 ->status_is(302)
142 ->header_is('Location' => '/');
143
144$t->get_ok('/')
145 ->status_is(200)
146 ->element_exists('div.notify-error')
147 ->element_exists_not('div.notify-success')
148 ->text_is('div.notify-error', 'Redirect failure')
149 ;
150
151$t->post_ok('/user/login' => form => {
152 handle_or_email => 'test',
153 pwd => 'pass',
154 csrf_token => $csrf,
155 fwd => $fwd
156})
157 ->status_is(302)
158 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
159
160
161
162
Akronbe9d5b32017-04-05 20:48:24 +0200163done_testing;
164__END__
Akrone8235be2016-06-27 11:02:18 +0200165
166
Akron1b0c2652017-04-27 15:28:49 +0200167# Login mit falschem Nutzernamen:
168# 400 und:
169{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
170
Akron741b2b12017-04-13 22:15:59 +0200171
172
Akrone8235be2016-06-27 11:02:18 +0200173ok(!$c->user->get('details'), 'User not logged in');
174
175# Login with user credentials
176ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
177is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
178like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
179
180my $details = $c->user->get('details');
181is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
182is($details->{firstName}, 'Kustvakt', 'Firstname');
183is($details->{lastName}, 'KorAP', 'Lastname');
184is($details->{country}, 'Germany', 'Country');
185is($details->{address}, 'Mannheim', 'Address');
186is($details->{username}, 'kustvakt', 'Username');
187is($details->{institution}, 'IDS Mannheim', 'Institution');
188
189my $settings = $c->user->get('settings');
190is($settings->{username}, 'kustvakt', 'Username');
191
192# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
193#ok($c->user->set(details => {
194# firstName => 'Akron',
195# lastName => 'Fuxfell'
196#}), 'Set first name');
197
198# diag Dumper $c->user->get('info');
199
200ok(1,'Fine');
201
202done_testing;
203__END__