Slightly improved test server communication

Change-Id: Id6adb2e6a1525a10e741027ace3168c42b2f1fbf
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index 92937d4..64220aa 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -3,6 +3,8 @@
 use Mojo::ByteStream 'b';
 use Mojo::File;
 use Mojo::JSON 'decode_json';
+use Mojo::Util qw/url_escape/;
+use File::Temp qw/tmpnam/;
 
 # Minor version - may be patched from package.json
 our $VERSION = '0.21';
@@ -61,6 +63,15 @@
   # Configuration framework
   $self->plugin('Config');
 
+  # Start fixture server
+  if ($self->mode eq 'test') {
+    $self->plugin(Mount => {
+      '/api/v0.1' => $self->home->child('lib/Kalamar/Apps/test_backend.pl')
+    });
+
+    $self->config('Kalamar')->{api} = "/api/v0.1/";
+  };
+
   # Client notifications
   $self->plugin(Notifications => {
     'Kalamar::Plugin::Notifications' => 1,
@@ -97,13 +108,6 @@
   # Configure mail exception
   $self->plugin('MailException' => $self->config('MailException'));
 
-  # Start fixture
-  if ($self->mode eq 'test') {
-    $self->plugin(Mount => {
-      'http://*:3001/api/v0.1/' => $self->home->child('lib/Kalamar/Apps/test_backend.pl')
-    });
-    $self->config('Kalamar')->{api} = 'http://*:3001/api/v0.1/';
-  };
 
 
   # Configure documentation navigation
diff --git a/lib/Kalamar/Apps/test_backend.pl b/lib/Kalamar/Apps/test_backend.pl
index 237d566..ba11f51 100644
--- a/lib/Kalamar/Apps/test_backend.pl
+++ b/lib/Kalamar/Apps/test_backend.pl
@@ -5,6 +5,10 @@
 
 # This is an API fake server with fixtures
 
+get '/' => sub {
+  shift->render(text => 'Fake server available');
+};
+
 # Request API token
 get '/auth/apiToken' => sub {
   my $c = shift;
@@ -45,5 +49,3 @@
 };
 
 app->start;
-
-1;
diff --git a/lib/Kalamar/Controller/User.pm b/lib/Kalamar/Controller/User.pm
index 6fb1f02..6d82dab 100644
--- a/lib/Kalamar/Controller/User.pm
+++ b/lib/Kalamar/Controller/User.pm
@@ -10,8 +10,12 @@
   $v->required('handle_or_email', 'trim');
   $v->required('pwd', 'trim');
 
+  if ($v->has_error) {
+    $c->notify(error => 'login fail');
+  }
+
   # Login user
-  if ($c->user->login(
+  elsif ($c->user->login(
     $v->param('handle_or_email'),
     $v->param('pwd')
   )) {
diff --git a/lib/Kalamar/Plugin/KalamarUser.pm b/lib/Kalamar/Plugin/KalamarUser.pm
index 81a5fda..3eda76a 100644
--- a/lib/Kalamar/Plugin/KalamarUser.pm
+++ b/lib/Kalamar/Plugin/KalamarUser.pm
@@ -29,6 +29,9 @@
     inactivity_timeout => 60
   ));
 
+  # Set app to server
+  $plugin->ua->server->app($mojo);
+
   # Get the user token necessary for authorization
   $mojo->helper(
     'user_auth' => sub {
@@ -56,6 +59,10 @@
       return $plugin->ua unless $auth;
 
       my $ua = Mojo::UserAgent->new;
+
+      # Set app to server
+      $ua->server->app($mojo);
+
       $ua->on(
         start => sub {
           my ($ua, $tx) = @_;