|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use Setup; |
| 4 | +use Test2::V0; |
| 5 | +use Catalyst::Test (); |
| 6 | +use HTTP::Request::Common; |
| 7 | + |
| 8 | +my %ctx = test_init(); |
| 9 | + |
| 10 | +Catalyst::Test->import('Hydra'); |
| 11 | + |
| 12 | +my $db = Hydra::Model::DB->new; |
| 13 | +hydra_setup($db); |
| 14 | + |
| 15 | +my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); |
| 16 | + |
| 17 | +# Create a simple Nix expression that uses the existing build-product-simple.sh |
| 18 | +my $jobsdir = $ctx{jobsdir}; |
| 19 | +my $nixfile = "$jobsdir/simple.nix"; |
| 20 | +open(my $fh, '>', $nixfile) or die "Cannot create simple.nix: $!"; |
| 21 | +print $fh <<"EOF"; |
| 22 | +with import ./config.nix; |
| 23 | +{ |
| 24 | + simple = mkDerivation { |
| 25 | + name = "build-product-simple"; |
| 26 | + builder = ./build-product-simple.sh; |
| 27 | + }; |
| 28 | +} |
| 29 | +EOF |
| 30 | +close($fh); |
| 31 | + |
| 32 | +# Create a jobset that uses the simple build |
| 33 | +my $jobset = createBaseJobset("simple", "simple.nix", $ctx{jobsdir}); |
| 34 | + |
| 35 | +ok(evalSucceeds($jobset), "Evaluating simple.nix should succeed"); |
| 36 | +is(nrQueuedBuildsForJobset($jobset), 1, "Should have 1 build queued"); |
| 37 | + |
| 38 | +my $build = (queuedBuildsForJobset($jobset))[0]; |
| 39 | +ok(runBuild($build), "Build should succeed"); |
| 40 | + |
| 41 | +$build->discard_changes(); |
| 42 | + |
| 43 | +subtest "Test downloading build products (regression test for #1520)" => sub { |
| 44 | + # Get the build URL |
| 45 | + my $build_id = $build->id; |
| 46 | + |
| 47 | + # First, check that the build has products |
| 48 | + my @products = $build->buildproducts; |
| 49 | + ok(scalar @products >= 1, "Build should have at least 1 product"); |
| 50 | + |
| 51 | + # Find the doc product (created by build-product-simple.sh) |
| 52 | + my ($doc_product) = grep { $_->type eq "doc" } @products; |
| 53 | + ok($doc_product, "Should have a doc product"); |
| 54 | + |
| 55 | + if ($doc_product) { |
| 56 | + # Test downloading via the download endpoint |
| 57 | + # This tests the serveFile function which was broken in #1520 |
| 58 | + my $download_url = "/build/$build_id/download/" . $doc_product->productnr . "/text.txt"; |
| 59 | + my $response = request(GET $download_url); |
| 60 | + |
| 61 | + # The key test: should not return 500 error with "Can't use string ("1") as a HASH ref" |
| 62 | + isnt($response->code, 500, "Download should not return 500 error (regression test for #1520)"); |
| 63 | + is($response->code, 200, "Download should succeed with 200") |
| 64 | + or diag("Response code: " . $response->code . ", Content: " . $response->content); |
| 65 | + |
| 66 | + like($response->header('Content-Security-Policy') // '', qr/\bsandbox\b/, 'CSP header present with sandbox'); |
| 67 | + |
| 68 | + # Check that we get actual content |
| 69 | + ok(length($response->content) > 0, "Should receive file content"); |
| 70 | + is($response->content, "Hello\n", "Should get expected content"); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +done_testing(); |
0 commit comments