cPanel-PublicAPI

 view release on metacpan or  search on metacpan

t/02-construct.t  view on Meta::CPAN

#!/usr/bin/perl

# Copyright 2017, cPanel, Inc.
# All rights reserved.
# http://cpanel.net
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the owner nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use warnings;

use Test::More;
use Test::Exception;

use cPanel::PublicAPI ();

my @getpwuid = getpwuid($>);
my $homedir  = $getpwuid[7];
my $user     = $getpwuid[0];

# test default settings
if ( !-e $homedir . '/.accesshash' ) {
    $ENV{'REMOTE_PASSWORD'} = 'b4r!' if !defined $ENV{'REMOTE_PASSWORD'};
    $ENV{'SERVER_SOFTWARE'} = 'cpsrvd fakeout';
}

check_options();
check_options( 'debug' => 1, 'error_log' => '/dev/null' );
check_options( 'timeout'   => 150 );
check_options( 'usessl'    => 0 );
check_options( 'ip'        => '4.2.2.2' );
check_options( 'host'      => 'zomg.cpanel.net' );
check_options( 'error_log' => '/dev/null' );
check_options( 'user'      => 'bar' );
check_options( 'pass'      => 'f00!3Df@' );
my $accesshash = 'sdflkjl
sdafjkl
sdlfkjh';
check_options( 'accesshash' => $accesshash );
check_options( 'api_token'  => 'sdfkjl' );
throws_ok {
    check_options( 'api_token' => 'sdfkjl', 'accesshash' => $accesshash );
}
qr/You cannot specify both an accesshash and an API token/, 'Dies when specifying both an accesshash and API token';

my $http_tiny_creator_called;

my $pubapi = cPanel::PublicAPI->new(
    'error_log' => '/dev/null',
        http_tiny_creator => sub {
            $http_tiny_creator_called = 1;

            return HTTP::Tiny->new(@_);
        },

);

ok( $http_tiny_creator_called, 'http_tiny_creator is called' );

$pubapi->error('random string');
is( $pubapi->{'error'}, 'random string', 'Error variable is stored correctly' );

$pubapi->_init();
is( ref $cPanel::PublicAPI::CFG{'uri_encoder_func'}, 'CODE', 'URI Encoder Detected' );

$pubapi->_init_serializer();
is( ref $cPanel::PublicAPI::CFG{'api_decode_func'}, 'CODE', 'Serializer parse function detected' );
like( $cPanel::PublicAPI::CFG{'serializer_module'}, qr/^JSON/, 'Serializer Module is Named' );
is( $cPanel::PublicAPI::CFG{'serializer'},           'json', 'Serailizer format is correct' );

my $query_result = $pubapi->format_http_query( { 'one' => 'uno', 'two' => 'dos' } );
is( $query_result, 'one=uno&two=dos', 'format_http_query' );

$pubapi->set_debug(1);
is( $pubapi->{'debug'}, 1, 'set_debug accessor' );

$pubapi->user('someuser');
is( $pubapi->{'user'}, 'someuser', 'user accessor' );



( run in 0.636 second using v1.01-cache-2.11-cpan-39bf76dae61 )