Adding a app_id field to applications database which must remain unique.

This commit is contained in:
sergiotarxz 2022-03-29 18:47:25 +02:00
parent cd17423eb5
commit 20b74912fa
11 changed files with 101 additions and 75 deletions

View File

@ -38,6 +38,8 @@ use Peace::Test::Mock::Model::Application;
$application->developer($developer); $application->developer($developer);
my $application_dao = Peace::DAO::Application->new( dbh => $dbh ); my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$application->app_id("com.example.desfronificator@{[int(rand(10000000))]}");
## WHEN ## WHEN
$application_dao->create( application => $application ); $application_dao->create( application => $application );
@ -67,6 +69,7 @@ use Peace::Test::Mock::Model::Application;
my $application_dao = Peace::DAO::Application->new( dbh => $dbh ); my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$application->app_id("com.example.desfronificator@{[int(rand(10000000))]}");
$application_dao->create( application => $application ); $application_dao->create( application => $application );
$developer->{dbh} = $dbh; $developer->{dbh} = $dbh;

View File

@ -41,6 +41,7 @@ use Peace::Test::Mock::Model::Release;
my $application_dao = Peace::DAO::Application->new( dbh => $dbh ); my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$application->app_id("com.example.desfronificator@{[int(rand(10000000))]}");
$application_dao->create( application => $application ); $application_dao->create( application => $application );
my $release = Peace::Test::Mock::Model::Release->new; my $release = Peace::Test::Mock::Model::Release->new;

View File

@ -28,6 +28,7 @@
<li><a href="#description">description</a></li> <li><a href="#description">description</a></li>
<li><a href="#url">url</a></li> <li><a href="#url">url</a></li>
<li><a href="#developer">developer</a></li> <li><a href="#developer">developer</a></li>
<li><a href="#app_id">app_id</a></li>
<li><a href="#price">price</a></li> <li><a href="#price">price</a></li>
<li><a href="#git_repo">git_repo</a></li> <li><a href="#git_repo">git_repo</a></li>
<li><a href="#flatpak_builder_file">flatpak_builder_file</a></li> <li><a href="#flatpak_builder_file">flatpak_builder_file</a></li>
@ -132,6 +133,14 @@ $application-&gt;developer($developer);</code></pre>
<p>Allows to retrieve and set the application&#39;s developer which is a <a href="Developer.pm.html">Peace::Model::Developer</a>.</p> <p>Allows to retrieve and set the application&#39;s developer which is a <a href="Developer.pm.html">Peace::Model::Developer</a>.</p>
<h2 id="app_id">app_id</h2>
<pre><code>my $app_id = $application-&gt;app_id;
$application-&gt;app_id($app_id);</code></pre>
<p>Allows to retrieve and set the application&#39;s id which represents the flatpak identifier.</p>
<h2 id="price">price</h2> <h2 id="price">price</h2>
<pre><code>my $price = $application-&gt;price; <pre><code>my $price = $application-&gt;price;

View File

@ -44,18 +44,18 @@ use Types::Standard qw/HasMethods InstanceOf Str HashRef/;
my $insert = <<'EOF'; my $insert = <<'EOF';
INSERT INTO applications INSERT INTO applications
(name, description, url, (name, description, url,
developer, price, git_repo, developer, app_id, price, git_repo,
flatpak_builder_file, flatpak_repo, verified) flatpak_builder_file, flatpak_repo, verified)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING uuid; RETURNING uuid;
EOF EOF
my $result = $dbh->selectrow_hashref( my $result = $dbh->selectrow_hashref(
$insert, undef, $insert, undef,
$application->name, $application->description, $application->name, $application->description,
$application->url, $application->developer->uuid, $application->url, $application->developer->uuid,
$application->price, $application->git_repo, $application->app_id, $application->price,
$application->flatpak_builder_file, $application->flatpak_repo, $application->git_repo, $application->flatpak_builder_file,
$application->verified $application->flatpak_repo, $application->verified
); );
my $uuid = $result->{uuid}; my $uuid = $result->{uuid};
my $new_application = $self->recover_by_uuid( uuid => $uuid ); my $new_application = $self->recover_by_uuid( uuid => $uuid );
@ -88,7 +88,7 @@ EOF
$dbh->selectall_arrayref( $dbh->selectall_arrayref(
<<'EOF', { Slice => {} }, $developer->uuid ); <<'EOF', { Slice => {} }, $developer->uuid );
SELECT uuid, date_creation, name, SELECT uuid, date_creation, name,
description, url, developer, description, url, developer, app_id,
price, git_repo, flatpak_builder_file, price, git_repo, flatpak_builder_file,
flatpak_repo, verified flatpak_repo, verified
FROM applications FROM applications
@ -114,7 +114,7 @@ EOF
my $uuid = $params{uuid}; my $uuid = $params{uuid};
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid ); my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
SELECT uuid, date_creation, name, SELECT uuid, date_creation, name,
description, url, developer, description, url, developer, app_id,
price, git_repo, flatpak_builder_file, price, git_repo, flatpak_builder_file,
flatpak_repo, verified flatpak_repo, verified
FROM applications FROM applications

View File

@ -45,6 +45,7 @@ my @migrations = (
developer UUID NOT NULL, developer UUID NOT NULL,
price INTEGER NOT NULL, price INTEGER NOT NULL,
git_repo TEXT NOT NULL, git_repo TEXT NOT NULL,
app_id TEXT NOT NULL UNIQUE,
flatpak_builder_file TEXT NOT NULL, flatpak_builder_file TEXT NOT NULL,
flatpak_repo TEXT NOT NULL, flatpak_repo TEXT NOT NULL,
verified BOOL DEFAULT false, verified BOOL DEFAULT false,
@ -64,6 +65,8 @@ my @migrations = (
uuid UUID NOT NULL DEFAULT gen_random_uuid(), uuid UUID NOT NULL DEFAULT gen_random_uuid(),
release UUID NOT NULL, release UUID NOT NULL,
date_creation timestamp DEFAULT NOW(), date_creation timestamp DEFAULT NOW(),
success BOOLEAN NOT NULL,
log TEXT NOT NULL,
arch TEXT NOT NULL, arch TEXT NOT NULL,
PRIMARY KEY (uuid), PRIMARY KEY (uuid),
FOREIGN KEY (release) REFERENCES releases (uuid) FOREIGN KEY (release) REFERENCES releases (uuid)

View File

@ -24,6 +24,7 @@ use DateTime;
{ type => InstanceOf ['Peace::Model::Developer'], optional => 1 }, { type => InstanceOf ['Peace::Model::Developer'], optional => 1 },
developer_uuid => { type => Str, optional => 1 }, developer_uuid => { type => Str, optional => 1 },
dbh => { type => HasMethods ['selectrow_hashref'], optional => 1 }, dbh => { type => HasMethods ['selectrow_hashref'], optional => 1 },
app_id => { type => Str },
price => { type => Num }, price => { type => Num },
git_repo => { type => Str }, git_repo => { type => Str },
flatpak_builder_file => { type => Str }, flatpak_builder_file => { type => Str },
@ -137,6 +138,26 @@ use DateTime;
} }
} }
{
my $validator = validation_for(
params => [
{
type => Str,
optional => 1,
}
]
);
sub app_id {
my $self = shift;
if (@_) {
my ($new_app_id) = $validator->(@_);
$self->{app_id} = $new_app_id;
}
return $self->{app_id};
}
}
{ {
my $validator = my $validator =
validation_for( params => [ { type => Num, optional => 1 } ] ); validation_for( params => [ { type => Num, optional => 1 } ] );
@ -310,6 +331,15 @@ Allows to retrieve and set the application's url.
Allows to retrieve and set the application's developer which Allows to retrieve and set the application's developer which
is a L<Peace::Model::Developer>. is a L<Peace::Model::Developer>.
=head2 app_id
my $app_id = $application->app_id;
$application->app_id($app_id);
Allows to retrieve and set the application's id which represents
the flatpak identifier.
=head2 price =head2 price
my $price = $application->price; my $price = $application->price;

View File

@ -15,6 +15,7 @@ sub new {
name => 'Desfronificator', name => 'Desfronificator',
description => 'Desfronifies the files.', description => 'Desfronifies the files.',
url => 'desfronificator.example.com', url => 'desfronificator.example.com',
app_id => 'com.example.desfronificator',
developer => Peace::Test::Mock::Model::Developer->new, developer => Peace::Test::Mock::Model::Developer->new,
price => 0, price => 0,
flatpak_builder_file => './resources/com.example.desfronificator.yml', flatpak_builder_file => './resources/com.example.desfronificator.yml',
@ -25,6 +26,7 @@ sub new {
); );
} }
1; 1;
=encoding utf8 =encoding utf8
=head1 NAME =head1 NAME

View File

@ -9,6 +9,7 @@ use Test::MockModule;
use DBI; use DBI;
use Peace::Model::Application; use Peace::Model::Application;
use Peace::Test::Mock::Model::Application;
BEGIN { BEGIN {
use_ok 'Peace::Model::Developer'; use_ok 'Peace::Model::Developer';
@ -78,21 +79,6 @@ BEGIN {
my $verified = 0; my $verified = 0;
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
### Application data
my $app_name0 = 'Desfronificator';
my $app_name1 = 'Desfronificator 1.0';
my $app_description = 'Desfronifies the files.';
my $app_url = 'https://desfronificator.example.com';
my $app_price = '4.20';
my $app_git_repo =
'https://git.desfronificator.example.com/larry/desfronificator.git';
my $app_flatpak_builder_file =
'./resources/com.example.desfronificator.yml';
my $app_flatpak_repo =
'https://nightly.gnome.org/gnome-nightly.flatpakrepo';
my $app_verified = 0;
my $developer = Peace::Model::Developer->new( my $developer = Peace::Model::Developer->new(
secret_bcrypt => $secret_bcrypt, secret_bcrypt => $secret_bcrypt,
name => $name, name => $name,
@ -104,29 +90,8 @@ BEGIN {
); );
my $applications = [ my $applications = [
Peace::Model::Application->new( Peace::Test::Mock::Model::Application->new,
name => $app_name0, Peace::Test::Mock::Model::Application->new,
description => $app_description,
url => $app_url,
price => $app_price,
git_repo => $app_git_repo,
flatpak_builder_file => $app_flatpak_builder_file,
flatpak_repo => $app_flatpak_repo,
verified => $app_verified,
developer => $developer,
),
Peace::Model::Application->new(
name => $app_name1,
description => $app_description,
url => $app_url,
price => $app_price,
git_repo => $app_git_repo,
flatpak_builder_file => $app_flatpak_builder_file,
flatpak_repo => $app_flatpak_repo,
verified => $app_verified,
developer => $developer,
),
]; ];
$application_dao_mock->mock( $application_dao_mock->mock(
recover_by_developer => sub { recover_by_developer => sub {

View File

@ -46,10 +46,12 @@ BEGIN {
country => $country, country => $country,
verified => $verified, verified => $verified,
); );
my $app_id = 'com.example.desfronificator';
## WHEN ## WHEN
my $application = Peace::Model::Application->new( my $application = Peace::Model::Application->new(
developer => $developer, developer => $developer,
app_id => $app_id,
name => $app_name, name => $app_name,
description => $app_description, description => $app_description,
url => $app_url, url => $app_url,
@ -96,6 +98,7 @@ BEGIN {
country => $country, country => $country,
verified => $verified, verified => $verified,
); );
my $app_id = 'com.example.desfronificator';
my $developer_dao = Test::MockModule->new('Peace::DAO::Developer'); my $developer_dao = Test::MockModule->new('Peace::DAO::Developer');
$developer_dao->mock( $developer_dao->mock(
@ -116,6 +119,7 @@ BEGIN {
verified => $app_verified, verified => $app_verified,
git_repo => $app_git_repo, git_repo => $app_git_repo,
dbh => $dbh, dbh => $dbh,
app_id => $app_id,
); );
## THEN ## THEN

View File

@ -22,9 +22,9 @@ BEGIN {
my $sql = <<'EOF'; my $sql = <<'EOF';
INSERT INTO applications INSERT INTO applications
(name, description, url, (name, description, url,
developer, price, git_repo, developer, app_id, price, git_repo,
flatpak_builder_file, flatpak_repo, verified) flatpak_builder_file, flatpak_repo, verified)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING uuid; RETURNING uuid;
EOF EOF
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
@ -35,7 +35,6 @@ EOF
my $application = Peace::Test::Mock::Model::Application->new; my $application = Peace::Test::Mock::Model::Application->new;
my $uuid = 'hola'; my $uuid = 'hola';
my $application_dao = Peace::DAO::Application->new( dbh => $dbh ); my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$dbh->{mock_add_resultset} = { $dbh->{mock_add_resultset} = {
@ -44,7 +43,7 @@ EOF
}; };
$sql = <<'EOF'; $sql = <<'EOF';
SELECT uuid, date_creation, name, SELECT uuid, date_creation, name,
description, url, developer, description, url, developer, app_id,
price, git_repo, flatpak_builder_file, price, git_repo, flatpak_builder_file,
flatpak_repo, verified flatpak_repo, verified
FROM applications FROM applications
@ -59,9 +58,9 @@ EOF
'uuid', 'date_creation', 'uuid', 'date_creation',
'name', 'description', 'name', 'description',
'url', 'developer', 'url', 'developer',
'price', 'git_repo', 'app_id', 'price',
'flatpak_builder_file', 'flatpak_repo', 'git_repo', 'flatpak_builder_file',
'verified', 'flatpak_repo', 'verified',
], ],
[ [
$uuid, $uuid,
@ -70,6 +69,7 @@ EOF
$application->description, $application->description,
$application->url, $application->url,
$developer->uuid, $developer->uuid,
$application->app_id,
$application->price, $application->price,
$application->git_repo, $application->git_repo,
$application->flatpak_builder_file, $application->flatpak_builder_file,
@ -97,7 +97,7 @@ EOF
my $sql = <<'EOF'; my $sql = <<'EOF';
SELECT uuid, date_creation, name, SELECT uuid, date_creation, name,
description, url, developer, description, url, developer, app_id,
price, git_repo, flatpak_builder_file, price, git_repo, flatpak_builder_file,
flatpak_repo, verified flatpak_repo, verified
FROM applications FROM applications
@ -112,9 +112,9 @@ EOF
'uuid', 'date_creation', 'uuid', 'date_creation',
'name', 'description', 'name', 'description',
'url', 'developer', 'url', 'developer',
'price', 'git_repo', 'app_id', 'price',
'flatpak_builder_file', 'flatpak_repo', 'git_repo', 'flatpak_builder_file',
'verified', 'flatpak_repo', 'verified',
], ],
[ [
$uuid, $uuid,
@ -123,6 +123,7 @@ EOF
$application_mock->description, $application_mock->description,
$application_mock->url, $application_mock->url,
$application_mock->developer->uuid, $application_mock->developer->uuid,
$application_mock->app_id,
$application_mock->price, $application_mock->price,
$application_mock->git_repo, $application_mock->git_repo,
$application_mock->flatpak_builder_file, $application_mock->flatpak_builder_file,
@ -142,7 +143,7 @@ EOF
## GIVEN ## GIVEN
my $sql = <<'EOF'; my $sql = <<'EOF';
SELECT uuid, date_creation, name, SELECT uuid, date_creation, name,
description, url, developer, description, url, developer, app_id,
price, git_repo, flatpak_builder_file, price, git_repo, flatpak_builder_file,
flatpak_repo, verified flatpak_repo, verified
FROM applications FROM applications
@ -168,6 +169,7 @@ EOF
'https://nightly.gnome.org/gnome-nightly.flatpakrepo'; 'https://nightly.gnome.org/gnome-nightly.flatpakrepo';
my $app_verified = 0; my $app_verified = 0;
my $iso8601 = DateTime::Format::Pg->new; my $iso8601 = DateTime::Format::Pg->new;
my $app_id = 'com.example.desfronificator';
$dbh->{mock_add_resultset} = { $dbh->{mock_add_resultset} = {
sql => $sql, sql => $sql,
results => [ results => [
@ -177,7 +179,7 @@ EOF
'url', 'price', 'url', 'price',
'git_repo', 'flatpak_builder_file', 'git_repo', 'flatpak_builder_file',
'flatpak_repo', 'verified', 'flatpak_repo', 'verified',
'developer' 'developer', 'app_id',
], ],
[ [
$app_uuid, $app_uuid,
@ -191,6 +193,7 @@ EOF
$app_flatpak_repo, $app_flatpak_repo,
$app_verified, $app_verified,
$developer->uuid, $developer->uuid,
$app_id,
], ],
] ]
}; };

View File

@ -47,6 +47,7 @@ BEGIN {
country => $developer_country, country => $developer_country,
verified => $developer_verified, verified => $developer_verified,
); );
my $app_id = 'com.example.desfronificator';
### Release data. ### Release data.
my $release_application = Peace::Model::Application->new( my $release_application = Peace::Model::Application->new(
developer => $app_developer, developer => $app_developer,
@ -58,6 +59,7 @@ BEGIN {
flatpak_repo => $app_flatpak_repo, flatpak_repo => $app_flatpak_repo,
verified => $app_verified, verified => $app_verified,
git_repo => $app_git_repo, git_repo => $app_git_repo,
app_id => $app_id,
); );
my $release_name = '0.0.1'; my $release_name = '0.0.1';
my $release_tag = 'v0.0.1'; my $release_tag = 'v0.0.1';
@ -105,6 +107,7 @@ BEGIN {
country => $developer_country, country => $developer_country,
verified => $developer_verified, verified => $developer_verified,
); );
my $app_id = 'com.example.desfronificator';
### Release data. ### Release data.
my $release_application = Peace::Model::Application->new( my $release_application = Peace::Model::Application->new(
developer => $app_developer, developer => $app_developer,
@ -116,6 +119,7 @@ BEGIN {
flatpak_repo => $app_flatpak_repo, flatpak_repo => $app_flatpak_repo,
verified => $app_verified, verified => $app_verified,
git_repo => $app_git_repo, git_repo => $app_git_repo,
app_id => $app_id,
); );
my $release_application_uuid = 'exampleuuid'; my $release_application_uuid = 'exampleuuid';
my $release_name = '0.0.1'; my $release_name = '0.0.1';
@ -153,6 +157,7 @@ BEGIN {
flatpak_builder_file => '../me.sergiotarxz.openmg.json', flatpak_builder_file => '../me.sergiotarxz.openmg.json',
flatpak_repo => 'https://nightly.gnome.org/gnome-nightly.flatpakrepo', flatpak_repo => 'https://nightly.gnome.org/gnome-nightly.flatpakrepo',
verified => 1, verified => 1,
app_id => 'com.example.desfronificator',
); );
my $uuid = 'random-uuid'; my $uuid = 'random-uuid';
my $release = Peace::Model::Release->new( my $release = Peace::Model::Release->new(
@ -162,5 +167,6 @@ BEGIN {
name => 'test', name => 'test',
); );
my $result = $release->generate_build( arch => 'x86_64' ); my $result = $release->generate_build( arch => 'x86_64' );
ok !$result->{success}, 'Build ends fail if flatpak_builder_file contains a .. component to avoid a directory escape vulnerability..'; ok !$result->{success},
'Build ends fail if flatpak_builder_file contains a .. component to avoid a directory escape vulnerability..';
} }