aboutsummaryrefslogtreecommitdiffstats
path: root/t/bin.t
blob: 2c59e20ef45b0fd6469c712539f344c10998efd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env perl

use Test::More tests => 9;

use IO::File;

use strict;
use warnings;

my ($tmpdir, $tmpconf);
BEGIN {
    $tmpdir = "/tmp/spamcat.t.$$";
    $tmpconf = "$tmpdir/spamcat.conf";

    system("rm -rf $tmpdir") == 0
        or die "Couldn't remove $tmpdir: $!\n";
    mkdir $tmpdir;
    system("sqlite3 $tmpdir/spamcat.sqlite3 < config/create-tables.sql") == 0
        or die "Couldn't create $tmpdir/spamcat.sqlite3: $!\n";

    my $fh = IO::File->new("> $tmpdir/spamcat.conf") ||
        die "Couldn't create $tmpdir/spamcat.conf: $!\n";
    print $fh "DBPATH = $tmpdir/spamcat.sqlite3\n";
    print $fh "DEFAULT_COUNT = 20\n";
    print $fh "DELIVER = t/delivert $tmpdir\n";
    print $fh "DOMAINS = spamcat.example.com\n";
}

END {
    system "rm -rf $tmpdir";
}

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 $conffile 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);

# Unknown senders have the default number of mails left.
my @got = `$spamcat -c $tmpconf get unknown-sender`;
is_deeply(\@got, ["unknown-sender has 20 messages remaining.\n"]);

# Missing argument to set fails.
my $got = system("$spamcat -c $tmpconf set missing-argument") >> 8;
is($got, 1);

# Can set/get remaining count.
@got = `$spamcat -c $tmpconf set foo 1`;
is($?, 0);
is_deeply(\@got, ["foo has 1 messages remaining.\n"]);
@got = `$spamcat -c $tmpconf get foo`;
is_deeply(\@got, ["foo has 1 messages remaining.\n"]);

@got = `$spamcat -c $tmpconf set foo 0`;
is($?, 0);
is_deeply(\@got, ["foo has 0 messages remaining.\n"]);
@got = `$spamcat -c $tmpconf get foo`;
is_deeply(\@got, ["foo has 0 messages remaining.\n"]);

sub parse_configdump {
  my %rc;

  while (my $line = shift) {
    $line =~ /(.*) = (.*)/;
    $rc{$1} = $2;
  }

  %rc;
}