import json

from django.core.urlresolvers import reverse
from django.test.utils import override_settings
from mock import patch
from moneyed.classes import EUR, XOF, Money

from bluebottle.test.utils import BluebottleTestCase

from .factory_models import VitepayOrderPaymentFactory, VitepayPaymentFactory

vitepay_settings = {
    'MERCHANT_ACCOUNTS': [
        {
            'merchant': 'vitepay',
            'currency': 'XOF',
            'api_key': '123',
            'api_secret': '123456789012345678901234567890123456789012345678901234567890',
            'payment_url': 'https://api.vitepay.com/v1/prod/payments'
        }
    ]
}


@patch('bluebottle.payments_vitepay.adapters.get_current_host',
        return_value='https://onepercentclub.com')
@override_settings(**vitepay_settings) 
class VitepayUpdateApiTest(BluebottleTestCase):
    def setUp(self):
        super(VitepayUpdateApiTest, self).setUp()
        pass

    def test_valid_update(self, get_current_host):
        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))
        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)

        data = {
            'success': 1,
            'authenticity': 'd2492ecd8d51ae72a43e5c6460e8da7ceae8195a',
            'order_id': 'opc-1'
        }
        response = self.client.post(
            reverse('vitepay-status-update'), data)

        payment.refresh_from_db()
        data = json.loads(response.content)
        self.assertEqual(data['status'], '1')
        self.assertEqual(payment.status, 'settled')

    def test_invalid_update(self, get_current_host):
        data = {}
        response = self.client.post(
            reverse('vitepay-status-update'), data)

        data = json.loads(response.content)
        self.assertEqual(data['status'], '0')
        self.assertEqual(data['message'], 'Order not found.')

    def test_invalid_auth(self, get_current_host):
        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))
        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)

        data = {
            'success': 1,
            'authenticity': 'blahblah',
            'order_id': 'opc-1'
        }
        response = self.client.post(
            reverse('vitepay-status-update'), data)

        data = json.loads(response.content)
        self.assertEqual(data['status'], '0')

    def test_mixed_status(self, get_current_host):
        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))
        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)

        data = {
            'success': 1,
            'failure': 1,
            'authenticity': 'd2492ecd8d51ae72a43e5c6460e8da7ceae8195a',
            'order_id': 'opc-1'
        }
        response = self.client.post(
            reverse('vitepay-status-update'), data)

        data = json.loads(response.content)
        self.assertEqual(data['status'], '0')

    def test_no_status(self, get_current_host):
        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))
        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)

        data = {
            'authenticity': 'd2492ecd8d51ae72a43e5c6460e8da7ceae8195a',
            'order_id': 'opc-1'
        }
        response = self.client.post(
            reverse('vitepay-status-update'), data)

        data = json.loads(response.content)
        self.assertEqual(data['status'], '0')

    def test_failed_status(self, get_current_host):
        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))
        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)

        data = {
            'failure': 1,
            'authenticity': 'd2492ecd8d51ae72a43e5c6460e8da7ceae8195a',
            'order_id': 'opc-1'
        }
        response = self.client.post(
            reverse('vitepay-status-update'), data)

        payment.refresh_from_db()
        data = json.loads(response.content)
        self.assertEqual(data['status'], '1')
        self.assertEqual(payment.status, 'failed')


@@ -912,72 +912,8 @@
 p()%0A
-        pass%0A%0A    def test_valid_update(self, get_current_host):
 %0A   
@@ -992,32 +992,37 @@
 , XOF))%0A        
+self.
 payment = Vitepa
@@ -1084,32 +1084,83 @@
 order_payment)%0A%0A
+    def test_valid_update(self, get_current_host):%0A
         data = %7B
@@ -1390,32 +1390,37 @@
  data)%0A%0A        
+self.
 payment.refresh_
@@ -1536,32 +1536,37 @@
 elf.assertEqual(
+self.
 payment.status, 
@@ -1946,186 +1946,8 @@
 t):%0A
-        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))%0A        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)%0A%0A
     
@@ -2303,186 +2303,8 @@
 t):%0A
-        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))%0A        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)%0A%0A
     
@@ -2715,186 +2715,8 @@
 t):%0A
-        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))%0A        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)%0A%0A
     
@@ -3079,186 +3079,8 @@
 t):%0A
-        order_payment = VitepayOrderPaymentFactory.create(amount=Money(2000, XOF))%0A        payment = VitepayPaymentFactory.create(order_id='opc-1', order_payment=order_payment)%0A%0A
     
@@ -3330,16 +3330,21 @@
         
+self.
 payment.
@@ -3476,16 +3476,21 @@
 rtEqual(
+self.
 payment.

test cleanup BB-8247