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