Some tests to check for compliance with new Kustvakt
Change-Id: I5d9db0725664af2c6f1164e4acb258a76d89a90f
diff --git a/kalamar.conf b/kalamar.conf
index 83866f9..29d5bf6 100644
--- a/kalamar.conf
+++ b/kalamar.conf
@@ -1,15 +1,15 @@
# api => 'http://10.0.10.51:7070/api/v0.1/'
# api => 'http://10.0.10.13:7070/api/v0.1/'
-my $api = 'http://localhost:9999/api/v0.1/';
+my $api = 'http://localhost:9998/api/v0.1/';
{
Search => {
engine => 'Kalamar::API',
# IDS Kustvakt server:
- api => $api
+ api => $ENV{'KALAMAR_API'} // $api
},
Kalamar => {
- api => $api
+ api => $ENV{'KALAMAR_API'} // $api
},
CHI => {
default => {
@@ -20,10 +20,10 @@
},
user => {
l1_cache => {
- driver => 'FastMmap',
- root_dir => app->home . '/cache/usermap',
- cache_size => '50m',
- max_size => '50m'
+ driver => 'FastMmap',
+ root_dir => app->home . '/cache/usermap',
+ cache_size => '50m',
+ max_size => '50m'
},
driver => 'File',
root_dir => app->home . '/cache/userfile',
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index 12b5ded..3f1e3bf 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -102,9 +102,19 @@
my $navi = Mojo::File->new($self->home->child('templates','doc','navigation.json'))->slurp;
$self->config(navi => decode_json($navi)) if $navi;
+ $self->log->info('API expected at ' . $self->config->{Kalamar}->{api});
- # Establish routes
- my $r = $self->routes;
+ # Establish routes with authentification
+ my $r = $self->routes->under(
+ '/' => sub {
+ my $c = shift;
+
+ if ($c->session('auth')) {
+ $c->stash(auth => $c->session('auth'));
+ };
+ return 1;
+ }
+ );
# Base query route
$r->get('/')->to('search#query')->name('index');
@@ -131,11 +141,11 @@
$match->to('search#match_info')->name('match');
# User Management
- $r->any('/user')->to(controller => 'User');
- $r->post('/login')->to(action => 'login')->name('login');
- $r->get('/logout')->to(action => 'logout')->name('logout');
- $r->any('/register')->to(action => 'register')->name('register');
- $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
+ my $user = $r->any('/user')->to(controller => 'User');
+ $user->post('/login')->to(action => 'login')->name('login');
+# $r->get('/logout')->to(action => 'logout')->name('logout');
+# $r->any('/register')->to(action => 'register')->name('register');
+# $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
# Default user is called 'korap'
# $r->route('/user/:user/:collection')
diff --git a/lib/Kalamar/API.pm b/lib/Kalamar/API.pm
index 84e208e..fcf1c5e 100644
--- a/lib/Kalamar/API.pm
+++ b/lib/Kalamar/API.pm
@@ -71,8 +71,10 @@
# Check cache for total results
my $total_results;
+ my $user = $c->stash('user') // '?';
+
if (!$index->no_cache &&
- defined ($total_results = $c->chi->get($index->_api_cache))) {
+ defined ($total_results = $c->chi->get($user . $index->_api_cache))) {
# Set total results from cache
$index->total_results($total_results);
@@ -86,7 +88,7 @@
$index->api_request($url->to_string);
# Create new user agent and set timeout to 2 minutes
- my $ua = $c->ua;
+ my $ua = $c->user->ua;
$ua->inactivity_timeout(120);
# Debugging
@@ -97,10 +99,10 @@
$ua->get(
$url => sub {
- my $tx = pop;
- $self->_process_response('matches', $index, $tx);
- weaken $index;
- return $cb->($index);
+ my $tx = pop;
+ $self->_process_response('matches', $index, $tx);
+ weaken $index;
+ return $cb->($index);
});
}
@@ -138,7 +140,7 @@
$url->path('search');
# Create new user agent and set timeout to 30 seconds
- my $ua = $c->ua; # Mojo::UserAgent->new;
+ my $ua = $c->user->ua; # Mojo::UserAgent->new;
$ua->inactivity_timeout(30);
# Build transaction
@@ -198,7 +200,7 @@
$c->app->log->debug('Match info: ' . $url);
# Create new user agent and set timeout to 30 seconds
- my $ua = $c->ua; # Mojo::UserAgent->new;
+ my $ua = $c->user->ua; # Mojo::UserAgent->new;
$ua->inactivity_timeout(30);
# non-blocking
@@ -206,9 +208,9 @@
weaken $index;
$ua->get(
$url => sub {
- my $tx = pop;
- $self->_process_response('match', $index, $tx);
- return $cb->($index);
+ my $tx = pop;
+ $self->_process_response('match', $index, $tx);
+ return $cb->($index);
});
}
@@ -228,6 +230,8 @@
# Get controller
my $c = $index->controller;
+ my $user = $c->stash('user') // '?';
+
# If there is a callback, do async
my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE';
@@ -244,7 +248,7 @@
$c->app->log->debug('Get resource info on '. $url);
# Check for cached information
- if (my $json = $c->chi->get($url->to_string)) {
+ if (my $json = $c->chi->get($user . $url->to_string)) {
# TODO: That's unfortunate, as it prohibits caching of multiple resources
$c->app->log->debug('Get resource info from cache');
@@ -264,8 +268,8 @@
weaken $index;
$ua->get(
$url => sub {
- $self->_process_response('resource', $index, pop);
- return $cb->($index);
+ $self->_process_response('resource', $index, pop);
+ return $cb->($index);
})
}
@@ -385,9 +389,10 @@
if ($meta->{totalResults} && $meta->{totalResults} > -1) {
my $c = $index->controller;
+ my $user = $c->stash('user') // '?';
$c->app->log->debug('Cache total result');
- $c->chi->set($index->_api_cache => $meta->{totalResults}, '120min');
+ $c->chi->set($user . $index->_api_cache => $meta->{totalResults}, '120min');
$index->total_results($meta->{totalResults});
};
};
@@ -413,10 +418,12 @@
my ($self, $index, $json) = @_;
my $c = $index->controller;
+ my $user = $c->stash('user') // '?';
+
# TODO: That's unfortunate, as it prohibits multiple resources
$c->stash('search.resource' => $json);
$c->app->log->debug('Cache resource info');
- $c->chi->set($c->stash('search._resource_cache') => $json, '24 hours');
+ $c->chi->set($user . $c->stash('search._resource_cache') => $json, '24 hours');
};
diff --git a/lib/Kalamar/Controller/User.pm b/lib/Kalamar/Controller/User.pm
index f59f932..1584225 100644
--- a/lib/Kalamar/Controller/User.pm
+++ b/lib/Kalamar/Controller/User.pm
@@ -1,18 +1,22 @@
package Kalamar::Controller::User;
use Mojo::Base 'Mojolicious::Controller';
+# Login action
sub login {
my $c = shift;
- my $v = $c->validator;
+
+ # Validate input
+ my $v = $c->validation;
$v->required('handle_or_email');
$v->required('pwd');
- my $handle = $v->param('handle_or_email');
- my $pwd = $v->param('pwd');
+ # Login user
+ $c->user->login(
+ $v->param('handle_or_email'),
+ $v->param('pwd')
+ );
- $c->user->login($handle, $pwd);
-
- return $c->redirect_to;
+ return $c->redirect_to('/');
};
sub logout {
diff --git a/lib/Kalamar/Plugin/KalamarUser.pm b/lib/Kalamar/Plugin/KalamarUser.pm
index 7650e02..76233c5 100644
--- a/lib/Kalamar/Plugin/KalamarUser.pm
+++ b/lib/Kalamar/Plugin/KalamarUser.pm
@@ -35,9 +35,28 @@
my $token = $c->stash('auth');
return $token if $token;
+ # Get auth from session
+ my $auth = $c->session('auth') or return;
+
# Set token to stash
- $c->stash(auth => $c->session('auth'));
- return $c->stash('auth');
+ $c->stash(auth => $auth);
+ return $auth;
+ }
+ );
+
+ $mojo->helper(
+ 'user.ua' => sub {
+ my $c = shift;
+ my $auth = $c->user_auth;
+ return $plugin->ua unless $auth;
+ my $ua = Mojo::UserAgent->new;
+ $ua->on(
+ start => sub {
+ my ($ua, $tx) = @_;
+ $tx->req->headers->header('Authorization' => $auth);
+ }
+ );
+ return $ua;
}
);
@@ -50,7 +69,7 @@
return if (index($user, ':') >= 0);
my $url = Mojo::URL->new($plugin->api)->path('auth/apiToken');
- my $tx = $c->ua->get($url => {
+ my $tx = $plugin->ua->get($url => {
Authorization => 'Basic ' . b($user . ':' . $pwd)->b64_encode
});
@@ -59,6 +78,9 @@
my $jwt = $res->json;
+
+ # TODO: Deal with user return values.
+
my $auth = $jwt->{token_type} . ' ' . $jwt->{token};
$mojo->log->debug(qq!Login successful: "$user" with "$auth"!);
diff --git a/t/user.t b/t/user.t
new file mode 100644
index 0000000..22297d3
--- /dev/null
+++ b/t/user.t
@@ -0,0 +1,19 @@
+use Mojo::Base -strict;
+use lib;
+use Test::More;
+use Test::Mojo;
+use Data::Dumper;
+
+$ENV{MOJO_USERAGENT_DEBUG} = 1;
+
+my $t = Test::Mojo->new('Kalamar');
+
+my $c = $t->app->build_controller;
+
+# Login with user credentials
+ok($c->user->login('test_h', 'p278h'), 'Login with demo user');
+is($c->stash('user'), 'test_h', 'Kustvakt is logged in');
+like($c->stash('auth'), qr/^api_token /, 'Kustvakt is logged in');
+
+done_testing;
+__END__
diff --git a/templates/layouts/main.html.ep b/templates/layouts/main.html.ep
index f92ab2d..ed45db1 100644
--- a/templates/layouts/main.html.ep
+++ b/templates/layouts/main.html.ep
@@ -23,7 +23,7 @@
%= include 'partial/header'
% }
-% if (1) { # user not logged in
+% unless (user_auth) { # user not logged in
% content_for 'sidebar', begin
<fieldset>
%= form_for 'login', begin