marketplace: Install plugins

Change-Id: I05f0d2b92c3da8aeaebd8c6db08ff8659feeeaa7
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index 2c661df..16b0f6e 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -497,12 +497,24 @@
   ->header_is('Pragma','no-cache')
   ;
 
+my $loglines = '';
+$t->app->log->on(
+  message => sub {
+    my ($log, $level, @lines) = @_;
+    if ($level eq 'warn') {
+      $loglines = join ',', @lines;
+    };
+  });
 
 $t->get_ok('/settings/marketplace')
   ->status_is(200)
   ->text_is('html head title' => 'Marketplace')
+  ->element_exists_not('ul.plugin_list')
+  ->element_exists_not('ul.plugin_in-list')
   ;
 
+is($loglines, '', 'Check log is fine');
+
 $csrf = $t->post_ok('/settings/oauth/register' => form => {
   name => 'MyApp',
   type => 'PUBLIC',
@@ -921,15 +933,14 @@
 
 $fake_backend_app->add_plugin({
 "source" => {"key1" => 'wert1', "key2" => 'wert2'},
-"client_id" => "52abc",
-"permitted" => 'true',
 "client_id" => '52abc',
+"permitted" => 'true',
 "client_name" => 'Plugin 1',
 "client_type" => 'CONFIDENTIAL',
-"client_description" =>"Description Plugin 1",
-"client_url" => "http://example.client.de",
-"registration_date" => "2022-05-31T14:30:09+02:00[Europe/Berlin]",
-"registered_by" => "system"
+"client_description" => 'Description Plugin 1',
+"client_url" => 'http://example.client.de',
+"registration_date" => '2022-05-31T14:30:09+02:00[Europe/Berlin]',
+"registered_by" => 'system'
 });
 
 
@@ -984,11 +995,12 @@
   ->element_exists('ul.plugin-list')
   ->element_exists('ul.plugin-list > li')
   ->element_exists('p.registration_date')
-  ->element_exists('p.registered_by')
   ->text_is('span.client-name','Plugin 1')
   ->text_is('p.plugin-desc','Description Plugin 1')
   ;
 
+
+
 $fake_backend_app->add_plugin({
 "source" => {"one" => '1', "two" => '2'},
 "permitted" => 'false',
@@ -998,15 +1010,6 @@
 "client_description" =>'Description Plugin 2'
 });
 
-$fake_backend_app->add_plugin({
-"source" => {"answer" => '42', "hello" => 'world'},
-"permitted" => 'true',
-"client_id" => '54abc',
-"client_name" => 'Plugin 3',
-"client_type" => 'CONFIDENTIAL',
-"client_description" =>'Description Plugin 3'
-});
-
 $t->get_ok('/settings/marketplace')
   ->status_is(200)
   ->element_exists('ul.plugin-list')
@@ -1014,12 +1017,47 @@
   ->text_is('span.client-name','Plugin 1')
   ->text_is('p.plugin-desc','Description Plugin 1')
   ->element_exists('ul.plugin-list > li + li')
-  ->text_isnt('ul.plugin-list > li + li >span.client-name','Plugin 2')
-  ->text_isnt('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 2')
-  ->text_is('ul.plugin-list > li + li >span.client-name','Plugin 3')
-  ->text_is('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 3')
+  ->text_is('ul.plugin-list > li + li >span.client-name','Plugin 2')
+  ->text_is('ul.plugin-list > li + li >p.plugin-desc','Description Plugin 2')
   ;
 
+$t->ua->max_redirects(0);
+
+$t->post_ok('/settings/marketplace', form => {'client-id' => '52abc'})
+  ->status_is(302)
+  ->header_is(location => '/settings/marketplace')
+  ;
+
+$t->ua->max_redirects(1);
+
+$t->post_ok('/settings/marketplace', form => {'client-id' => '52abc'})
+  ->status_is(200)
+  ->element_exists('ul.plugin-list')
+  ->element_exists('ul.plugin-list > li')
+  ->text_is('ul.plugin-list > li > span.client-name','Plugin 2')
+  ->text_is('ul.plugin-list > li > p.plugin-desc','Description Plugin 2')
+  ->element_exists_not('ul.plugin-list > li + li')
+  ->element_exists('ul.plugin_in-list')
+  ->element_exists('ul.plugin_in-list > li')
+  ->text_is('ul.plugin_in-list > li > span.client-name','Plugin 1')
+  ->text_is('ul.plugin_in-list > li > p.inst_date','Date of Installation: 2022-12-13T16:33:27.621+01:00[Europe/Berlin]')
+  ;
+
+$t->ua->max_redirects(0);
+
+ $t->post_ok('/settings/marketplace', form => {'client-id' => 'unsinn31'})
+  ->status_is(302)
+  ->header_is(location => '/settings/marketplace')
+  ;
+
+$t->ua->max_redirects(1);
+
+$t->post_ok('/settings/marketplace', form => {'client-id' => 'unsinn31'})
+  ->status_is(200)
+  ->text_is('div.notify-error', 'Plugin could not be installed')
+  ;
+
+$t->ua->max_redirects(0);
 
 $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
   client_id => 'xyz',
diff --git a/t/server/mock.pl b/t/server/mock.pl
index 7331c81..e76f8c5 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -65,6 +65,14 @@
   push @$pl_list, $cplugin;
 };
 
+helper 'add_instplugin' => sub {
+   my $c = shift;
+   my $cplugin = shift;
+   my $pl_list = $c->app->defaults('oauth.pluginin_list');
+   push @$pl_list, $cplugin; 
+};
+
+
 # Load fixture responses
 helper 'load_response' => sub {
   my $c = shift;
@@ -97,7 +105,7 @@
 
 app->defaults('oauth.client_list' => []);
 app->defaults('oauth.plugin_list' => []);
-
+app->defaults('oauth.pluginin_list' => []);
 
 # Base page
 get '/v1.0/' => sub {
@@ -560,12 +568,29 @@
   });
 };
 
-# List plugins
+# Mock API list plugins
 post '/v1.0/plugins' => sub {
   my $c = shift;
-
   my $v = $c->validation;
+  $v->required('super_client_id');
+  $v->required('super_client_secret');
+  if ($v->has_error) {
+    return $c->render(
+      json => [],
+      status => 400
+    );
+  };
 
+  return $c->render(
+   json => $c->stash('oauth.plugin_list'),
+   status => 200
+  );
+};
+
+# Mock API list installed plugins
+post '/v1.0/plugins/installed' => sub {
+  my $c = shift;
+  my $v = $c->validation;
   $v->required('super_client_id');
   $v->required('super_client_secret');
 
@@ -575,33 +600,56 @@
       status => 400
     );
   };
-  
-  my $p;
-  if($c->param("permitted_only")){
-    $p = $c->param("permitted_only");
-  }
-  else{
-    $p="false";
-  }
 
-  #Mocks the return only of permitted plugins
-  if($p eq "true"){
-    my @p_plugin_list = grep{$_->{permitted} eq "true"}  @{$c->stash('oauth.plugin_list')};
-    my $listref = \@p_plugin_list;
+  return $c->render(
+    json => $c->stash('oauth.pluginin_list'),
+    status => 200
+  );
+};
+
+
+# Mock API plugin installation
+post '/v1.0/plugins/install' => sub {
+  my $c = shift;
+  my $v = $c->validation;
+  $v->required('super_client_id');
+  $v->required('super_client_secret');
+  $v->required('client_id');
+  my $cl_id = $c->param('client_id');
+  if ($v->has_error) {
     return $c->render(
-      json => $listref,
+      json => [],
+      status => 400
+    );
+  };
+  
+  my $date = "2022-12-13T16:33:27.621+01:00[Europe/Berlin]";
+  my $pl_list =  $c->app->defaults('oauth.plugin_list');
+  my $cl_name = (grep{($_->{client_id} eq $cl_id)}@$pl_list)[0]->{client_name};
+  
+  if (length $cl_name){
+    
+    my %inst_plugin = (
+      "name" => $cl_name,
+      "client_id" => $cl_id,
+      "installed_date" => $date,
+      );
+
+    $c->add_instplugin(\%inst_plugin);
+ 
+    return $c->render(
+      json => %inst_plugin,
       status => 200
     );
   }
-  else{
-  return $c->render(
-   json => $c->stash('oauth.plugin_list'),
-    status => 200
-  );
-  }
 
+  return $c->render(
+    json => [],
+    status => 400
+  );
 };
 
+
 # Register a client
 post '/v1.0/oauth2/client/list' => sub {
   my $c = shift;