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

# cpanel - t/medium/Whostmgr-API-1-Sitejet.t       Copyright 2023 cPanel, L.L.C.
#                                                           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::API::Sitejet;

use cPstrict;

use Test2::V0;

use Test::Exception;
use Test::MockFile;
use Test::MockObject;

use Test::Cpanel::Policy;

use parent qw(
  Cpanel::TestObj
);

use Cpanel::Result ();
use Cpanel::Args   ();

use Whostmgr::API::1::Sitejet();

use Whostmgr::API::1::Utils ();

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

#----------------------------------------------------------------------

#e.g., num_method_tests()
sub new ( $class, @args ) {
    my $self = $class->SUPER::new(@args);
    return $self;
}

#e.g., set environs before running tests
sub runtests ( $self, @args ) {
    return $self->SUPER::runtests(@args);
}

sub test_set_ecommerce : Tests(5) ($self) {
    my @mocks = $self->mock_subs(
        'Cpanel::Config::CpConfGuard::save' => 1,
        'Cpanel::Config::CpConfGuard::new'  => sub {
            return bless {}, 'Cpanel::Config::CpConfGuard';
        },

    );

    my %good_data = (
        'case1' => {
            'allowEcommerce' => 1,
            'storeurl'  => 'http://www.google.com',
        },
        'case2' => {
            'allowEcommerce' => 0,
            'storeurl'  => 'http://www.cpanel.com/store',
        },
    );

    my $expected_metadata = {
        'result' => 1,
        'reason' => 'OK'
    };
    foreach my $case ( keys %good_data ) {
        my $metadata = {};
        Whostmgr::API::1::Sitejet::set_ecommerce( $good_data{$case}, $metadata );
        is( $metadata, $expected_metadata, "set_ecommerce works as expected for $case." );
    }

    my %bad_data = (
        'invalid_args' => {
            'allowEcommerce' => 'yup',
            'storeurl'  => 'string',
        },
        'no_args' => {
            'storeurl' => 'string',
        },
        'bad_url' => {
            'allowEcommerce' => 1,
            'storeurl'  => 'http://jira.cpanel.net/someotherplace/^replacevar/test.php'
        }
    );

    foreach my $case ( keys %bad_data ) {
        my $metadata = {};
        dies_ok(
            sub {
                Whostmgr::API::1::Sitejet::set_ecommerce( $bad_data{$case}, $metadata );
            },
            "set_ecommerce dies as expected for $case ."
        );
    }

    return;

}

sub test_get_ecommerce : Tests(6) ($self) {
    my %ecommerce_data = (
        'case1' => {
            'sitejet_ecommerce' => 1,
            'sitejet_storeurl'  => 'http:://www.cpanel.com/store',
        },
        'case2' => {
            'sitejet_ecommerce' => 0,
            'sitejet_storeurl'  => '',
        }
    );
    my $expected_metadata = {
        'result' => 1,
        'reason' => 'OK'
    };
    foreach my $case ( keys %ecommerce_data ) {
        my @mocks = ();
        @mocks = $self->mock_subs( 'Cpanel::Config::LoadCpConf::loadcpconf' => $ecommerce_data{$case} );

        my $metadata = {};
        my $output   = Whostmgr::API::1::Sitejet::get_ecommerce( '', $metadata );
        is( $output->{'is_enabled'}, $ecommerce_data{$case}->{'sitejet_ecommerce'}, "get_ecommerce works as expected for '$case' ecommerce status." );
        is( $output->{'storeurl'},   $ecommerce_data{$case}->{'sitejet_storeurl'},  "get_ecommerce works as expected for '$case' ecommerce storeurl." );
        is( $metadata,               $expected_metadata,                    "get_ecommerce returns the expected metadata for '$case'." );

    }

    return;

}
1;
