Cpanel-iContact-Provider-Telegram.t 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. 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 = { 'TELEGRAMBOTTOKEN' => '420SWAGYOLO69696969' };
  19. my $ua_mocker = Test::MockModule->new("WWW::Telegram::BotAPI");
  20. # Mock has to be used instead of redefine due to AUTOLOAD
  21. $ua_mocker->mock( 'getMe' => sub {}, 'sendMessage' => sub {} );
  22. isa_ok( my $spammer = Cpanel::iContact::Provider::Telegram->new(), "Cpanel::iContact::Provider::Telegram" );
  23. $spammer->{'contact'} = $contact_cfg;
  24. is( exception { $spammer->send() }, undef, "send doesn't throw on GreatSuccess" );
  25. $ua_mocker->mock( 'getMe' => sub { die "401 Unauthorized" } );
  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__) . "/../.telegramtestrc" );
  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);
  33. my $text_body = "This is a test of Cpanel::iContact::Provider::Telegram. Please Ignore";
  34. my %args = (
  35. 'to' => [ $test_conf->param('CONTACTTELEGRAM') ],
  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::Telegram::new = sub {
  42. return bless {
  43. 'contact' => { 'TELEGRAMBOTTOKEN' => $test_conf->param('TELEGRAMBOTTOKEN') },
  44. 'args' => \%args,
  45. }, $_[0];
  46. };
  47. my $spammer = Cpanel::iContact::Provider::Telegram->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. };