#!/usr/local/cpanel/3rdparty/bin/perl -w

#                                      Copyright 2025 WebPros International, LLC
#                                                            All rights reserved.
# copyright@cpanel.net                                          http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package t::medium::Cpanel::Plugins::UUID;

use cPstrict;
use Test::Cpanel::Policy;

use Test2::V0;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Exception;

use Test::MockModule          ();
use Test::MockObject::Extends ();
use Test::MockFile            ();

use Data::Dumper;

use Cpanel::Plugins::UUID ();

use base qw(Test::Class);

my $mock_privs = Test::MockModule->new('Cpanel::AccessIds::ReducedPrivileges');
$mock_privs->redefine( 'new' => sub { return 1 } );

my $mock_val_username = Test::MockModule->new('Cpanel::Validate::Username');
$mock_val_username->redefine( 'user_exists_or_die' => sub { return 1 } );

my $mock_val_uuid = Test::MockModule->new('Cpanel::Validate::UUID');
$mock_val_uuid->redefine( 'validate_uuid_or_die' => sub { return 1 } );

my $mock_uuid_good = '7bde24ae-d42d-4d50-bd85-9e3d733424c1';
my $mock_uuid_bad  = 'invalid';
my $mock_uuid;

my $mock_uuid_gen = Test::MockModule->new('Cpanel::UUID');
$mock_uuid_gen->redefine( 'random_uuid' => sub { return $mock_uuid } );

my $homedir      = File::Temp->newdir();
my $mock_pwcache = Test::MockModule->new('Cpanel::PwCache');
$mock_pwcache->redefine( 'gethomedir' => sub { return $homedir } );

__PACKAGE__->new()->runtests() if !caller;

sub _test_setup : Tests(setup) {

    $Cpanel::user = 'human';
    unlink "$homedir/.cpanel/plugin_uuid";
    unlink "$homedir/.cpanel";
    $mock_uuid = $mock_uuid_good;

    return;
}

sub test_constructor : Tests(4) {

    local $Cpanel::user = '';
    ok( dies { Cpanel::Plugins::UUID->new() }, "new: Dies when there is no user passed, and \$Cpanel::user is not set." );

    my $uuid = Cpanel::Plugins::UUID->new( user => 'orc' );
    is( $uuid->user(), 'orc', "new: User is set when passed in and \$Cpanel::user is not set." );

    local $Cpanel::user = 'elf';
    $uuid = Cpanel::Plugins::UUID->new();
    is( $uuid->user(), 'elf', "new: User is set to \$Cpanel::user when user is not passed in." );

    $uuid = Cpanel::Plugins::UUID->new( user => 'troll' );
    is( $uuid->user(), 'troll', "new: \$Cpanel::user is overrriden if user is passed in." );

    return;
}

sub test_uuid_file : Tests(2) {

    local $Cpanel::user = 'gnome';
    mkdir "$homedir/.cpanel";
    my $uuid = Cpanel::Plugins::UUID->new();
    is( $uuid->uuid_file(), "$homedir/.cpanel/plugin_uuid", "uuid_file: The correct file path is calculated when .cpanel exists." );

    unlink "$homedir/.cpanel/plugin_uuid";
    unlink "$homedir/.cpanel";
    $uuid = Cpanel::Plugins::UUID->new();
    is( $uuid->uuid_file(), "$homedir/.cpanel/plugin_uuid", "uuid_file: The correct file path is calculated when .cpanel does not exist." );

    return;
}

sub test_uuid : Tests(2) {

    my $uuid = Cpanel::Plugins::UUID->new();
    is( $uuid->uuid(), $mock_uuid_good, "uuid: correct uuid is returned when the .cpanel directory does not exist." );

    $mock_uuid = $mock_uuid_bad;
    $uuid      = Cpanel::Plugins::UUID->new();
    is( $uuid->uuid(), $mock_uuid_good, "uuid: correct uuid is returned when the uuid already exists, a new one is not generated." );

    return;

}

sub test_read : Tests(2) {

    my $uuid = Cpanel::Plugins::UUID->new();
    is( $uuid->read(), $mock_uuid_good, "read: correct uuid is returned when calling the read method." );

    unlink "$homedir/.cpanel/plugin_uuid";
    $uuid = Cpanel::Plugins::UUID->new();
    is( $uuid->read(), $mock_uuid_good, "read: correct uuid is returned when calling the read method and the plugin_uuid file did not exist." );

    return;
}

sub test_save : Tests(1) {

    my $uuid     = Cpanel::Plugins::UUID->new();
    my $new_uuid = '11111111-1111-1111-1111-111111111111';
    $uuid->save($new_uuid);
    is( $new_uuid, $uuid->read(), "save: correct uuid is saved to the plugin file." );

    return;
}

sub test_reset : Tests(1) {

    my $uuid = Cpanel::Plugins::UUID->new();
    $mock_uuid = '22222222-2222-2222-2222-222222222222';
    my $ret = $uuid->reset();
    is( $ret, $mock_uuid, "reset: uuid is reset to a new value." );

    return;
}

1;
