Bugzilla – Attachment 119388 Details for
Bug 23666
Add routes for extended patron attributes
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23666: Unit tests
Bug-23666-Unit-tests.patch (text/plain), 14.94 KB, created by
Kyle M Hall (khall)
on 2021-04-09 15:17:45 UTC
(
hide
)
Description:
Bug 23666: Unit tests
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2021-04-09 15:17:45 UTC
Size:
14.94 KB
patch
obsolete
>From 59d82f5eb3d2f585942560135c15999be63ce0ca Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Fri, 19 Mar 2021 08:01:31 -0300 >Subject: [PATCH] Bug 23666: Unit tests > >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> > >Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> >--- > .../api/v1/patrons_extended_attributes.t | 396 ++++++++++++++++++ > 1 file changed, 396 insertions(+) > create mode 100755 t/db_dependent/api/v1/patrons_extended_attributes.t > >diff --git a/t/db_dependent/api/v1/patrons_extended_attributes.t b/t/db_dependent/api/v1/patrons_extended_attributes.t >new file mode 100755 >index 0000000000..3ee17affa8 >--- /dev/null >+++ b/t/db_dependent/api/v1/patrons_extended_attributes.t >@@ -0,0 +1,396 @@ >+#!/usr/bin/env perl >+ >+# 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 Test::More tests => 4; >+use Test::Mojo; >+ >+use t::lib::TestBuilder; >+use t::lib::Mocks; >+ >+use Koha::Database; >+ >+my $schema = Koha::Database->new->schema; >+my $builder = t::lib::TestBuilder->new(); >+ >+my $t = Test::Mojo->new('Koha::REST::V1'); >+ >+t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); >+ >+subtest 'list_patron_attributes() tests' => sub { >+ >+ plan tests => 9; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object({ >+ class => 'Koha::Patrons', >+ value => { flags => 2 ** 2 } # 'catalogue' flag == 2 >+ }); >+ my $password = 'thePassword123'; >+ $patron->set_password({ password => $password, skip_validation => 1 }); >+ my $userid = $patron->userid; >+ >+ $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/extended_attributes') >+ ->status_is( 200, 'SWAGGER3.2.2' ) >+ ->json_is( [] ); >+ >+ # Let's add 3 attributes >+ foreach my $i ( 1..5 ) { >+ $builder->build_object({ class => 'Koha::Patron::Attributes', value => { borrowernumber => $patron->id } }); >+ } >+ >+ $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/extended_attributes') >+ ->status_is( 200, 'SWAGGER3.2.2' ) >+ ->json_is( '' => $patron->extended_attributes->to_api, 'Extended attributes retrieved correctly' ); >+ >+ my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ my $non_existent_patron_id = $non_existent_patron->id; >+ # get rid of the patron >+ $non_existent_patron->delete; >+ >+ $t->get_ok("//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/extended_attributes') >+ ->status_is( 404 ) >+ ->json_is( '/error' => 'Patron not found' ); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'add() tests' => sub { >+ >+ plan tests => 9; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object({ >+ class => 'Koha::Patrons', >+ value => { flags => 2 ** 4 } # 'borrowers' flag == 4 >+ }); >+ my $password = 'thePassword123'; >+ $patron->set_password({ password => $password, skip_validation => 1 }); >+ my $userid = $patron->userid; >+ >+ my $mandatory_attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 1, >+ repeatable => 0, >+ unique_id => 0, >+ category_code => undef >+ } >+ } >+ ); >+ my $repeatable_attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 0, >+ repeatable => 1, >+ unique_id => 0, >+ category_code => undef >+ } >+ } >+ ); >+ my $unique_attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 0, >+ repeatable => 0, >+ unique_id => 1, >+ category_code => undef >+ } >+ } >+ ); >+ >+ my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ my $non_existent_patron_id = $non_existent_patron->id; >+ # get rid of the patron >+ $non_existent_patron->delete; >+ >+ $t->post_ok( "//$userid:$password@/api/v1/patrons/" >+ . $non_existent_patron_id >+ . '/extended_attributes' => json => >+ { type => $repeatable_attr_type->code, value => 'something' } ) >+ ->status_is(404)->json_is( '/error' => 'Patron not found' ); >+ >+ my $response = $t->post_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ { type => $repeatable_attr_type->code, value => 'something' } ) >+ ->status_is(201)->tx->res->json; >+ >+ is_deeply( >+ Koha::Patron::Attributes->find( $response->{extended_attribute_id} )->to_api, >+ $response, >+ "The returned object is on the DB" >+ ); >+ >+ subtest 'Repeatability tests' => sub { >+ $t->post_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ { type => $repeatable_attr_type->code, value => 'something' } ) >+ ->status_is(201, 'Repeatable attributes go through'); >+ >+ # Let's add a non-repeatable one >+ $patron->add_extended_attribute( >+ { code => $unique_attr_type->code, attribute => 'non_repeatable_1' } ); >+ $t->post_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ { type => $unique_attr_type->code, value => 'non_repeatable_2' } ) >+ ->status_is(409) >+ ->json_is( '/error' => >+ 'Tried to add more than one non-repeatable attributes. type=' >+ . $unique_attr_type->code >+ . ' value=non_repeatable_2' ); >+ }; >+ >+ subtest 'Attribute uniqueness tests' => sub { >+ >+ plan tests => 3; >+ >+ my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } ); >+ $t->post_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron_2->id >+ . '/extended_attributes' => json => >+ { type => $unique_attr_type->code, value => 'non_repeatable_1' } ) >+ ->status_is(409) >+ ->json_is( '/error' => >+ 'Your action breaks a unique constraint on the attribute. type=' >+ . $unique_attr_type->code >+ . ' value=non_repeatable_1' ); >+ }; >+ >+ subtest 'Invalid type tests' => sub { >+ >+ plan tests => 3; >+ >+ my $invalid_type_obj = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' }); >+ my $invalid_type = $invalid_type_obj->code; >+ $invalid_type_obj->delete; >+ >+ $t->post_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ { type => $invalid_type, value => 'blah' } ) >+ ->status_is(400) >+ ->json_is( '/error' => "Tried to use an invalid attribute type. type=$invalid_type" ); >+ }; >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'overwrite() tests' => sub { >+ >+ plan tests => 29; >+ >+ $schema->storage->txn_begin; >+ >+ Koha::Patron::Attribute::Types->delete; >+ >+ my $patron = $builder->build_object({ >+ class => 'Koha::Patrons', >+ value => { flags => 2 ** 4 + 2 ** 2 } # 'borrowers' flag == 4 >+ }); # 'catalogue' flag == 2 >+ my $password = 'thePassword123'; >+ $patron->set_password({ password => $password, skip_validation => 1 }); >+ my $userid = $patron->userid; >+ >+ my $mandatory_attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 1, >+ repeatable => 0, >+ unique_id => 0, >+ category_code => undef >+ } >+ } >+ ); >+ my $repeatable_attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 0, >+ repeatable => 1, >+ unique_id => 0, >+ category_code => undef >+ } >+ } >+ ); >+ my $unique_attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 0, >+ repeatable => 0, >+ unique_id => 1, >+ category_code => undef >+ } >+ } >+ ); >+ my $invalid_type_obj = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types' >+ } >+ ); >+ my $invalid_type = $invalid_type_obj->code; >+ $invalid_type_obj->delete; >+ >+ my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ my $non_existent_patron_id = $non_existent_patron->id; >+ # get rid of the patron >+ $non_existent_patron->delete; >+ >+ $t->put_ok( "//$userid:$password@/api/v1/patrons/" >+ . $non_existent_patron_id >+ . '/extended_attributes' => json => >+ [ { type => $repeatable_attr_type->code, value => 'something' } ] ) >+ ->status_is(404) >+ ->json_is( '/error' => 'Patron not found' ); >+ >+ $t->put_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ [ { type => $invalid_type, value => 'something' } ] ) >+ ->status_is(400) >+ ->json_is( '/error' => "Tried to use an invalid attribute type. type=$invalid_type" ); >+ >+ my $unique_value = 'The only one!'; >+ my $dummy_patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ $dummy_patron->add_extended_attribute({ code => $unique_attr_type->code, attribute => $unique_value }); >+ >+ $t->put_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ [ { type => $unique_attr_type->code, value => $unique_value } ] ) >+ ->status_is(409) >+ ->json_is( '/error' => "Your action breaks a unique constraint on the attribute. type=" . $unique_attr_type->code . " value=$unique_value" ); >+ >+ my $value_1 = 'value_1'; >+ my $value_2 = 'value_2'; >+ >+ $t->put_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ [ { type => $unique_attr_type->code, value => $value_1 }, >+ { type => $unique_attr_type->code, value => $value_2 } ] ) >+ ->status_is(409) >+ ->json_is( '/error' => "Tried to add more than one non-repeatable attributes. type=" . $unique_attr_type->code . " value=$value_2" ); >+ >+ $t->put_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => >+ [ { type => $unique_attr_type->code, value => $value_1 } ] ) >+ ->status_is(400) >+ ->json_is( '/error' => 'Missing mandatory attribute of type "' . $mandatory_attr_type->code . '".' ); >+ >+ $patron->add_extended_attribute({ code => $repeatable_attr_type->code, attribute => 'repeatable_1' }); >+ $patron->add_extended_attribute({ code => $repeatable_attr_type->code, attribute => 'repeatable_2' }); >+ $patron->add_extended_attribute({ code => $mandatory_attr_type->code, attribute => 'mandatory' }); >+ $patron->add_extended_attribute({ code => $unique_attr_type->code, attribute => 'unique' }); >+ >+ $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/extended_attributes') >+ ->status_is( 200, 'SWAGGER3.2.2' ) >+ ->json_is( '' => $patron->extended_attributes->to_api, 'Extended attributes retrieved correctly' ); >+ >+ my $updated_attributes = [ >+ { >+ type => $repeatable_attr_type->code, >+ value => 'updated_repeatable_1' >+ }, >+ { >+ type => $repeatable_attr_type->code, >+ value => 'updated_repeatable_2' >+ }, >+ { >+ type => $repeatable_attr_type->code, >+ value => 'updated_repeatable_3' >+ }, >+ { >+ type => $mandatory_attr_type->code, >+ value => 'updated_mandatory' >+ } >+ ]; >+ >+ $t->put_ok( "//$userid:$password@/api/v1/patrons/" >+ . $patron->id >+ . '/extended_attributes' => json => $updated_attributes ) >+ ->status_is(200) >+ ->json_is( '/0/type' => $updated_attributes->[0]->{type} ) >+ ->json_is( '/0/value' => $updated_attributes->[0]->{value} ) >+ ->json_is( '/1/type' => $updated_attributes->[1]->{type} ) >+ ->json_is( '/1/value' => $updated_attributes->[1]->{value} ) >+ ->json_is( '/2/type' => $updated_attributes->[2]->{type} ) >+ ->json_is( '/2/value' => $updated_attributes->[2]->{value} ) >+ ->json_is( '/3/type' => $updated_attributes->[3]->{type} ) >+ ->json_is( '/3/value' => $updated_attributes->[3]->{value} ) >+ ->json_hasnt( '/4'); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'delete() tests' => sub { >+ >+ plan tests => 9; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object({ >+ class => 'Koha::Patrons', >+ value => { flags => 2 ** 4 } # 'borrowers' flag == 4 >+ }); >+ my $password = 'thePassword123'; >+ $patron->set_password({ password => $password, skip_validation => 1 }); >+ my $userid = $patron->userid; >+ >+ my $attr_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { >+ mandatory => 0, >+ repeatable => 1, >+ unique_id => 0, >+ category_code => undef >+ } >+ } >+ ); >+ >+ my $dummy_patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ >+ my $attr = $dummy_patron->add_extended_attribute({ code => $attr_type->code, attribute => 'blah' }); >+ >+ $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $dummy_patron->id . '/extended_attributes/' . $attr->id ) >+ ->status_is(204, 'SWAGGER3.2.4') >+ ->content_is('', 'SWAGGER3.3.4'); >+ >+ $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $dummy_patron->id . '/extended_attributes/' . $attr->id ) >+ ->status_is(404) >+ ->json_is( '/error' => 'Attribute not found' ); >+ >+ $dummy_patron->delete; >+ >+ $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $dummy_patron->id . '/extended_attributes/' . $attr->id ) >+ ->status_is(404) >+ ->json_is( '/error' => 'Patron not found' ); >+ >+ $schema->storage->txn_rollback; >+}; >-- >2.24.3 (Apple Git-128)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 23666
:
118508
|
118509
|
118510
|
118550
|
118699
|
118700
|
118701
|
118702
|
118818
|
118854
|
119042
| 119388 |
119389
|
119390
|
119391
|
119392
|
119393