View | Details | Raw Unified | Return to bug 29523
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Object.t (-15 / +54 lines)
Lines 397-403 subtest "to_api() tests" => sub { Link Here
397
    'Koha::Exceptions::Object::MethodNotCoveredByTests',
397
    'Koha::Exceptions::Object::MethodNotCoveredByTests',
398
        'Unknown method exception thrown if is_count not specified';
398
        'Unknown method exception thrown if is_count not specified';
399
399
400
    subtest 'unprivileged request tests' => sub {
400
    subtest 'public request tests' => sub {
401
401
402
        my @all_attrs = Koha::Libraries->columns();
402
        my @all_attrs = Koha::Libraries->columns();
403
        my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
403
        my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
Lines 535-544 subtest "to_api() tests" => sub { Link Here
535
        $schema->storage->txn_rollback;
535
        $schema->storage->txn_rollback;
536
    };
536
    };
537
537
538
    subtest 'accessible usage tests' => sub {
538
    subtest 'unprivileged requests redaction tests' => sub {
539
539
540
        plan tests => 2;
540
        my @all_attrs = Koha::Patrons->columns();
541
        my $unredacted_attrs = { map { $_ => 1 } @{ Koha::Patron->unredact_list() } };
542
        my $mapping = Koha::Patron->to_api_mapping;
543
        my @mapped_to_null = grep { !defined($mapping->{$_}) } keys %{$mapping};
541
544
545
        plan tests => ( scalar @all_attrs * 3 ) - scalar @mapped_to_null; 
546
            
542
        $schema->storage->txn_begin;
547
        $schema->storage->txn_begin;
543
548
544
        my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
549
        my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
Lines 554-573 subtest "to_api() tests" => sub { Link Here
554
            }
559
            }
555
        );
560
        );
556
561
557
        my $patron_1 = $builder->build_object(
562
        my $patron_1 =
558
            { class => 'Koha::Patrons', value => { branchcode => $library_1->id } }
563
            $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_1->id } } );
559
        );
564
        my $patron_2 =
560
        my $patron_2 = $builder->build_object(
565
            $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_2->id } } );
561
            { class => 'Koha::Patrons', value => { branchcode => $library_2->id } }
562
        );
563
566
564
        t::lib::Mocks::mock_userenv( { patron => $patron } );
567
        t::lib::Mocks::mock_userenv( { patron => $patron } );
565
568
566
        is(
569
        my $visible_user  = $patron_1->to_api( { user => $patron } );
567
            $patron_1->to_api( { user => $patron } )->{firstname}, $patron_1->firstname,
570
        my $redacted_user = $patron_2->to_api( { user => $patron } );
568
            'Returns unredacted object hash'
571
569
        );
572
        foreach my $attr (@all_attrs) {
570
        is( $patron_2->to_api( { user => $patron } )->{firstname}, undef, 'Returns redacted object hash' );
573
            my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr;
574
            if ( defined($mapped) ) {
575
                ok(
576
                    exists $visible_user->{$mapped},
577
                    "Mapped attribute '$attr' is present"
578
                );
579
                if ( exists $unredacted_attrs->{$attr} ) {
580
                    is(
581
                        $visible_user->{$mapped}, $patron_1->TO_JSON->{$attr},
582
                        "Unredacted attribute '$attr' is visible when user is accessible"
583
                    );
584
                    is(
585
                        $redacted_user->{$mapped}, $patron_2->TO_JSON->{$attr},
586
                        "Unredacted attribute '$attr' is visible when user is inaccessible"
587
                    );
588
                }
589
                else {
590
                    is(
591
                        $visible_user->{$mapped}, $patron_1->TO_JSON->{$attr},
592
                        "Redacted attribute '$attr' is visible when user is accessible"
593
                    );
594
                    is(
595
                        $redacted_user->{$mapped}, undef,
596
                        "Redacted attribute '$attr' is undefined when user is inaccessible"
597
                    );
598
                }
599
            }
600
            else {
601
                ok(
602
                    !exists $visible_user->{$attr},
603
                    "Mapped to null attribute '$attr' is not present when accessible"
604
                );
605
                ok(
606
                    !exists $redacted_user->{$attr},
607
                    "Mapped to null attribute '$attr' is not present when inaccessible"
608
                );
609
            }
610
        }
571
611
572
        $schema->storage->txn_rollback;
612
        $schema->storage->txn_rollback;
573
    };
613
    };
574
- 

Return to bug 29523