3 |
use strict; |
use strict; |
4 |
use blib; |
use blib; |
5 |
|
|
6 |
use Test::More tests => 3; |
use Test::More tests => 85; |
7 |
BEGIN { use_ok('Nos') }; |
use Cwd qw/abs_path/; |
8 |
|
|
9 |
|
BEGIN { |
10 |
|
use_ok('Nos'); |
11 |
|
use_ok('Mail::Alias'); |
12 |
|
}; |
13 |
|
|
14 |
|
my $self_path = abs_path($0); |
15 |
|
$self_path =~ s#/[^/]+$##; |
16 |
|
ok($self_path, "self_path: $self_path"); |
17 |
|
|
18 |
ok(my $nos = new Nos('dsn' => 'dbi:Pg:dbname=notices'), "new"); |
ok(my $nos = new Nos('dsn' => 'dbi:Pg:dbname=notices'), "new"); |
19 |
|
|
20 |
ok($nos->add_member_to_list( list => 'My list', email => 'foo@example.com' ), "add_member_to_list"); |
my $aliases = $self_path . '/aliases'; |
21 |
|
|
22 |
|
ok(! -e $aliases or unlink $aliases, "$aliases doesn't exist"); |
23 |
|
|
24 |
|
open(my $a, "> $aliases") || die "can't create empty $aliases: $!"; |
25 |
|
ok(close($a), "empty $aliases created"); |
26 |
|
|
27 |
|
ok(check_aliases(), "aliases $aliases exist"); |
28 |
|
|
29 |
|
sub check_aliases { |
30 |
|
return 0 unless (-e $aliases); |
31 |
|
|
32 |
|
my $email = shift || return 1; |
33 |
|
|
34 |
|
my $a = new Mail::Alias($aliases); |
35 |
|
return $a->exists($email); |
36 |
|
} |
37 |
|
|
38 |
|
ok($nos->create_list( list => 'My list', email => 'my-list@example.com', aliases => $aliases), "create_list my-list"); |
39 |
|
ok(check_aliases('my-list@example.com'), "alias added"); |
40 |
|
|
41 |
|
ok($nos->create_list( list => 'another', email => 'another@example.com', aliases => $aliases), "create_list another"); |
42 |
|
ok(check_aliases('another@example.com'), "alias added"); |
43 |
|
|
44 |
|
ok($nos->create_list( list => 'test', email => 'test@example.com', aliases => $aliases), "create_list test"); |
45 |
|
ok(check_aliases('test@example.com'), "alias added"); |
46 |
|
|
47 |
|
ok($nos->_get_list('My list'), "_get_list"); |
48 |
|
ok($nos->_get_list('another'), "_get_list"); |
49 |
|
ok($nos->_get_list('test'), "_get_list"); |
50 |
|
|
51 |
|
ok(-e $aliases, "$aliases exist"); |
52 |
|
|
53 |
|
my %ext_id; |
54 |
|
|
55 |
|
my $i = 0; |
56 |
|
foreach my $email (qw(foo@example.com bar@example.com baz@example.com)) { |
57 |
|
my $full_name = uc($1)." Doe" if ($email =~ m/^([^@]+)@/); |
58 |
|
ok($nos->add_member_to_list( list => 'My list', email => $email, name => $full_name, ext_id => $i ), "add_member_to_list $email, ext_id = $i"); |
59 |
|
ok($nos->add_member_to_list( list => 'another', email => $email ), "add_member_to_list $email") if ($i % 2 == 0); |
60 |
|
ok($nos->add_member_to_list( list => 'test', email => $email, ext_id => $i ), "add_member_to_list $email, ext_id = $i") if ($i % 3 == 0); |
61 |
|
$ext_id{$email} = $i; |
62 |
|
$i++; |
63 |
|
} |
64 |
|
|
65 |
|
ok($nos->add_message_to_list( list => 'My list', message => "Subject: test\n\nJust a test\n\n" ), "add_message_to_list"); |
66 |
|
|
67 |
|
foreach my $i ( 1 .. 10 ) { |
68 |
|
ok($nos->add_message_to_list( list => 'another', message => "Subject: test $i" ), "add_message_to_list another $i"); |
69 |
|
ok($nos->add_message_to_list( list => 'test', message => "Subject: test e-mail $i\n\nIsn't life great?\n" ), "add_message_to_list test $i") if ($i % 3 == 0); |
70 |
|
} |
71 |
|
|
72 |
|
cmp_ok(scalar $nos->list_members( list => 'My list' ), '==', 3, 'members of My list'); |
73 |
|
cmp_ok(scalar $nos->list_members( list => 'another' ), '==', 2, 'members of another'); |
74 |
|
cmp_ok(scalar $nos->list_members( list => 'test' ), '==', 1, 'members of test'); |
75 |
|
|
76 |
|
foreach my $list (('My list', 'another', 'test')) { |
77 |
|
diag "checking list $list"; |
78 |
|
foreach my $m ( $nos->list_members( list => $list ) ) { |
79 |
|
ok(my $email = $m->{'email'}, "have email"); |
80 |
|
cmp_ok($m->{'ext_id'}, 'eq', $ext_id{$email}, "ext_id($email)=$ext_id{$email}"); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
ok($nos->delete_member( email => 'foo@example.com' ), "delete"); |
86 |
|
|
87 |
|
cmp_ok(scalar $nos->list_members( list => 'My list' ), '==', 2, 'members of My list'); |
88 |
|
cmp_ok(scalar $nos->list_members( list => 'another' ), '==', 1, 'members of another'); |
89 |
|
cmp_ok(scalar $nos->list_members( list => 'test' ), '==', 0, 'members of test'); |
90 |
|
|
91 |
|
ok($nos->delete_member( name => 'BAZ Doe' ), "delete"); |
92 |
|
|
93 |
|
cmp_ok(scalar $nos->list_members( list => 'My list' ), '==', 1, 'members of My list'); |
94 |
|
cmp_ok(scalar $nos->list_members( list => 'another' ), '==', 0, 'members of another'); |
95 |
|
|
96 |
|
# now test delete |
97 |
|
|
98 |
|
ok($nos->create_list( list => 'delete', email => 'delete@example.com', aliases => $aliases), "create_list error"); |
99 |
|
|
100 |
|
ok($nos->add_member_to_list( list => 'delete', email => 'delete_me@example.com' ), "add test member"); |
101 |
|
|
102 |
|
my $nr; |
103 |
|
ok($nr = scalar $nos->list_members( list => 'delete' ), "get delete list members"); |
104 |
|
|
105 |
|
foreach my $i (1 .. 5) { |
106 |
|
ok($nos->add_member_to_list( list => 'delete', 'email' => 'delete_'.$i.'@example.com'), "add bogus member $i"); |
107 |
|
} |
108 |
|
|
109 |
|
cmp_ok(scalar $nos->list_members( list => 'delete' ), '==', $nr + 5, "new member"); |
110 |
|
|
111 |
|
ok($nos->delete_member_from_list( list => 'delete', email => 'delete_me@example.com'), "delete_memeber_from_list"); |
112 |
|
|
113 |
|
cmp_ok(scalar $nos->list_members( list => 'delete' ), '==', $nr + 4, 'member deleted'); |
114 |
|
|
115 |
|
ok(! $nos->delete_member_from_list( list => 'delete', email => 'delete_me@example.com'), "delete non-existant member"); |
116 |
|
|
117 |
|
foreach my $i (1 .. 5) { |
118 |
|
ok($nos->delete_member( email => 'delete_'.$i.'@example.com'), "delete_member $i"); |
119 |
|
} |
120 |
|
|
121 |
|
cmp_ok(scalar $nos->list_members( list => 'delete' ), '==', 0, "list empty"); |
122 |
|
|
123 |
|
# test delete lists |
124 |
|
|
125 |
|
foreach my $list (( 'My list', 'test', 'another', 'delete' )) { |
126 |
|
ok($nos->drop_list( list => $list, aliases => $aliases ), "drop $list"); |
127 |
|
ok(! $nos->_get_list($list), "$list doesn't exist"); |
128 |
|
} |
129 |
|
|