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