blob: cb9a7b61c2a9afc0462e7bc9da7eb7b0cd0eb326 [file] [log] [blame]
Akrone8235be2016-06-27 11:02:18 +02001use Mojo::Base -strict;
2use lib '../lib', 'lib';
Akronbe9d5b32017-04-05 20:48:24 +02003use Test::More;
Akrone8235be2016-06-27 11:02:18 +02004use Test::Mojo;
5use Data::Dumper;
6
Akron741b2b12017-04-13 22:15:59 +02007$ENV{MOJO_MODE} = 'test';
Akrone8235be2016-06-27 11:02:18 +02008
9my $t = Test::Mojo->new('Kalamar');
10
Akron7d75ee32017-05-02 13:42:41 +020011$t->app->defaults(auth_support => 1);
12
Akronbc213c02017-04-20 16:45:55 +020013$t->get_ok('/?q=Baum')
14 ->status_is(200)
15 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
16 ->text_like('#total-results', qr/\d+$/)
17 ->content_like(qr/\"authorized\"\:null/)
18 ;
19
Akronbe9d5b32017-04-05 20:48:24 +020020$t->get_ok('/')
21 ->element_exists('form[action=/user/login] input[name=handle_or_email]');
22
Akron741b2b12017-04-13 22:15:59 +020023$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'fail' })
24 ->status_is(302)
25 ->header_is('Location' => '/');
26
27$t->get_ok('/')
28 ->status_is(200)
29 ->element_exists('div.notify-error')
Akron2e3d3772017-04-14 16:20:40 +020030 ->element_exists('input[name=handle_or_email][value=test]')
Akron741b2b12017-04-13 22:15:59 +020031 ;
Akronbe9d5b32017-04-05 20:48:24 +020032
Akrone5ef4e02017-04-19 17:07:52 +020033$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass' })
34 ->status_is(302)
35 ->header_is('Location' => '/');
36
Akron15158e02018-03-19 12:42:46 +010037my $csrf = $t->get_ok('/')
38 ->status_is(200)
39 ->element_exists('div.notify-error')
40 ->text_is('div.notify-error', 'Bad CSRF token')
41 ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
42 ;
43
44$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'pass', csrf_token => $csrf })
45 ->status_is(302)
46 ->header_is('Location' => '/');
47
Akrone5ef4e02017-04-19 17:07:52 +020048$t->get_ok('/')
49 ->status_is(200)
50 ->element_exists_not('div.notify-error')
51 ->element_exists('div.notify-success')
Akronbc213c02017-04-20 16:45:55 +020052 ->text_is('div.notify-success', 'Login successful')
53 ;
54
55# Now the user is logged in and should be able to
56# search with authorization
57$t->get_ok('/?q=Baum')
58 ->status_is(200)
59 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
60 ->text_like('#total-results', qr/\d+$/)
61 ->element_exists_not('div.notify-error')
62 ->content_like(qr/\"authorized\"\:\"test\"/)
Akrone5ef4e02017-04-19 17:07:52 +020063 ;
64
Akron2e3d3772017-04-14 16:20:40 +020065
Akronbc213c02017-04-20 16:45:55 +020066# Logout
67$t->get_ok('/user/logout')
68 ->status_is(302)
69 ->header_is('Location' => '/');
70
71$t->get_ok('/')
72 ->status_is(200)
73 ->element_exists_not('div.notify-error')
74 ->element_exists('div.notify-success')
75 ->text_is('div.notify-success', 'Logout successful')
76 ;
77
78$t->get_ok('/?q=Baum')
79 ->status_is(200)
80 ->text_like('h1 span', qr/KorAP: Find .Baum./i)
81 ->text_like('#total-results', qr/\d+$/)
82 ->content_like(qr/\"authorized\"\:null/)
83 ;
84
Akronbe9d5b32017-04-05 20:48:24 +020085done_testing;
86__END__
Akrone8235be2016-06-27 11:02:18 +020087
88
Akron1b0c2652017-04-27 15:28:49 +020089# Login mit falschem Nutzernamen:
90# 400 und:
91{"errors":[[2022,"LDAP Authentication failed due to unknown user or password!"]]}
92
Akron741b2b12017-04-13 22:15:59 +020093
94
Akrone8235be2016-06-27 11:02:18 +020095ok(!$c->user->get('details'), 'User not logged in');
96
97# Login with user credentials
98ok($c->user->login('kustvakt', 'kustvakt2015'), 'Login with demo user');
99is($c->stash('user'), 'kustvakt', 'Kustvakt is logged in');
100like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
101
102my $details = $c->user->get('details');
103is($details->{email}, 'kustvakt@ids-mannheim.de', 'Email');
104is($details->{firstName}, 'Kustvakt', 'Firstname');
105is($details->{lastName}, 'KorAP', 'Lastname');
106is($details->{country}, 'Germany', 'Country');
107is($details->{address}, 'Mannheim', 'Address');
108is($details->{username}, 'kustvakt', 'Username');
109is($details->{institution}, 'IDS Mannheim', 'Institution');
110
111my $settings = $c->user->get('settings');
112is($settings->{username}, 'kustvakt', 'Username');
113
114# ok($c->user->set(details => { firstName => 'Me' }), 'Set first name');
115#ok($c->user->set(details => {
116# firstName => 'Akron',
117# lastName => 'Fuxfell'
118#}), 'Set first name');
119
120# diag Dumper $c->user->get('info');
121
122ok(1,'Fine');
123
124done_testing;
125__END__