Added support for OAuth2 client unregistration

Change-Id: Ib67c63ffd8398b733a2633ca3ac1865a1feb66ef
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index 8556319..cc52f9e 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -407,8 +407,13 @@
 $t->get_ok('/settings/oauth')
   ->text_is('form.form-table legend', 'Register new client application')
   ->attr_is('form.oauth-register','action', '/settings/oauth/register')
-  ->text_is('ul.client-list > li > span.client-name', 'R statistical computing tool')
-  ->text_is('ul.client-list > li > span.client-desc', 'R is a free software environment for statistical computing and graphics.')
+  ->element_exists('ul.client-list')
+  ->element_exists_not('ul.client-list > li')
+#  ->text_is('ul.client-list > li > span.client-name', 'R statistical computing tool ')
+#  ->text_is('ul.client-list > li > span.client-desc', 'R is a free software environment for statistical computing and graphics.')
+#  ->text_is('ul.client-list > li > span.client-url a', 'https://www.r-project.org/')
+#  ->text_is('ul.client-list > li a.client-unregister', 'Unregister')
+#  ->attr_is('ul.client-list > li a.client-unregister', 'href', '/settings/oauth/unregister/9aHsGW6QflV13ixNpez?name=R+statistical+computing+tool')
   ;
 
 $csrf = $t->post_ok('/settings/oauth/register' => form => {
@@ -435,6 +440,55 @@
   ->element_exists('input[name=client_secret][readonly][value]')
   ;
 
+$t->get_ok('/settings/oauth')
+  ->text_is('form.form-table legend', 'Register new client application')
+  ->attr_is('form.oauth-register','action', '/settings/oauth/register')
+  ->text_is('ul.client-list > li > span.client-name', 'MyApp')
+  ->text_is('ul.client-list > li > span.client-desc', 'This is my application')
+  ->text_is('ul.client-list > li > span.client-url a', '')
+  ->text_is('ul.client-list > li a.client-unregister', 'Unregister')
+  ->attr_is('ul.client-list > li a.client-unregister', 'href', '/settings/oauth/unregister/fCBbQkA2NDA3MzM1Yw==?name=MyApp')
+  ;
+
+$csrf = $t->get_ok('/settings/oauth/unregister/fCBbQkA2NDA3MzM1Yw==?name=MyApp')
+  ->content_like(qr!Do you really want to unregister \<span class="client-name"\>MyApp\<\/span\>?!)
+  ->attr_is('form.form-table input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==')
+  ->attr_is('form.form-table input[name=client-name]', 'value', 'MyApp')
+  ->tx->res->dom->at('input[name="csrf_token"]')
+  ->attr('value')
+  ;
+
+$t->post_ok('/settings/oauth/unregister' => form => {
+  'client-name' => 'MyApp',
+  'client-id' => 'xxxx==',
+  'csrf_token' => $csrf
+})->status_is(302)
+  ->content_is('')
+  ->header_is('Location' => '/settings/oauth')
+  ;
+
+$t->get_ok('/settings/oauth')
+  ->text_is('form.form-table legend', 'Register new client application')
+  ->attr_is('form.oauth-register','action', '/settings/oauth/register')
+  ->element_exists('ul.client-list > li')
+  ->text_is('div.notify', 'Unknown client with xxxx==.')
+  ;
+
+$t->post_ok('/settings/oauth/unregister' => form => {
+  'client-name' => 'MyApp',
+  'client-id' => 'fCBbQkA2NDA3MzM1Yw==',
+  'csrf_token' => $csrf
+})->status_is(302)
+  ->content_is('')
+  ->header_is('Location' => '/settings/oauth')
+  ;
+
+$t->get_ok('/settings/oauth')
+  ->text_is('form.form-table legend', 'Register new client application')
+  ->attr_is('form.oauth-register','action', '/settings/oauth/register')
+  ->element_exists_not('ul.client-list > li')
+  ->text_is('div.notify-success', 'Successfully deleted MyApp')
+  ;
+
 done_testing;
 __END__
-
diff --git a/t/server/mock.pl b/t/server/mock.pl
index 4073d8d..38a3300 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -95,6 +95,8 @@
   return $decode;
 };
 
+app->defaults('oauth.client_list' => []);
+
 
 # Base page
 get '/v1.0/' => sub {
@@ -495,11 +497,20 @@
   my $json = $c->req->json;
 
   my $name = $json->{name};
-  my $desc = $json->{desc};
+  my $desc = $json->{description};
   my $type = $json->{type};
   my $url  = $json->{url};
   my $redirect_url = $json->{redirectURI};
 
+  my $list = $c->app->defaults('oauth.client_list');
+
+  push @$list, {
+    "clientId" => $tokens{new_client_id},
+    "clientName" => $name,
+    "description" => $desc,
+    "url" => $url
+  };
+
   # Confidential server application
   if ($type eq 'CONFIDENTIAL') {
     return $c->render(json => {
@@ -520,21 +531,45 @@
   my $c = shift;
 
   # $c->param('client_secret');
+
+  # Is empty [] when nothing registered
+
   return $c->render(
-    json => [
-      {
-        "clientId" => "9aHsGW6QflV13ixNpez",
-        "clientName" => "R statistical computing tool",
-        "description" => "R is a free software environment for statistical computing and graphics.",
-        "url" => "https://www.r-project.org/"
+    json => $c->stash('oauth.client_list'),
+    status => 200
+  );
+};
+
+del '/v1.0/oauth2/client/deregister/:client_id' => sub {
+  my $c = shift;
+  my $client_id = $c->stash('client_id');
+
+  my $list = $c->app->defaults('oauth.client_list');
+
+  my $break = -1;
+  for (my $i = 0; $i < @$list; $i++) {
+    if ($list->[$i]->{clientId} eq $client_id) {
+      $break = $i;
+      last;
+    };
+  };
+
+  if ($break != -1) {
+    splice @$list, $break, 1;
+  }
+
+  else {
+    return $c->render(
+      json => {
+        error_description => "Unknown client with $client_id.",
+        error => "invalid_client"
       },
-      {
-        "clientId" => "8bIDtZnH6NvRkW2Fq",
-        "clientName" => "EasyPDF Exporter",
-        "description" => "EasyPDF is a tool for exporting data to PDF.",
-        "url" => "https://www.easypdf.org/"
-      }
-    ],
+      status => 401
+    );
+  };
+
+  return $c->render(
+    json => $c->stash('oauth.client_list'),
     status => 200
   );
 };