cPanel-PublicAPI

 view release on metacpan or  search on metacpan

t/04-tfa-sessions.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;    # 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)';
}

check_cpanel_version() or plan skip_all => 'This test requires cPanel version 54 or higher';

eval { require MIME::Base32; require Digest::SHA; 1; } or do {
    plan skip_all => 'This test requires the MIME::Base32 and Digest::SHA modules';
};
unshift @INC, '/usr/local/cpanel';
require Cpanel::Security::Authn::TwoFactorAuth::Google;

my $pubapi = check_api_access_and_config();

if ( !-e '/var/cpanel/users/papiunit' ) {
    my $password = generate_password();
    my $res      = $pubapi->whm_api(
        'createacct',
        {
            'username' => 'papiunit',
            'password' => $password,
            'domain'   => 'cpanel-public-api-test.acct',
            'reseller' => 1,
        }
    );
    like( $res->{'metadata'}->{'reason'}, qr/Account Creation Ok/, 'Test account created' );

    $res = $pubapi->whm_api(
        'setacls',
        {
            'reseller'        => 'papiunit',
            'acl-create-acct' => 1,
        }
    );
    ok( $res->{'metadata'}->{'result'}, 'Assigned create-acct ACL successfully' );

    _test_tfa_as_reseller( 'papiunit', $password );

    $res = $pubapi->whm_api(
        'removeacct',
        {
            'user' => 'papiunit',
        }
    );
    like( $res->{'metadata'}->{'reason'}, qr/papiunit account removed/, 'Test Account Removed' );
}
else {
    plan skip_all => 'Unable to create test account. It already exists';
}

done_testing();

sub _test_tfa_as_reseller {
    my ( $reseller, $password ) = @_;



( run in 2.731 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )