Business-OnlinePayment-BitPay-Client

 view release on metacpan or  search on metacpan

t/client.t  view on Meta::CPAN

use strict;
use Test::More tests => 16;
use Test::Exception;
use Test::Builder::Tester;
use Test::Builder::Tester::Color;
use Test::Fake::HTTPD;
use JSON::Parse 'parse_json';
use JSON 'decode_json';
use Business::OnlinePayment::BitPay::KeyUtils;
use URI;
use lib 'lib';

BEGIN{
    my $tokensResponse = '{"data":[{"policies":[{"policy":"id", "method":"inactive", "params":["TfFkHfxvFx7wvLtnqrudrZRqPEuwCpE8X9L"]}], "token":"DCvbrN5iXzo4X4s4bgBiDjS624o72MVQfacoghGWhCqz", "dateCreated":1436809787782, "pairingExpiration":14368961...
    my $paircodeResponse = '{"data":[{"policies":[{"policy":"id", "method":"require", "params":["TfLAXsWtvWpSgqMYjJ1QvJEx2Bdob1mDeK4"]}], "resource":"Gd1q7mZJQU5zGoHAFsh1bmYEcWYQnzHZW6sjWatxtEr2", "token":"9nYwJ7KRRAcH1rXwJZRonXYrJJjcwAUowTC63UVLSYaC...
    my $invoiceResponse = '{"facade":"pos/invoice", "data": {"url":"https://paul.bp:8088/invoice?id=FAdAZK8FwmHyUZSzRvT9GW", "status":"new", "btcPrice":"0.035314", "btcDue":"0.035314", "price":10, "currency":"USD", "exRates":{"USD":283.1700000000001}...
    my $getTokens = '{"data": [{"pos/invoice":"7zjXTgeWaTj7xXNBA1qV7iBGGHJZNiLjPXNquHdk8BUN"}, {"pos":"HwC497kDGihPFUDiPfbedj6etbrVHyKQaiFHPGspYHHV"}]}';
    my $httpd = run_http_server {
        my $request = shift;
        my $decoded = decode_json($request->content) if $request->content;
        my $uri = $request->uri;
        my $method = $request->method;
        if ($uri->path eq '/postthis') {
        }
        return do {
            if ($method eq "POST") {
                if( $uri->path eq '/postthis' && $decoded->{"token"} && $decoded->{"facade"} ) {
                    [
                        200,
                        [ 'Content-Type' => 'application/json' ],
                        [ '{"facade":"correct", "token":"correct"}']
                    ]
                } elsif( $decoded->{"pairingCode"} && $uri->path eq '/tokens' ) {
                    [
                        200,
                        [ 'Content-Type' => 'application/json' ],
                        [ $paircodeResponse ]
                    ]
                } elsif( $uri->path eq '/tokens' ){
                    [
                        200,
                        [ 'Content-Type' => 'application/json' ],
                        [ $tokensResponse ]
                    ]
                } elsif( $uri->path eq '/invoices' ){
                    [
                        200,
                        [ 'Content-Type' => 'application/json' ],
                        [ $invoiceResponse ]
                    ]
                } else {
                    [
                        500,
                        [ 'Content-Type' => 'application/json' ],
                        [ '{"error":"something has certainly gone wrong"}' ]
                    ]
                }
            } elsif ($method eq "GET") {            
                if( $uri->path eq '/tokens' ){
                    [
                        200,
                        [ 'Content-Type' => 'application/json' ],
                        [ $getTokens ]
                    ]
                } elsif ($uri->path eq '/getthis') {
                    [
                        200,
                        [ 'Content-Type' => 'application/json' ],
                        [ '{"hello":"world"}']
                    ]
                } else {
                    [
                        500,
                        [ 'Content-Type' => 'application/json' ],
                        [ '{"error":"something has certainly gone wrong"}' ]
                    ]
                }
            }
        }
    };

    my $uri = URI->new( $httpd->endpoint );

    my $string = "astring";
    my $pem = Business::OnlinePayment::BitPay::KeyUtils::bpGeneratePem();
    my %opt = ("pem" => $pem);
    $opt{"apiUri"} = $uri;
    use_ok('Business::OnlinePayment::BitPay::Client');
    ok(Business::OnlinePayment::BitPay::Client->new(%opt), "accept new with pem passed");
    throws_ok(sub { Business::OnlinePayment::BitPay::Client->new() }, qr/no pem passed to constructor/, "catches no pem passed to constructor");
    my $client = Business::OnlinePayment::BitPay::Client->new(%opt);
    my @response = $client->pair_client;
    my $pairing = shift(shift(@response))->{'pairingCode'};



( run in 0.507 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )