Punk-Push

 view release on metacpan or  search on metacpan

t/11-delivery.t  view on Meta::CPAN

use Punk ();
use Punk::Plugin::Push ();

BEGIN {
    plan skip_all => 'DBD::SQLite is needed to exercise the storage'
        unless eval { require DBD::SQLite; 1 };
}

# ---- a push service that never touches the network ------------------------
#
# Fetch's shape, as far as this plugin uses it: ->post returns something with
# ->get, and that yields a response with ->status.

{
    package TFake::Res;
    sub new    { my ($c, %a) = @_; bless {%a}, $c }
    sub status { $_[0]{status} }

    package TFake::Future;
    sub new { my ($c, %a) = @_; bless {%a}, $c }
    sub get {
        my ($self) = @_;
        die $self->{die} if $self->{die};
        return TFake::Res->new(status => $self->{status});
    }

    package TFake::UA;
    sub new { my ($c, %a) = @_; bless { calls => [], %a }, $c }
    sub post {
        my ($self, $url, %opt) = @_;
        push @{ $self->{calls} }, { url => $url, %opt };
        my $next = shift @{ $self->{plan} || [] };
        $next = { status => 201 } unless $next;
        return TFake::Future->new(%$next);
    }
    sub calls { @{ $_[0]{calls} } }
    sub last_call { $_[0]{calls}[-1] }
}

my ($PUB, $PRIV) = VAPID::generate_vapid_keys();
my $tmp = File::Temp->newdir;
our $DSN = "dbi:SQLite:dbname=$tmp/push.db";

{
    my $dir = Punk::Plugin::Push->shipped_dir;
    open my $fh, '<', "$dir/sqlite/deploy/push_subscriptions.sql" or die $!;
    my $sql = do { local $/; <$fh> };
    $sql =~ s/^\s*--.*$//mg;
    require DBI;
    my $dbh = DBI->connect($DSN, '', '', { RaiseError => 1, AutoCommit => 1 });
    for my $stmt (split /;/, $sql) {
        $stmt =~ s/\A\s+|\s+\z//g;
        next unless length $stmt;
        next if $stmt =~ /\A(?:BEGIN|COMMIT)\z/i;
        $dbh->do($stmt);
    }
    $dbh->disconnect;
}

our %OPTS = (subject => 'mailto:ops@example.com',
             public_key => $PUB, private_key => $PRIV,
             guard => sub { 1 });

eval <<'PERL' or die $@;
package PushSend;
use Punk;
host 'https://example.com';
database dsn => $main::DSN;
plugin 'Push' => { %main::OPTS };
1;
PERL

my $app = PushSend->punk_app;
PushSend->to_app;

# A real subscription: the receiver key has to be a genuine P-256 point, since
# the body is genuinely encrypted before the fake service ever sees it.
sub real_sub {
    my ($n, $host) = @_;
    $host ||= 'push.example.net';
    my $k = Crypt::PK::ECC->new;
    $k->generate_key('prime256v1');
    return {
        endpoint => "https://$host/p/$n",
        keys => { p256dh => encode_base64url($k->export_key_raw('public')),
                  auth   => encode_base64url(join '', map { chr rand 256 } 1..16) },
    };
}

sub fake {
    my (@plan) = @_;
    my $ua = TFake::UA->new(plan => [@plan]);
    Punk::Plugin::Push->config_for($app)->{ua} = $ua;
    return $ua;
}

# ---- what a send actually puts on the wire --------------------------------

{
    my $ua = fake({ status => 201 });
    Punk::Plugin::Push->store($app, 1, real_sub('a'));
    my ($r) = Punk::Plugin::Push->send($app, 1, { title => 'Hi', body => 'there' });

    ok($r->delivered, 'a 201 is delivered');
    is($r->status, 201, '  with the status reported');
    ok(!$r->pruned, '  and nothing pruned');

    my $call = $ua->last_call;
    is($call->{url}, 'https://push.example.net/p/a', 'posted to the endpoint');
    my $h = $call->{headers};
    like($h->{Authorization}, qr/^vapid t=\S+, k=\S+$/,
        'the RFC 8292 single-header Authorization');
    is($h->{'Content-Encoding'}, 'aes128gcm', 'aes128gcm');
    is($h->{'Content-Type'}, 'application/octet-stream', 'octet-stream');
    is($h->{TTL}, 2419200, 'the configured TTL');
    ok(!exists $h->{Urgency},
        'no Urgency header when it is normal - a header restating a default '
      . 'is noise on every request');
    ok(!exists $h->{Topic}, 'and no Topic unless one was given');

    # the body is the RFC 8188 record: 86-byte header, then the sealed record



( run in 0.649 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )