blob: 665a623e3bbf1315743eacba7ffe3f9899ff3405 [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
Akron73f36082018-10-25 15:34:59 +020019my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
Akron0e1ed242018-10-11 13:22:00 +020020my $fake_backend = $t->app->plugin(
21 Mount => {
22 $mount_point =>
Akron73f36082018-10-25 15:34:59 +020023 $fixtures_path->child('mock.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
Akrond7ed34b2018-10-22 18:42:28 +020066$t->post_ok('/user/login' => form => {
67 handle_or_email => 'test',
68 pwd => 'pass',
69 csrf_token => $csrf
70})
Akron15158e02018-03-19 12:42:46 +010071 ->status_is(302)
72 ->header_is('Location' => '/');
73
Akrone5ef4e02017-04-19 17:07:52 +020074$t->get_ok('/')
75 ->status_is(200)
76 ->element_exists_not('div.notify-error')
77 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020078 ->text_is('div.notify-success', 'Login successful')
79 ;
80
81# Now the user is logged in and should be able to
82# search with authorization
83$t->get_ok('/?q=Baum')
84 ->status_is(200)
85 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
86 ->text_like('#total-results', qr/\d+$/)
87 ->element_exists_not('div.notify-error')
88 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020089 ;
90
Akronbc213c02017-04-20 16:45:55 +020091# Logout
92$t->get_ok('/user/logout')
93 ->status_is(302)
94 ->header_is('Location' => '/');
95
96$t->get_ok('/')
97 ->status_is(200)
98 ->element_exists_not('div.notify-error')
99 ->element_exists('div.notify-success')
100 ->text_is('div.notify-success', 'Logout successful')
101 ;
102
103$t->get_ok('/?q=Baum')
104 ->status_is(200)
105 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
106 ->text_like('#total-results', qr/\d+$/)
107 ->content_like(qr/\"authorized\"\:null/)
108 ;
109
Akron429aeda2018-03-19 16:02:29 +0100110# Get redirect
111my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
112 ->status_is(200)
113 ->element_exists_not('div.notify-error')
114 ->tx->res->dom->at('input[name=fwd]')->attr('value')
115 ;
116
117is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
118
119$t->post_ok('/user/login' => form => {
120 handle_or_email => 'test',
121 pwd => 'pass',
122 csrf_token => $csrf,
123 fwd => 'http://bad.example.com/test'
124})
125 ->status_is(302)
126 ->header_is('Location' => '/');
127
128$t->get_ok('/')
129 ->status_is(200)
130 ->element_exists('div.notify-error')
131 ->element_exists_not('div.notify-success')
132 ->text_is('div.notify-error', 'Redirect failure')
133 ;
134
135$t->post_ok('/user/login' => form => {
136 handle_or_email => 'test',
137 pwd => 'pass',
138 csrf_token => $csrf,
139 fwd => $fwd
140})
141 ->status_is(302)
142 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
143
144
145
146
Akronbe9d5b32017-04-05 20:48:24 +0200147done_testing;
148__END__
Akrone8235be2016-06-27 11:02:18 +0200149
150
Akron1b0c2652017-04-27 15:28:49 +0200151# Login mit falschem Nutzernamen:
152# 400 und:
153{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
154
Akron741b2b12017-04-13 22:15:59 +0200155
156
Akrone8235be2016-06-27 11:02:18 +0200157ok(!$c->user->get('details'), 'User not logged in');
158
159# Login with user credentials
160ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
161is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
162like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
163
164my $details = $c->user->get('details');
165is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
166is($details->{firstName}, 'Kustvakt', 'Firstname');
167is($details->{lastName}, 'KorAP', 'Lastname');
168is($details->{country}, 'Germany', 'Country');
169is($details->{address}, 'Mannheim', 'Address');
170is($details->{username}, 'kustvakt', 'Username');
171is($details->{institution}, 'IDS Mannheim', 'Institution');
172
173my $settings = $c->user->get('settings');
174is($settings->{username}, 'kustvakt', 'Username');
175
176# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
177#ok($c->user->set(details => {
178# firstName => 'Akron',
179# lastName => 'Fuxfell'
180#}), 'Set first name');
181
182# diag Dumper $c->user->get('info');
183
184ok(1,'Fine');
185
186done_testing;
187__END__