blob: aa94bade32bb12afe9d7961f83a3d61116684d34 [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 =>
19 $fixtures_path->child('test_backend.pl')
20 }
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
Akron0e1ed242018-10-11 13:22:00 +020037
Akronbe9d5b32017-04-05 20:48:24 +020038$t->get_ok('/')
39 ->element_exists('form[action=/user/login] input[name=handle_or_email]');
40
Akron741b2b12017-04-13 22:15:59 +020041$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
42 ->status_is(302)
43 ->header_is('Location' => '/');
44
45$t->get_ok('/')
46 ->status_is(200)
47 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020048 ->element_exists('input[name=handle_or_email][value=test]')
Akron741b2b12017-04-13 22:15:59 +020049 ;
Akronbe9d5b32017-04-05 20:48:24 +020050
Akrone5ef4e02017-04-19 17:07:52 +020051$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
52 ->status_is(302)
53 ->header_is('Location' => '/');
54
Akron15158e02018-03-19 12:42:46 +010055my $csrf = $t->get_ok('/')
56 ->status_is(200)
57 ->element_exists('div.notify-error')
58 ->text_is('div.notify-error', 'Bad CSRF token')
59 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
60 ;
61
62$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass', csrf_token => $csrf })
63 ->status_is(302)
64 ->header_is('Location' => '/');
65
Akrone5ef4e02017-04-19 17:07:52 +020066$t->get_ok('/')
67 ->status_is(200)
68 ->element_exists_not('div.notify-error')
69 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020070 ->text_is('div.notify-success', 'Login successful')
71 ;
72
73# Now the user is logged in and should be able to
74# search with authorization
75$t->get_ok('/?q=Baum')
76 ->status_is(200)
77 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
78 ->text_like('#total-results', qr/\d+$/)
79 ->element_exists_not('div.notify-error')
80 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020081 ;
82
Akron2e3d3772017-04-14 16:20:40 +020083
Akronbc213c02017-04-20 16:45:55 +020084# Logout
85$t->get_ok('/user/logout')
86 ->status_is(302)
87 ->header_is('Location' => '/');
88
89$t->get_ok('/')
90 ->status_is(200)
91 ->element_exists_not('div.notify-error')
92 ->element_exists('div.notify-success')
93 ->text_is('div.notify-success', 'Logout successful')
94 ;
95
96$t->get_ok('/?q=Baum')
97 ->status_is(200)
98 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
99 ->text_like('#total-results', qr/\d+$/)
100 ->content_like(qr/\"authorized\"\:null/)
101 ;
102
Akron429aeda2018-03-19 16:02:29 +0100103# Get redirect
104my $fwd = $t->get_ok('/?q=Baum&ql=poliqarp')
105 ->status_is(200)
106 ->element_exists_not('div.notify-error')
107 ->tx->res->dom->at('input[name=fwd]')->attr('value')
108 ;
109
110is($fwd, '/?q=Baum&ql=poliqarp', 'Redirect is valid');
111
112$t->post_ok('/user/login' => form => {
113 handle_or_email => 'test',
114 pwd => 'pass',
115 csrf_token => $csrf,
116 fwd => 'http://bad.example.com/test'
117})
118 ->status_is(302)
119 ->header_is('Location' => '/');
120
121$t->get_ok('/')
122 ->status_is(200)
123 ->element_exists('div.notify-error')
124 ->element_exists_not('div.notify-success')
125 ->text_is('div.notify-error', 'Redirect failure')
126 ;
127
128$t->post_ok('/user/login' => form => {
129 handle_or_email => 'test',
130 pwd => 'pass',
131 csrf_token => $csrf,
132 fwd => $fwd
133})
134 ->status_is(302)
135 ->header_is('Location' => '/?q=Baum&ql=poliqarp');
136
137
138
139
Akronbe9d5b32017-04-05 20:48:24 +0200140done_testing;
141__END__
Akrone8235be2016-06-27 11:02:18 +0200142
143
Akron1b0c2652017-04-27 15:28:49 +0200144# Login mit falschem Nutzernamen:
145# 400 und:
146{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
147
Akron741b2b12017-04-13 22:15:59 +0200148
149
Akrone8235be2016-06-27 11:02:18 +0200150ok(!$c->user->get('details'), 'User not logged in');
151
152# Login with user credentials
153ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
154is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
155like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
156
157my $details = $c->user->get('details');
158is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
159is($details->{firstName}, 'Kustvakt', 'Firstname');
160is($details->{lastName}, 'KorAP', 'Lastname');
161is($details->{country}, 'Germany', 'Country');
162is($details->{address}, 'Mannheim', 'Address');
163is($details->{username}, 'kustvakt', 'Username');
164is($details->{institution}, 'IDS Mannheim', 'Institution');
165
166my $settings = $c->user->get('settings');
167is($settings->{username}, 'kustvakt', 'Username');
168
169# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
170#ok($c->user->set(details => {
171# firstName => 'Akron',
172# lastName => 'Fuxfell'
173#}), 'Set first name');
174
175# diag Dumper $c->user->get('info');
176
177ok(1,'Fine');
178
179done_testing;
180__END__