Ver Fonte

Add tests for Slack provider

Andy Baugh há 8 anos atrás
pai
commit
eb8f4f89fd
3 ficheiros alterados com 65 adições e 2 exclusões
  1. 1 2
      TODO
  2. 29 0
      t/Cpanel-iContact-Provider-Schema-Slack.t
  3. 35 0
      t/Cpanel-iContact-Provider-Slack.t

+ 1 - 2
TODO

@@ -5,8 +5,7 @@ XMPP provider:
 * Register the user? Haven't tried that.
 
 Slack Provider:
-* Add tests, remove Cpanel namespace deps from driver lib
-* Slack external auth module integration?
+* Add more tests, remove Cpanel namespace deps from driver lib
 
 Other Providers:
 * IRC

+ 29 - 0
t/Cpanel-iContact-Provider-Schema-Slack.t

@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+use Cwd qw{abs_path};
+use File::Basename qw{dirname};
+use lib abs_path( dirname(__FILE__) . "/../lib" );
+
+use Test::More;
+use Test::Deep;
+
+use Cpanel::iContact::Provider::Schema::Slack ();
+
+plan tests => 2;
+
+subtest "Settings getter method performs as expected" => sub {
+    my $model = [ 'CONTACTSLACK' ];
+    my $settings = Cpanel::iContact::Provider::Schema::Slack::get_settings();
+    is_deeply( [ sort keys( %{$settings} ) ], $model, "Settings returned look OK so far" );
+    foreach my $key (@$model) {
+        is(
+            $settings->{$key}{'checkval'}->('https://foo.bar.baz'),
+            'https://foo.bar.baz', "Valid values passed to checkval subroutine of '$key' assumed GreatSuccess"
+        ) if ref $settings->{$key}{'checkval'} eq 'CODE';
+    }
+    is( $settings->{'CONTACTSLACK'}{'checkval'}->('    '), '', "Invalid values passed to checkval subroutine of 'CONTACTSLACK' engaged Maximum NO" );
+};
+
+my $config_model = { 'default_level' => 'All', 'display_name' => 'Slack', 'icon' => ignore() };
+cmp_deeply( Cpanel::iContact::Provider::Schema::Slack::get_config(), $config_model, "Config getter method return looks OK" );

+ 35 - 0
t/Cpanel-iContact-Provider-Slack.t

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