# -*- coding: utf-8 -*-

from openerp.addons.payment.models.payment_acquirer import ValidationError
from openerp.addons.payment.tests.common import PaymentAcquirerCommon
from openerp.addons.payment_paypal.controllers.main import PaypalController
from openerp.tools import mute_logger

from lxml import objectify
import urlparse


class PaypalCommon(PaymentAcquirerCommon):

    def setUp(self):
        super(PaypalCommon, self).setUp()
        cr, uid = self.cr, self.uid
        self.base_url = self.registry('ir.config_parameter').get_param(cr, uid, 'web.base.url')

        # get the paypal account
        model, self.paypal_id = self.registry('ir.model.data').get_object_reference(cr, uid, 'payment_paypal', 'payment_acquirer_paypal')
        # tde+seller@openerp.com - tde+buyer@openerp.com - tde+buyer-it@openerp.com

        # some CC
        self.amex = (('378282246310005', '123'), ('371449635398431', '123'))
        self.amex_corporate = (('378734493671000', '123'))
        self.autralian_bankcard = (('5610591081018250', '123'))
        self.dinersclub = (('30569309025904', '123'), ('38520000023237', '123'))
        self.discover = (('6011111111111117', '123'), ('6011000990139424', '123'))
        self.jcb = (('3530111333300000', '123'), ('3566002020360505', '123'))
        self.mastercard = (('5555555555554444', '123'), ('5105105105105100', '123'))
        self.visa = (('4111111111111111', '123'), ('4012888888881881', '123'), ('4222222222222', '123'))
        self.dankord_pbs = (('76009244561', '123'), ('5019717010103742', '123'))
        self.switch_polo = (('6331101999990016', '123'))


class PaypalServer2Server(PaypalCommon):

    def test_00_tx_management(self):
        cr, uid, context = self.cr, self.uid, {}
        # be sure not to do stupid things
        paypal = self.payment_acquirer.browse(self.cr, self.uid, self.paypal_id, None)
        self.assertEqual(paypal.environment, 'test', 'test without test environment')

        res = self.payment_acquirer._paypal_s2s_get_access_token(cr, uid, [self.paypal_id], context=context)
        self.assertTrue(res[self.paypal_id] is not False, 'paypal: did not generate access token')

        tx_id = self.payment_transaction.s2s_create(
            cr, uid, {
                'amount': 0.01,
                'acquirer_id': self.paypal_id,
                'currency_id': self.currency_euro_id,
                'reference': 'test_reference',
                'partner_id': self.buyer_id,
            }, {
                'number': self.visa[0][0],