blob: 96897e59059c20e5ee0726f3f8446fb35b2e57ef [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
Akron0e1ed242018-10-11 13:22:00 +02007my $mount_point = '/api/';
8$ENV{KALAMAR_API} = $mount_point;
Akrone8235be2016-06-27 11:02:18 +02009
10my $t = Test::Mojo->new('Kalamar');
Akron0e1ed242018-10-11 13:22:00 +020011$t->app->defaults('auth_support' => 1);
Akrone8235be2016-06-27 11:02:18 +020012
Akron0e1ed242018-10-11 13:22:00 +020013# Mount fake backend
14# Get the fixture path
15my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
16my $fake_backend = $t->app->plugin(
17 Mount => {
18 $mount_point =>
Akron6d49c1f2018-10-11 14:22:21 +020019 $fixtures_path->child('fake_backend.pl')
Akron0e1ed242018-10-11 13:22:00 +020020 }
21);
22
23# Configure fake backend
24$fake_backend->pattern->defaults->{app}->log($t->app->log);
25
26$t->get_ok('/api')
27 ->status_is(200)
28 ->content_is('Fake server available');
Akron7d75ee32017-05-02 13:42:41 +020029
Akronbc213c02017-04-20 16:45:55 +020030$t->get_ok('/?q=Baum')
31 ->status_is(200)
32 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
33 ->text_like('#total-results', qr/\d+$/)
34 ->content_like(qr/\"authorized\"\:null/)
35 ;
36
Akronbe9d5b32017-04-05 20:48:24 +020037$t->get_ok('/')
38 ->element_exists('form[action=/user/login] input[name=handle_or_email]');
39
Akron741b2b12017-04-13 22:15:59 +020040$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
41 ->status_is(302)
42 ->header_is('Location' => '/');
43
44$t->get_ok('/')
45 ->status_is(200)
46 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020047 ->element_exists('input[name=handle_or_email][value=test]')
Akron741b2b12017-04-13 22:15:59 +020048 ;
Akronbe9d5b32017-04-05 20:48:24 +020049
Akrone5ef4e02017-04-19 17:07:52 +020050$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
51 ->status_is(302)
52 ->header_is('Location' => '/');
53
Akron15158e02018-03-19 12:42:46 +010054my $csrf = $t->get_ok('/')
55 ->status_is(200)
56 ->element_exists('div.notify-error')
57 ->text_is('div.notify-error', 'Bad CSRF token')
58 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
59 ;
60
61$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass', csrf_token => $csrf })
62 ->status_is(302)
63 ->header_is('Location' => '/');
64
Akrone5ef4e02017-04-19 17:07:52 +020065$t->get_ok('/')
66 ->status_is(200)
67 ->element_exists_not('div.notify-error')
68 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020069 ->text_is('div.notify-success', 'Login successful')
70 ;
71
72# Now the user is logged in and should be able to
73# search with authorization
74$t->get_ok('/?q=Baum')
75 ->status_is(200)
76 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
77 ->text_like('#total-results', qr/\d+$/)
78 ->element_exists_not('div.notify-error')
79 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020080 ;
81
Akron2e3d3772017-04-14 16:20:40 +020082
Akronbc213c02017-04-20 16:45:55 +020083# Logout
84$t->get_ok('/user/logout')
85 ->status_is(302)
86 ->header_is('Location' => '/');
87
88$t->get_ok('/')
89 ->status_is(200)
90 ->element_exists_not('div.notify-error')
91 ->element_exists('div.notify-success')
92 ->text_is('div.notify-success', 'Logout successful')
93 ;
94
95$t->get_ok('/?q=Baum')
96 ->status_is(200)
97 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
98 ->text_like('#total-results', qr/\d+$/)
99 ->content_like(qr/\"authorized\"\:null/)
100 ;
101
Akron429aeda2018-03-19 16:02:29 +0100102# Get redirect
103my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
104 ->status_is(200)
105 ->element_exists_not('div.notify-error')
106 ->tx->res->dom->at('input[name=fwd]')->attr('value')
107 ;
108
109is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
110
111$t->post_ok('/user/login' => form => {
112 handle_or_email => 'test',
113 pwd => 'pass',
114 csrf_token => $csrf,
115 fwd => 'http://bad.example.com/test'
116})
117 ->status_is(302)
118 ->header_is('Location' => '/');
119
120$t->get_ok('/')
121 ->status_is(200)
122 ->element_exists('div.notify-error')
123 ->element_exists_not('div.notify-success')
124 ->text_is('div.notify-error', 'Redirect failure')
125 ;
126
127$t->post_ok('/user/login' => form => {
128 handle_or_email => 'test',
129 pwd => 'pass',
130 csrf_token => $csrf,
131 fwd => $fwd
132})
133 ->status_is(302)
134 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
135
136
137
138
Akronbe9d5b32017-04-05 20:48:24 +0200139done_testing;
140__END__
Akrone8235be2016-06-27 11:02:18 +0200141
142
Akron1b0c2652017-04-27 15:28:49 +0200143# Login mit falschem Nutzernamen:
144# 400 und:
145{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
146
Akron741b2b12017-04-13 22:15:59 +0200147
148
Akrone8235be2016-06-27 11:02:18 +0200149ok(!$c->user->get('details'), 'User not logged in');
150
151# Login with user credentials
152ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
153is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
154like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
155
156my $details = $c->user->get('details');
157is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
158is($details->{firstName}, 'Kustvakt', 'Firstname');
159is($details->{lastName}, 'KorAP', 'Lastname');
160is($details->{country}, 'Germany', 'Country');
161is($details->{address}, 'Mannheim', 'Address');
162is($details->{username}, 'kustvakt', 'Username');
163is($details->{institution}, 'IDS Mannheim', 'Institution');
164
165my $settings = $c->user->get('settings');
166is($settings->{username}, 'kustvakt', 'Username');
167
168# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
169#ok($c->user->set(details => {
170# firstName => 'Akron',
171# lastName => 'Fuxfell'
172#}), 'Set first name');
173
174# diag Dumper $c->user->get('info');
175
176ok(1,'Fine');
177
178done_testing;
179__END__