Cpanel-iContact-Provider-Slack.t 1.7 KB

123456789101112131415161718192021222324252627282930313233
  1. use strict;
  2. use warnings;
  3. use Cwd qw{abs_path};
  4. use File::Basename qw{dirname};
  5. use lib abs_path( dirname(__FILE__) . "/../lib" );
  6. use Test::More;
  7. use Test::Fatal;
  8. use Test::MockModule();
  9. -d "/usr/local/cpanel" ? plan tests => 3 : plan skip_all => "cPanel not installed";
  10. require Cpanel::HTTP::Client;
  11. require Cpanel::HTTP::Client::Response;
  12. require Cpanel::iContact::Provider::Slack;
  13. # First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
  14. my $provider = Test::MockModule->new("Cpanel::iContact::Provider");
  15. my $text_scalar = 'lol, jk';
  16. my $send_args = { 'subject' => "[test.host.tld] YOUR COMIC BOOKS ARE DYING!!!1", 'text_body' => \$text_scalar, 'to' => [ 'SalinasPunishmentRoom', '@cPSaurus' ] };
  17. my $contact_cfg = {};
  18. $provider->mock( 'new' => sub { return bless { 'args' => $send_args, 'contact' => $contact_cfg }, "Cpanel::iContact::Provider::Slack"; } );
  19. my $ua_mocker = Test::MockModule->new("Cpanel::HTTP::Client");
  20. $ua_mocker->mock( 'post_form' => sub { return bless {}, "Cpanel::HTTP::Client::Response"; }, 'die_on_http_error' => sub { return $_[0]; } );
  21. my $resp_mocker = Test::MockModule->new("Cpanel::HTTP::Client::Response");
  22. $resp_mocker->mock( 'success' => sub { return 1; }, 'status' => sub { return 200; }, 'reason' => sub { return 'OK'; } );
  23. isa_ok( my $spammer = Cpanel::iContact::Provider::Slack->new(), "Cpanel::iContact::Provider::Slack" );
  24. is( exception { $spammer->send() }, undef, "send doesn't throw on GreatSuccess" );
  25. my $redirect_mocker = Test::MockModule->new("Cpanel::Redirect");
  26. $resp_mocker->mock( 'success' => sub { return 0; }, 'status' => sub { return 500; }, 'reason' => sub { return 'ENOHUGS'; } );
  27. isnt( exception { $spammer->send() }, undef, "send throws whenever anything goes wrong" );