aboutsummaryrefslogtreecommitdiff
path: root/t/tests/sietima/role/subscriberonly/moderate.t
blob: 44999bda61101a432f8f30217bb4fe957c173d8c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!perl 
use lib 't/lib';
use Test::Sietima;
use Test::Sietima::MailStore;
 
my @subscriber_addresses = (
    'one@users.example.com',
    'two@users.example.com',
);
my $owner = 'owner@lists.example.com';
my $ms = Test::Sietima::MailStore->new();
my $s = make_sietima(
    with_traits => ['SubscriberOnly::Moderate'],
    subscribers => \@subscriber_addresses,
    owner => $owner,
    mail_store => $ms,
);
 
subtest 'from subscriber' => sub {
    $ms->clear;
    test_sending(
        sietima => $s,
        mail => { from=>'one@users.example.com' },
    );
    is(
        $ms->retrieve_by_tags('moderation'),
        [],
        'no mails held for moderation',
    );
};
 
sub test_from_non_sub() {
    my $from = $s->return_path->address;
    test_sending(
        sietima => $s,
        mail => { from=>'someone@users.example.com' },
        mails => [{
            => object {
                call [header_str => 'subject'] => match qr{\bheld for moderation\b};
                call [header_str => 'from'] => match qr{\b\Q$from\E\b};
                call [header_str => 'to'] => match qr{\b\Q$owner\E\b};
                call_list parts => [
                    object {
                        call body => match qr{Use id \S+ to refer to it};
                    },
                    object {
                        call sub {Email::MIME->new(shift->body)} => object {
                            call [header_str => 'subject'] => 'Test Message';
                        };
                    },
                ];
            },
            from => $from,
            to => [$owner],
        }],
    );
}
 
subtest 'from non-subscriber' => sub {
    $ms->clear;
    test_from_non_sub;
 
    is(
        my $to_moderate = $ms->retrieve_by_tags('moderation'),
        [
            {
                id => T(),
                mail => object {
                    call [header_str => 'from'] => 'someone@users.example.com';
                    call [header_str => 'to'] => $s->return_path->address,
                },
            },
        ],
        'mails was held for moderation',
    );
 
    like(
        run_cmdline_sub($s'list_mails_in_moderation_queue'),
        hash {
            field exit => 0;
            field error => DNE;
            field output => qr{\A
                               ^\N+\b\s+ message\N+$ \n
                               ^\* \s\w\s+ someone\@users\.example\.com
                               \s+ "Test[ ]Message"
                               \s\(\N+?\)$
                          }smx;
        },
        'mails in queue should be listed from command line',
    );
 
    my $msg_id = $to_moderate->[0]->{id};
 
    like(
        run_cmdline_sub(
            $s'show_mail_from_moderation_queue',
            {}, { 'mail-id' => $msg_id },
        ),
        hash {
            field exit => 0;
            field error => DNE;
            field output => qr{\A
                               ^Message \s\w+:$
                               .*?
                               ^From: \s+ someone\@users\.example\.com \s*$
                          }smx;
        },
        'mail in queue should be shown from command line',
    );
 
    transport->clear_deliveries;
    $s->resume($msg_id);
    deliveries_are(
        to => \@subscriber_addresses,
    );
};
 
subtest 'from non-subscriber, drop' => sub {
    $ms->clear;
    test_from_non_sub;
 
    my $msg_id = $ms->retrieve_by_tags('moderation')->[0]{id};
    $s->drop($msg_id);
    is(
        $ms->retrieve_by_tags('moderation'),
        [],
        'message should be dropped',
    );
};
 
done_testing;