diff options
author | Brian Cully <bjc@kublai.com> | 2014-11-21 11:02:46 -0500 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2014-11-23 14:19:29 -0500 |
commit | cacbab512d4102e6db19c6487c4d291006dd0fda (patch) | |
tree | bc0e623db6b94384c9305529311148712786426c /t | |
download | spamcat-cacbab512d4102e6db19c6487c4d291006dd0fda.tar.gz spamcat-cacbab512d4102e6db19c6487c4d291006dd0fda.zip |
Initial commit.
Diffstat (limited to 't')
-rw-r--r-- | t/bin.t | 39 | ||||
-rw-r--r-- | t/conf.t | 18 | ||||
-rwxr-xr-x | t/delivert | 25 | ||||
-rw-r--r-- | t/fixtures/bar | 14 | ||||
-rw-r--r-- | t/fixtures/foo | 14 | ||||
-rw-r--r-- | t/fixtures/foo.expected | 14 | ||||
-rw-r--r-- | t/fixtures/foo2 | 14 | ||||
-rw-r--r-- | t/fixtures/foo2.expected | 14 | ||||
-rw-r--r-- | t/fixtures/multiple | 15 | ||||
-rw-r--r-- | t/fixtures/multiple.expected | 15 | ||||
-rw-r--r-- | t/fixtures/nosubj | 13 | ||||
-rw-r--r-- | t/fixtures/nosubj.expected | 14 | ||||
-rw-r--r-- | t/fixtures/sample.conf | 6 | ||||
-rw-r--r-- | t/fixtures/wrongdomain | 14 | ||||
-rw-r--r-- | t/fixtures/wrongdomain.expected | 14 | ||||
-rw-r--r-- | t/lib.t | 94 |
16 files changed, 337 insertions, 0 deletions
@@ -0,0 +1,39 @@ +#!/usr/bin/perl + +use Test::More tests => 1; + +use IO::File; + +use strict; +use warnings; + +system "/usr/bin/env > /tmp/fpp"; + +my $spamcat = 'bin/spamcat'; +my $conffile = 't/fixtures/sample.conf'; + +# Add testlib which has createdb and possibly population of said db. + +my @dumpconfig = `$spamcat -c t/fixtures/sample.conf --dumpconfig`; +my %got = parse_configdump(@dumpconfig); +my %expected = (DBPATH => '/tmp/spamcat.sqlite3', + DEFAULT_COUNT => 10, + DELIVER => 't/delivert', + DOMAINS => "spamcat.example.com, spamcat2.example.com, spamcat3"); +is_deeply(\%got, \%expected); + +# Test for proper delivery. +my $fh = IO::File->new("|$spamcat -c $conffile") || + die "Couldn't open pipe to $spamcat: $!\n"; +$fh->close; + +sub parse_configdump { + my %rc; + + while (my $line = shift) { + $line =~ /(.*) = (.*)/; + $rc{$1} = $2; + } + + %rc; +} diff --git a/t/conf.t b/t/conf.t new file mode 100644 index 0000000..dbe2217 --- /dev/null +++ b/t/conf.t @@ -0,0 +1,18 @@ +# -*- mode: cperl -*- + +use Test::More tests => 7; + +use strict; +use warnings; + +require_ok 'SpamCat::Conf'; + +ok(SpamCat::Conf->can('read')); +my %conf = SpamCat::Conf::read('t/fixtures/sample.conf'); +ok(%conf); + +is($conf{dbpath}, '/tmp/spamcat.sqlite3'); +is($conf{default_count}, 10); +is($conf{deliver}, 't/delivert'); +is_deeply($conf{domains}, + ['spamcat.example.com', 'spamcat2.example.com', 'spamcat3']); diff --git a/t/delivert b/t/delivert new file mode 100755 index 0000000..63c3864 --- /dev/null +++ b/t/delivert @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use Email::Simple; +use IO::File; + +use strict; +use warnings; + +die usage() unless $#ARGV == 0; +my $path = shift; + +local $/; +my $email = Email::Simple->new(<>); + +my $msgid = $email->header('Message-ID'); +$msgid =~ s/<(.*)@.*>/$1/; + +my $fh = IO::File->new(">$path/$msgid") || + die "Couldn't write to $path/$msgid: $!\n"; +print $fh $email->as_string; +$fh->close; + +sub usage { + "Usage: $0 path\n"; +} diff --git a/t/fixtures/bar b/t/fixtures/bar new file mode 100644 index 0000000..fd600a6 --- /dev/null +++ b/t/fixtures/bar @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: bar@spamcat.example.com
+Subject: test
+Message-Id: <bar@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/foo b/t/fixtures/foo new file mode 100644 index 0000000..6365fab --- /dev/null +++ b/t/fixtures/foo @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: foo@spamcat.example.com
+Subject: test
+Message-Id: <foo@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/foo.expected b/t/fixtures/foo.expected new file mode 100644 index 0000000..9da87a9 --- /dev/null +++ b/t/fixtures/foo.expected @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: foo@spamcat.example.com
+Subject: test - [18/20]
+Message-Id: <foo@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/foo2 b/t/fixtures/foo2 new file mode 100644 index 0000000..1696186 --- /dev/null +++ b/t/fixtures/foo2 @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: foo@spamcat2.example.com
+Subject: test
+Message-Id: <foo2@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/foo2.expected b/t/fixtures/foo2.expected new file mode 100644 index 0000000..e4d9335 --- /dev/null +++ b/t/fixtures/foo2.expected @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: foo@spamcat2.example.com
+Subject: test - [17/20]
+Message-Id: <foo2@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/multiple b/t/fixtures/multiple new file mode 100644 index 0000000..2da9b27 --- /dev/null +++ b/t/fixtures/multiple @@ -0,0 +1,15 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: name1@spamcat.example.com, "Someone else" <foo@spamcat.example.com>,
+ foo@wrongdomain.example.com
+Subject: test
+Message-Id: <multiple@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/multiple.expected b/t/fixtures/multiple.expected new file mode 100644 index 0000000..d5c17a5 --- /dev/null +++ b/t/fixtures/multiple.expected @@ -0,0 +1,15 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: name1@spamcat.example.com, "Someone else" <foo@spamcat.example.com>,
+ foo@wrongdomain.example.com
+Subject: test - [20/20]
+Message-Id: <multiple@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/nosubj b/t/fixtures/nosubj new file mode 100644 index 0000000..3a58556 --- /dev/null +++ b/t/fixtures/nosubj @@ -0,0 +1,13 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: nosubj@spamcat.example.com
+Message-Id: <nosubj@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/nosubj.expected b/t/fixtures/nosubj.expected new file mode 100644 index 0000000..94db30e --- /dev/null +++ b/t/fixtures/nosubj.expected @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: nosubj@spamcat.example.com
+Message-Id: <nosubj@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+Subject: [20/20]
+
+Sample email.
diff --git a/t/fixtures/sample.conf b/t/fixtures/sample.conf new file mode 100644 index 0000000..e0d644b --- /dev/null +++ b/t/fixtures/sample.conf @@ -0,0 +1,6 @@ +# Comments and newlines are ignored. + +DBPATH=/tmp/spamcat.sqlite3 # So should most whitespace and inline comments. +DEFAULT_COUNT =10 # Make sure key=val doesn't work in comments +DELIVER= t/delivert # by saying DEFAULT_COUNT=8 +DOMAINS = spamcat.example.com, spamcat2.example.com spamcat3 diff --git a/t/fixtures/wrongdomain b/t/fixtures/wrongdomain new file mode 100644 index 0000000..a65cb0f --- /dev/null +++ b/t/fixtures/wrongdomain @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: bar@wrongdomain.example.com
+Subject: test
+Message-Id: <wrongdomain@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
diff --git a/t/fixtures/wrongdomain.expected b/t/fixtures/wrongdomain.expected new file mode 100644 index 0000000..a65cb0f --- /dev/null +++ b/t/fixtures/wrongdomain.expected @@ -0,0 +1,14 @@ +Return-Path: <test@mta.example.com>
+Delivered-To: spamcat@example.com
+Received: from mta.example.com
+ by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg
+ for <spamcat@example.com>; Thu, 20 Nov 2014 17:17:18 -0500
+Received: by mta.example.com (Postfix, from userid 1001)
+ id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+To: bar@wrongdomain.example.com
+Subject: test
+Message-Id: <wrongdomain@mta.example.com>
+Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST)
+From: sender@example.com (Spamcat Sender)
+
+Sample email.
@@ -0,0 +1,94 @@ +# -*- Mode: cperl -*- + +use Test::More tests => 31; + +use strict; +use warnings; + +my ($tmpdir, %conf); +BEGIN { + $tmpdir = "/tmp/spamcat.t.$$"; + %conf = (dbpath => "$tmpdir/spamcat.sqlite3", + default_count => 20, + deliver => "t/delivert $tmpdir", + domains => ['spamcat.example.com', 'spamcat2.example.com']); + + system "rm -rf $tmpdir"; + mkdir $tmpdir; + system "/usr/local/bin/sqlite3 $conf{dbpath} < config/create-tables.sql"; +} + +END { + system "rm -rf $tmpdir"; +} + +require_ok 'SpamCat'; + +ok(SpamCat->can('new'), 'Has constructor'); +my $sch = SpamCat->new(%conf); +ok(defined $sch, 'Constructor returns instance'); + +ok(SpamCat->can('decrement_count'), 'Has count decrementor'); +is($sch->decrement_count('foo'), $conf{default_count}, + 'Default count for new sender'); +is($sch->decrement_count('foo'), 19, 'Existing sender decrements'); + +ok(SpamCat->can('get_count'), 'Has count getter'); +is($sch->get_count('foo'), 19, 'Returns existing sender count'); +ok(!defined $sch->get_count('doesntexist'), + 'Non-existant sender has undefined count'); + +ok(SpamCat->can('set_count'), 'Has count setter'); +is($sch->set_count('bar', 1), 1, 'Setting count returns existing count'); +is($sch->decrement_count('bar'), 0, 'Decrementing count from 1 returns 0'); +is($sch->decrement_count('bar'), 0, 'Decrementing count from 0 returns 0'); + +ok(SpamCat->can('parse_to')); +my @addrs; +@addrs = $sch->parse_to('foo@bar.com'); +is($addrs[0], 'foo@bar.com'); +@addrs = $sch->parse_to('"FooBar" <foo@bar.com>'); +is($addrs[0], 'foo@bar.com'); +@addrs = $sch->parse_to('"Foo@Bar" <baz@pham.com>'); +is($addrs[0], 'baz@pham.com'); +@addrs = $sch->parse_to('"Foo@Bar" <baz@pham.com>', '"a@b <one@two.com>"'); +is($addrs[0], 'baz@pham.com'); +is($addrs[1], 'one@two.com'); + +ok(SpamCat->can('deliver'), 'Has delivery method'); +test_file('foo', 1); +test_file('foo2', 1); +test_file('multiple', 1); +test_file('wrongdomain', 1); +test_file('nosubj', 1); +test_file('bar', 0); + +sub test_file { + my ($filen, $should_exist) = @_; + + my $input = IO::File->new("<t/fixtures/$filen") || + die "Couldn't open $filen: $!\n"; + my $inputfd = fileno($input); + open STDIN, ">&$inputfd" || die "Couldn't open $inputfd: $!\n"; + + $sch->deliver(); + + if ($should_exist) { + ok(-f "$tmpdir/$filen") || diag("$tmpdir/$filen doesn't exist."); + + local $/; + my $fh = IO::File->new("<$tmpdir/$filen") || + die "Couldn't open $tmpdir/$filen for reading: $!\n"; + my $got = <$fh>; + $fh->close; + + $fh = IO::File->new("<t/fixtures/$filen.expected") || + die "Couldn't open t/fixtures/$filen.expected for reading: $!\n"; + my $expected = <$fh>; + $fh->close; + + is($got, $expected) || diag("Test for $filen output failed."); + } else { + ok(! -f "$tmpdir/$filen") || diag("$tmpdir/$filen exists."); + } +} |