# Path: payway/accounts/models.py
# class Account(RandomUIDAbstractModel):
#     user = models.ForeignKey(User, related_name='accounts', verbose_name=_('user'))
#     currency_code = models.CharField(_('currency code'), max_length=3, choices=CURRENCY_CHOICES, default=moneyed.RUB)
# 
#     class Meta:
#         verbose_name = _('account')
#         verbose_name_plural = _('accounts')
#         permissions = (('can_view_account_report', 'Can view account report'),)
#         db_table = 'payway_accounts'
# 
#     @property
#     def currency(self):
#         currency = None
#         if self.currency_code:
#             currency = get_currency(self.currency_code)
#         return currency
# 
#     def __unicode__(self):
#         return u"{0} {1}".format(self.uid, self.currency)
# 
#     def get_balance(self):
#         sum = Transaction.objects.filter(account=self, money_amount_currency=self.currency).sum_values()
#         return round_down(Money(sum, self.currency))
# 
#     def withdraw(self, money, allow_overdraft=False):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't withdraw a negative amount")
# 
#         if not allow_overdraft and (self.get_balance() - money) < Money(0, self.currency):
#             raise Overdraft
# 
#         return Transaction.objects.create(
#             account=self,
#             money_amount=money * '-1.0',
#         )
# 
#     def add(self, money):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't add a negative money amount")
# 
#         return Transaction.objects.create(
#             account=self,
#             money_amount=money,
#         )
# 
#     def transfer(self, money, to_account):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't transfer a negative money amount")
# 
#         self.withdraw(money)
# 
#         return Transaction.objects.create(
#             account=to_account,
#             money_amount=money,
#         )
# 
#     def assert_correct_currency(self, money):
#         if money.currency != self.currency:
#             raise ValueError("You can't add money with %s currency. Current currency %s" % (money.currency, self.currency))
# 
# Path: payway/webmoney/models.py
# class ResultResponsePayment(ResponsePayment):
# 
#     class Meta:
#         db_table = 'payway_webmoney_resultresponsepayment'
# 
#     payment_system = _('Webmoney')
# 
#     PAYMENT_MODE = Choices((0, _('real')), (1, _('test')))
# 
#     LMI_MODE = models.PositiveSmallIntegerField(choices=PAYMENT_MODE)
#     payee_purse =  models.ForeignKey(Purse, related_name='success_payments')
# 
#     LMI_SYS_INVS_NO = models.PositiveIntegerField()
#     LMI_SYS_TRANS_NO = models.PositiveIntegerField()
#     LMI_SYS_TRANS_DATE = models.DateTimeField()
#     LMI_PAYMENT_NO = models.PositiveIntegerField()
# 
#     LMI_PAYER_PURSE = models.CharField(max_length=13)
#     LMI_PAYER_WM = models.CharField(max_length=12)
#     LMI_HASH = models.CharField(max_length=255)
# 
#     LMI_CAPITALLER_WMID = models.CharField(max_length=12, blank=True)
#     LMI_WMCHECK_NUMBER = models.CharField(max_length=20, blank=True)
#     LMI_PAYMER_NUMBER = models.CharField(max_length=30, blank=True)
#     LMI_PAYMER_EMAIL = models.EmailField(blank=True)
#     LMI_TELEPAT_PHONENUMBER = models.CharField(max_length=30, blank=True)
#     LMI_TELEPAT_ORDERID = models.CharField(max_length=30, blank=True)
#     LMI_PAYMENT_DESC = models.CharField(max_length=255, blank=True)
#     LMI_LANG = models.CharField(max_length=10, blank=True)


from django.contrib.auth.models import User
from django.test import TestCase
from django.core.urlresolvers import reverse
from moneyed.classes import Money, RUB
from django_simptools.tests import AuthorizedViewTestCase
from payway.accounts.models import Account
from payway.webmoney.models import ResultResponsePayment
        from django.core.mail import outbox
    def test_prerequest(self):
        prerequest_data = {
            u'LMI_PREREQUEST': u'1',
            u'LMI_PAYMENT_DESC': u'admin payment for 1961380063 invoice',
            u'LMI_MODE': u'1',
            u'LMI_PAYMENT_AMOUNT': u'1.00',
            u'LMI_LANG': u'ru-RU',
            u'LMI_PAYMENT_NO': u'1961380063',
            u'LMI_PAYER_PURSE': u'R172474141621',
            u'LMI_PAYER_WM': u'326319216579',
            u'LMI_PAYEE_PURSE': u'R834617269654',
            u'LMI_DBLCHK': u'SMS'
        }
        url = reverse('webmoney_result')
        response = self.client.post(url, prerequest_data)
        self.failUnlessEqual(response.status_code, 200)
        self.assertContains(response, "YES")



    def test_result_response(self):
        success_payments_count = ResultResponsePayment.objects.count()
        account = Account.objects.filter(user=self.user)[0]
        balance_before = account.get_balance()

        url = reverse('webmoney_result')
        response = self.client.post(url, self.data)
        self.failUnlessEqual(response.status_code, 200)

        # payment response should be saved

# Path: payway/accounts/models.py
# class Account(RandomUIDAbstractModel):
#     user = models.ForeignKey(User, related_name='accounts', verbose_name=_('user'))
#     currency_code = models.CharField(_('currency code'), max_length=3, choices=CURRENCY_CHOICES, default=moneyed.RUB)
# 
#     class Meta:
#         verbose_name = _('account')
#         verbose_name_plural = _('accounts')
#         permissions = (('can_view_account_report', 'Can view account report'),)
#         db_table = 'payway_accounts'
# 
#     @property
#     def currency(self):
#         currency = None
#         if self.currency_code:
#             currency = get_currency(self.currency_code)
#         return currency
# 
#     def __unicode__(self):
#         return u"{0} {1}".format(self.uid, self.currency)
# 
#     def get_balance(self):
#         sum = Transaction.objects.filter(account=self, money_amount_currency=self.currency).sum_values()
#         return round_down(Money(sum, self.currency))
# 
#     def withdraw(self, money, allow_overdraft=False):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't withdraw a negative amount")
# 
#         if not allow_overdraft and (self.get_balance() - money) < Money(0, self.currency):
#             raise Overdraft
# 
#         return Transaction.objects.create(
#             account=self,
#             money_amount=money * '-1.0',
#         )
# 
#     def add(self, money):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't add a negative money amount")
# 
#         return Transaction.objects.create(
#             account=self,
#             money_amount=money,
#         )
# 
#     def transfer(self, money, to_account):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't transfer a negative money amount")
# 
#         self.withdraw(money)
# 
#         return Transaction.objects.create(
#             account=to_account,
#             money_amount=money,
#         )
# 
#     def assert_correct_currency(self, money):
#         if money.currency != self.currency:
#             raise ValueError("You can't add money with %s currency. Current currency %s" % (money.currency, self.currency))
# 
# Path: payway/webmoney/models.py
# class ResultResponsePayment(ResponsePayment):
# 
#     class Meta:
#         db_table = 'payway_webmoney_resultresponsepayment'
# 
#     payment_system = _('Webmoney')
# 
#     PAYMENT_MODE = Choices((0, _('real')), (1, _('test')))
# 
#     LMI_MODE = models.PositiveSmallIntegerField(choices=PAYMENT_MODE)
#     payee_purse =  models.ForeignKey(Purse, related_name='success_payments')
# 
#     LMI_SYS_INVS_NO = models.PositiveIntegerField()
#     LMI_SYS_TRANS_NO = models.PositiveIntegerField()
#     LMI_SYS_TRANS_DATE = models.DateTimeField()
#     LMI_PAYMENT_NO = models.PositiveIntegerField()
# 
#     LMI_PAYER_PURSE = models.CharField(max_length=13)
#     LMI_PAYER_WM = models.CharField(max_length=12)
#     LMI_HASH = models.CharField(max_length=255)
# 
#     LMI_CAPITALLER_WMID = models.CharField(max_length=12, blank=True)
#     LMI_WMCHECK_NUMBER = models.CharField(max_length=20, blank=True)
#     LMI_PAYMER_NUMBER = models.CharField(max_length=30, blank=True)
#     LMI_PAYMER_EMAIL = models.EmailField(blank=True)
#     LMI_TELEPAT_PHONENUMBER = models.CharField(max_length=30, blank=True)
#     LMI_TELEPAT_ORDERID = models.CharField(max_length=30, blank=True)
#     LMI_PAYMENT_DESC = models.CharField(max_length=255, blank=True)
#     LMI_LANG = models.CharField(max_length=10, blank=True)


# Path: payway/webmoney/tests/views.py
from django.contrib.auth.models import User
from django.test import TestCase
from django.core.urlresolvers import reverse
from moneyed.classes import Money, RUB
from django_simptools.tests import AuthorizedViewTestCase
from payway.accounts.models import Account
from payway.webmoney.models import ResultResponsePayment
        from django.core.mail import outbox

    def test_prerequest(self):
        prerequest_data = {
            u'LMI_PREREQUEST': u'1',
            u'LMI_PAYMENT_DESC': u'admin payment for 1961380063 invoice',
            u'LMI_MODE': u'1',
            u'LMI_PAYMENT_AMOUNT': u'1.00',
            u'LMI_LANG': u'ru-RU',
            u'LMI_PAYMENT_NO': u'1961380063',
            u'LMI_PAYER_PURSE': u'R172474141621',
            u'LMI_PAYER_WM': u'326319216579',
            u'LMI_PAYEE_PURSE': u'R834617269654',
            u'LMI_DBLCHK': u'SMS'
        }
        url = reverse('webmoney_result')
        response = self.client.post(url, prerequest_data)
        self.failUnlessEqual(response.status_code, 200)
        self.assertContains(response, "YES")



    def test_result_response(self):
        success_payments_count = ResultResponsePayment.objects.count()
        account = Account.objects.filter(user=self.user)[0]
        balance_before = account.get_balance()

        url = reverse('webmoney_result')
        response = self.client.post(url, self.data)
        self.failUnlessEqual(response.status_code, 200)

        # payment response should be saved

        self.assertEquals(success_payments_count + 1, ResultResponsePayment.objects.count())