cPanel-PublicAPI

 view release on metacpan or  search on metacpan

t/03-api-query.t  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use Test::More;    # last test to print

use cPanel::PublicAPI ();

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

if ( !-d '/usr/local/cpanel' ) {
    plan skip_all => 'This test requires that cPanel and WHM are installed on a server';
}

if ( !-e $homedir . '/.accesshash' ) {
    plan skip_all => 'This test requires that an account hash is defined (see "Setup Remote Access Keys" in WHM)';
}

# SSL tests
my $pubapi = check_api_access();

isa_ok( $pubapi, 'cPanel::PublicAPI' );

my $res = $pubapi->api_request( 'whostmgr', '/xml-api/loadavg', 'GET', {} );
like( $$res, qr/<loadavg>\s*<one>\d+\.\d+<\/one>\s*<five>\d+\.\d+<\/five>\s*<fifteen>\d+\.\d+<\/fifteen>\s*<\/loadavg>*/, 'whm get no params' );

# Create the test regex for reuse
my $createacct_regex = qr/<statusmsg>.*is a reserved username.*<\/statusmsg>/;

$res = $pubapi->api_request( 'whostmgr', '/xml-api/createacct', 'GET', { 'username' => 'test', 'domain' => 'test.com' } );
like( $$res, $createacct_regex, 'ssl whm get hash params' );

$res = $pubapi->api_request( 'whostmgr', '/xml-api/createacct', 'GET', 'username=test&domain=test.com' );
like( $$res, $createacct_regex, 'ssl whm get string params' );

$res = $pubapi->api_request( 'whostmgr', '/xml-api/createacct', 'POST', { 'username' => 'test', 'domain' => 'test.com' } );
like( $$res, $createacct_regex, 'ssl whm post hash params' );

$res = $pubapi->api_request( 'whostmgr', '/xml-api/createacct', 'POST', 'username=test&domain=test.com' );
like( $$res, $createacct_regex, 'ssl whm post string params' );

# Create account for cpanel & reseller testing

if ( !-e '/var/cpanel/users/papiunit' ) {
    my $password = generate_password();
    $res = $pubapi->api_request(
        'whostmgr',
        '/xml-api/createacct',
        'POST',
        {
            'username' => 'papiunit',
            'password' => $password,
            'domain'   => 'cpanel-public-api-test.acct',
        }
    );

    like( $$res, qr/Account Creation Ok/, 'Test account created' );

    # skip is not used here due to the other code contained within this block.
    if ( $$res =~ /Account Creation Ok/ ) {
        my $cp_pubapi = cPanel::PublicAPI->new(
            'user'            => 'papiunit',
            'pass'            => $password,
            'ssl_verify_mode' => 0,
        );
        isa_ok( $cp_pubapi, 'cPanel::PublicAPI' );
        is( $cp_pubapi->{'operating_mode'}, 'session', 'Session operating mode is set properly when user/pass is used' );
        ok( !defined $cp_pubapi->{'cookie_jars'}->{'cpanel'},     'no cookies have been established for the cpanel service before the first query is made' );
        ok( !defined $cp_pubapi->{'security_tokens'}->{'cpanel'}, 'no security_token has been set for the cpanel service before the first query is made' );



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