API-ISPManager

 view release on metacpan or  search on metacpan

t/01-test.t  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

our $ONLINE;

BEGIN {
    $ONLINE = $ENV{password};
}

use Test::More tests => $ONLINE ? 42 : 40;

my $test_host = $ENV{host} || 'ultrasam.ru';

ok(1, 'Test OK');
use_ok('API::ISPManager');

$a = 'refs';

is( refs( undef ),   '',       $a);
is( refs( {} ),      'HASH',   $a );
is( refs( [] ),      'ARRAY',  $a );
is( refs( sub {} ),  'CODE',   $a );
is( refs( \$a ),     'SCALAR', $a );

$a = 'is_success';

ok(! is_success(), $a);
ok(! is_success( { error => {}, data => {} } ), $a);
ok(  is_success( { data  => {} } ), $a);
ok(! is_success( { } ), $a);

$a = 'get_data';

ok(! get_data(), $a);
ok(! get_data( { } ), $a);
ok(! get_data( { error => {}, data => {} } ), $a);
is_deeply(  get_data( { data => { aaa => 'bbb' } } ), { aaa => 'bbb' }, $a);

$a = 'filter_hash';
is_deeply( API::ISPManager::filter_hash( {  }, [ ]), {}, $a );
is_deeply( API::ISPManager::filter_hash( { aaa => 555, bbb => 111 }, [ 'aaa' ]), { aaa => 555 }, $a );
is_deeply( API::ISPManager::filter_hash( { aaa => 555, bbb => 111 }, [ ]), { }, $a );
is_deeply( API::ISPManager::filter_hash( { }, [ 'aaa' ]), { }, $a );

$a = 'mk_query_string';
is( API::ISPManager::mk_query_string( {  }  ), '', $a );
is( API::ISPManager::mk_query_string( ''    ), '', $a );
is( API::ISPManager::mk_query_string( undef ), '', $a );
is( API::ISPManager::mk_query_string( { aaa => 111, bbb => 222 } ), 'aaa=111&bbb=222', $a );
is( API::ISPManager::mk_query_string( { bbb => 222, aaa => 111 } ), 'aaa=111&bbb=222', $a );
is( API::ISPManager::mk_query_string( [ ] ), '', $a );
is( API::ISPManager::mk_query_string( { dddd => 'dfdf' } ), 'dddd=dfdf', $a );

my $kill_start_end_slashes_test = {
    '////aaa////' => 'aaa',
    'bbb////'     => 'bbb',
    '////ccc'     => 'ccc', 
    ''            => '',
};

for (keys %$kill_start_end_slashes_test) {
    is(
        API::ISPManager::kill_start_end_slashes ($_),
        $kill_start_end_slashes_test->{$_},
        'kill_start_end_slashes'
    );
}

$a = 'mk_full_query_string';
is( API::ISPManager::mk_full_query_string( {
        host => $test_host, 
    } ), 
    '',
    $a
);

is( API::ISPManager::mk_full_query_string( {
        host       => $test_host,
        allow_http => 1,
        path       => 'manager',
    } ), 
    '',
    $a
);

is(  API::ISPManager::mk_full_query_string( {
        host       => $test_host,
        allow_http => 1,
        path       => '//my_manager///',
        param1     => 'val1',
        param2     => 'val2',
    } ), 
    "http://$test_host/my_manager/ispmgr?param1=val1&param2=val2",
    $a
);

is(  API::ISPManager::mk_full_query_string( {
        host       => $test_host,
        param1     => 'val1',
        param2     => 'val2',
    } ), 
    "https://$test_host/manager/ispmgr?param1=val1&param2=val2",
    $a
);


$a = 'mk_query_to_server';
is( API::ISPManager::mk_query_to_server( '' ), '', $a );

my %correct_params = (
    username => $ENV{username} || 'root',
    password => $ENV{password},
    host     => $test_host,
    path     => 'manager',
);

### Services

my $fake_services = <<DOC;
<?xml version="1.0" encoding="UTF-8"?>
<doc><elem><name>HTTP</name><proc>apache2</proc><autostart/><count>33</count><active/></elem><elem><name>FTP</name><proc>proftpd</proc><autostart/><count>1</count><active/></elem><elem><name>DNS</name><proc>named</proc><autostart/><count>1</count><ac...
DOC

is_deeply( API::ISPManager::services::get( { %correct_params }, $fake_services ), {
        'data' => {
            'FTP' => {
                'count' => '1',
                'proc' => 'proftpd',
                'autostart' => {},
                'active' => {}
            },
            'HTTP' => {
                'count' => '33',
                'proc' => 'apache2',
                'autostart' => {},
                'active' => {}
            },
            'SMTP' => {
                'count' => '1',
                'proc' => 'exim4',
                'autostart' => {},
                'active' => {}
            },
            'MySQL' => {
                'count' => '3',
                'proc' => 'mysqld',
                'autostart' => {}
            },
            'POP3' => {
                'count' => '2',
                'proc' => 'dovecot',
                'autostart' => {},
                'active' => {}
             },
            'DNS' => {
                'count' => '1',
                'proc' => 'named',
                'autostart' => {},
                'active' => {}
            }
        }
    },
    'services test'
);


### Services end

### Databases

my $fake_db = <<DOC;
<?xml version="1.0" encoding="UTF-8"?>
<doc><elem><dbkey>MySQL-&gt;bt</dbkey><dbtype>MySQL</dbtype><name>bt</name><dbuser>bt</dbuser><owner>bt</owner><size>0.055</size></elem><elem><dbkey>MySQL-&gt;howtouse</dbkey><dbtype>MySQL</dbtype><name>howtouse</name><dbuser>howtouse</dbuser><owner>...

DOC

is_deeply( API::ISPManager::db::list( { %correct_params } , $fake_db ), {
        'data' => {
            'kapella2'   => {
                'owner'  => 'kolian',
                'dbuser' => 'datakapella2',
                'dbtype' => 'MySQL',
                'dbkey'  => 'MySQL->kapella2',
                'size'   => '1.276'
            },
            'howtouse' => {
                'owner'  => 'howtouse',
                'dbuser' => 'howtouse',
                'dbtype' => 'MySQL',
                'dbkey'  => 'MySQL->howtouse',
                'size'   => '0.642'
            },
            'bt' => {
                'owner'  => 'bt',
                'dbuser' => 'bt',
                'dbtype' => 'MySQL',
                'dbkey'  => 'MySQL->bt',
                'size'   => '0.055'
            }
        }
    },
    'db list test'
);

### Databases end


### DB users

my $fake_db_user = <<DOC;
<?xml version="1.0" encoding="UTF-8"?>
<doc><plid>MySQL-&gt;blog</plid><elem><name>blog</name><read/><write/><manage/></elem></doc>
DOC

is_deeply(
    API::ISPManager::db_user::list( { %correct_params, elid => 'MySQL->blog' }, $fake_db_user ), {
        'data' => {
            'read' => {},



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