cPanel-APIClient

 view release on metacpan or  search on metacpan

t/lib/TestHTTPUAPIMixin.pm  view on Meta::CPAN

    return;
}

sub why_u_no_create_session { undef }

sub simple_with_password_and_tfa_token : Tests(4) {
    my ($self) = shift;

  SKIP: {
        my $why_skip = $self->why_u_no_create_session();

        skip $why_skip, $self->num_tests() if $why_skip;

        my $remote_cp = $self->CREATE(
            service => 'cpanel',

            credentials => {
                username  => 'johnny',
                password  => 'mysecret',
                tfa_token => 'mytfa',
            },
        );

        my $got = $self->AWAIT( $remote_cp->call_uapi( 'Email', 'list_forwarders' ) );

        cmp_deeply(
            $got,
            all(
                methods(
                    [ isa => 'cPanel::APIClient::Response::UAPI' ] => bool(1),
                    succeeded => 1,
                    get_data  => {
                        content => q<>,
                        method  => 'POST',
                        uri     => '/cpses123123123/execute/Email/list_forwarders',
                        headers => ignore(),
                    },
                ),
                listmethods(
                    get_errors   => [],
                    get_warnings => [],
                    get_messages => [],
                ),
            ),
            'UAPI response',
        ) or diag explain $got;

        my %headers = @{ $got->get_data()->{'headers'} };

        cmp_deeply(
            \%headers,
            superhashof(
                {
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    Cookie         => ignore(),
                }
            ),
            'headers',
        ) or diag explain \%headers;

        my @cookies = sort split m<\s*;\s*>, $headers{'Cookie'};

        cmp_bag(
            \@cookies,
            [
                'cpsession=johnny%3a3KCfM88PHoZ4MoUf%2ce95011e2b6a51118250861a505638a8c',
                ignore(),
            ],
            'session cookie sent',
        );

        $cookies[1] =~ s<\Alogin=><> or die "bad login cookie: $cookies[1]";
        my $login = URI::Escape::uri_unescape( $cookies[1] );
        my @params = split m<&>, $login;

        cmp_bag(
            \@params,
            [
                'user=johnny',
                'pass=mysecret',
                'tfa_token=mytfa',
                'login_only=1',
            ],
            'login parameters',
        ) or diag explain \@params;
    }

    return;
}

sub simple_with_password : Tests(2) {
    my ($self) = shift;

    my $remote_cp = $self->CREATE(
        service => 'cpanel',

        credentials => {
            username => 'johnny',
            password => 'mysecret',
        },
    );

    my $got = $self->AWAIT( $remote_cp->call_uapi( 'Email', 'list_forwarders' ) );

    cmp_deeply(
        $got,
        all(
            methods(
                [ isa => 'cPanel::APIClient::Response::UAPI' ] => bool(1),
                succeeded => 1,
                get_data  => {
                    content => q<>,
                    method  => 'POST',
                    uri     => '/execute/Email/list_forwarders',
                    headers => ignore(),
                },
            ),
            listmethods(
                get_errors   => [],
                get_warnings => [],
                get_messages => [],
            ),
        ),
        'UAPI response',
    ) or diag explain $got;

    my %headers = @{ $got->get_data()->{'headers'} };

    cmp_deeply(
        \%headers,
        superhashof(
            {
                'Content-Type' => 'application/x-www-form-urlencoded',
                Authorization  => 'Basic am9obm55Om15c2VjcmV0',
            }
        ),
        'headers',
    ) or diag explain \%headers;

    return;
}

sub with_payload : Tests(2) {
    my ($self) = shift;

    my $remote_cp = $self->CREATE(
        service => 'cpanel',

        credentials => {
            username  => 'johnny',
            api_token => 'MYTOKEN',
        },
    );

    my $got = $self->AWAIT(
        $remote_cp->call_uapi(
            'Email', 'list_forwarders',
            {
                foo => 1,
                bar => [ 2, 3, '"' ],
                baz => q<>,
                '"' => '"',
            },
        )
    );

    my $content = $got->get_data()->{'content'};
    my @pieces = split m<&>, $content, -1;

    cmp_bag(
        \@pieces,
        [
            'foo=1',
            'bar=2',
            'bar=3',
            'bar=%22',
            'baz=',
            '%22=%22',
        ],
        'UAPI response - content',
    ) or diag explain $got;

    like(
        $content,
        qr<bar=2.+bar=3.+bar=%22>,
        '“bar” args are in correct order',
    );

    return;
}

1;



( run in 0.898 second using v1.01-cache-2.11-cpan-71847e10f99 )