]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_altid.t
337303d9fc45bc22164850c34afe175737abd7dc
[public-inbox.git] / t / www_altid.t
1 # Copyright (C) 2020 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 Test::More;
5 use PublicInbox::TestCommon;
6 use PublicInbox::Inbox;
7 use PublicInbox::InboxWritable;
8 use PublicInbox::Config;
9 use PublicInbox::Spawn qw(which spawn);
10 which('sqlite3') or plan skip_all => 'sqlite3 binary missing';
11 require_mods(qw(DBD::SQLite HTTP::Request::Common Plack::Test URI::Escape
12         Plack::Builder IO::Uncompress::Gunzip));
13 use_ok($_) for qw(Plack::Test HTTP::Request::Common);
14 require_ok 'PublicInbox::Msgmap';
15 require_ok 'PublicInbox::AltId';
16 require_ok 'PublicInbox::WWW';
17 my ($inboxdir, $for_destroy) = tmpdir();
18 my $aid = 'xyz';
19 my $spec = "serial:$aid:file=blah.sqlite3";
20 if ('setup') {
21         my $opts = {
22                 inboxdir => $inboxdir,
23                 name => 'test',
24                 -primary_address => 'test@example.com',
25         };
26         my $ibx = PublicInbox::Inbox->new($opts);
27         $ibx = PublicInbox::InboxWritable->new($ibx, 1);
28         my $im = $ibx->importer(0);
29         my $mime = PublicInbox::Eml->new(<<'EOF');
30 From: a@example.com
31 Message-Id: <a@example.com>
32
33 EOF
34         $im->add($mime);
35         $im->done;
36         mkdir "$inboxdir/public-inbox" or die;
37         my $altid = PublicInbox::AltId->new($ibx, $spec, 1);
38         $altid->mm_alt->mid_set(1, 'a@example.com');
39 }
40
41 my $cfgpath = "$inboxdir/cfg";
42 open my $fh, '>', $cfgpath or die;
43 print $fh <<EOF or die;
44 [publicinbox "test"]
45         inboxdir = $inboxdir
46         address = test\@example.com
47         altid = $spec
48         url = http://example.com/test
49 EOF
50 close $fh or die;
51 my $cfg = PublicInbox::Config->new($cfgpath);
52 my $www = PublicInbox::WWW->new($cfg);
53 my $cmpfile = "$inboxdir/cmp.sqlite3";
54 my $client = sub {
55         my ($cb) = @_;
56         my $res = $cb->(POST("/test/$aid.sql.gz"));
57         is($res->code, 200, 'retrieved gzipped dump');
58         IO::Uncompress::Gunzip::gunzip(\($res->content) => \(my $buf));
59         pipe(my ($r, $w)) or die;
60         my $cmd = ['sqlite3', $cmpfile];
61         my $pid = spawn($cmd, undef, { 0 => $r });
62         print $w $buf or die;
63         close $w or die;
64         is(waitpid($pid, 0), $pid, 'sqlite3 exited');
65         is($?, 0, 'sqlite3 loaded dump');
66         my $mm_cmp = PublicInbox::Msgmap->new_file($cmpfile);
67         is($mm_cmp->mid_for(1), 'a@example.com', 'sqlite3 dump valid');
68         $mm_cmp = undef;
69         unlink $cmpfile or die;
70 };
71 test_psgi(sub { $www->call(@_) }, $client);
72 SKIP: {
73         require_mods(qw(Plack::Test::ExternalServer), 4);
74         my $env = { PI_CONFIG => $cfgpath };
75         my $sock = tcp_server() or die;
76         my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
77         my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
78         my $td = start_script($cmd, $env, { 3 => $sock });
79         my ($h, $p) = ($sock->sockhost, $sock->sockport);
80         local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
81         Plack::Test::ExternalServer::test_psgi(client => $client);
82 }
83 done_testing;