Punk-Push

 view release on metacpan or  search on metacpan

t/10-storage.t  view on Meta::CPAN

#!perl
use 5.010;
use strict;
use warnings;
use Test::More;
use File::Temp ();
use MIME::Base64 qw/encode_base64url/;
use VAPID ();
use Punk ();
use Punk::Plugin::Push ();

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

my ($PUB, $PRIV) = VAPID::generate_vapid_keys();

my $tmp = File::Temp->newdir;
my $db  = "$tmp/push.db";

# The table, from the shipped SQLite change - so the test deploys the DDL this
# distribution actually ships rather than a copy of it that could drift.
{
    my $sql = do {
        my $dir = Punk::Plugin::Push->shipped_dir
            or plan skip_all => 'the shipped sqitch project is not beside the module';
        open my $fh, '<', "$dir/sqlite/deploy/push_subscriptions.sql"
            or plan skip_all => "cannot read the shipped DDL: $!";
        local $/; <$fh>;
    };
    require DBI;
    my $dbh = DBI->connect("dbi:SQLite:dbname=$db", '', '',
                           { RaiseError => 1, AutoCommit => 1 });
    $sql =~ s/^\s*--.*$//mg;               # comments first, or they hide the ;
    for my $stmt (split /;/, $sql) {
        $stmt =~ s/\A\s+|\s+\z//g;
        next unless length $stmt;
        next if $stmt =~ /\A(?:BEGIN|COMMIT)\z/i;   # AutoCommit owns those
        $dbh->do($stmt);
    }
    $dbh->disconnect;
}

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

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

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

sub sub_for {
    my ($n) = @_;
    return {
        endpoint => "https://push.example.net/p/$n",
        keys     => { p256dh => encode_base64url("\x04" . ('k' x 64)),
                      auth   => encode_base64url('a' x 16) },
    };
}

# ---- storing ---------------------------------------------------------------

{
    ok(Punk::Plugin::Push->store($app, 7, sub_for('a'), 'Firefox/1'),
        'a subscription stores');
    my @rows = Punk::Plugin::Push->for_user($app, 7);
    is(scalar(@rows), 1, 'and comes back for its user');
    is($rows[0]{endpoint}, 'https://push.example.net/p/a', 'with its endpoint');
    is($rows[0]{user_agent}, 'Firefox/1', 'and its user agent');
    ok($rows[0]{created_at}, 'and a created_at');
}

# ---- re-subscribing updates rather than duplicating ------------------------
#
# A browser re-subscribing produces the same endpoint. Without the upsert every
# re-subscribe adds a row and one send fans out across duplicates.

{
    my $again = sub_for('a');
    $again->{keys}{p256dh} = encode_base64url("\x04" . ('m' x 64));
    Punk::Plugin::Push->store($app, 7, $again, 'Firefox/2');

    my @rows = Punk::Plugin::Push->for_user($app, 7);
    is(scalar(@rows), 1, 're-subscribing the same endpoint does not add a row');
    is($rows[0]{p256dh}, $again->{keys}{p256dh}, '  the keys are refreshed');
    is($rows[0]{user_agent}, 'Firefox/2',        '  and the user agent');
}

# ---- several devices -------------------------------------------------------

{
    Punk::Plugin::Push->store($app, 7, sub_for('b'), 'Chrome/1');
    my @rows = Punk::Plugin::Push->for_user($app, 7);
    is(scalar(@rows), 2, 'a second device is a second subscription');
}

{



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