Cpanel-iContact-Provider-Slack.t 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Config::Simple ();
  10. use Cpanel::HTTP::Client ();
  11. use Cpanel::HTTP::Client::Response ();
  12. use Cpanel::iContact::Provider::Slack ();
  13. plan tests => 2;
  14. # First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
  15. subtest "Provider bits work as expected ('unit' test)" => sub {
  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. my $ua_mocker = Test::MockModule->new("Cpanel::HTTP::Client");
  20. $ua_mocker->mock( 'request' => sub { return bless {}, "Cpanel::HTTP::Client::Response"; } );
  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. $resp_mocker->mock( 'success' => sub { return 0; }, 'status' => sub { return 500; }, 'reason' => sub { return 'ENOHUGS'; } );
  26. isnt( exception { $spammer->send() }, undef, "send throws whenever anything goes wrong" );
  27. };
  28. subtest "Can send a message to somewhere (systems level/integration test)" => sub {
  29. SKIP: {
  30. my $conf_file = abs_path( dirname(__FILE__) . "/../.slacktestrc" );
  31. skip "Skipping functional testing, needful not supplied", 1 if !$ENV{'AUTHOR_TESTS'} || !-f $conf_file;
  32. my $test_conf = { Config::Simple->import_from($conf_file)->vars() };
  33. my $text_body = "This is a test of Cpanel::iContact::Provider::Slack. Please Ignore";
  34. my %args = (
  35. 'to' => [ $test_conf->{'CONTACTSLACK'} ],
  36. 'subject' => 'My Super cool test notification',
  37. 'text_body' => \$text_body,
  38. );
  39. {
  40. no warnings qw{redefine once};
  41. local *Cpanel::iContact::Provider::Slack::new = sub {
  42. return bless {
  43. 'contact' => $test_conf,
  44. 'args' => \%args,
  45. }, $_[0];
  46. };
  47. my $spammer = Cpanel::iContact::Provider::Slack->new();
  48. my $ex;
  49. is( $ex = exception { $spammer->send() }, undef, "Didn't fail to send notification using full functional test" ) || diag explain $ex;
  50. }
  51. }
  52. };