Slightly improved test server communication

Change-Id: Id6adb2e6a1525a10e741027ace3168c42b2f1fbf
diff --git a/Makefile.PL b/Makefile.PL
index 4ad02be..0e006e9 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -21,10 +21,11 @@
     'Mojolicious::Plugin::MailException' => 0.18,
     'Mojolicious::Plugin::CHI' => 0.09,
     'Cache::FastMmap' => 0,
+    'File::Temp' => 1,
 
     # Currently on GitHub only (github.com/akron)
     'Mojolicious::Plugin::Search' => 0.04,
-    'Mojolicious::Plugin::Localize' => 0.15
+    'Mojolicious::Plugin::Localize' => 0.15,
   },
   test => {
     TESTS => 't/*.t'
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) = @_;
diff --git a/t/remote_user.t b/t/remote_user.t
index 1c0d1d1..2110b78 100644
--- a/t/remote_user.t
+++ b/t/remote_user.t
@@ -1,6 +1,6 @@
 use Mojo::Base -strict;
 use lib '../lib', 'lib';
-use Test::More skip_all => 'No remote tests';
+use Test::More;
 use Test::Mojo;
 use Data::Dumper;
 
@@ -8,7 +8,20 @@
 
 my $t = Test::Mojo->new('Kalamar');
 
-my $c = $t->app->build_controller;
+$t->app->mode('test');
+
+# my $c = $t->app->build_controller;
+
+$t->get_ok('/')
+  ->element_exists('form[action=/user/login] input[name=handle_or_email]');
+
+$t->post_ok('/user/login' => form => { handle_or_email => 'test' })
+  ->status_is(302);
+
+$t->post_ok('/user/login' => form => { handle_or_email => 'test', pwd => 'xyz' });
+
+done_testing;
+__END__
 
 
 ok(!$c->user->get('details'), 'User not logged in');