Bug 25411: Regression tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 14 May 2020 18:15:52 +0000 (15:15 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 22 May 2020 08:03:51 +0000 (09:03 +0100)
This patch adds tests the new RESTPublicAnonymousRequests syspref
doesn't apply to routes added by plugins.

It is up to the plugin developer to handle those conditions.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/REST/Plugin/PluginRoutes.t
=> FAIL: Notice the tests fail

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

t/db_dependent/Koha/REST/Plugin/PluginRoutes.t
t/lib/Koha/Plugin/Test.pm
t/lib/Koha/Plugin/Test/Controller.pm [new file with mode: 0644]

index 75a2f66..27af737 100644 (file)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 3;
+use Test::More tests => 4;
 use Test::Mojo;
 use Test::Warn;
 
@@ -69,8 +69,8 @@ subtest 'Bad plugins tests' => sub {
     my $routes = get_defined_routes($t);
     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
     # TODO: remove () if minimum version is bumped to at least 1.28.
-    ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'} && !exists $routes->{'/contrib/badass/patrons/<:patron_id>/bother_wrong'}, 'Route doesn\'t exist' );
-    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, 'Route exists' );
+    ok( !exists $routes->{'/contrib/badass/patrons/bother_wrong'}, 'Route doesn\'t exist' );
+    ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' );
 
     $schema->storage->txn_rollback;
 };
@@ -102,7 +102,7 @@ subtest 'Disabled plugins tests' => sub {
     my $routes = get_defined_routes($t);
     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
     # TODO: remove () if minimum version is bumped to at least 1.28.
-    ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
+    ok( !exists $routes->{'/contrib/testplugin/patrons/bother'},
         'Plugin disabled, route not defined' );
 
     $good_plugin->enable;
@@ -112,12 +112,57 @@ subtest 'Disabled plugins tests' => sub {
 
     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
     # TODO: remove () if minimum version is bumped to at least 1.28.
-    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
+    ok( exists $routes->{'/contrib/testplugin/patrons/bother'},
         'Plugin enabled, route defined' );
 
     $schema->storage->txn_rollback;
 };
 
+subtest 'Anonymous access routes plugins tests' => sub {
+
+    plan tests => 9;
+
+    $schema->storage->txn_begin;
+
+    # enable plugins
+    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
+
+    # remove any existing plugins that might interfere
+    Koha::Plugins::Methods->search->delete;
+    my $plugins = Koha::Plugins->new;
+    $plugins->InstallPlugins;
+
+    my @plugins = $plugins->GetPlugins( { all => 1 } );
+    foreach my $plugin (@plugins) {
+        $plugin->enable;
+    }
+
+    # initialize Koha::REST::V1 after mocking
+    my $t;
+    warning_is
+        { $t = Test::Mojo->new('Koha::REST::V1'); }
+        'The resulting spec is invalid. Skipping Bad API Route Plugin',
+        'Bad plugins raise warning';
+
+    my $routes = get_defined_routes($t);
+    ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' );
+    ok( exists $routes->{'/contrib/testplugin/public/patrons/bother'}, 'Route exists' );
+
+    C4::Context->set_preference( 'RESTPublicAnonymousRequests', 0 );
+
+   $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother')
+     ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests')
+     ->json_is( { bothered => Mojo::JSON->true } );
+
+    C4::Context->set_preference( 'RESTPublicAnonymousRequests', 1 );
+
+    $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother')
+      ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests')
+      ->json_is( { bothered => Mojo::JSON->true } );
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'needs_install use case tests' => sub {
 
     plan tests => 2;
@@ -142,8 +187,7 @@ subtest 'needs_install use case tests' => sub {
     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
     # TODO: remove () if minimum version is bumped to at least 1.28.
     ok(
-        !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}
-          && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
+        !exists $routes->{'/contrib/testplugin/patrons/bother'},
         'Plugin enabled, route not defined as C4::Context->needs_install is true'
     );
 
@@ -159,8 +203,7 @@ subtest 'needs_install use case tests' => sub {
     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
     # TODO: remove () if minimum version is bumped to at least 1.28.
     ok(
-        exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}
-          || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
+        exists $routes->{'/contrib/testplugin/patrons/bother'},
         'Plugin enabled, route defined as C4::Context->needs_install is false'
     );
 
index b7b85d6..8036c1f 100644 (file)
@@ -160,18 +160,11 @@ sub api_routes {
 
     my $spec = qq{
 {
-  "/patrons/{patron_id}/bother": {
-    "put": {
-      "x-mojo-to": "Koha::Plugin::Test#bother",
+  "/patrons/bother": {
+    "get": {
+      "x-mojo-to": "Test::Controller#bother",
       "operationId": "BotherPatron",
       "tags": ["patrons"],
-      "parameters": [{
-        "name": "patron_id",
-        "in": "path",
-        "description": "Internal patron identifier",
-        "required": true,
-        "type": "integer"
-      }],
       "produces": [
         "application/json"
       ],
@@ -188,16 +181,16 @@ sub api_routes {
                 }
           }
         },
-        "404": {
+        "401": {
           "description": "An error occurred",
           "schema": {
               "type": "object",
-                "properties": {
-                  "error": {
-                    "description": "An explanation for the error",
-                    "type": "string"
-                  }
+              "properties": {
+                "error": {
+                  "description": "An explanation for the error",
+                  "type": "string"
                 }
+              }
           }
         }
       },
@@ -207,6 +200,42 @@ sub api_routes {
         }
       }
     }
+  },
+  "/public/patrons/bother": {
+    "get": {
+      "x-mojo-to": "Test::Controller#bother",
+      "operationId": "PubliclyBotherPatron",
+      "tags": ["patrons"],
+      "produces": [
+        "application/json"
+      ],
+      "responses": {
+        "200": {
+          "description": "A bothered patron",
+          "schema": {
+              "type": "object",
+              "properties": {
+                "bothered": {
+                  "description": "If the patron has been bothered",
+                  "type": "boolean"
+                }
+              }
+          }
+        },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "type": "object",
+            "properties": {
+              "error": {
+                "description": "An explanation for the error",
+                "type": "string"
+              }
+            }
+          }
+        }
+      }
+    }
   }
 }
     };
diff --git a/t/lib/Koha/Plugin/Test/Controller.pm b/t/lib/Koha/Plugin/Test/Controller.pm
new file mode 100644 (file)
index 0000000..0b90e48
--- /dev/null
@@ -0,0 +1,38 @@
+package Koha::Plugin::Test::Controller;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Mojo::Base 'Mojolicious::Controller';
+
+=head1 API
+
+=head2 Methods
+
+=head3 bother
+
+=cut
+
+sub bother {
+    my ($c) = @_;
+    return $c->render(
+        status  => 200,
+        openapi => { bothered => Mojo::JSON->true }
+    );
+}
+
+1;