|
| 1 | +# Copyright (C) 2025 SUSE LLC |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation; either version 2 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License along |
| 14 | +# with this program; if not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +use Mojo::Base -strict; |
| 17 | + |
| 18 | +use FindBin; |
| 19 | +use lib "$FindBin::Bin/lib"; |
| 20 | + |
| 21 | +use Test::More; |
| 22 | +use Test::Mojo; |
| 23 | +use Cavil::Test; |
| 24 | +use Mojo::File qw(path); |
| 25 | + |
| 26 | +plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE}; |
| 27 | + |
| 28 | +my $cavil_test = Cavil::Test->new(online => $ENV{TEST_ONLINE}, schema => 'incompatible_licenses_test'); |
| 29 | +my $t = Test::Mojo->new(Cavil => $cavil_test->default_config); |
| 30 | +$cavil_test->mojo_fixtures($t->app); |
| 31 | + |
| 32 | +# Add patterns for known incompatible licenses |
| 33 | +$t->app->pg->db->query('DELETE FROM license_patterns'); |
| 34 | +$t->app->patterns->create(pattern => 'SPDX-License-Identifier: Apache-2.0', license => 'Apache-2.0'); |
| 35 | +$t->app->patterns->create(pattern => 'SPDX-License-Identifier: GPL-2.0-only', license => 'GPL-2.0-only'); |
| 36 | +$t->app->pg->db->query('UPDATE license_patterns SET spdx = $1 WHERE license = $1', $_) for qw(Apache-2.0 GPL-2.0-only); |
| 37 | + |
| 38 | +# Add files with incompatible licenses |
| 39 | +my $pkg = $t->app->packages->find(1); |
| 40 | +my $dir = path($cavil_test->checkout_dir, $pkg->{name}, $pkg->{checkout_dir}); |
| 41 | +$dir->child('apache_file.txt')->spurt("# SPDX-License-Identifier: Apache-2.0\n\nThis is a test file.\n"); |
| 42 | +$dir->child('gpl2_file.txt')->spurt("# SPDX-License-Identifier: GPL-2.0-only\n\nThis is another test file.\n"); |
| 43 | + |
| 44 | +# Unpack and index |
| 45 | +$t->app->minion->enqueue(unpack => [1]); |
| 46 | +$t->app->minion->perform_jobs; |
| 47 | + |
| 48 | +subtest 'GPL-2.0-only and Apache-2.0 detected as incompatible' => sub { |
| 49 | + $t->get_ok('/login')->status_is(302)->header_is(Location => '/'); |
| 50 | + |
| 51 | + subtest 'Details after indexing' => sub { |
| 52 | + $t->get_ok('/reviews/meta/1') |
| 53 | + ->status_is(200) |
| 54 | + ->json_like('/package_license/name', qr!Artistic-2.0!) |
| 55 | + ->json_is('/package_license/spdx', 1) |
| 56 | + ->json_like('/package_version', qr!7\.25!) |
| 57 | + ->json_like('/package_summary', qr!Real-time web framework!) |
| 58 | + ->json_like('/package_group', qr!Development/Libraries/Perl!) |
| 59 | + ->json_like('/package_url', qr!http://search\.cpan\.org/dist/Mojolicious/!) |
| 60 | + ->json_like('/state', qr!new!) |
| 61 | + ->json_is('/unpacked_files', 341) |
| 62 | + ->json_is('/unpacked_size', '2.5MiB'); |
| 63 | + |
| 64 | + $t->json_like('/package_files/0/file', qr/perl-Mojolicious\.spec/) |
| 65 | + ->json_like('/package_files/0/licenses/0', qr/Artistic-2.0/) |
| 66 | + ->json_like('/package_files/0/version', qr/7\.25/) |
| 67 | + ->json_like('/package_files/0/sources/0', qr/http:\/\/www\.cpan\.org/) |
| 68 | + ->json_like('/package_files/0/summary', qr/Real-time web framework/) |
| 69 | + ->json_like('/package_files/0/url', qr/http:\/\//) |
| 70 | + ->json_like('/package_files/0/group', qr/Development\/Libraries\/Perl/); |
| 71 | + |
| 72 | + $t->json_is('/errors', [])->json_is('/warnings', []); |
| 73 | + }; |
| 74 | + |
| 75 | + subtest 'JSON report' => sub { |
| 76 | + $t->get_ok('/reviews/report/1.json')->status_is(200); |
| 77 | + ok my $json = $t->tx->res->json, 'JSON response'; |
| 78 | + |
| 79 | + ok my $pkg = $json->{package}, 'package'; |
| 80 | + is $pkg->{id}, 1, 'id'; |
| 81 | + is $pkg->{name}, 'perl-Mojolicious', 'name'; |
| 82 | + like $pkg->{checksum}, qr!Artistic-2.0-9!, 'checksum with elevated risk because of incompatible licenses'; |
| 83 | + is $pkg->{state}, 'new', 'state'; |
| 84 | + is $pkg->{notice}, 'Manual review is required because no previous reports are available', 'requires manual review'; |
| 85 | + |
| 86 | + ok my $report = $json->{report}, 'report'; |
| 87 | + is_deeply $report->{incompatible_licenses}, [{licenses => ['GPL-2.0-only', 'Apache-2.0']}], 'incompatible licenses'; |
| 88 | + |
| 89 | + }; |
| 90 | + |
| 91 | + subtest 'Text report' => sub { |
| 92 | + $t->get_ok('/reviews/report/1.txt')->status_is(200); |
| 93 | + ok my $text = $t->tx->res->text, 'text response'; |
| 94 | + like $text, qr/Elevated risk, package might contain incompatible licenses:/, |
| 95 | + 'text report contains warning about incompatible licenses'; |
| 96 | + like $text, qr/GPL-2.0-only, Apache-2.0/, 'text report lists incompatible licenses'; |
| 97 | + }; |
| 98 | + |
| 99 | + $t->get_ok('/logout')->status_is(302)->header_is(Location => '/'); |
| 100 | +}; |
| 101 | + |
| 102 | +done_testing; |
0 commit comments