blob: e63b1b263919b775172599c7142789fec7224b05 [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/)
Akronc82b1bc2018-11-18 18:06:14 +010042 ->element_exists_not('div.button.top a')
Akronbc213c02017-04-20 16:45:55 +020043 ;
44
Akronbe9d5b32017-04-05 20:48:24 +020045$t->get_ok('/')
Akrona9c8b0e2018-11-16 20:20:28 +010046 ->status_is(200)
47 ->element_exists('form[action=/user/login] input[name=handle_or_email]')
48 ;
Akronbe9d5b32017-04-05 20:48:24 +020049
Akron741b2b12017-04-13 22:15:59 +020050$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
51 ->status_is(302)
52 ->header_is('Location' => '/');
53
54$t->get_ok('/')
55 ->status_is(200)
56 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020057 ->element_exists('input[name=handle_or_email][value=test]')
Akronc82b1bc2018-11-18 18:06:14 +010058 ->element_exists_not('div.button.top a')
Akron741b2b12017-04-13 22:15:59 +020059 ;
Akronbe9d5b32017-04-05 20:48:24 +020060
Akrone5ef4e02017-04-19 17:07:52 +020061$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
62 ->status_is(302)
63 ->header_is('Location' => '/');
64
Akron15158e02018-03-19 12:42:46 +010065my $csrf = $t->get_ok('/')
66 ->status_is(200)
67 ->element_exists('div.notify-error')
68 ->text_is('div.notify-error', 'Bad CSRF token')
Akronc82b1bc2018-11-18 18:06:14 +010069 ->element_exists_not('div.button.top a')
Akron15158e02018-03-19 12:42:46 +010070 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
71 ;
72
Akrond7ed34b2018-10-22 18:42:28 +020073$t->post_ok('/user/login' => form => {
74 handle_or_email => 'test',
75 pwd => 'pass',
76 csrf_token => $csrf
77})
Akron15158e02018-03-19 12:42:46 +010078 ->status_is(302)
Akron864c2932018-11-16 17:18:55 +010079 ->content_is('')
Akron15158e02018-03-19 12:42:46 +010080 ->header_is('Location' => '/');
81
Akrone5ef4e02017-04-19 17:07:52 +020082$t->get_ok('/')
83 ->status_is(200)
84 ->element_exists_not('div.notify-error')
85 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020086 ->text_is('div.notify-success', 'Login successful')
87 ;
88
89# Now the user is logged in and should be able to
90# search with authorization
91$t->get_ok('/?q=Baum')
92 ->status_is(200)
93 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
94 ->text_like('#total-results', qr/\d+$/)
95 ->element_exists_not('div.notify-error')
96 ->content_like(qr/\"authorized\"\:\"test\"/)
Akronc82b1bc2018-11-18 18:06:14 +010097 ->element_exists('div.button.top a')
98 ->element_exists('div.button.top a.logout[title~="test"]')
Akrone5ef4e02017-04-19 17:07:52 +020099 ;
100
Akronbc213c02017-04-20 16:45:55 +0200101# Logout
102$t->get_ok('/user/logout')
103 ->status_is(302)
104 ->header_is('Location' => '/');
105
106$t->get_ok('/')
107 ->status_is(200)
108 ->element_exists_not('div.notify-error')
109 ->element_exists('div.notify-success')
110 ->text_is('div.notify-success', 'Logout successful')
111 ;
112
113$t->get_ok('/?q=Baum')
114 ->status_is(200)
115 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
116 ->text_like('#total-results', qr/\d+$/)
117 ->content_like(qr/\"authorized\"\:null/)
118 ;
119
Akron429aeda2018-03-19 16:02:29 +0100120# Get redirect
121my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
122 ->status_is(200)
123 ->element_exists_not('div.notify-error')
124 ->tx->res->dom->at('input[name=fwd]')->attr('value')
125 ;
126
127is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
128
129$t->post_ok('/user/login' => form => {
130 handle_or_email => 'test',
131 pwd => 'pass',
132 csrf_token => $csrf,
133 fwd => 'http://bad.example.com/test'
134})
135 ->status_is(302)
136 ->header_is('Location' => '/');
137
138$t->get_ok('/')
139 ->status_is(200)
140 ->element_exists('div.notify-error')
141 ->element_exists_not('div.notify-success')
142 ->text_is('div.notify-error', 'Redirect failure')
143 ;
144
145$t->post_ok('/user/login' => form => {
146 handle_or_email => 'test',
147 pwd => 'pass',
148 csrf_token => $csrf,
149 fwd => $fwd
150})
151 ->status_is(302)
152 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
153
154
155
156
Akronbe9d5b32017-04-05 20:48:24 +0200157done_testing;
158__END__
Akrone8235be2016-06-27 11:02:18 +0200159
160
Akron1b0c2652017-04-27 15:28:49 +0200161# Login mit falschem Nutzernamen:
162# 400 und:
163{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
164
Akron741b2b12017-04-13 22:15:59 +0200165
166
Akrone8235be2016-06-27 11:02:18 +0200167ok(!$c->user->get('details'), 'User not logged in');
168
169# Login with user credentials
170ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
171is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
172like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
173
174my $details = $c->user->get('details');
175is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
176is($details->{firstName}, 'Kustvakt', 'Firstname');
177is($details->{lastName}, 'KorAP', 'Lastname');
178is($details->{country}, 'Germany', 'Country');
179is($details->{address}, 'Mannheim', 'Address');
180is($details->{username}, 'kustvakt', 'Username');
181is($details->{institution}, 'IDS Mannheim', 'Institution');
182
183my $settings = $c->user->get('settings');
184is($settings->{username}, 'kustvakt', 'Username');
185
186# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
187#ok($c->user->set(details => {
188# firstName => 'Akron',
189# lastName => 'Fuxfell'
190#}), 'Set first name');
191
192# diag Dumper $c->user->get('info');
193
194ok(1,'Fine');
195
196done_testing;
197__END__