sunnyman's (sunnyman) wrote in changelog,
sunnyman's
sunnyman
changelog

[ljcom] r10759: OPSC-331: Unit tests for Payments API

Committer: vtroitsky
OPSC-331: Unit tests for Payments API
A   trunk/bin/test/payments-api/
A   trunk/bin/test/payments-api/README
A   trunk/bin/test/payments-api/t/
A   trunk/bin/test/payments-api/t/Test/
A   trunk/bin/test/payments-api/t/Test/LJ/
A   trunk/bin/test/payments-api/t/Test/LJ/API/
A   trunk/bin/test/payments-api/t/Test/LJ/API/Payments/
A   trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentCheckPaymentJSONTest.pm
A   trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentFetchTest.pm
A   trunk/bin/test/payments-api/t/Test/LJ/API/Payments/GetBalanceTest.pm
A   trunk/bin/test/payments-api/t/Test/LJ/API/Payments/ResultTest.pm
A   trunk/bin/test/payments-api/t/Test/LJ/Client/
A   trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcher/
A   trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcher/RequestTest.pm
A   trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcherTest.pm
Added: trunk/bin/test/payments-api/README
===================================================================
--- trunk/bin/test/payments-api/README	                        (rev 0)
+++ trunk/bin/test/payments-api/README	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,15 @@
+LJ Payment API UnitTests package
+
+Required Perl modules installed: 
+Test::Deep 
+Test::Class 
+Test::Exception 
+
+Required users & applications on test server: 
+   test - test uset 
+   payment - test application 
+
+Run:
+
+
+./run.t

Added: trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentCheckPaymentJSONTest.pm
===================================================================
--- trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentCheckPaymentJSONTest.pm	                        (rev 0)
+++ trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentCheckPaymentJSONTest.pm	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,216 @@
+package Test::LJ::API::Payments::AppPaymentCheckPaymentJSONTest;
+use Test::Class;
+use base qw(Test::Class);
+use Test::More;
+use Test::Deep;
+use Data::Dumper;
+use Test::Exception;
+
+require_ok( 'LJ::API::Payments::AppPayment' );
+require_ok( 'LJ::API::Payments::Error' );
+require_ok( 'LJ::API::Payments::Result' );
+
+sub test_check_payment_json : Test(3) {
+    # input payment json 
+
+    my $json_payment = '{   
+        "sandbox": true,
+        "items": [
+            { 
+                "skuId": "64goldcoinspack",
+                "count": 1,
+                "price": 100,
+                "description": "64-Gold coins pack"
+            },
+            {
+                "skuId": "pinkpig",
+                "count": 1,
+                "price": 20,
+                "description": "Pink Pig"
+            }
+        ],
+        "amount": 120,
+        "message": "Buy some money and stuff",
+        "parameters": { "coinspack64": 1, "pinkpig": 1 },
+        "paymentType": "payment",
+        "orderedTime": "12345342"
+    }';
+    my $result;
+    eval { $result = LJ::API::Payments::AppPayment->check_payment_json($json_payment); };
+    if($@) {
+        print Dumper($@);
+    }
+    ok($result);
+    cmp_deeply($result, {
+                            sandbox => 1,
+                            items => [  
+                                        {
+                                            skuId => '64goldcoinspack',    
+                                            count => 1,
+                                            price => 100,
+                                            description => '64-Gold coins pack'
+                                        },
+                                        {
+                                            skuId => 'pinkpig',    
+                                            count => 1,
+                                            price => 20,
+                                            description => 'Pink Pig'
+                                        }
+                                        ],
+                            amount => 120,
+                            message => 'Buy some money and stuff',
+                            parameters => { coinspack64 => 1, pinkpig => 1 },
+                            paymentType => 'payment',
+                            orderedTime => 12345342,
+                        }, 'Check correct Payment JSON checking');     
+}
+
+sub test_check_payment_json_syntax_error : Test(3) {
+    my $json_payment = '{ fdf: "fdf,  }';
+    my $result;
+    throws_ok( sub { $result = LJ::API::Payments::AppPayment->check_payment_json($json_payment) }, 'LJ::API::Payments::Error');
+
+    eval {
+        $result =  LJ::API::Payments::AppPayment->check_payment_json($json_payment); 
+    };
+    if($@) {
+        isa_ok($@, 'LJ::API::Payments::Error'); 
+        cmp_deeply($@, noclass( {
+                                    code => 2,
+                                    details => {
+                                            parse_error => re('.+'),
+                                        },
+                                }
+                                ), "Check that parse_error returned");
+
+        #diag(Dumper($json_payment), Dumper($@));
+    }
+}
+
+sub test_check_payment_json_incomplete : Test(3) {
+    my $json_payment = '{   
+        "sandbox": true,
+        "items": [
+            { 
+                "skuId": "64goldcoinspack",
+                "count": 1,
+                "price": 100,
+                "description": "64 gold coins pack"
+            },
+            {
+                "skuId": "pinkpig",
+                "count": 1,
+                "price": 20,
+                "description": "Pink Pig"
+            }
+        ],
+        "message": "Buy 65 gold coins pack and Pink Pig",
+        "parameters": { "coinspack": 65, "pinkpig": 1 },
+        "paymentType": "payment",
+        "orderedType": "12345342"
+    }';
+
+    my $result;
+    throws_ok( sub { $result = LJ::API::Payments::AppPayment->check_payment_json($json_payment) }, 'LJ::API::Payments::Error');
+    eval {
+        $result =  LJ::API::Payments::AppPayment->check_payment_json($json_payment); 
+    };
+    if($@) {
+        isa_ok($@, 'LJ::API::Payments::Error');  
+        cmp_deeply($@, noclass( {
+                                    code => 2,
+                                    details => {
+                                            field_absent => 'amount',
+                                        },
+                                }
+                                ), "Check that error about absent amount field returned");
+    }
+}
+
+sub test_check_payment_json_wrong_field_values : Test(3) {
+    my $json_payment = '{   
+        "sandbox": true,
+        "items": [
+            { 
+                "skuId": "64goldcoinspack",
+                "count": 1,
+                "price": 100,
+                "description": "64 gold coins pack"
+            },
+            {
+                "skuId": "pinkpig",
+                "count": 1,
+                "price": 20,
+                "description": "Pink Pig"
+            }
+        ],
+        "amount": 120,
+        "message": "Buy 65 gold coins pack and Pink Pig",
+        "paramaters": { "coinspack": 65, "pinkpig": 1 },
+        "paymentType": "debit",
+        "orderedTime": 12345342
+    }';
+
+    my $result;
+    throws_ok( sub { $result = LJ::API::Payments::AppPayment->check_payment_json($json_payment) }, 'LJ::API::Payments::Error');
+
+    eval {
+        $result =  LJ::API::Payments::AppPayment->check_payment_json($json_payment); 
+    };
+    if($@) {
+        isa_ok($@, 'LJ::API::Payments::Error'); 
+        cmp_deeply($@, noclass( {
+                                    code => 2,
+                                    details => {
+                                            field_error => 'paymentType',
+                                            wrong_value => 'debit',
+                                        },
+                                }
+                                ), "Check that error about wrong paymentType value");
+    }
+}
+
+sub test_check_payment_json_wrong_amount_sum : Test(3) {
+    my $json_payment = '{   
+        "sandbox": true,
+        "items": [
+            { 
+                "skuId": "64goldcoinspack",
+                "count": 2,
+                "price": 100,
+                "description": "64 gold coins pack"
+            },
+            {
+                "skuId": "pinkpig",
+                "count": 3,
+                "price": 20,
+                "description": "Pink Pig"
+            }
+        ],
+        "amount": 220,
+        "message": "Buy 2 of 65 gold coins pack and 3 Pink Pig",
+        "parameters": { "coinspack65": 2, "pinkpig": 3 },
+        "paymentType": "payment",
+        "orderedTime": 1234534232
+    }';
+
+    my $result;
+    throws_ok( sub { $result = LJ::API::Payments::AppPayment->check_payment_json($json_payment) }, 'LJ::API::Payments::Error');
+
+    eval {
+        $result =  LJ::API::Payments::AppPayment->check_payment_json($json_payment); 
+    };
+    if($@) {
+        isa_ok($@, 'LJ::API::Payments::Error'); 
+ #       diag(Dumper($@));
+        cmp_deeply($@, noclass( {
+                                    code => 2,
+                                    details => {
+                                            field_error => 'amount',
+                                            field_value => 220,
+                                        },
+                                } ), "Check that error about wrong amount field value");
+    }
+}
+
+1;

Added: trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentFetchTest.pm
===================================================================
--- trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentFetchTest.pm	                        (rev 0)
+++ trunk/bin/test/payments-api/t/Test/LJ/API/Payments/AppPaymentFetchTest.pm	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,34 @@
+package Test::LJ::API::Payments::AppPaymentFetchTest;
+use Test::Class;
+use base qw(Test::Class);
+use Test::More;
+use Test::Deep;
+use Data::Dumper;
+use Test::Exception;
+
+use LJ::API::Payments::AppPayment;
+
+sub test_fetch_payments : Test(6) {
+
+    my $user = LJ::load_user('test');  
+    ok($user);
+    isa_ok($user, 'LJ::User');
+
+    my $app = LJ::UserApps->get_application(key => 'payment');
+    ok($app);
+    isa_ok($app, 'LJ::UserApps::Application');
+
+    my $payments;
+    eval {
+        $payments  = LJ::API::Payments::AppPayment->fetch_payments(app => $app, user => $user, max => 5, type => 'payment');
+    };
+    if($@) {
+        diag(Dumper($@));
+    }
+    ok(scalar(@$payments) <= 5, 'ensure that we get no more that 5 objects');
+    cmp_deeply($payments, array_each(superhashof({
+                                            paymentType => 'payment',
+                                    })), 'ensure that we get array of payment type objects');
+}
+
+1;

Added: trunk/bin/test/payments-api/t/Test/LJ/API/Payments/GetBalanceTest.pm
===================================================================
--- trunk/bin/test/payments-api/t/Test/LJ/API/Payments/GetBalanceTest.pm	                        (rev 0)
+++ trunk/bin/test/payments-api/t/Test/LJ/API/Payments/GetBalanceTest.pm	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,23 @@
+package Test::LJ::API::Payments::GetBalanceTest;
+use Test::Class;
+use base qw(Test::Class);
+use Test::More;
+use Test::Deep;
+use Data::Dumper;
+
+require_ok( 'LJ::API::Payments' );
+require_ok( 'LJ::API::Payments::Result' );
+require_ok( 'LJ::API::Payments::Error' );
+
+sub test_getbalance : Test(3) {
+    my $user = LJ::load_user('test');
+    ok($user, "user test created");
+    my $result = LJ::API::Payments->get_user_balance($user);
+    isa_ok($result, 'LJ::API::Payments::Result');
+
+    cmp_deeply($result , noclass({ result => {
+                                            balance => re('\d+\.\d{3}'),
+                                            responseCode => 'OK',
+                                    }}));
+}
+1;

Added: trunk/bin/test/payments-api/t/Test/LJ/API/Payments/ResultTest.pm
===================================================================
--- trunk/bin/test/payments-api/t/Test/LJ/API/Payments/ResultTest.pm	                        (rev 0)
+++ trunk/bin/test/payments-api/t/Test/LJ/API/Payments/ResultTest.pm	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,18 @@
+package Test::LJ::API::Payments::ResultTest;
+use Test::Class;
+use base qw(Test::Class);
+use Test::More;
+use Test::Deep;
+use Data::Dumper;
+
+require_ok( 'LJ::API::Payments::Result' );
+use LJ::API::Payments::Result;
+
+sub test_result_create : Test(2) {    
+    my $result = LJ::API::Payments::Result->create( { test => 'ok', ttt => 'ttt' } );
+    isa_ok($result, 'LJ::API::Payments::Result');
+    my $json = $result->to_json();
+    is($json, '{"test":"ok","ttt":"ttt"}', 'ensure that we get proper JSON');
+}
+
+1;

Added: trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcher/RequestTest.pm
===================================================================
--- trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcher/RequestTest.pm	                        (rev 0)
+++ trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcher/RequestTest.pm	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,37 @@
+package Test::LJ::Client::SigningFetcher::RequestTest;
+use Test::Class;
+use base qw(Test::Class);
+use Test::More;
+use Test::Deep;
+use Data::Dumper;
+
+require_ok('LJ::Client::SigningFetcher::Request');
+
+use LJ::Client::SigningFetcher::Request;
+use LJ::UserApps;
+
+sub test_create_request : Test(5) {
+
+    my $app = LJ::UserApps->get_application(key => 'payment');
+    ok($app);
+    isa_ok($app, 'LJ::UserApps::Application');
+
+    my $api_keys = $app->api_keys();
+    ok($api_keys);
+
+
+    my $request = LJ::Client::SigningFetcher::Request->create(
+        request_method => 'POST',
+        consumer_key => $api_keys->{consumer_key},
+        consumer_secret => $api_keys->{consumer_secret},
+        request_url => $app->payments_callback,
+        params => {
+            order_id => 12345,
+        },
+    );
+    ok($request);
+    isa_ok($request, 'LJ::Client::SigningFetcher::Request');
+
+   # diag(Dumper($request));
+}
+1;

Added: trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcherTest.pm
===================================================================
--- trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcherTest.pm	                        (rev 0)
+++ trunk/bin/test/payments-api/t/Test/LJ/Client/SigningFetcherTest.pm	2011-07-12 05:27:01 UTC (rev 10759)
@@ -0,0 +1,44 @@
+package Test::LJ::Client::SigningFetcherTest;
+use Test::Class;
+use base qw(Test::Class);
+use Test::More;
+use Test::Deep;
+use Data::Dumper;
+
+require_ok('LJ::Client::SigningFetcher');
+
+use LJ::Client::SigningFetcher;
+use LJ::UserApps;
+
+sub test_make_request : Test(6) {
+    my $app = LJ::UserApps->get_application(key => 'payment');
+    ok($app);
+    isa_ok($app, 'LJ::UserApps::Application');
+
+    my $api_keys = $app->api_keys();
+    ok($api_keys);
+
+
+    my $request = LJ::Client::SigningFetcher::Request->create(
+        request_method => 'POST',
+        consumer_key => $api_keys->{consumer_key},
+        consumer_secret => $api_keys->{consumer_secret},
+        request_url => $app->payments_callback,
+        params => {
+            order_id => 12345,
+        },
+    );
+    ok($request);
+
+    my $result = LJ::Client::SigningFetcher->makeRequest($request);
+
+    ok($result);
+
+    diag($result->content);
+
+    ok($result->is_success, 'ensure that response is OK');
+
+#    diag(Dumper($result));
+
+}
+1;

Tags: ljcom, pm, sunnyman
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments