Marketplace: List plugins

Change-Id: I3bd0d0f1db8224cbfe7efd255eb99d7cd54faf11
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index 3e5b78c..9deec0e 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -126,6 +126,11 @@
   ->text_is('p.no-results', 'Not authenticated')
   ;
 
+$t->get_ok('/settings/marketplace')
+  ->status_is(401)
+  ->text_is('p.no-results', 'Not authenticated')
+  ;
+
 # Test for bug with long password
 $t->post_ok('/user/login' => form => {
   handle_or_email => 'test',
@@ -491,6 +496,12 @@
   ->header_is('Pragma','no-cache')
   ;
 
+
+$t->get_ok('/settings/marketplace')
+  ->status_is(200)
+  ->text_is('html head title' => 'Marketplace')
+  ;
+
 $csrf = $t->post_ok('/settings/oauth/register' => form => {
   name => 'MyApp',
   type => 'PUBLIC',
@@ -843,6 +854,7 @@
 #  "client_redirect_uri" => $redirect_uri
 });
 
+
 $fake_backend_app->add_client({
   "client_id" => 'xyz2',
   "client_name" => 'New added client',
@@ -878,6 +890,21 @@
   ->tx->res->dom->at('input[name=csrf_token]')->attr('value')
   ;
 
+
+$fake_backend_app->add_plugin({
+"source" => {"key1" => 'wert1', "key2" => 'wert2'},
+"client_id" => "52abc",
+"permitted" => 'true',
+"client_id" => '52abc',
+"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"
+});
+
+
 $fwd = $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
   client_id => 'xyz',
   state => 'abcde',
@@ -923,6 +950,49 @@
   ->element_exists_not('blockquote.warning')
   ;
 
+$t->get_ok('/settings/marketplace')
+  ->status_is(200)
+  ->text_is('html head title' => 'Marketplace')
+  ->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',
+"client_id" => '53abc',
+"client_name" => 'Plugin 2',
+"client_type" => 'CONFIDENTIAL',
+"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')
+  ->element_exists('ul.plugin-list > li')
+  ->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')
+  ;
+
+
 $t->get_ok(Mojo::URL->new('/settings/oauth/authorize')->query({
   client_id => 'xyz',
   state => 'abcde',
diff --git a/t/server/mock.pl b/t/server/mock.pl
index e615306..af03832 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -57,6 +57,13 @@
   push @$list, $client;
 };
 
+# Add plugin to plugin list for marketplace
+helper 'add_plugin' => sub {
+  my $c = shift;
+  my $cplugin = shift;
+  my $pl_list = $c->app->defaults('oauth.plugin_list');
+  push @$pl_list, $cplugin;
+};
 
 # Load fixture responses
 helper 'load_response' => sub {
@@ -89,6 +96,7 @@
 };
 
 app->defaults('oauth.client_list' => []);
+app->defaults('oauth.plugin_list' => []);
 
 
 # Base page
@@ -552,9 +560,8 @@
   });
 };
 
-
-# Register a client
-post '/v1.0/oauth2/client/list' => sub {
+# List plugins
+post '/v1.0/plugins' => sub {
   my $c = shift;
 
   my $v = $c->validation;
@@ -568,6 +575,48 @@
       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 => $listref,
+      status => 200
+    );
+  }
+  else{
+  return $c->render(
+   json => $c->stash('oauth.plugin_list'),
+    status => 200
+  );
+  }
+
+};
+
+# Register a client
+post '/v1.0/oauth2/client/list' => 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
+    );
+  };
+
 
   # $c->param('client_secret');