Bug 30536: Unit tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 14 Apr 2022 15:07:49 +0000 (12:07 -0300)
committerFridolin Somers <fridolin.somers@biblibre.com>
Tue, 3 May 2022 21:19:51 +0000 (11:19 -1000)
This patch adds tests to make sure there's no behavior change regarding
error conditions. When requests include wrong x-koha-embed values, a 400
error should be returned, both in our original implementation or relying
on the Mojolicious::Plugin::OpenAPI features.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/query.t
=> SUCCESS: Tests pass
3. Apply the rest of the patches
4. Repeat 2
=> SUCCESS: Tests still pass
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
t/db_dependent/api/v1/query.t

index dd73f2095cd07d05c6f357fe129a1c9108a924b4..3b8be18f8a1fc68b2be0e4e044ef0e92e0ccc873 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 1;
+use Test::More tests => 2;
 use Test::Mojo;
 
 use t::lib::TestBuilder;
@@ -105,3 +105,38 @@ subtest 'q handling tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'x-koha-embed tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $librarian = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { flags => 1 }     # superlibrarian
+        }
+    );
+    my $password = 'thePassword123';
+    $librarian->set_password( { password => $password, skip_validation => 1 } );
+    my $userid = $librarian->userid;
+
+    my $patron_id = $builder->build_object( { class => 'Koha::Patrons' } )->id;
+
+    my $res = $t->get_ok(
+        "//$userid:$password@/api/v1/patrons?q={\"me.patron_id\":$patron_id}"
+          => { 'x-koha-embed' => 'extended_attributes' } )->status_is(200)
+      ->tx->res->json;
+
+    is( scalar @{$res}, 1, 'One patron returned' );
+
+    $res = $t->get_ok(
+        "//$userid:$password@/api/v1/patrons?q={\"me.patron_id\":$patron_id}" => {
+            'x-koha-embed' =>
+              'extended_attributes,custom_bad_embed,another_bad_embed'
+        }
+    )->status_is(400);
+
+    $schema->storage->txn_rollback;
+};