From 7037f88a2e5f3347d82840eefb79024e35ea7ab0 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 24 Dec 2024 11:15:21 +0000 Subject: [PATCH] Bug 38782: mock_userenv in API tests Reorder logic slightly for each to only create the counter_file after the mock_userenv has taken place prove t/db_dependent/api/v1/erm_custom_reports.t --- t/db_dependent/api/v1/erm_custom_reports.t | 116 +++++++++++---------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/t/db_dependent/api/v1/erm_custom_reports.t b/t/db_dependent/api/v1/erm_custom_reports.t index 1317c61761e..fcb7eba1b02 100755 --- a/t/db_dependent/api/v1/erm_custom_reports.t +++ b/t/db_dependent/api/v1/erm_custom_reports.t @@ -50,20 +50,6 @@ subtest "monthly_report" => sub { $schema->storage->txn_begin; - # Run a harvest to populate the database with data - my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); - my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; - - $usage_data_provider->counter_files( - [ - { - usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, - file_content => $counter_file, - filename => "Test_TR_J1", - } - ] - ); - my $librarian = $builder->build_object( { class => 'Koha::Patrons', @@ -73,6 +59,7 @@ subtest "monthly_report" => sub { my $password = 'thePassword123'; $librarian->set_password( { password => $password, skip_validation => 1 } ); my $userid = $librarian->userid; + t::lib::Mocks::mock_userenv( { number => $userid } ); my $patron = $builder->build_object( { @@ -84,6 +71,20 @@ subtest "monthly_report" => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; + # Run a harvest to populate the database with data + my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); + my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; + + $usage_data_provider->counter_files( + [ + { + usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, + file_content => $counter_file, + filename => "Test_TR_J1", + } + ] + ); + # Unauthorized access $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eUsage/monthly_report/title")->status_is(403); @@ -148,20 +149,6 @@ subtest "yearly_report" => sub { $schema->storage->txn_begin; - # Run a harvest to populate the database with data - my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); - my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; - - $usage_data_provider->counter_files( - [ - { - usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, - file_content => $counter_file, - filename => "Test_TR_J1", - } - ] - ); - my $librarian = $builder->build_object( { class => 'Koha::Patrons', @@ -171,6 +158,7 @@ subtest "yearly_report" => sub { my $password = 'thePassword123'; $librarian->set_password( { password => $password, skip_validation => 1 } ); my $userid = $librarian->userid; + t::lib::Mocks::mock_userenv( { number => $userid } ); my $patron = $builder->build_object( { @@ -182,6 +170,20 @@ subtest "yearly_report" => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; + # Run a harvest to populate the database with data + my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); + my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; + + $usage_data_provider->counter_files( + [ + { + usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, + file_content => $counter_file, + filename => "Test_TR_J1", + } + ] + ); + # Unauthorized access $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eUsage/yearly_report/title")->status_is(403); @@ -237,20 +239,6 @@ subtest "metric_types_report" => sub { $schema->storage->txn_begin; - # Run a harvest to populate the database with data - my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); - my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; - - $usage_data_provider->counter_files( - [ - { - usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, - file_content => $counter_file, - filename => "Test_TR_J1", - } - ] - ); - my $librarian = $builder->build_object( { class => 'Koha::Patrons', @@ -260,6 +248,7 @@ subtest "metric_types_report" => sub { my $password = 'thePassword123'; $librarian->set_password( { password => $password, skip_validation => 1 } ); my $userid = $librarian->userid; + t::lib::Mocks::mock_userenv( { number => $userid } ); my $patron = $builder->build_object( { @@ -271,6 +260,20 @@ subtest "metric_types_report" => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; + # Run a harvest to populate the database with data + my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); + my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; + + $usage_data_provider->counter_files( + [ + { + usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, + file_content => $counter_file, + filename => "Test_TR_J1", + } + ] + ); + # Unauthorized access $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eUsage/metric_types_report/title")->status_is(403); @@ -332,20 +335,6 @@ subtest "provider_rollup_report" => sub { $schema->storage->txn_begin; - # Run a harvest to populate the database with data - my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); - my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; - - $usage_data_provider->counter_files( - [ - { - usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, - file_content => $counter_file, - filename => "Test_TR_J1", - } - ] - ); - my $librarian = $builder->build_object( { class => 'Koha::Patrons', @@ -355,6 +344,7 @@ subtest "provider_rollup_report" => sub { my $password = 'thePassword123'; $librarian->set_password( { password => $password, skip_validation => 1 } ); my $userid = $librarian->userid; + t::lib::Mocks::mock_userenv( { number => $userid } ); my $patron = $builder->build_object( { @@ -366,6 +356,20 @@ subtest "provider_rollup_report" => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; + # Run a harvest to populate the database with data + my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); + my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; + + $usage_data_provider->counter_files( + [ + { + usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, + file_content => $counter_file, + filename => "Test_TR_J1", + } + ] + ); + # Unauthorized access $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title")->status_is(403); -- 2.39.5