From 1faebf4689d69c1faf5db38b0b4076704ea7a134 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 23 Oct 2023 16:19:18 +0100 Subject: [PATCH] Bug 29523: (follow-up) Comprehensive tests for redaction Signed-off-by: Martin Renvoize --- t/db_dependent/Koha/Object.t | 66 ++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index ecf0bed621a..7921b3d4378 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -397,7 +397,7 @@ subtest "to_api() tests" => sub { 'Koha::Exceptions::Object::MethodNotCoveredByTests', 'Unknown method exception thrown if is_count not specified'; - subtest 'unprivileged request tests' => sub { + subtest 'public request tests' => sub { my @all_attrs = Koha::Libraries->columns(); my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } }; @@ -535,9 +535,14 @@ subtest "to_api() tests" => sub { $schema->storage->txn_rollback; }; - subtest 'accessible usage tests' => sub { + subtest 'unprivileged requests redaction tests' => sub { - plan tests => 2; + my @all_attrs = Koha::Patrons->columns(); + my $unredacted_attrs = { map { $_ => 1 } @{ Koha::Patron->unredact_list() } }; + my $mapping = Koha::Patron->to_api_mapping; + my @mapped_to_null = grep { !defined( $mapping->{$_} ) } keys %{$mapping}; + + plan tests => ( scalar @all_attrs * 3 ) - scalar @mapped_to_null; $schema->storage->txn_begin; @@ -554,20 +559,53 @@ subtest "to_api() tests" => sub { } ); - my $patron_1 = $builder->build_object( - { class => 'Koha::Patrons', value => { branchcode => $library_1->id } } - ); - my $patron_2 = $builder->build_object( - { class => 'Koha::Patrons', value => { branchcode => $library_2->id } } - ); + my $patron_1 = + $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_1->id } } ); + my $patron_2 = + $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_2->id } } ); t::lib::Mocks::mock_userenv( { patron => $patron } ); - is( - $patron_1->to_api( { user => $patron } )->{firstname}, $patron_1->firstname, - 'Returns unredacted object hash' - ); - is( $patron_2->to_api( { user => $patron } )->{firstname}, undef, 'Returns redacted object hash' ); + my $visible_user = $patron_1->to_api( { user => $patron } ); + my $redacted_user = $patron_2->to_api( { user => $patron } ); + + foreach my $attr (@all_attrs) { + my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr; + if ( defined($mapped) ) { + ok( + exists $visible_user->{$mapped}, + "Mapped attribute '$attr' is present" + ); + if ( exists $unredacted_attrs->{$attr} ) { + is( + $visible_user->{$mapped}, $patron_1->TO_JSON->{$attr}, + "Unredacted attribute '$attr' is visible when user is accessible" + ); + is( + $redacted_user->{$mapped}, $patron_2->TO_JSON->{$attr}, + "Unredacted attribute '$attr' is visible when user is inaccessible" + ); + } else { + is( + $visible_user->{$mapped}, $patron_1->TO_JSON->{$attr}, + "Redacted attribute '$attr' is visible when user is accessible" + ); + is( + $redacted_user->{$mapped}, undef, + "Redacted attribute '$attr' is undefined when user is inaccessible" + ); + } + } else { + ok( + !exists $visible_user->{$attr}, + "Mapped to null attribute '$attr' is not present when accessible" + ); + ok( + !exists $redacted_user->{$attr}, + "Mapped to null attribute '$attr' is not present when inaccessible" + ); + } + } $schema->storage->txn_rollback; }; -- 2.41.0