blob: 63433b2853cd6f5cea39310c3e5ba7cc7bfef308 [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#####################
Akron63d963b2019-07-05 15:35:51 +020011my $mount_point = '/realapi/';
Akron0e1ed242018-10-11 13:22:00 +020012$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 => {
Akron864c2932018-11-16 17:18:55 +010016 plugins => ['Auth']
17 }
18});
Akrone8235be2016-06-27 11:02:18 +020019
Akron0e1ed242018-10-11 13:22:00 +020020# Mount fake backend
21# Get the fixture path
Akron864c2932018-11-16 17:18:55 +010022my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server');
Akron0e1ed242018-10-11 13:22:00 +020023my $fake_backend = $t->app->plugin(
24 Mount => {
25 $mount_point =>
Akron73f36082018-10-25 15:34:59 +020026 $fixtures_path->child('mock.pl')
Akron0e1ed242018-10-11 13:22:00 +020027 }
28);
Akron0e1ed242018-10-11 13:22:00 +020029# Configure fake backend
30$fake_backend->pattern->defaults->{app}->log($t->app->log);
31
Akron63d963b2019-07-05 15:35:51 +020032$t->get_ok('/realapi/v1.0')
Akron0e1ed242018-10-11 13:22:00 +020033 ->status_is(200)
34 ->content_is('Fake server available');
Akron7d75ee32017-05-02 13:42:41 +020035
Akronbc213c02017-04-20 16:45:55 +020036$t->get_ok('/?q=Baum')
37 ->status_is(200)
38 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
39 ->text_like('#total-results', qr/\d+$/)
40 ->content_like(qr/\"authorized\"\:null/)
Akronc82b1bc2018-11-18 18:06:14 +010041 ->element_exists_not('div.button.top a')
Akron2d01d292018-11-23 11:17:35 +010042 ->element_exists_not('aside.active')
43 ->element_exists_not('aside.off')
Akronbc213c02017-04-20 16:45:55 +020044 ;
45
Akronbe9d5b32017-04-05 20:48:24 +020046$t->get_ok('/')
Akrona9c8b0e2018-11-16 20:20:28 +010047 ->status_is(200)
Akrone208d302020-11-28 11:14:50 +010048 ->element_exists('form[action=/user/login] input[name=handle]')
Akron2d01d292018-11-23 11:17:35 +010049 ->element_exists('aside.active')
50 ->element_exists_not('aside.off')
Akrona9c8b0e2018-11-16 20:20:28 +010051 ;
Akronbe9d5b32017-04-05 20:48:24 +020052
Akrone208d302020-11-28 11:14:50 +010053$t->post_ok('/user/login' => form => { handle => 'test', pwd => 'fail' })
Akron741b2b12017-04-13 22:15:59 +020054 ->status_is(302)
55 ->header_is('Location' => '/');
56
57$t->get_ok('/')
58 ->status_is(200)
59 ->element_exists('div.notify-error')
Akron3d673062019-01-29 15:54:16 +010060 ->text_is('div.notify-error', 'Bad CSRF token')
Akrone208d302020-11-28 11:14:50 +010061 ->element_exists('input[name=handle][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
Akrone208d302020-11-28 11:14:50 +010065$t->post_ok('/user/login' => form => { handle => 'test', pwd => 'pass' })
Akrone5ef4e02017-04-19 17:07:52 +020066 ->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 => {
Akrone208d302020-11-28 11:14:50 +010078 handle => 'test',
Akron3d673062019-01-29 15:54:16 +010079 pwd => 'ldaperr',
80 csrf_token => $csrf
81})
82 ->status_is(302)
83 ->content_is('')
84 ->header_is('Location' => '/');
85
86$csrf = $t->get_ok('/')
87 ->status_is(200)
88 ->element_exists('div.notify-error')
89 ->text_is('div.notify-error', '2022: LDAP Authentication failed due to unknown user or password!')
Akrone208d302020-11-28 11:14:50 +010090 ->element_exists('input[name=handle][value=test]')
Akron3d673062019-01-29 15:54:16 +010091 ->element_exists_not('div.button.top a')
92 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
93 ;
94
95$t->post_ok('/user/login' => form => {
Akrone208d302020-11-28 11:14:50 +010096 handle => 'test',
Akron3d673062019-01-29 15:54:16 +010097 pwd => 'unknown',
98 csrf_token => $csrf
99})
100 ->status_is(302)
101 ->content_is('')
102 ->header_is('Location' => '/');
103
104$csrf = $t->get_ok('/')
105 ->status_is(200)
106 ->element_exists('div.notify-error')
107 ->text_is('div.notify-error', 'Access denied')
Akrone208d302020-11-28 11:14:50 +0100108 ->element_exists('input[name=handle][value=test]')
Akron3d673062019-01-29 15:54:16 +0100109 ->element_exists_not('div.button.top a')
110 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
111 ;
112
113$t->post_ok('/user/login' => form => {
Akrone208d302020-11-28 11:14:50 +0100114 handle => 'test',
Akrond7ed34b2018-10-22 18:42:28 +0200115 pwd => 'pass',
116 csrf_token => $csrf
117})
Akron15158e02018-03-19 12:42:46 +0100118 ->status_is(302)
Akron864c2932018-11-16 17:18:55 +0100119 ->content_is('')
Akron15158e02018-03-19 12:42:46 +0100120 ->header_is('Location' => '/');
121
Akrone5ef4e02017-04-19 17:07:52 +0200122$t->get_ok('/')
123 ->status_is(200)
124 ->element_exists_not('div.notify-error')
125 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +0200126 ->text_is('div.notify-success', 'Login successful')
Akron2d01d292018-11-23 11:17:35 +0100127 ->element_exists('aside.off')
128 ->element_exists_not('aside.active')
Akronbc213c02017-04-20 16:45:55 +0200129 ;
130
131# Now the user is logged in and should be able to
132# search with authorization
133$t->get_ok('/?q=Baum')
134 ->status_is(200)
135 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
136 ->text_like('#total-results', qr/\d+$/)
137 ->element_exists_not('div.notify-error')
138 ->content_like(qr/\"authorized\"\:\"test\"/)
Akronc82b1bc2018-11-18 18:06:14 +0100139 ->element_exists('div.button.top a')
140 ->element_exists('div.button.top a.logout[title~="test"]')
Akrone5ef4e02017-04-19 17:07:52 +0200141 ;
142
Akronbc213c02017-04-20 16:45:55 +0200143# Logout
144$t->get_ok('/user/logout')
145 ->status_is(302)
146 ->header_is('Location' => '/');
147
148$t->get_ok('/')
149 ->status_is(200)
150 ->element_exists_not('div.notify-error')
151 ->element_exists('div.notify-success')
152 ->text_is('div.notify-success', 'Logout successful')
153 ;
154
155$t->get_ok('/?q=Baum')
156 ->status_is(200)
157 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
158 ->text_like('#total-results', qr/\d+$/)
159 ->content_like(qr/\"authorized\"\:null/)
160 ;
161
Akron429aeda2018-03-19 16:02:29 +0100162# Get redirect
163my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
164 ->status_is(200)
165 ->element_exists_not('div.notify-error')
166 ->tx->res->dom->at('input[name=fwd]')->attr('value')
167 ;
168
169is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
170
171$t->post_ok('/user/login' => form => {
Akrone208d302020-11-28 11:14:50 +0100172 handle => 'test',
Akron429aeda2018-03-19 16:02:29 +0100173 pwd => 'pass',
174 csrf_token => $csrf,
175 fwd => 'http://bad.example.com/test'
176})
177 ->status_is(302)
178 ->header_is('Location' => '/');
179
180$t->get_ok('/')
181 ->status_is(200)
182 ->element_exists('div.notify-error')
183 ->element_exists_not('div.notify-success')
184 ->text_is('div.notify-error', 'Redirect failure')
185 ;
186
187$t->post_ok('/user/login' => form => {
Akrone208d302020-11-28 11:14:50 +0100188 handle => 'test',
Akron429aeda2018-03-19 16:02:29 +0100189 pwd => 'pass',
190 csrf_token => $csrf,
191 fwd => $fwd
192})
193 ->status_is(302)
194 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
195
196
197
198
Akronbe9d5b32017-04-05 20:48:24 +0200199done_testing;
200__END__
Akrone8235be2016-06-27 11:02:18 +0200201
202
Akron1b0c2652017-04-27 15:28:49 +0200203# Login mit falschem Nutzernamen:
204# 400 und:
205{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
206
Akron741b2b12017-04-13 22:15:59 +0200207
208
Akrone8235be2016-06-27 11:02:18 +0200209ok(!$c->user->get('details'), 'User not logged in');
210
211# Login with user credentials
212ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
213is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
214like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
215
216my $details = $c->user->get('details');
217is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
218is($details->{firstName}, 'Kustvakt', 'Firstname');
219is($details->{lastName}, 'KorAP', 'Lastname');
220is($details->{country}, 'Germany', 'Country');
221is($details->{address}, 'Mannheim', 'Address');
222is($details->{username}, 'kustvakt', 'Username');
223is($details->{institution}, 'IDS Mannheim', 'Institution');
224
225my $settings = $c->user->get('settings');
226is($settings->{username}, 'kustvakt', 'Username');
227
228# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
229#ok($c->user->set(details => {
230# firstName => 'Akron',
231# lastName => 'Fuxfell'
232#}), 'Set first name');
233
234# diag Dumper $c->user->get('info');
235
236ok(1,'Fine');
237
238done_testing;
239__END__