Request_Handler_add+del.t 12 KB
Newer Older
IKEDA Soji's avatar
IKEDA Soji committed
1
2
3
4
5
6
7
8
9
10
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4

use strict;
use warnings;
use Data::Dumper;
use English qw(-no_match_vars);
use File::Temp qw();
use Test::More;

11
use Sympa::ConfDef;
IKEDA Soji's avatar
IKEDA Soji committed
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
use Sympa::DatabaseManager;
use Sympa::List;
use Sympa::Log;
use Sympa::Spindle::ProcessRequest;

BEGIN {
    eval 'use DBD::SQLite';
}
unless ($DBD::SQLite::VERSION) {
    plan skip_all => 'DBD::SQLite required';
}

my $listname = 'test';

my $tempdir = File::Temp->newdir(CLEANUP => ($ENV{TEST_DEBUG} ? 0 : 1));
%Conf::Conf = (
    domain                => 'mail.example.org',
    listmaster            => 'dude@example.com',
    lang                  => 'en-US',
    db_type               => 'SQLite',
    db_name               => ':memory:',
    update_db_field_types => 'auto',

    queuesubscribe => $tempdir . '/auth',
    bounce_path    => $tempdir . '/bounce',
    etc            => $tempdir . '/etc',
38
39

    allow_subscribe_if_pending => 'off',
IKEDA Soji's avatar
IKEDA Soji committed
40
);
41
42
43
44
45
46
47
# Apply defaults.
foreach my $pinfo (grep { $_->{name} and exists $_->{default} }
    @Sympa::ConfDef::params) {
    $Conf::Conf{$pinfo->{name}} = $pinfo->{default}
        unless exists $Conf::Conf{$pinfo->{name}};
}

IKEDA Soji's avatar
IKEDA Soji committed
48
49
50
51
52
53
54
55
56
57
58
mkdir $Conf::Conf{queuesubscribe};
mkdir $Conf::Conf{bounce_path};
mkdir $Conf::Conf{bounce_path} . '/' . $listname . '@' . $Conf::Conf{domain};
mkdir $Conf::Conf{etc};
mkdir $Conf::Conf{etc} . '/data_sources';

my $fake_list = bless {
    name   => $listname,
    domain => $Conf::Conf{'domain'},
    dir    => $tempdir . '/list',
    admin  => {
59
60
        max_list_members => 2,

IKEDA Soji's avatar
IKEDA Soji committed
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
        available_user_options => {reception => [qw(mail digest)],},
        default_user_options   => {
            reception  => 'digest',
            visibility => 'conceal',
        },
        default_owner_options => {
            profile    => 'privileged',
            reception  => 'nomail',
            visibility => 'conceal',
        },
        default_editor_options => {
            reception  => 'nomail',
            visibility => 'conceal',
        },

        member_include => [
            {   source    => 'include_file',
                reception => 'mail',
            }
        ],
        owner_include => [
            {   source  => 'include_file',
                profile => 'normal',
            }
        ],
        editor_include => [
            {   source     => 'include_file',
                visibility => 'noconceal',
            }
        ],
    },
} => 'Sympa::List';
mkdir $tempdir . '/list';
open my $fh, '>', $Conf::Conf{etc} . '/data_sources/include_file.incl'
    or die $ERRNO;
print $fh "include_file $tempdir/source\n";
close $fh;

Sympa::Log->instance->{level} = -1;
do {
    no warnings 'redefine';
    local (*Sympa::send_notify_to_listmaster) = sub {1};
    Sympa::DatabaseManager::probe_db();
};
$SIG{__WARN__} = sub {
    print STDERR @_ unless 0 == index $_[0], 'Use of uninitialized value';
};

my $sdm = Sympa::DatabaseManager->instance or die;

# Now do testing

my $member_none = 'member0@example.name';
my $member1     = [qw(member1@example.name Member1 digest conceal)];
115
116
my $member2     = [qw(member2@example.name Member2 digest conceal)];
my $member3     = [qw(member3@example.name Member3 digest conceal)];
IKEDA Soji's avatar
IKEDA Soji committed
117
118
119
120
121
122
123
124
125
126
127
128
129
my $owner_none  = 'owner0@example.name';
my $owner1 = [qw(owner owner1@example.name Owner1 privileged nomail conceal)];
my $editor_none = 'editor0@example.name';
my $editor1 =
    [qw(editor editor1@example.name Editor1), undef, qw(nomail conceal)];
my $editor2 =
    [qw(editor editor2@example.name Editor2), undef, qw(nomail conceal)];
my $editor3 =
    [qw(editor editor3@example.name Editor3), undef, qw(nomail conceal)];

do_test(
    request => {
        action => 'add',
IKEDA Soji's avatar
IKEDA Soji committed
130
131
        email  => [$member1->[0], $member2->[0],],
        gecos  => [$member1->[1], $member2->[1],],
IKEDA Soji's avatar
IKEDA Soji committed
132
133
134
135
136
137
138
139
140
141
142
    },
    result => [[qw(user list_not_open)]],
    data   => [],
    name   => 'add subscriber: List not open'
);
$fake_list->{'admin'}{'status'} = 'open';
do_test(
    request => {
        action => 'add',
        email  => $member1->[0],
        gecos  => $member1->[1],
143
        quiet  => 1,
IKEDA Soji's avatar
IKEDA Soji committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    },
    data => [$member1],
    name => 'add subscriber'
);
do_test(
    request => {
        action => 'add',
        email  => $member1->[0],
        gecos  => $member1->[1],
    },
    result => [[qw(user already_subscriber)]],
    data   => [$member1],
    name   => 'add subscriber: Already subscriber'
);
158
159
160
161
162
163

do_test(
    request => {
        action => 'add',
        email  => $member2->[0],
        gecos  => $member2->[1],
164
        quiet  => 1,
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    },
    data => [$member1, $member2],
    name => 'add subscriber: Not exceeding max_list_members'
);
do_test(
    request => {
        action => 'add',
        email  => $member3->[0],
        gecos  => $member3->[1],
    },
    data   => [$member1, $member2],
    result => [[qw(user max_list_members_exceeded)]],
    name   => 'add subscriber: Exceeding max_list_members'
);

IKEDA Soji's avatar
IKEDA Soji committed
180
181
182
183
184
185
do_test(
    request => {
        action => 'add',
        role   => 'owner',
        email  => $owner1->[1],
        gecos  => $owner1->[2],
186
        quiet  => 1,
IKEDA Soji's avatar
IKEDA Soji committed
187
188
189
190
191
192
193
194
195
196
    },
    data => [$owner1],
    name => 'add owner'
);
do_test(
    request => {
        action => 'add',
        role   => 'editor',
        email  => $editor1->[1],
        gecos  => $editor1->[2],
197
        quiet  => 1,
IKEDA Soji's avatar
IKEDA Soji committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    },
    data => [$editor1, $owner1],
    name => 'add moderator'
);
do_test(
    request => {
        action => 'add',
        role   => 'owner',
        email  => $owner1->[1],
    },
    result => [[qw(user already_user)]],
    data   => [$editor1, $owner1],
    name   => 'add owner: Already owner'
);
do_test(
    request => {
        action => 'add',
        role   => 'editor',
        email  => [map { $_->[1] } ($editor1, $editor2, $editor3)],
        gecos  => [map { $_->[2] } ($editor1, $editor2, $editor3)],
218
        quiet  => 1,
IKEDA Soji's avatar
IKEDA Soji committed
219
220
221
222
223
224
225
226
227
228
229
230
    },
    result => [[qw(user already_user)], [qw(notice add_performed)]],
    data   => [$editor1, $editor2, $editor3, $owner1],
    name   => 'add moderators'
);

do_test(
    request => {
        action => 'del',
        email  => $member_none,
    },
    result => [[qw(user user_not_subscriber)]],
231
    data   => [$member1, $member2],
IKEDA Soji's avatar
IKEDA Soji committed
232
233
234
235
236
237
    name   => 'del subscriber: Not a subscriber'
);
do_test(
    request => {
        action => 'del',
        email  => $member1->[0],
238
        quiet  => 1,
IKEDA Soji's avatar
IKEDA Soji committed
239
    },
240
    data => [$member2],
IKEDA Soji's avatar
IKEDA Soji committed
241
242
    name => 'del subscriber'
);
243
244
245
246
do_test(
    request => {
        action => 'del',
        email  => [$member1->[0], $member2->[0]],
247
        quiet  => 1,
248
249
250
251
252
253
    },
    data   => [],
    result => [[qw(user user_not_subscriber)], [qw(notice removed)]],
    name   => 'del subscribers'
);

IKEDA Soji's avatar
IKEDA Soji committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
do_test(
    request => {
        action => 'del',
        role   => 'owner',
        email  => $owner_none,
    },
    result => [[qw(user user_not_admin)]],
    data   => [$editor1, $editor2, $editor3, $owner1],
    name   => 'del owner: Not a owner'
);
do_test(
    request => {
        action => 'del',
        role   => 'owner',
        email  => $owner1->[1],
    },
    data => [$editor1, $editor2, $editor3],
    name => 'del owner'
);
do_test(
    request => {
        action => 'del',
        role   => 'editor',
        email =>
            [$editor_none, map { $_->[1] } ($editor1, $editor2, $editor3)],
    },
    result => [[qw(user user_not_admin)], [qw(notice removed)]],
    data   => [],
    name   => 'del moderators'
);

do_test_include(
    role   => 'member',
    source => [$member1],
    data   => [[@{$member1}[0, 1], qw(mail conceal)]],
    name   => 'include subscriber',
);
sleep 2;
do_test_include(
    role   => 'member',
    source => [],
    data   => [],
    name   => 'include subscriber: emptied',
);
do_test_include(
    role   => 'member',
    source => [$member1],
    data   => [[@{$member1}[0, 1], qw(mail conceal)]],
    name   => 'include subscriber',
);
do_test_include(
    role   => 'owner',
    source => [[@{$owner1}[1, 2]]],
    data   => [[@{$owner1}[0 .. 2], qw(normal nomail conceal)]],
    name   => 'include owner',
);
do_test_include(
    role   => 'editor',
    source => [[@{$editor1}[1, 2]]],
    data   => [
        [@{$editor1}[0 .. 3], qw(nomail noconceal)],
        [@{$owner1}[0 .. 2],  qw(normal nomail conceal)],
    ],
    name => 'include moderator',
);
sleep 2;
do_test_include(
    role   => 'owner',
    source => [],
    data   => [[@{$editor1}[0 .. 3], qw(nomail noconceal)]],
    name   => 'include owner: emptied',
);
do_test_include(
    role   => 'editor',
    source => [],
    data   => [],
    name   => 'include moderator: emptied',
);

done_testing();

sub do_test {
    my %options = @_;

    my $role = $options{request}->{role} // 'member';

    my @stash;
    my $spindle = Sympa::Spindle::ProcessRequest->new(
        context => $fake_list,
        %{$options{request}},
        sender           => $Conf::Conf{listmaster},
        stash            => \@stash,
        scenario_context => {skip => 1},
    );
    die unless $spindle and $spindle->spin;

    if ($options{result}) {
        unless (
            grep {
                my $s = $_;
                grep { $_->[0] eq $s->[1] and $_->[1] eq $s->[2] }
                    @{$options{result}}
            } @stash
        ) {
            fail $options{name};
            diag 'No result ' . join "\n",
                map { sprintf '"%s" "%s"', @$_ } @{$options{result}};
            diag join "\n", map { join ',', @$_ } @stash;
        }
    } elsif (
        grep {
            $_->[1] ne 'notice'
        } @stash
    ) {
        fail $options{name};
        diag join "\n", map { join ',', @$_ } @stash;
    }

    return unless $options{data};

    if ('member' eq $role) {
        is_deeply $sdm->do_prepared_query(
            q{SELECT user_subscriber, comment_subscriber,
                     reception_subscriber, visibility_subscriber
              FROM subscriber_table
              ORDER BY user_subscriber}
        )->fetchall_arrayref, $options{data}, $options{name};
    } else {
        is_deeply $sdm->do_prepared_query(
            q{SELECT role_admin, user_admin, comment_admin,
                     profile_admin, reception_admin, visibility_admin
              FROM admin_table
              ORDER BY user_admin}
        )->fetchall_arrayref, $options{data}, $options{name};
    }
}

sub do_test_include {
    my %options = @_;

    my $role = $options{role} or die 'no role specified';

    open my $fh, '>', $tempdir . '/source' or die $ERRNO;
    foreach my $u (@{$options{source} // []}) {
        printf $fh "%s %s\n", $u->[0], $u->[1];
    }
    close $fh;

    my @stash;
    my $spindle = Sympa::Spindle::ProcessRequest->new(
        context          => $fake_list,
        action           => 'include',
        role             => $role,
        sender           => $Conf::Conf{listmaster},
        stash            => \@stash,
        scenario_context => {skip => 1},
    );
    die unless $spindle and $spindle->spin;

    if ($options{result}) {
        unless (
            grep {
                my $s = $_;
                grep { $_->[0] eq $s->[1] and $_->[1] eq $s->[2] }
                    @{$options{result}}
            } @stash
        ) {
            fail $options{name};
            diag 'No result ' . join "\n",
                map { sprintf '"%s" "%s"', @$_ } @{$options{result}};
            diag join "\n", map { join ',', @$_ } @stash;
        }
    } elsif (
        grep {
            $_->[1] ne 'notice'
        } @stash
    ) {
        fail $options{name};
        diag join "\n", map { join ',', @$_ } @stash;
    }

    return unless $options{data};

    if ('member' eq $role) {
        is_deeply $sdm->do_prepared_query(
            q{SELECT user_subscriber, comment_subscriber,
                     reception_subscriber, visibility_subscriber
              FROM subscriber_table
              ORDER BY user_subscriber}
        )->fetchall_arrayref, $options{data}, $options{name};
    } else {
        is_deeply $sdm->do_prepared_query(
            q{SELECT role_admin, user_admin, comment_admin,
                     profile_admin, reception_admin, visibility_admin
              FROM admin_table
              ORDER BY user_admin}
        )->fetchall_arrayref, $options{data}, $options{name};
    }
}