Skip to content

Commit

Permalink
refactor: add integration tests that check the HTML content of differ…
Browse files Browse the repository at this point in the history
…ent types of pages + fix issues (#10043)
  • Loading branch information
stephanegigandet authored and john-gom committed May 24, 2024
1 parent 22b419e commit d408694
Show file tree
Hide file tree
Showing 147 changed files with 68,719 additions and 6,644 deletions.
4 changes: 2 additions & 2 deletions cgi/display.pl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@

if (defined $request_ref->{error_message}) {
$log->debug("analyze_request error", {request_ref => $request_ref});
display_error($request_ref->{error_message}, $request_ref->{status_code});
display_error($request_ref, $request_ref->{error_message}, $request_ref->{status_code});
$log->debug("analyze_request error - return Apache2::Const::OK");
return Apache2::Const::OK;
}
Expand Down Expand Up @@ -139,7 +139,7 @@
)
{

display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

if ((defined $request_ref->{api}) and (defined $request_ref->{api_action})) {
Expand Down
2 changes: 1 addition & 1 deletion cgi/export_products.pl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
my $html = '';

if (not defined $Owner_id) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

# Require moderator status to launch the export / import process,
Expand Down
8 changes: 4 additions & 4 deletions cgi/import_file_process.pl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
my $template_data_ref;

if (not defined $Owner_id) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

my $import_files_ref = retrieve("$BASE_DIRS{IMPORT_FILES}/${Owner_id}/import_files.sto");
Expand All @@ -78,7 +78,7 @@
}
else {
$log->debug("File not found in import_files.sto", {file_id => $file_id}) if $log->is_debug();
display_error_and_exit("File not found.", 404);
display_error_and_exit($request_ref, "File not found.", 404);
}

$log->debug("File found in import_files.sto",
Expand All @@ -95,7 +95,7 @@
my $results_ref = load_csv_or_excel_file($file);

if ($results_ref->{error}) {
display_error_and_exit($results_ref->{error}, 200);
display_error_and_exit($request_ref, $results_ref->{error}, 200);
}

my $headers_ref = $results_ref->{headers};
Expand Down Expand Up @@ -152,7 +152,7 @@
if ($results_ref->{error}) {
$import_files_ref->{$file_id}{imports}{$import_id}{convert_error} = $results_ref->{error};
store("$BASE_DIRS{IMPORT_FILES}/${Owner_id}/import_files.sto", $import_files_ref);
display_error_and_exit($results_ref->{error}, 200);
display_error_and_exit($request_ref, $results_ref->{error}, 200);
}

my $args_ref = {
Expand Down
6 changes: 3 additions & 3 deletions cgi/import_file_select_format.pl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
my $template_data_ref = {};

if (not defined $Owner_id) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

my $import_files_ref = retrieve("$BASE_DIRS{IMPORT_FILES}/${Owner_id}/import_files.sto");
Expand All @@ -84,7 +84,7 @@
}
else {
$log->debug("File not found in import_files.sto", {file_id => $file_id}) if $log->is_debug();
display_error_and_exit("File not found.", 404);
display_error_and_exit($request_ref, "File not found.", 404);
}

$log->debug("File found in import_files.sto",
Expand All @@ -96,7 +96,7 @@
my $results_ref = load_csv_or_excel_file($file);

if ($results_ref->{error}) {
display_error_and_exit($results_ref->{error}, 200);
display_error_and_exit($request_ref, $results_ref->{error}, 200);
}

my $headers_ref = $results_ref->{headers};
Expand Down
5 changes: 3 additions & 2 deletions cgi/import_file_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
local $log->context->{action} = $action;

if (not defined $Owner_id) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

if ($action eq "process") {
Expand All @@ -81,7 +81,8 @@
$log->debug("processing upload form", {filename => $filename, file_id => $file_id, extension => $extension})
if $log->is_debug();

ensure_dir_created("$BASE_DIRS{IMPORT_FILES}/${Owner_id}") or display_error_and_exit("Missing path", 503);
ensure_dir_created("$BASE_DIRS{IMPORT_FILES}/${Owner_id}")
or display_error_and_exit($request_ref, "Missing path", 503);

open(my $out, ">", "$BASE_DIRS{IMPORT_FILES}/${Owner_id}/$file_id.$extension");
while (my $chunk = <$file>) {
Expand Down
2 changes: 1 addition & 1 deletion cgi/import_photos_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
local $log->context->{action} = $action;

if (not defined $Owner_id) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

else {
Expand Down
2 changes: 1 addition & 1 deletion cgi/import_products_categories_from_public_database.pl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
my $template_data_ref = {};

if (not defined $Owner_id) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

if ($action eq "display") {
Expand Down
8 changes: 4 additions & 4 deletions cgi/org.pl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
$template_data_ref->{org_does_not_exist} = 1;
}
else {
display_error_and_exit($Lang{error_org_does_not_exist}{$lc}, 404);
display_error_and_exit($request_ref, $Lang{error_org_does_not_exist}{$lc}, 404);
}
}

Expand All @@ -82,7 +82,7 @@
$log->debug("user does not have permission to edit org",
{orgid => $orgid, org_admins => $org_ref->{admins}, User_id => $User_id})
if $log->is_debug();
display_error_and_exit($Lang{error_no_permission}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}

my @errors = ();
Expand All @@ -95,7 +95,7 @@
$type = 'delete';
}
else {
display_error_and_exit($Lang{error_no_permission}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}
}
else {
Expand Down Expand Up @@ -369,7 +369,7 @@
$template_data_ref->{result} = lang("edit_org_result");
}
else {
display_error_and_exit($Lang{error_no_permission}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}

}
Expand Down
6 changes: 3 additions & 3 deletions cgi/product_image.pl
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@
$log->debug("start", {code => $code, id => $id}) if $log->is_debug();

if (not defined $code) {
display_error_and_exit(sprintf(lang("no_product_for_barcode"), $code), 404);
display_error_and_exit($request_ref, sprintf(lang("no_product_for_barcode"), $code), 404);
}

my $product_id = product_id_for_owner($Owner_id, $code);

my $product_ref = retrieve_product($product_id);

if (not(defined $product_ref)) {
display_error_and_exit(sprintf(lang("no_product_for_barcode"), $code), 404);
display_error_and_exit($request_ref, sprintf(lang("no_product_for_barcode"), $code), 404);
}

if ((not(defined $product_ref->{images})) or (not(defined $product_ref->{images}{$id}))) {
display_error_and_exit(sprintf(lang("no_product_for_barcode"), $code), 404);
display_error_and_exit($request_ref, sprintf(lang("no_product_for_barcode"), $code), 404);
}

my $imagetext;
Expand Down
4 changes: 2 additions & 2 deletions cgi/product_image_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
my $extension = lc($1);
$tmp_filename = get_string_id_for_lang("no_language", remote_addr() . '_' . $`);

ensure_dir_created($BASE_DIRS{CACHE_TMP}) or display_error_and_exit("Missing path", 503);
ensure_dir_created($BASE_DIRS{CACHE_TMP}) or display_error_and_exit($request_ref, "Missing path", 503);
open(my $out, ">", "$BASE_DIRS{CACHE_TMP}/$tmp_filename.$extension");
while (my $chunk = <$file>) {
print $out $chunk;
Expand Down Expand Up @@ -176,7 +176,7 @@
my $interface_version = '20120622';

# Check that the image directory exists
ensure_dir_created($BASE_DIRS{PRODUCTS_IMAGES}) or display_error_and_exit("Missing path", 503);
ensure_dir_created($BASE_DIRS{PRODUCTS_IMAGES}) or display_error_and_exit($request_ref, "Missing path", 503);

if ($imagefield) {

Expand Down
2 changes: 1 addition & 1 deletion cgi/product_jqm_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ =head1 DESCRIPTION
my @errors = ();

# Store parameters for debug purposes
ensure_dir_created($BASE_DIRS{CACHE_DEBUG}) or display_error_and_exit("Missing path", 503);
ensure_dir_created($BASE_DIRS{CACHE_DEBUG}) or display_error_and_exit($request_ref, "Missing path", 503);
open(my $out, ">", "$BASE_DIRS{CACHE_DEBUG}/product_jqm_multilingual." . time() . "." . $code);
print $out encode_json(Vars());
close $out;
Expand Down
21 changes: 11 additions & 10 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
use File::Copy qw(move);
use Data::Dumper;

my $request_ref = ProductOpener::Display::init_request();

# Function to display a form to add a product with a specific barcode (either typed in a field, or extracted from a barcode photo)
# or without a barcode

Expand All @@ -76,7 +78,7 @@ ()
if (($server_options{producers_platform})
and not((defined $Owner_id) and (($Owner_id =~ /^org-/) or ($User{moderator}) or $User{pro_moderator})))
{
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

my $html = '';
Expand Down Expand Up @@ -165,10 +167,9 @@ ($product_ref)
return;
}

my $request_ref = ProductOpener::Display::init_request();

if ($User_id eq 'unwanted-user-french') {
display_error_and_exit(
$request_ref,
"<b>Il y a des problèmes avec les modifications de produits que vous avez effectuées. Ce compte est temporairement bloqué, merci de nous contacter.</b>",
403
);
Expand Down Expand Up @@ -223,7 +224,7 @@ ($product_ref)
$code = process_search_image_form(\$filename);
}
elsif (not is_valid_code($code)) {
display_error_and_exit($Lang{invalid_barcode}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{invalid_barcode}{$lc}, 403);
}

my $r = Apache2::RequestUtil->request();
Expand Down Expand Up @@ -315,28 +316,28 @@ ($product_ref)
else {
# We should have a code
if ((not defined $code) or ($code eq '')) {
display_error_and_exit($Lang{missing_barcode}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{missing_barcode}{$lc}, 403);
}
elsif (not is_valid_code($code)) {
display_error_and_exit($Lang{invalid_barcode}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{invalid_barcode}{$lc}, 403);
}
else {
if ( ((defined $server_options{private_products}) and ($server_options{private_products}))
and (not defined $Owner_id))
{

display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}
$product_id = product_id_for_owner($Owner_id, $code);
$product_ref = retrieve_product_or_deleted_product($product_id, $User{moderator});
if (not defined $product_ref) {
display_error_and_exit(sprintf(lang("no_product_for_barcode"), $code), 404);
display_error_and_exit($request_ref, sprintf(lang("no_product_for_barcode"), $code), 404);
}
}
}

if (($type eq 'delete') and (not $User{moderator})) {
display_error_and_exit($Lang{error_no_permission}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}

if ($User_id eq 'unwanted-bot-id') {
Expand Down Expand Up @@ -382,7 +383,7 @@ ($product_ref)

if (not $proceed_with_edit) {

display_error_and_exit("Edit against edit rules", 403);
display_error_and_exit($request_ref, "Edit against edit rules", 403);
}

$log->debug("phase 1", {code => $code, type => $type}) if $log->is_debug();
Expand Down
6 changes: 3 additions & 3 deletions cgi/remove_products.pl
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
$template_data_ref->{action} = $action;

if (not $server_options{producers_platform}) {
display_error_and_exit(lang("function_not_available"), 200);
display_error_and_exit($request_ref, lang("function_not_available"), 200);
}

if ((not defined $Owner_id) or ($Owner_id !~ /^(user|org)-\S+$/)) {
display_error_and_exit(lang("no_owner_defined"), 200);
display_error_and_exit($request_ref, lang("no_owner_defined"), 200);
}

if ($action eq "display") {
Expand All @@ -82,7 +82,7 @@
File::Copy::Recursive->import(qw( dirmove ));

my $deleted_dir = $BASE_DIRS{DELETED_PRIVATE_PRODUCTS} . "/" . $Owner_id . "." . time();
ensure_dir_created($BASE_DIRS{PRODUCTS_IMAGES}) or display_error_and_exit("Missing path", 503);
ensure_dir_created($BASE_DIRS{PRODUCTS_IMAGES}) or display_error_and_exit($request_ref, "Missing path", 503);

$log->debug("Moving data to deleted dir", {owner => $Owner_id, deleted_dir => $deleted_dir}) if $log->is_debug();

Expand Down
6 changes: 3 additions & 3 deletions cgi/reset_password.pl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
my $html = '';

if (defined $User_id) {
display_error_and_exit($Lang{error_reset_already_connected}{$lc}, undef);
display_error_and_exit($request_ref, $Lang{error_reset_already_connected}{$lc}, undef);
}

if ($action eq 'process') {
Expand Down Expand Up @@ -95,7 +95,7 @@
}
else {
$log->debug("invalid address", {type => $type}) if $log->is_debug();
display_error_and_exit(lang("error_invalid_address"), 404);
display_error_and_exit($request_ref, lang("error_invalid_address"), 404);
}

if ($#errors >= 0) {
Expand Down Expand Up @@ -173,7 +173,7 @@
}
else {
$log->debug("token is invalid", {userid => $userid}) if $log->is_debug();
display_error_and_exit($Lang{error_reset_invalid_token}{$lc}, undef);
display_error_and_exit($request_ref, $Lang{error_reset_invalid_token}{$lc}, undef);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion cgi/search.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
use JSON::PP;
use Log::Any qw($log);

my $request_ref = ProductOpener::Display::init_request();

# Passing values to the template
my $template_data_ref = {};

Expand All @@ -58,12 +60,12 @@
if (user_agent() =~ /apps-spreadsheets/) {

display_error_and_exit(
$request_ref,
"Automated queries using Google Spreadsheet overload the Open Food Facts server. We cannot support them. You can contact us at contact\@openfoodfacts.org to tell us about your use case, so that we can see if there is another way to support it.",
200
);
}

my $request_ref = ProductOpener::Display::init_request();
$request_ref->{search} = 1;

my $action = single_param('action') || 'display';
Expand Down
8 changes: 4 additions & 4 deletions cgi/user.pl
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@
if ($type =~ /^edit/) {
$user_ref = retrieve_user($userid);
if (not defined $user_ref) {
display_error_and_exit($Lang{error_invalid_user}{$lc}, 404);
display_error_and_exit($request_ref, $Lang{error_invalid_user}{$lc}, 404);
}
}
else {
$type = 'add';
}

if (($type =~ /^edit/) and ($User_id ne $userid) and not $admin) {
display_error_and_exit($Lang{error_no_permission}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}

my $debug = 0;
Expand All @@ -117,11 +117,11 @@
ProductOpener::Users::check_edit_owner($user_ref, \@errors);
}
else {
display_error_and_exit($Lang{error_no_permission}{$lc}, 403);
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}
}
elsif ($type ne 'delete') {
ProductOpener::Users::check_user_form($type, $user_ref, \@errors);
ProductOpener::Users::check_user_form($request_ref, $type, $user_ref, \@errors);
}

if ($#errors >= 0) {
Expand Down
Loading

0 comments on commit d408694

Please sign in to comment.