Cpanel-iContact-Provider-Slack.t 1.8 KB

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