Improve handling of non-existing or malformed JSON conf-files

Change-Id: Ia89be3459d60a11302ddd18a0c57a01b38996137
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index 04fcc3c..f83bd2a 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -507,14 +507,11 @@
   ->header_is('Pragma','no-cache')
   ;
 
-my $loglines = '';
-$t->app->log->on(
-  message => sub {
-    my ($log, $level, @lines) = @_;
-    if ($level eq 'warn') {
-      $loglines = join ',', @lines;
-    };
-  });
+
+my $log_output = '';
+open my $log_fh, '>', \$log_output or die $!;
+$t->app->log->handle($log_fh);
+$t->app->log->level('warn');
 
 $t->get_ok('/settings/marketplace')
   ->status_is(200)
@@ -523,7 +520,7 @@
   ->element_exists_not('ul.plugin_in-list')
   ;
 
-is($loglines, '', 'Check log is fine');
+is($log_output, '', 'Check log is fine');
 
 $csrf = $t->post_ok('/settings/oauth/register' => form => {
   name => 'MyApp',
@@ -1382,7 +1379,43 @@
   ->element_exists('aside.settings')
   ;
 
+$log_output = '';
 
+# Test non-existing client file (app should initialize without crashing)
+$t = Test::Mojo->new('Kalamar');
+$t->app->log->handle($log_fh);
+$t->app->log->level('error');
+$t->app->plugin('Mount' => {
+  $mount_point => $fixtures_path->child('mock.pl')
+});
+
+$t->app->plugin('Auth' => {
+    client_file => '/nonexistent/client.json',
+    oauth2 => 1
+  }
+);
+ok($t->app, 'App initializes with non-existing client file');
+like($log_output, qr'provided client file does not exist', 'Check log is fine');
+
+$log_output = '';
+
+# Test malformed JSON client file (app should initialize without crashing)
+my $bad_client_file = tempfile();
+$bad_client_file->spew('{invalid json}');
+$t = Test::Mojo->new('Kalamar');
+$t->app->log->handle($log_fh);
+$t->app->log->level('error');
+$t->app->plugin('Mount' => {
+  $mount_point => $fixtures_path->child('mock.pl')
+});
+
+$t->app->plugin('Auth' => {
+    client_file => $bad_client_file->to_string,
+    oauth2 => 1
+  }
+);
+ok($t->app, 'App initializes with malformed JSON client file');
+like($log_output, qr'provided client file syntax invalid', 'Check log is fine');
 done_testing;
 __END__
 
diff --git a/t/plugin/plugins.t b/t/plugin/plugins.t
index 93ebc29..d68730e 100644
--- a/t/plugin/plugins.t
+++ b/t/plugin/plugins.t
@@ -69,4 +69,28 @@
   ->json_is('/1/embed/0/title','Full Text')
   ;
 
+# Test non-existing file
+my $t2 = Test::Mojo->new('Kalamar');
+my $log_output = '';
+open my $log_fh, '>', \$log_output or die $!;
+$t2->app->log->handle($log_fh);
+$t2->app->log->level('error');
+$t2->app->plugin('Plugins' => {
+  default_plugins => '/nonexistent/file.json'
+});
+like($log_output, qr/provided default_plugins file does not exist/, 'Non-existing file logged');
+
+# Test malformed JSON
+$log_output = '';
+my $t3 = Test::Mojo->new('Kalamar');
+my $bad_json = tempfile();
+$bad_json->spew('{invalid json}');
+#$t3->app->log->level('error');
+$t3->app->log->handle($log_fh);
+$t3->app->log->level('error');
+$t3->app->plugin('Plugins' => {
+  default_plugins => $bad_json->to_string
+});
+like($log_output, qr/provided plugin file syntax invalid/, 'Malformed JSON logged');
+
 done_testing;