blob: ee0b87247f2b6964ea035ad7500cbb538d904350 [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
14my $t = Test::Mojo->new('Kalamar');
Akron0e1ed242018-10-11 13:22:00 +020015$t->app->defaults('auth_support' => 1);
Akrone8235be2016-06-27 11:02:18 +020016
Akron0e1ed242018-10-11 13:22:00 +020017# Mount fake backend
18# Get the fixture path
19my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
20my $fake_backend = $t->app->plugin(
21 Mount => {
22 $mount_point =>
Akron6d49c1f2018-10-11 14:22:21 +020023 $fixtures_path->child('fake_backend.pl')
Akron0e1ed242018-10-11 13:22:00 +020024 }
25);
Akron0e1ed242018-10-11 13:22:00 +020026# Configure fake backend
27$fake_backend->pattern->defaults->{app}->log($t->app->log);
28
Akron32396632018-10-11 17:08:37 +020029
Akron0e1ed242018-10-11 13:22:00 +020030$t->get_ok('/api')
31 ->status_is(200)
32 ->content_is('Fake server available');
Akron7d75ee32017-05-02 13:42:41 +020033
Akronbc213c02017-04-20 16:45:55 +020034$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
Akronbe9d5b32017-04-05 20:48:24 +020041$t->get_ok('/')
42 ->element_exists('form[action=/user/login] input[name=handle_or_email]');
43
Akron741b2b12017-04-13 22:15:59 +020044$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')
Akron2e3d3772017-04-14 16:20:40 +020051 ->element_exists('input[name=handle_or_email][value=test]')
Akron741b2b12017-04-13 22:15:59 +020052 ;
Akronbe9d5b32017-04-05 20:48:24 +020053
Akrone5ef4e02017-04-19 17:07:52 +020054$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
55 ->status_is(302)
56 ->header_is('Location' => '/');
57
Akron15158e02018-03-19 12:42:46 +010058my $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
Akron32396632018-10-11 17:08:37 +020065
Akron15158e02018-03-19 12:42:46 +010066$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass', csrf_token => $csrf })
67 ->status_is(302)
68 ->header_is('Location' => '/');
69
Akrone5ef4e02017-04-19 17:07:52 +020070$t->get_ok('/')
71 ->status_is(200)
72 ->element_exists_not('div.notify-error')
73 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020074 ->text_is('div.notify-success', 'Login successful')
75 ;
76
77# Now the user is logged in and should be able to
78# search with authorization
79$t->get_ok('/?q=Baum')
80 ->status_is(200)
81 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
82 ->text_like('#total-results', qr/\d+$/)
83 ->element_exists_not('div.notify-error')
84 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020085 ;
86
Akron2e3d3772017-04-14 16:20:40 +020087
Akronbc213c02017-04-20 16:45:55 +020088# Logout
89$t->get_ok('/user/logout')
90 ->status_is(302)
91 ->header_is('Location' => '/');
92
93$t->get_ok('/')
94 ->status_is(200)
95 ->element_exists_not('div.notify-error')
96 ->element_exists('div.notify-success')
97 ->text_is('div.notify-success', 'Logout successful')
98 ;
99
100$t->get_ok('/?q=Baum')
101 ->status_is(200)
102 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
103 ->text_like('#total-results', qr/\d+$/)
104 ->content_like(qr/\"authorized\"\:null/)
105 ;
106
Akron429aeda2018-03-19 16:02:29 +0100107# Get redirect
108my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
109 ->status_is(200)
110 ->element_exists_not('div.notify-error')
111 ->tx->res->dom->at('input[name=fwd]')->attr('value')
112 ;
113
114is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
115
116$t->post_ok('/user/login' => form => {
117 handle_or_email => 'test',
118 pwd => 'pass',
119 csrf_token => $csrf,
120 fwd => 'http://bad.example.com/test'
121})
122 ->status_is(302)
123 ->header_is('Location' => '/');
124
125$t->get_ok('/')
126 ->status_is(200)
127 ->element_exists('div.notify-error')
128 ->element_exists_not('div.notify-success')
129 ->text_is('div.notify-error', 'Redirect failure')
130 ;
131
132$t->post_ok('/user/login' => form => {
133 handle_or_email => 'test',
134 pwd => 'pass',
135 csrf_token => $csrf,
136 fwd => $fwd
137})
138 ->status_is(302)
139 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
140
141
142
143
Akronbe9d5b32017-04-05 20:48:24 +0200144done_testing;
145__END__
Akrone8235be2016-06-27 11:02:18 +0200146
147
Akron1b0c2652017-04-27 15:28:49 +0200148# Login mit falschem Nutzernamen:
149# 400 und:
150{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
151
Akron741b2b12017-04-13 22:15:59 +0200152
153
Akrone8235be2016-06-27 11:02:18 +0200154ok(!$c->user->get('details'), 'User not logged in');
155
156# Login with user credentials
157ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
158is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
159like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
160
161my $details = $c->user->get('details');
162is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
163is($details->{firstName}, 'Kustvakt', 'Firstname');
164is($details->{lastName}, 'KorAP', 'Lastname');
165is($details->{country}, 'Germany', 'Country');
166is($details->{address}, 'Mannheim', 'Address');
167is($details->{username}, 'kustvakt', 'Username');
168is($details->{institution}, 'IDS Mannheim', 'Institution');
169
170my $settings = $c->user->get('settings');
171is($settings->{username}, 'kustvakt', 'Username');
172
173# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
174#ok($c->user->set(details => {
175# firstName => 'Akron',
176# lastName => 'Fuxfell'
177#}), 'Set first name');
178
179# diag Dumper $c->user->get('info');
180
181ok(1,'Fine');
182
183done_testing;
184__END__