Bläddra i källkod

Merge pull request #8 from troglodyne/testing_for_dummies

Add script for generating test configuration, finish Slack test
Andy Baugh 7 år sedan
förälder
incheckning
a7f0826b52

+ 5 - 5
Makefile

@@ -1,13 +1,13 @@
-install:
-	make depend
+all: install
+
+install: depend
 	[ -d /var/cpanel/perl/Cpanel/iContact/Provider/Schema ] || mkdir -p /var/cpanel/perl/Cpanel/iContact/Provider/Schema/
 	cp -f lib/Cpanel/iContact/Provider/Schema/*.pm /var/cpanel/perl/Cpanel/iContact/Provider/Schema/
 	cp -f lib/Cpanel/iContact/Provider/*.pm /var/cpanel/perl/Cpanel/iContact/Provider/
 
-test:
-	make depend
+test: depend
 	[ ! -x /usr/local/cpanel/3rdparty/bin/prove ] || /usr/local/cpanel/3rdparty/bin/prove t/*.t
 	[ -x /usr/local/cpanel/3rdparty/bin/prove ] || prove t/*.t
 
 depend:
-	perl -MNet::XMPP -MMozilla::CA -MTest::More -MTest::Fatal -MTest::MockModule -MConfig::Simple -MIO::Socket::INET -MIO::Socket::SSL -e 'exit 0;' || sudo cpan -i Net::XMPP Mozilla::CA Test::More Test::Fatal Test::MockModule Config::Simple IO::Socket::INET IO::Socket::SSL
+	perl -MNet::XMPP -MMozilla::CA -MTest::More -MTest::Fatal -MTest::MockModule -MConfig::Simple -MIO::Socket::INET -MIO::Socket::SSL -MHTTP::Tiny::UA -MHTTP::Tiny::UA::Response -e 'exit 0;' || sudo cpan -i Net::XMPP Mozilla::CA Test::More Test::Fatal Test::MockModule Config::Simple IO::Socket::INET IO::Socket::SSL HTTP::Tiny::UA HTTP::Tiny::UA::Response

+ 11 - 11
README.md

@@ -9,18 +9,14 @@ Current plugins:
 
 Installation and Use:
 ---------------------
-* Clone this repo with git onto a cPanel host of at least 11.64+
-* Run the Tests
+* Clone this repo with git onto a cPanel host of at least 11.64+, then run `make` after changing the current working directory of your terminal session into the repo's directory.
+* (Optionally) Run the Tests
 
     make test
 
-* Install the Modules
-
-    make install
-
 * Go to WHM >> Basic Setup and configure the provider options
-* Go to WHM >> Contact Manager and make sure it is set up to spam you mercilessly
-* Do something to trigger notifications from cPanel & WHM
+* Go to WHM >> Contact Manager and make sure it is set up to spam you mercilessly (and for the notifications you care about!).
+* Do something to trigger a notification that should fire notifications from cPanel & WHM per your preference in /etc/clevels.conf
 
 KNOWN BUGS
 ----------
@@ -31,8 +27,12 @@ to avoid this problem. See issue #2 on the tracker.
 
 TESTING NOTES
 -------------
-If you want to run a functional test of the XMPP iContact code in order to debug problems, set the env var `AUTHOR_TESTS`
-and write out .xmpptestrc in the toplevel directory of this git repository with content like:
+If you want to run a functional test for any of these (to debug problems), please run the following script:
+`scripts/generate_testing_configuration.pl`
+as this will prompt you for all the needed values to make the test run (it tells you how to run it too).
+
+Anyways, this script will write out a file like the following to the repo's top level directory.
+In this example, we're using the XMPP provider, so it will be .xmpptestrc:
 
 `XMPPUSERNAME: user@domain.tld`
 
@@ -47,4 +47,4 @@ and write out .xmpptestrc in the toplevel directory of this git repository with
 You'll note these correspond to the values in the Provider's Schema module. With that set, you should spam yourself with
 a message if the t/Cpanel-iContact-Provider-XMPP.t test passes.
 
-Same goes for the IRC provider... use the same keys as in the schema module for dopeouts in its' test.
+Same goes for the IRC or Slack provider... use the same keys as in the schema module for dopeouts in its' test.

+ 9 - 0
lib/Cpanel/AdminBin/Serializer.pm

@@ -0,0 +1,9 @@
+package Cpanel::AdminBin::Serializer;
+
+use Cpanel::JSON::XS ();
+
+sub Dump {
+    return Cpanel::JSON::XS->new()->allow_blessed()->encode(@_);
+}
+
+1;

+ 7 - 0
lib/Cpanel/Exception.pm

@@ -0,0 +1,7 @@
+package Cpanel::Exception;
+
+use Test::More ();
+
+*create = &Test::More::explain;
+
+1;

+ 6 - 0
lib/Cpanel/HTTP/Client.pm

@@ -0,0 +1,6 @@
+package Cpanel::HTTP::Client 0.0;
+use parent qw{HTTP::Tiny::UA};
+
+sub die_on_http_error { return $_[0]; }
+
+1;

+ 4 - 0
lib/Cpanel/HTTP/Client/Response.pm

@@ -0,0 +1,4 @@
+package Cpanel::HTTP::Client::Response;
+use parent qw{HTTP::Tiny::UA::Response};
+
+1;

+ 3 - 4
lib/Cpanel/iContact/Provider/Slack.pm

@@ -6,7 +6,6 @@ use warnings;
 use parent 'Cpanel::iContact::Provider';
 
 use Try::Tiny;
-use Cpanel::LoadModule ();
 
 =encoding utf-8
 
@@ -62,13 +61,13 @@ sub send {
 
     my @errs;
 
-    Cpanel::LoadModule::load_perl_module("Cpanel::HTTP::Client");
+    require Cpanel::HTTP::Client;
     my $ua = Cpanel::HTTP::Client->new( 'default_headers' => { 'content-type' => 'application/json' } )->die_on_http_error();
 
     my $subject = $args_hr->{'subject'};
     my $message = ${ $args_hr->{'text_body'} };
 
-    Cpanel::LoadModule::load_perl_module("Cpanel::AdminBin::Serializer");
+    require Cpanel::AdminBin::Serializer;
     my $message_json = Cpanel::AdminBin::Serializer::Dump(
         {
             'text'        => $subject,
@@ -83,7 +82,7 @@ sub send {
             die( sprintf "Error %d: %s", $res->status(), $res->reason() ) if !$res->success();
         }
         catch {
-            Cpanel::LoadModule::load_perl_module("Cpanel::Exception");
+            require Cpanel::Exception;
             push(
                 @errs,
                 Cpanel::Exception::create(

+ 37 - 0
scripts/generate_testing_configuration.pl

@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use File::Basename qw{dirname};
+use Cwd qw{abs_path};
+
+my $gitdir = abs_path( dirname( __FILE__ ) . "/.." );
+unshift @INC, "$gitdir/lib";
+
+print "Please input the provider name you are attempting to test: ";
+chomp( my $input = <STDIN> );
+$input = uc($input);
+
+my $module_name = $input;
+if( $input eq 'SLACK' ) {
+    $module_name = 'Slack';
+}
+my $package = "Cpanel::iContact::Provider::Schema::$module_name";
+my $ret = eval "require $package";
+die "Couldn't load $package! Check input or file permissions" if !$ret;
+my $settings_hr = $package->get_settings();
+
+my $file_name_prefix = lc( $input );
+open( my $fh, ">", "$gitdir/.${file_name_prefix}testrc" );
+foreach my $key ( keys( %$settings_hr ) ) {
+    print "Please input the '", $settings_hr->{$key}{'label'}, "' ($key): ";
+    print $fh "$key: " . <STDIN>;
+}
+close( $fh );
+print STDOUT (
+    "Done writing to $gitdir/.${file_name_prefix}testrc.\n",
+    "The functional test in t/ should now work when ran like so:\n",
+    "AUTHOR_TESTS=1 prove $gitdir/t/Cpanel-iContact-Provider-$module_name.t\n"
+);
+
+0;

+ 44 - 20
t/Cpanel-iContact-Provider-Slack.t

@@ -7,27 +7,51 @@ use lib abs_path( dirname(__FILE__) . "/../lib" );
 
 use Test::More;
 use Test::Fatal;
-use Test::MockModule();
+use Test::MockModule ();
+use Config::Simple   ();
 
--d "/usr/local/cpanel" ? plan tests => 3 : plan skip_all => "cPanel not installed";
+use Cpanel::HTTP::Client              ();
+use Cpanel::HTTP::Client::Response    ();
+use Cpanel::iContact::Provider::Slack ();
 
-require Cpanel::HTTP::Client;
-require Cpanel::HTTP::Client::Response;
-require Cpanel::iContact::Provider::Slack;
+plan tests => 2;
 
 # 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" );
+subtest "Provider bits work as expected ('unit' test)" => sub {
+    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 = {};
+    my $ua_mocker = Test::MockModule->new("Cpanel::HTTP::Client");
+    $ua_mocker->mock( 'request' => sub { return bless {}, "Cpanel::HTTP::Client::Response"; } );
+    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" );
+    $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" );
+};
+
+subtest "Can send a message to somewhere (systems level/integration test)" => sub {
+    my $conf_file = abs_path( dirname(__FILE__) . "/../.slacktestrc" );
+    skip "Skipping functional testing, needful not supplied", 1 if !$ENV{'AUTHOR_TESTS'} || !-f $conf_file;
+    my $test_conf = { Config::Simple->import_from($conf_file)->vars() };
+    my $text_body = "This is a test of Cpanel::iContact::Provider::Slack. Please Ignore";
+    my %args = (
+        'to'        => [ $test_conf->{'CONTACTSLACK'} ],
+        'subject'   => 'My Super cool test notification',
+        'text_body' => \$text_body,
+    );
+
+    {
+        no warnings qw{redefine once};
+        local *Cpanel::iContact::Provider::Slack::new = sub {
+            return bless {
+                'contact' => $test_conf,
+                'args'    => \%args,
+            }, $_[0];
+        };
+        my $spammer = Cpanel::iContact::Provider::Slack->new();
+        is( exception { $spammer->send() }, undef, "Didn't fail to send notification using full functional test" );
+    }
+};