blob: a81c06f89825b202f5e50f814ba5d18e2fe45453 [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/)
42 ;
43
Akronbe9d5b32017-04-05 20:48:24 +020044$t->get_ok('/')
Akrona9c8b0e2018-11-16 20:20:28 +010045 ->status_is(200)
46 ->element_exists('form[action=/user/login] input[name=handle_or_email]')
47 ;
Akronbe9d5b32017-04-05 20:48:24 +020048
Akron741b2b12017-04-13 22:15:59 +020049$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
50 ->status_is(302)
51 ->header_is('Location' => '/');
52
53$t->get_ok('/')
54 ->status_is(200)
55 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020056 ->element_exists('input[name=handle_or_email][value=test]')
Akron741b2b12017-04-13 22:15:59 +020057 ;
Akronbe9d5b32017-04-05 20:48:24 +020058
Akrone5ef4e02017-04-19 17:07:52 +020059$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
60 ->status_is(302)
61 ->header_is('Location' => '/');
62
Akron15158e02018-03-19 12:42:46 +010063my $csrf = $t->get_ok('/')
64 ->status_is(200)
65 ->element_exists('div.notify-error')
66 ->text_is('div.notify-error', 'Bad CSRF token')
67 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
68 ;
69
Akrond7ed34b2018-10-22 18:42:28 +020070$t->post_ok('/user/login' => form => {
71 handle_or_email => 'test',
72 pwd => 'pass',
73 csrf_token => $csrf
74})
Akron15158e02018-03-19 12:42:46 +010075 ->status_is(302)
Akron864c2932018-11-16 17:18:55 +010076 ->content_is('')
Akron15158e02018-03-19 12:42:46 +010077 ->header_is('Location' => '/');
78
Akrone5ef4e02017-04-19 17:07:52 +020079$t->get_ok('/')
80 ->status_is(200)
81 ->element_exists_not('div.notify-error')
82 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020083 ->text_is('div.notify-success', 'Login successful')
84 ;
85
86# Now the user is logged in and should be able to
87# search with authorization
88$t->get_ok('/?q=Baum')
89 ->status_is(200)
90 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
91 ->text_like('#total-results', qr/\d+$/)
92 ->element_exists_not('div.notify-error')
93 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020094 ;
95
Akronbc213c02017-04-20 16:45:55 +020096# Logout
97$t->get_ok('/user/logout')
98 ->status_is(302)
99 ->header_is('Location' => '/');
100
101$t->get_ok('/')
102 ->status_is(200)
103 ->element_exists_not('div.notify-error')
104 ->element_exists('div.notify-success')
105 ->text_is('div.notify-success', 'Logout successful')
106 ;
107
108$t->get_ok('/?q=Baum')
109 ->status_is(200)
110 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
111 ->text_like('#total-results', qr/\d+$/)
112 ->content_like(qr/\"authorized\"\:null/)
113 ;
114
Akron429aeda2018-03-19 16:02:29 +0100115# Get redirect
116my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
117 ->status_is(200)
118 ->element_exists_not('div.notify-error')
119 ->tx->res->dom->at('input[name=fwd]')->attr('value')
120 ;
121
122is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
123
124$t->post_ok('/user/login' => form => {
125 handle_or_email => 'test',
126 pwd => 'pass',
127 csrf_token => $csrf,
128 fwd => 'http://bad.example.com/test'
129})
130 ->status_is(302)
131 ->header_is('Location' => '/');
132
133$t->get_ok('/')
134 ->status_is(200)
135 ->element_exists('div.notify-error')
136 ->element_exists_not('div.notify-success')
137 ->text_is('div.notify-error', 'Redirect failure')
138 ;
139
140$t->post_ok('/user/login' => form => {
141 handle_or_email => 'test',
142 pwd => 'pass',
143 csrf_token => $csrf,
144 fwd => $fwd
145})
146 ->status_is(302)
147 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
148
149
150
151
Akronbe9d5b32017-04-05 20:48:24 +0200152done_testing;
153__END__
Akrone8235be2016-06-27 11:02:18 +0200154
155
Akron1b0c2652017-04-27 15:28:49 +0200156# Login mit falschem Nutzernamen:
157# 400 und:
158{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
159
Akron741b2b12017-04-13 22:15:59 +0200160
161
Akrone8235be2016-06-27 11:02:18 +0200162ok(!$c->user->get('details'), 'User not logged in');
163
164# Login with user credentials
165ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
166is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
167like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
168
169my $details = $c->user->get('details');
170is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
171is($details->{firstName}, 'Kustvakt', 'Firstname');
172is($details->{lastName}, 'KorAP', 'Lastname');
173is($details->{country}, 'Germany', 'Country');
174is($details->{address}, 'Mannheim', 'Address');
175is($details->{username}, 'kustvakt', 'Username');
176is($details->{institution}, 'IDS Mannheim', 'Institution');
177
178my $settings = $c->user->get('settings');
179is($settings->{username}, 'kustvakt', 'Username');
180
181# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
182#ok($c->user->set(details => {
183# firstName => 'Akron',
184# lastName => 'Fuxfell'
185#}), 'Set first name');
186
187# diag Dumper $c->user->get('info');
188
189ok(1,'Fine');
190
191done_testing;
192__END__