Issue a new token for a public client

Change-Id: Id44501d46aff4fd540339c0b2901879ab8a77734
diff --git a/t/plugin/auth-oauth.t b/t/plugin/auth-oauth.t
index 22054d2..adb7378 100644
--- a/t/plugin/auth-oauth.t
+++ b/t/plugin/auth-oauth.t
@@ -522,5 +522,47 @@
   ->text_is('div.notify-success', 'Successfully deleted MyApp')
   ;
 
+$t->post_ok('/settings/oauth/register' => form => {
+  name => 'MyApp2',
+  type => 'PUBLIC',
+  desc => 'This is my application',
+  csrf_token => $csrf
+})->status_is(200)
+  ->element_exists('div.notify-success')
+  ->text_is('legend', 'Client Credentials')
+  ->text_is('label[for=client_id]', 'ID of the client application')
+  ->element_exists('input[name=client_id][readonly][value]')
+  ->element_exists_not('input[name=client_secret][readonly][value]')
+  ;
+
+$t->get_ok('/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==')
+  ->text_is('.client-name', 'MyApp2')
+  ->text_is('.client-desc', 'This is my application')
+  ->text_is('.client-issue-token', 'IssueToken')
+  ->attr_is('.client-issue-token', 'href', '/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2')
+  ;
+
+$csrf = $t->get_ok('/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==/token?name=MyApp2')
+  ->status_is(200)
+  ->attr_is('#issue-token','action', '/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==/token')
+  ->attr_is('input[name=client-id]', 'value', 'fCBbQkA2NDA3MzM1Yw==')
+  ->attr_is('input[name=name]', 'value', 'MyApp2')
+  ->tx->res->dom->at('input[name="csrf_token"]')
+  ->attr('value')
+  ;
+
+$t->post_ok('/settings/oauth/client/fCBbQkA2NDA3MzM1Yw==/token' => form => {
+  csrf_token => $csrf,
+  name => 'MyApp2',
+  'client-id' => 'fCBbQkA2NDA3MzM1Yw=='
+})
+  ->status_is(200)
+  ->attr_is('input[name=access_token]', 'value', 'jvgjbvjgzucgdwuiKHJK')
+  ->text_is('p[name=expires] tt', '31536000')
+  ->text_is('p[name=scope] tt', 'match_info search openid')
+  ->text_is('p[name=type] tt', 'Bearer')
+  ;
+
+
 done_testing;
 __END__
diff --git a/t/plugin/auth.t b/t/plugin/auth.t
index f5351de..bd9a3b1 100644
--- a/t/plugin/auth.t
+++ b/t/plugin/auth.t
@@ -196,8 +196,6 @@
   ->header_is('Location' => '/?q=Baum&ql=poliqarp');
 
 
-
-
 done_testing;
 __END__
 
diff --git a/t/server/mock.pl b/t/server/mock.pl
index 31bd494..21b938f 100644
--- a/t/server/mock.pl
+++ b/t/server/mock.pl
@@ -18,9 +18,11 @@
   'access_token'    => "4dcf8784ccfd26fac9bdb82778fe60e2",
   'refresh_token'   => "hlWci75xb8atDiq3924NUSvOdtAh7Nlf9z",
   'access_token_2'  => "abcde",
+  'access_token_3' => 'jvgjbvjgzucgdwuiKHJK',
   'refresh_token_2' => "fghijk",
   'new_client_id' => 'fCBbQkA2NDA3MzM1Yw==',
   'new_client_secret' => 'KUMaFxs6R1WGud4HM22w3HbmYKHMnNHIiLJ2ihaWtB4N5JxGzZgyqs5GTLutrORj',
+  'auth_token_1'    => 'mscajfdghnjdfshtkjcuynxahgz5il'
 );
 
 helper get_token => sub {
@@ -461,6 +463,21 @@
     );
   }
 
+  # Get auth_token_1
+  elsif ($grant_type eq 'authorization_code') {
+    if ($c->param('code') eq $tokens{auth_token_1}) {
+      return $c->render(
+        status => 200,
+        json => {
+          "access_token" => $tokens{access_token_3},
+          "expires_in" => 31536000,
+          "scope" => 'match_info search openid',
+          "token_type" => "Bearer"
+        }
+      );
+    };
+  }
+
   # Unknown token grant
   else {
     return $c->render(
@@ -580,6 +597,23 @@
   );
 };
 
+post '/v1.0/oauth2/authorize' => sub {
+  my $c = shift;
+  my $type = $c->param('response_type');
+  my $client_id = $c->param('client_id');
+  my $redirect_uri = $c->param('redirect_uri');
+
+  if ($type eq 'code') {
+
+    return $c->redirect_to(
+      Mojo::URL->new($redirect_uri)->query({
+        code => $tokens{auth_token_1},
+        scope => 'match_info search openid'
+      })
+      );
+  }
+};
+
 
 
 app->start;