App-KGB

 view release on metacpan or  search on metacpan

t/70-webhook-push.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;

use autodie qw(:all);
use Test::More;
use Test::Exception;

BEGIN {
    eval { require LWP::UserAgent; 1 }
        or plan skip_all => "LWP::UserAgent is required for testing webhooks";
}

use lib 't';
use TestBot;

use App::KGB::Change;
use File::Temp qw(tempdir);
use File::Spec;
use JSON qw(to_json);
use Test::Differences;

unified_diff();

use utf8;
my $builder = Test::More->builder;
binmode $builder->output,         ":utf8";
binmode $builder->failure_output, ":utf8";
binmode $builder->todo_output,    ":utf8";

my $tmp_cleanup = not $ENV{TEST_KEEP_TMP};
my $dir = tempdir( 'kgb-XXXXXXX', CLEANUP => $tmp_cleanup, DIR => File::Spec->tmpdir );
diag "Temp directory $dir will be kept" unless $tmp_cleanup;

my $test_bot = TestBot->start;

use Cwd;
my $R = getcwd;

my $ua = LWP::UserAgent->new();
my $webhook_url = sprintf(
    'http://%s:%d/webhook/?%s',
    $test_bot->addr,
    $test_bot->port,
    join( '&',
        'channel=test',   'network=dummy',
        'shorten_urls=0', 'only_branch=master',
        "only_branch=old_branch" )
);

sub webhook_post {
    my $response = $ua->post(
        $webhook_url,
        'Content-Type' => 'text/json',
        Content        => to_json(shift, {utf8=>1}),
    );
}

# Verify invalid network is rejected.

my $bad_webhook_url = sprintf(
    'http://%s:%d/webhook/?%s',
    $test_bot->addr,
    $test_bot->port,
    join( '&', 'channel=test', 'network=invalid' )
);
my $content = to_json(
    {   object_kind   => 'push',
        ref           => 'refs/heads/old_branch',
        user_name     => 'Test User',
        user_username => 'ser',
        project       => { name => 'test-repo', },
        before        => '284ffdd4c525547f6ae848d768fff92ff9a89743',
        after         => '0000000000000000000000000000000000000000',
        commits       => [ ],
    }, {utf8=>1});

my $resp = $ua->post(
    $bad_webhook_url,
    'Content-Type' => 'text/json',
    Content        => $content,
);

is( $resp->code, 400, 'response status is 400' ) or diag $resp->as_string;
is( $resp->message, '(Bad Request) Unknown network "invalid".',
    'invalid network detected' ) or diag $resp->as_string;

$resp = webhook_post(
    {   object_kind   => 'push',



( run in 0.665 second using v1.01-cache-2.11-cpan-5735350b133 )