From: Dylan William Hardison Date: Tue, 20 Nov 2018 22:23:46 +0000 (-0500) Subject: Bug 1508201 - Some cleanups to the testing code X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9855d83da8e29296672a6b40053da992fcd57c42;p=thirdparty%2Fbugzilla.git Bug 1508201 - Some cleanups to the testing code --- diff --git a/Bugzilla/Test/MockDB.pm b/Bugzilla/Test/MockDB.pm index 84e4a3bf3..9acc945fb 100644 --- a/Bugzilla/Test/MockDB.pm +++ b/Bugzilla/Test/MockDB.pm @@ -11,110 +11,263 @@ use warnings; use Try::Tiny; use Capture::Tiny qw(capture_merged); -use Bugzilla::Test::MockLocalconfig ( - db_driver => 'sqlite', - db_name => $ENV{test_db_name} // ':memory:', -); +use Bugzilla::Test::MockLocalconfig (db_driver => 'sqlite', + db_name => $ENV{test_db_name} // ':memory:',); use Bugzilla; -BEGIN { Bugzilla->extensions }; -use Bugzilla::Test::MockParams ( - emailsuffix => '', - emailregexp => '.+', -); +BEGIN { Bugzilla->extensions } +use Bugzilla::Test::MockParams (emailsuffix => '', emailregexp => '.+',); +use Bugzilla::Test::Util qw(create_user); sub import { - require Bugzilla::Install; - require Bugzilla::Install::DB; - require Bugzilla::Field;; + require Bugzilla::Install; + require Bugzilla::Install::DB; + require Bugzilla::Field; - state $first_time = 0; + state $first_time = 0; - return undef if $first_time++; + return undef if $first_time++; - return capture_merged { - Bugzilla->dbh->bz_setup_database(); + return capture_merged { + Bugzilla->dbh->bz_setup_database(); - # Populate the tables that hold the values for the fields. + Bugzilla->dbh->bz_populate_enum_tables(); - Bugzilla::Install::DB::update_fielddefs_definition(); - Bugzilla::Field::populate_field_definitions(); - Bugzilla::Install::init_workflow(); - Bugzilla::Install::DB->update_table_definitions({}); - Bugzilla::Install::update_system_groups(); + Bugzilla::Install::DB::update_fielddefs_definition(); + Bugzilla::Field::populate_field_definitions(); + Bugzilla::Install::init_workflow(); + Bugzilla::Install::DB->update_table_definitions({}); + Bugzilla::Install::update_system_groups(); - Bugzilla->set_user(Bugzilla::User->super_user); + Bugzilla->set_user(Bugzilla::User->super_user); - Bugzilla::Install::update_settings(); + Bugzilla::Install::update_settings(); - my $dbh = Bugzilla->dbh; - if ( !$dbh->selectrow_array("SELECT 1 FROM priority WHERE value = 'P1'") ) { - $dbh->do("DELETE FROM priority"); - my $count = 100; - foreach my $priority (map { "P$_" } 1..5) { - $dbh->do( "INSERT INTO priority (value, sortkey) VALUES (?, ?)", undef, ( $priority, $count + 100 ) ); - } - } - my @flagtypes = ( - { - name => 'review', - desc => 'The patch has passed review by a module owner or peer.', - is_requestable => 1, - is_requesteeble => 1, - is_multiplicable => 1, - grant_group => '', - target_type => 'a', - cc_list => '', - inclusions => [''] - }, - { - name => 'feedback', - desc => 'A particular person\'s input is requested for a patch, ' . - 'but that input does not amount to an official review.', - is_requestable => 1, - is_requesteeble => 1, - is_multiplicable => 1, - grant_group => '', - target_type => 'a', - cc_list => '', - inclusions => [''] - } - ); + my $dbh = Bugzilla->dbh; + if (!$dbh->selectrow_array("SELECT 1 FROM priority WHERE value = 'P1'")) { + $dbh->do("DELETE FROM priority"); + my $count = 100; + foreach my $priority (map {"P$_"} 1 .. 5) { + $dbh->do("INSERT INTO priority (value, sortkey) VALUES (?, ?)", + undef, ($priority, $count + 100)); + } + } + my @flagtypes = ( + { + name => 'review', + desc => 'The patch has passed review by a module owner or peer.', + is_requestable => 1, + is_requesteeble => 1, + is_multiplicable => 1, + grant_group => '', + target_type => 'a', + cc_list => '', + inclusions => [''] + }, + { + name => 'feedback', + desc => 'A particular person\'s input is requested for a patch, ' + . 'but that input does not amount to an official review.', + is_requestable => 1, + is_requesteeble => 1, + is_multiplicable => 1, + grant_group => '', + target_type => 'a', + cc_list => '', + inclusions => [''] + } + ); - foreach my $flag (@flagtypes) { - next if Bugzilla::FlagType->new({ name => $flag->{name} }); - my $grant_group_id = $flag->{grant_group} - ? Bugzilla::Group->new({ name => $flag->{grant_group} })->id - : undef; - my $request_group_id = $flag->{request_group} - ? Bugzilla::Group->new({ name => $flag->{request_group} })->id - : undef; + foreach my $flag (@flagtypes) { + next if Bugzilla::FlagType->new({name => $flag->{name}}); + my $grant_group_id + = $flag->{grant_group} + ? Bugzilla::Group->new({name => $flag->{grant_group}})->id + : undef; + my $request_group_id + = $flag->{request_group} + ? Bugzilla::Group->new({name => $flag->{request_group}})->id + : undef; - $dbh->do('INSERT INTO flagtypes (name, description, cc_list, target_type, is_requestable, + $dbh->do( + 'INSERT INTO flagtypes (name, description, cc_list, target_type, is_requestable, is_requesteeble, is_multiplicable, grant_group_id, request_group_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', - undef, ($flag->{name}, $flag->{desc}, $flag->{cc_list}, $flag->{target_type}, - $flag->{is_requestable}, $flag->{is_requesteeble}, $flag->{is_multiplicable}, - $grant_group_id, $request_group_id)); - - my $type_id = $dbh->bz_last_key('flagtypes', 'id'); - - foreach my $inclusion (@{$flag->{inclusions}}) { - my ($product, $component) = split(':', $inclusion); - my ($prod_id, $comp_id); - if ($product) { - my $prod_obj = Bugzilla::Product->new({ name => $product }); - $prod_id = $prod_obj->id; - if ($component) { - $comp_id = Bugzilla::Component->new({ name => $component, product => $prod_obj})->id; - } - } - $dbh->do('INSERT INTO flaginclusions (type_id, product_id, component_id) - VALUES (?, ?, ?)', - undef, ($type_id, $prod_id, $comp_id)); - } + undef, + ( + $flag->{name}, $flag->{desc}, + $flag->{cc_list}, $flag->{target_type}, + $flag->{is_requestable}, $flag->{is_requesteeble}, + $flag->{is_multiplicable}, $grant_group_id, + $request_group_id + ) + ); + + my $type_id = $dbh->bz_last_key('flagtypes', 'id'); + + foreach my $inclusion (@{$flag->{inclusions}}) { + my ($product, $component) = split(':', $inclusion); + my ($prod_id, $comp_id); + if ($product) { + my $prod_obj = Bugzilla::Product->new({name => $product}); + $prod_id = $prod_obj->id; + if ($component) { + $comp_id + = Bugzilla::Component->new({name => $component, product => $prod_obj})->id; + } + } + $dbh->do( + 'INSERT INTO flaginclusions (type_id, product_id, component_id) + VALUES (?, ?, ?)', undef, ($type_id, $prod_id, $comp_id) + ); + } + } + + create_user('nobody@mozilla.org', '*'); + my @classifications = ( + { + name => "Client Software", + description => "End User Products developed by mozilla.org contributors" + }, + { + name => "Components", + description => "Standalone components that can be used by other products. " + . "Core, Directory, NSPR, NSS and Toolkit are used by Gecko " + . "(which is in turn used by Firefox, Thunderbird, SeaMonkey, " + . "Fennec, and others)", + }, + { + name => "Server Software", + description => "Web Server software developed by mozilla.org contributors " + . "to aid the development of mozilla.org products" + }, + { + name => "Other", + description => + "Everything else - websites, Labs, important things which aren't code" + }, + {name => "Graveyard", description => "Old, retired products"}, + ); + + for my $class (@classifications) { + my $new_class = Bugzilla::Classification->new({name => $class->{name}}); + if (!$new_class) { + $dbh->do('INSERT INTO classifications (name, description) VALUES (?, ?)', + undef, ($class->{name}, $class->{description})); + } + } + + my @products = ( + { + classification => 'Client Software', + product_name => 'Firefox', + description => 'For bugs in Firefox Desktop, the Mozilla Foundations ' + . 'web browser. For Firefox user interface issues in ' + . 'menus, developer tools, bookmarks, location bar, and ' + . 'preferences. Many Firefox bugs will either be filed ' + . 'here or in the Core product.' + . '(more info)', + versions => + ['34 Branch', '35 Branch', '36 Branch', '37 Branch', 'Trunk', 'unspecified'], + milestones => + ['Firefox 36', '---', 'Firefox 37', 'Firefox 38', 'Firefox 39', 'Future'], + defaultmilestone => '---', + components => [{ + name => 'General', + description => 'For bugs in Firefox which do not fit into ' + . 'other more specific Firefox components', + initialowner => 'nobody@mozilla.org', + initialqaowner => '', + initial_cc => [], + watch_user => 'general@firefox.bugs' + }], + }, + { + classification => 'Other', + product_name => 'bugzilla.mozilla.org', + description => 'For issues relating to the bugzilla.mozilla.org website, ' + . 'also known as BMO.', + versions => ['Development/Staging', 'Production'], + milestones => ['---'], + defaultmilestone => '---', + components => [{ + name => 'General', + description => + 'This is the component for issues specific to bugzilla.mozilla.org ' + . 'that do not belong in other components.', + initialowner => 'nobody@mozilla.org', + initialqaowner => '', + initial_cc => [], + watch_user => 'general@bugzilla.bugs' + }], + }, + ); + + my $default_op_sys_id + = $dbh->selectrow_array("SELECT id FROM op_sys WHERE value = 'Unspecified'"); + my $default_platform_id = $dbh->selectrow_array( + "SELECT id FROM rep_platform WHERE value = 'Unspecified'"); + + for my $product (@products) { + my $new_product = Bugzilla::Product->new({name => $product->{product_name}}); + if (!$new_product) { + my $class_id = 1; + if ($product->{classification}) { + $class_id + = Bugzilla::Classification->new({name => $product->{classification}})->id; } - }; + $dbh->do( + 'INSERT INTO products (name, description, classification_id, + default_op_sys_id, default_platform_id) + VALUES (?, ?, ?, ?, ?)', + undef, + ( + $product->{product_name}, $product->{description}, $class_id, + $default_op_sys_id, $default_platform_id + ) + ); + + $new_product = new Bugzilla::Product({name => $product->{product_name}}); + + $dbh->do('INSERT INTO milestones (product_id, value) VALUES (?, ?)', + undef, ($new_product->id, $product->{defaultmilestone})); + + # Now clear the internal list of accessible products. + delete Bugzilla->user->{selectable_products}; + + foreach my $component (@{$product->{components}}) { + if (!Bugzilla::User->new({name => $component->{watch_user}})) { + Bugzilla::User->create({ + login_name => $component->{watch_user}, cryptpassword => '*', + }); + } + Bugzilla->input_params({watch_user => $component->{watch_user}}); + Bugzilla::Component->create({ + name => $component->{name}, + product => $new_product, + description => $component->{description}, + initialowner => $component->{initialowner}, + initialqacontact => $component->{initialqacontact} || '', + initial_cc => $component->{initial_cc} || [], + }); + } + } + + foreach my $version (@{$product->{versions}}) { + if (!new Bugzilla::Version({name => $version, product => $new_product})) { + Bugzilla::Version->create({value => $version, product => $new_product}); + } + } + + foreach my $milestone (@{$product->{milestones}}) { + if (!new Bugzilla::Milestone({name => $milestone, product => $new_product})) { + $dbh->do('INSERT INTO milestones (product_id, value) VALUES (?,?)', + undef, $new_product->id, $milestone); + } + } + } + + }; } 1; diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index b8485f29e..cf0bcb6ee 100644 --- a/Bugzilla/Test/Util.pm +++ b/Bugzilla/Test/Util.pm @@ -16,22 +16,31 @@ our @EXPORT = qw(create_user create_oauth_client issue_api_key mock_useragent_tx); use Bugzilla::User; +use Bugzilla::Bug; use Bugzilla::User::APIKey; use Bugzilla::Util qw(generate_random_password); use Mojo::Message::Response; use Test2::Tools::Mock qw(mock); +use Type::Params -all; +use Types::Standard -types; + +our $Product = 'Firefox'; +our $Component = 'General'; +our $Version = 'unspecified'; +our $Priority = 'P1'; + sub create_user { - my ($login, $password, %extra) = @_; - require Bugzilla; - return Bugzilla::User->create({ - login_name => $login, - cryptpassword => $password, - disabledtext => "", - disable_mail => 0, - extern_id => undef, - %extra, - }); + my ($login, $password, %extra) = @_; + require Bugzilla; + return Bugzilla::User->create({ + login_name => $login, + cryptpassword => $password, + disabledtext => "", + disable_mail => 0, + extern_id => undef, + %extra, + }); } sub create_oauth_client { @@ -75,24 +84,93 @@ sub issue_api_key { return Bugzilla::User::APIKey->create_special($params); } else { return Bugzilla::User::APIKey->create($params); + +sub create_bug { + state $check = compile_named( + short_desc => Str, + product => Str, + { + default => sub {$Product} + }, + component => Str, + { + default => sub {$Component} + }, + bug_severity => Str, + {default => 'normal'}, + groups => ArrayRef [Str], + { + default => sub { [] } + }, + op_sys => Str, + {default => 'Unspecified'}, + rep_platform => Str, + {default => 'Unspecified'}, + version => Str, + { + default => sub {$Version} + }, + keywords => ArrayRef [Str], + { + default => sub { [] } + }, + cc => ArrayRef [Str], + { + default => sub { [] } + }, + comment => Str, + dependson => Str, + {default => ""}, + blocked => Str, + {default => ""}, + assigned_to => Str, + bug_mentors => ArrayRef [Str], + { + default => sub { [] } + }, + priority => Str, + { + default => sub {$Priority} } + ); + my ($option) = $check->(@_); + + return Bugzilla::Bug->create($option); +} + +sub issue_api_key { + my ($login, $given_api_key) = @_; + my $user = Bugzilla::User->check({name => $login}); + + my $params = { + user_id => $user->id, + description => 'Bugzilla::Test::Util::issue_api_key', + api_key => $given_api_key, + }; + + if ($given_api_key) { + return Bugzilla::User::APIKey->create_special($params); + } + else { + return Bugzilla::User::APIKey->create($params); + } } sub _json_content_type { $_->headers->content_type('application/json') } sub mock_useragent_tx { - my ($body, $modify) = @_; - $modify //= \&_json_content_type; - - my $res = Mojo::Message::Response->new; - $res->code(200); - $res->body($body); - if ($modify) { - local $_ = $res; - $modify->($res); - } + my ($body, $modify) = @_; + $modify //= \&_json_content_type; + + my $res = Mojo::Message::Response->new; + $res->code(200); + $res->body($body); + if ($modify) { + local $_ = $res; + $modify->($res); + } - return mock({result => $res}); + return mock({result => $res}); } 1; diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm index 780cd45d4..190aebe60 100644 --- a/Bugzilla/Util.pm +++ b/Bugzilla/Util.pm @@ -323,7 +323,7 @@ sub do_ssl_redirect_if_required { # Returns the real remote address of the client, sub remote_ip { - if ($ENV{SERVER_SOFTWARE} eq 'Bugzilla::Quantum::CGI') { + if ( ( $ENV{SERVER_SOFTWARE} // '') eq 'Bugzilla::Quantum::CGI') { my $c = $Bugzilla::Quantum::CGI::C or LOGDIE("Cannot find controller!"); state $better_xff = Bugzilla->has_feature('better_xff');