aboutsummaryrefslogtreecommitdiff
path: root/t/lib/Test/Sietima.pm
blob: e20aacf68cddead5539340254a897ea86291ef70 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package Test::Sietima; 
use lib 't/lib';
use Import::Into;
use Email::Stuffer;
use Email::Sender::Transport::Test;
use Data::Printer;
use Sietima;
use Test2::V0;
use Test2::API qw(context);
use Sietima::Policy;
use namespace::clean;
 
sub import {
    my $target = caller;
    Test2::Bundle::Extended->import::into($target);
    Test2::Plugin::DieOnFail->import::into($target);
    Data::Printer->import::into($target);
    Sietima::Policy->import::into($target);
 
    for my $function (qw(transport make_sietima make_mail
                         deliveries_are test_sending
                         run_cmdline_sub)) {
        no strict 'refs';
        "${target}::${function}"->** = __PACKAGE__->can($function);
    }
    return;
}
 
my $return_path = 'sietima-test@list.example.com';
 
sub transport {
    state $transport = Email::Sender::Transport::Test->new;
    return $transport;
}
 
sub make_sietima (%args) {
    my $class = 'Sietima';
    if (my $traits = delete $args{with_traits}) {
        $class = $class->with_traits($traits->@*);
    }
 
    $class->new({
        return_path => $return_path,
        %args,
        transport => transport(),
    });
}
 
my $maybe = sub ($obj,$method,$arg) {
    return $obj unless $arg;
    return $obj->$method($arg);
};
 
my $mapit = sub ($obj,$method,$arg) {
    return $obj unless $arg;
    for my $k (keys $arg->%*) {
        $obj = $obj->$method($k$arg->{$k});
    }
    return $obj;
};
 
sub make_mail (%args) {
    Email::Stuffer
          ->from($args{from}||'someone@users.example.com')
          ->to($args{to}||$return_path)
          ->$maybe(cc => $args{cc})
          ->$mapit(header => $args{headers})
          ->subject($args{subject}||'Test Message')
          ->text_body($args{body}||'some simple message')
          ->email;
}
 
sub deliveries_are (%args) {
    my $ctx = context();
 
    my $checker;
    if (my @mails = ($args{mails}||[])->@*) {
        $checker = bag {
            for my $m (@mails) {
                item hash {
                    if (ref($meq 'HASH') {
                        field email => object {
                            call [cast=>'Email::MIME'] => $m->{o};
                        };
                        field envelope => hash {
                            field to => bag {
                                item $_ for $m->{to}->@*;
                            if $m->{to};
                            field from => $m->{fromif $m->{from};
                            etc();
                        };
                    }
                    else {
                        field email => object {
                            call [cast=>'Email::MIME'] => $m;
                        };
                    }
                    etc();
                };
            }
            end();
        };
    }
    elsif (my @recipients = do {my $to = $args{to}; ref($to) ? $to->@* : $to // () }) {
        $checker = array {
            item hash {
                field envelope => hash {
                    field from => $args{from}||$return_path;
                    field to => bag {
                        for (@recipients) {
                            item $_;
                        }
                        end();
                    };
                    etc();
                };
                etc();
            };
            end();
        };
    }
    else {
        $checker = [];
    }
 
    my @deliveries = transport->deliveries;
    is(
        \@deliveries,
        $checker,
        $args{test_message}//'the deliveries should be as expected',
        np @deliveries,
    );
    $ctx->release;
}
 
sub test_sending (%args) {
    my $ctx = context();
 
    my $sietima = delete $args{sietima};
    if (!$sietima or ref($sietimaeq 'HASH') {
        $sietima = make_sietima(%{$sietima||{}});
    }
    my $mail = delete $args{mail};
    if (!$mail or ref($maileq 'HASH') {
        $mail = make_mail(
            to => $sietima->return_path,
            %{$mail||{}},
        );
    }
 
    transport->clear_deliveries;
 
    ok(
        lives { $sietima->handle_mail($mail) },
        'should handle the mail',
        $@,
    );
 
    $args{from} ||= $sietima->return_path;
    $args{to} ||= [ map { $_->address} $sietima->subscribers->@* ];
    deliveries_are(%args);
 
    $ctx->release;
}
 
sub run_cmdline_sub($sietima,$method,$options={},$parameters={}) {
    require Sietima::Runner;
    my $r = Sietima::Runner->new({
        options => $options,
        parameters => $parameters,
        cmd => $sietima,
        op => $method,
    });
    $r->response(App::Spec::Run::Response->new(buffered=>1));
    ok(
        lives { $sietima->$method($r) },
        "calling $method should live",
    );
    my %ret;
    for my $output ($r->response->outputs->@*) {
        $ret{
            $output->error ? 'error' : 'output'
        } .= $output->content;
    }
    $ret{exit} = $r->response->exit();
    return \%ret;
}
 
1;