]> Sergey Matveev's repositories - public-inbox.git/blob - t/replace.t
tests: quiet down commit graph
[public-inbox.git] / t / replace.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::MIME;
7 use PublicInbox::InboxWritable;
8 require './t/common.perl';
9 use Cwd qw(abs_path);
10 require_git(2.6); # replace is v2 only, for now...
11 foreach my $mod (qw(DBD::SQLite)) {
12         eval "require $mod";
13         plan skip_all => "$mod missing for $0" if $@;
14 }
15
16 local $ENV{HOME} = abs_path('t');
17
18 sub test_replace ($$$) {
19         my ($v, $level, $opt) = @_;
20         diag "v$v $level replace";
21         my $this = "pi-$v-$level-replace";
22         my ($tmpdir, $for_destroy) = tmpdir($this);
23         my $ibx = PublicInbox::Inbox->new({
24                 inboxdir => "$tmpdir/testbox",
25                 name => $this,
26                 version => $v,
27                 -primary_address => 'test@example.com',
28                 indexlevel => $level,
29         });
30
31         my $orig = PublicInbox::MIME->new(<<'EOF');
32 From: Barbra Streisand <effect@example.com>
33 To: test@example.com
34 Subject: confidential
35 Message-ID: <replace@example.com>
36 Date: Fri, 02 Oct 1993 00:00:00 +0000
37
38 Top secret info about my house in Malibu...
39 EOF
40         my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
41         # fake a bunch of epochs
42         $im->{rotate_bytes} = $opt->{rotate_bytes} if $opt->{rotate_bytes};
43
44         if ($opt->{pre}) {
45                 $opt->{pre}->($im, 1, 2);
46                 $orig->header_set('References', '<1@example.com>');
47         }
48         ok($im->add($orig), 'add message to be replaced');
49         if ($opt->{post}) {
50                 $opt->{post}->($im, 3, { 4 => 'replace@example.com' });
51         }
52         $im->done;
53         my $thread_a = $ibx->over->get_thread('replace@example.com');
54
55         my %before = map {; delete($_->{blob}) => $_ } @{$ibx->recent};
56         my $reject = PublicInbox::MIME->new($orig->as_string);
57         foreach my $mid (['<replace@example.com>', '<extra@example.com>'],
58                                 [], ['<replaced@example.com>']) {
59                 $reject->header_set('Message-ID', @$mid);
60                 my $ok = eval { $im->replace($orig, $reject) };
61                 like($@, qr/Message-ID.*may not be changed/,
62                         '->replace died on Message-ID change');
63                 ok(!$ok, 'no replacement happened');
64         }
65
66         # prepare the replacement
67         my $expect = "Move along, nothing to see here\n";
68         my $repl = PublicInbox::MIME->new($orig->as_string);
69         $repl->header_set('From', '<redactor@example.com>');
70         $repl->header_set('Subject', 'redacted');
71         $repl->header_set('Date', 'Sat, 02 Oct 2010 00:00:00 +0000');
72         $repl->body_str_set($expect);
73
74         my @warn;
75         local $SIG{__WARN__} = sub { push @warn, @_ };
76         ok(my $cmts = $im->replace($orig, $repl), 'replaced message');
77         my $changed_epochs = 0;
78         for my $tip (@$cmts) {
79                 next if !defined $tip;
80                 $changed_epochs++;
81                 like($tip, qr/\A[a-f0-9]{40}\z/,
82                         'replace returned current commit');
83         }
84         is($changed_epochs, 1, 'only one epoch changed');
85
86         $im->done;
87         my $m = PublicInbox::MIME->new($ibx->msg_by_mid('replace@example.com'));
88         is($m->body, $expect, 'replaced message');
89         is_deeply(\@warn, [], 'no warnings on noop');
90
91         my @cat = qw(cat-file --buffer --batch --batch-all-objects);
92         my $git = $ibx->git;
93         my @all = $git->qx(@cat);
94         is_deeply([grep(/confidential/, @all)], [], 'nothing confidential');
95         is_deeply([grep(/Streisand/, @all)], [], 'Streisand who?');
96         is_deeply([grep(/\bOct 1993\b/, @all)], [], 'nothing from Oct 1993');
97         my $t19931002 = qr/ 749520000 /;
98         is_deeply([grep(/$t19931002/, @all)], [], "nothing matches $t19931002");
99
100         for my $dir (glob("$ibx->{inboxdir}/git/*.git")) {
101                 my ($bn) = ($dir =~ m!([^/]+)\z!);
102                 is(system(qw(git --git-dir), $dir, qw(fsck --strict)), 0,
103                         "git fsck is clean in epoch $bn");
104         }
105
106         my $thread_b = $ibx->over->get_thread('replace@example.com');
107         is_deeply([sort map { $_->{mid} } @$thread_b],
108                 [sort map { $_->{mid} } @$thread_a], 'threading preserved');
109
110         if (my $srch = $ibx->search) {
111                 for my $q ('f:streisand', 's:confidential', 'malibu') {
112                         my $msgs = $srch->query($q);
113                         is_deeply($msgs, [], "no match for $q");
114                 }
115                 my @ok = ('f:redactor', 's:redacted', 'nothing to see');
116                 if ($opt->{pre}) {
117                         push @ok, 'm:1@example.com', 'm:2@example.com',
118                                 's:message2', 's:message1';
119                 }
120                 if ($opt->{post}) {
121                         push @ok, 'm:3@example.com', 'm:4@example.com',
122                                 's:message3', 's:message4';
123                 }
124                 for my $q (@ok) {
125                         my $msgs = $srch->query($q);
126                         ok($msgs->[0], "got match for $q");
127                 }
128         }
129
130         # check overview matches:
131         my %after = map {; delete($_->{blob}) => $_ } @{$ibx->recent};
132         my @before_blobs = keys %before;
133         foreach my $blob (@before_blobs) {
134                 delete $before{$blob} if delete $after{$blob};
135         }
136
137         is(scalar keys %before, 1, 'one unique blob from before left');
138         is(scalar keys %after, 1, 'one unique blob from after left');
139         foreach my $blob (keys %before) {
140                 is($git->check($blob), undef, 'old blob not found');
141                 my $smsg = $before{$blob};
142                 is($smsg->{subject}, 'confidential', 'before subject');
143                 is($smsg->{mid}, 'replace@example.com', 'before MID');
144         }
145         foreach my $blob (keys %after) {
146                 ok($git->check($blob), 'new blob found');
147                 my $smsg = $after{$blob};
148                 is($smsg->{subject}, 'redacted', 'after subject');
149                 is($smsg->{mid}, 'replace@example.com', 'before MID');
150         }
151         @warn = ();
152         is($im->replace($orig, $repl), undef, 'no-op replace returns undef');
153         is($im->purge($orig), undef, 'no-op purge returns undef');
154         is_deeply(\@warn, [], 'no warnings on noop');
155 }
156
157 sub pad_msgs {
158         my ($im, @range) = @_;
159         for my $i (@range) {
160                 my $irt;
161                 if (ref($i) eq 'HASH') {
162                         ($i, $irt) = each %$i;
163                 }
164                 my $sec = sprintf('%0d', $i);
165                 my $mime = PublicInbox::MIME->new(<<EOF);
166 From: foo\@example.com
167 To: test\@example.com
168 Message-ID: <$i\@example.com>
169 Date: Fri, 02, Jan 1970 00:00:$sec +0000
170 Subject: message$i
171
172 message number$i
173 EOF
174
175                 if (defined($irt)) {
176                         $mime->header_set('References', "<$irt>");
177                 }
178
179                 $im->add($mime);
180         }
181 }
182
183 my $opt = { pre => *pad_msgs };
184 test_replace(2, 'basic', {});
185 test_replace(2, 'basic', $opt);
186 test_replace(2, 'basic', $opt = { %$opt, post => *pad_msgs });
187 test_replace(2, 'basic', $opt = { %$opt, rotate_bytes => 1 });
188
189 SKIP: {
190         require PublicInbox::Search;
191         PublicInbox::Search::load_xapian() or skip 'Search::Xapian missing', 8;
192         for my $l (qw(medium)) {
193                 test_replace(2, $l, {});
194                 $opt = { pre => *pad_msgs };
195                 test_replace(2, $l, $opt);
196                 test_replace(2, $l, $opt = { %$opt, post => *pad_msgs });
197                 test_replace(2, $l, $opt = { %$opt, rotate_bytes => 1 });
198         }
199 };
200
201 done_testing();