blob: 4ac8a8ec8f50ff7cc88ab6dfc4af10777117409f [file] [log] [blame]
Akrone8235be2016-06-27 11:02:18 +02001use Mojo::Base -strict;
2use lib '../lib', 'lib';
Akronbe9d5b32017-04-05 20:48:24 +02003use Test::More;
Akrone8235be2016-06-27 11:02:18 +02004use Test::Mojo;
5use Data::Dumper;
6
Akron741b2b12017-04-13 22:15:59 +02007$ENV{MOJO_MODE} = 'test';
Akrone8235be2016-06-27 11:02:18 +02008
9my $t = Test::Mojo->new('Kalamar');
10
Akron7d75ee32017-05-02 13:42:41 +020011$t->app->defaults(auth_support => 1);
12
Akronbc213c02017-04-20 16:45:55 +020013$t->get_ok('/?q=Baum')
14 ->status_is(200)
15 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
16 ->text_like('#total-results', qr/\d+$/)
17 ->content_like(qr/\"authorized\"\:null/)
18 ;
19
Akronbe9d5b32017-04-05 20:48:24 +020020$t->get_ok('/')
21 ->element_exists('form[action=/user/login] input[name=handle_or_email]');
22
Akron741b2b12017-04-13 22:15:59 +020023$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
24 ->status_is(302)
25 ->header_is('Location' => '/');
26
27$t->get_ok('/')
28 ->status_is(200)
29 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020030 ->element_exists('input[name=handle_or_email][value=test]')
Akron741b2b12017-04-13 22:15:59 +020031 ;
Akronbe9d5b32017-04-05 20:48:24 +020032
Akrone5ef4e02017-04-19 17:07:52 +020033$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
34 ->status_is(302)
35 ->header_is('Location' => '/');
36
Akron15158e02018-03-19 12:42:46 +010037my $csrf = $t->get_ok('/')
38 ->status_is(200)
39 ->element_exists('div.notify-error')
40 ->text_is('div.notify-error', 'Bad CSRF token')
41 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
42 ;
43
44$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass', csrf_token => $csrf })
45 ->status_is(302)
46 ->header_is('Location' => '/');
47
Akrone5ef4e02017-04-19 17:07:52 +020048$t->get_ok('/')
49 ->status_is(200)
50 ->element_exists_not('div.notify-error')
51 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020052 ->text_is('div.notify-success', 'Login successful')
53 ;
54
55# Now the user is logged in and should be able to
56# search with authorization
57$t->get_ok('/?q=Baum')
58 ->status_is(200)
59 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
60 ->text_like('#total-results', qr/\d+$/)
61 ->element_exists_not('div.notify-error')
62 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020063 ;
64
Akron2e3d3772017-04-14 16:20:40 +020065
Akronbc213c02017-04-20 16:45:55 +020066# Logout
67$t->get_ok('/user/logout')
68 ->status_is(302)
69 ->header_is('Location' => '/');
70
71$t->get_ok('/')
72 ->status_is(200)
73 ->element_exists_not('div.notify-error')
74 ->element_exists('div.notify-success')
75 ->text_is('div.notify-success', 'Logout successful')
76 ;
77
78$t->get_ok('/?q=Baum')
79 ->status_is(200)
80 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
81 ->text_like('#total-results', qr/\d+$/)
82 ->content_like(qr/\"authorized\"\:null/)
83 ;
84
Akron429aeda2018-03-19 16:02:29 +010085# Get redirect
86my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
87 ->status_is(200)
88 ->element_exists_not('div.notify-error')
89 ->tx->res->dom->at('input[name=fwd]')->attr('value')
90 ;
91
92is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
93
94$t->post_ok('/user/login' => form => {
95 handle_or_email => 'test',
96 pwd => 'pass',
97 csrf_token => $csrf,
98 fwd => 'http://bad.example.com/test'
99})
100 ->status_is(302)
101 ->header_is('Location' => '/');
102
103$t->get_ok('/')
104 ->status_is(200)
105 ->element_exists('div.notify-error')
106 ->element_exists_not('div.notify-success')
107 ->text_is('div.notify-error', 'Redirect failure')
108 ;
109
110$t->post_ok('/user/login' => form => {
111 handle_or_email => 'test',
112 pwd => 'pass',
113 csrf_token => $csrf,
114 fwd => $fwd
115})
116 ->status_is(302)
117 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
118
119
120
121
Akronbe9d5b32017-04-05 20:48:24 +0200122done_testing;
123__END__
Akrone8235be2016-06-27 11:02:18 +0200124
125
Akron1b0c2652017-04-27 15:28:49 +0200126# Login mit falschem Nutzernamen:
127# 400 und:
128{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
129
Akron741b2b12017-04-13 22:15:59 +0200130
131
Akrone8235be2016-06-27 11:02:18 +0200132ok(!$c->user->get('details'), 'User not logged in');
133
134# Login with user credentials
135ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
136is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
137like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
138
139my $details = $c->user->get('details');
140is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
141is($details->{firstName}, 'Kustvakt', 'Firstname');
142is($details->{lastName}, 'KorAP', 'Lastname');
143is($details->{country}, 'Germany', 'Country');
144is($details->{address}, 'Mannheim', 'Address');
145is($details->{username}, 'kustvakt', 'Username');
146is($details->{institution}, 'IDS Mannheim', 'Institution');
147
148my $settings = $c->user->get('settings');
149is($settings->{username}, 'kustvakt', 'Username');
150
151# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
152#ok($c->user->set(details => {
153# firstName => 'Akron',
154# lastName => 'Fuxfell'
155#}), 'Set first name');
156
157# diag Dumper $c->user->get('info');
158
159ok(1,'Fine');
160
161done_testing;
162__END__