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

# cpanel - t/medium/Cpanel-Koality-Validate.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::Koality::Validate;

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

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

use Cpanel::Koality::Validate ();

use base qw(Test::Class);

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

sub test_valid_api_url : Tests(6) {

    my $tests = [
        {
            url  => 'https://url.com/api/',
            dies => 0
        },
        {
            url  => 'http://url.com/api/',
            dies => 1
        },
        {
            url  => 'url.com/api/',
            dies => 1
        },
        {
            url  => 'http://url.com/api',
            dies => 1
        },
        {
            url  => '',
            dies => 0
        },
        {
            url  => undef,
            dies => 0
        },
    ];

    foreach my $test ( $tests->@* ) {
        if ( $test->{dies} ) {
            ok( dies { Cpanel::Koality::Validate::valid_api_url( $test->{url} ) }, "valid_api_url(): Dies on invalid url." );
        }
        else {
            ok( lives { Cpanel::Koality::Validate::valid_api_url( $test->{url} ) }, "valid_api_url(): Lives on valid url." );
        }
    }

    return;
}

sub test_valid_api_object : Tests(3) {

    my $tests = [
        {
            module => 'foo',
            dies   => 1,
        },
        {
            module => bless( {}, 'Cpanel::Not::Valid' ),
            dies   => 1,
        },
        {
            module => bless( {}, 'Cpanel::Plugins::RestApiClient' ),
            dies   => 0,
        },
    ];

    foreach my $test ( $tests->@* ) {
        if ( $test->{dies} ) {
            ok( dies { Cpanel::Koality::Validate::valid_api_object( $test->{module} ) }, "valid_api_object(): Dies on invalid module." );
        }
        else {
            ok( lives { Cpanel::Koality::Validate::valid_api_object( $test->{module} ) }, "valid_api_object(): Lives on valid module." );
        }
    }

}

sub test_valid_token : Tests(6) {

    my $tests = [
        {
            token => 'token!',
            dies  => 1,
        },
        {
            token => '!token',
            dies  => 1,
        },
        {
            token => '@#$%^&',
            dies  => 1,
        },
        {
            token => '',
            dies  => 0,
        },
        {
            token => undef,
            dies  => 0,
        },
        {
            token => '1234abcd_abcd.1234',
            dies  => 0,
        },
    ];

    foreach my $test ( $tests->@* ) {
        if ( $test->{dies} ) {
            ok( dies { Cpanel::Koality::Validate::valid_token( $test->{token} ) }, "valid_token(): Dies on invalid token." );
        }
        else {
            ok( lives { Cpanel::Koality::Validate::valid_token( $test->{token} ) }, "valid_token(): Lives on valid token." );
        }
    }

    return;
}

sub test_valid_locale : Tests(10) {

    my $tests = [
        {
            locale => 'az',
            dies   => 0,
        },
        {
            locale => '!locale',
            dies   => 1,
        },
        {
            locale => '@#$%^&',
            dies   => 1,
        },
        {
            locale => 'en',
            dies   => 0,
        },
        {
            locale => 'pt-br',
            dies   => 1,
        },
        {
            locale => 'ru',
            dies   => 0,
        },
        {
            locale => '',
            dies   => 1,
        },
        {
            locale => 'a$',
            dies   => 1,
        },
        {
            locale => '*z',
            dies   => 1,
        },
        {
            locale => 'abcd',
            dies   => 1,
        },
    ];

    foreach my $test ( $tests->@* ) {
        if ( $test->{dies} ) {
            ok( dies { Cpanel::Koality::Validate::valid_activation_email_locale( $test->{locale} ) }, "valid_activation_email_locale(): Dies on invalid locale $test->{locale}." );
        }
        else {
            ok( lives { Cpanel::Koality::Validate::valid_activation_email_locale( $test->{locale} ) }, "valid_activation_email_locale(): Returns correct valid locale $test->{locale}." );
        }
    }

    return;
}

sub test_valid_geo_location : Tests(5) {

    my $tests = [
        {
            location => 'us_east',
            dies     => 0,
        },
        {
            location => 'de',
            dies     => 0,
        },
        {
            location => 'asia_jp',
            dies     => 0,
        },
        {
            location => 'uk',
            dies     => 1,
        },
        {
            location => 'us_west',
            dies     => 1,
        },
    ];

    foreach my $test ( $tests->@* ) {
        if ( $test->{dies} ) {
            ok( dies { Cpanel::Koality::Validate::valid_geo_location( $test->{location} ) }, "valid_geo_location(): Dies on invalid location." );
        }
        else {
            ok( lives { Cpanel::Koality::Validate::valid_geo_location( $test->{location} ) }, "valid_geo_location(): Lives on valid location." );
        }
    }

    return;
}

1;
