Cpanel-iContact-Provider-Telegram.t 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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::Telegram ();
  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. pass("WIP");
  17. return;
  18. my $text_scalar = 'lol, jk';
  19. my $send_args = { 'subject' => "[test.host.tld] YOUR COMIC BOOKS ARE DYING!!!1", 'text_body' => \$text_scalar, 'to' => [ 'SalinasPunishmentRoom', '@cPSaurus' ] };
  20. my $contact_cfg = {};
  21. my $ua_mocker = Test::MockModule->new("Cpanel::HTTP::Client");
  22. $ua_mocker->mock( 'request' => sub { return bless {}, "Cpanel::HTTP::Client::Response"; } );
  23. my $resp_mocker = Test::MockModule->new("Cpanel::HTTP::Client::Response");
  24. $resp_mocker->mock( 'success' => sub { return 1; }, 'status' => sub { return 200; }, 'reason' => sub { return 'OK'; } );
  25. isa_ok( my $spammer = Cpanel::iContact::Provider::Telegram->new(), "Cpanel::iContact::Provider::Telegram" );
  26. is( exception { $spammer->send() }, undef, "send doesn't throw on GreatSuccess" );
  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" );
  29. };
  30. subtest "Can send a message to somewhere (systems level/integration test)" => sub {
  31. SKIP: {
  32. my $conf_file = abs_path( dirname(__FILE__) . "/../.telegramtestrc" );
  33. skip "Skipping functional testing, needful not supplied", 1 if !$ENV{'AUTHOR_TESTS'} || !-f $conf_file;
  34. my $test_conf = Config::Simple->import_from($conf_file);
  35. my $text_body = "This is a test of Cpanel::iContact::Provider::Telegram. Please Ignore";
  36. my %args = (
  37. 'to' => [ $test_conf->param('CONTACTTELEGRAM') ],
  38. 'subject' => 'My Super cool test notification',
  39. 'text_body' => \$text_body,
  40. );
  41. {
  42. no warnings qw{redefine once};
  43. local *Cpanel::iContact::Provider::Telegram::new = sub {
  44. return bless {
  45. 'contact' => { 'TELEGRAMBOTTOKEN' => $test_conf->param('TELEGRAMBOTTOKEN') },
  46. 'args' => \%args,
  47. }, $_[0];
  48. };
  49. my $spammer = Cpanel::iContact::Provider::Telegram->new();
  50. my $ex;
  51. is( $ex = exception { $spammer->send() }, undef, "Didn't fail to send notification using full functional test" ) || diag explain $ex;
  52. }
  53. }
  54. };