## $Id$
##
## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import datetime
import re
import unittest
from zope.component import getUtilitiesFor, getSiteManager
from zope.interface import implements
from zope.interface.verify import verifyClass, verifyObject
from waeup.ikoba.payments.interfaces import (
    IPayment, STATE_UNPAID, STATE_PAID, STATE_FAILED,
    IPaymentGatewayService, IPaymentItem
    )
from waeup.ikoba.payments.payment import (
    Payment, get_payment_providers, PaymentItem,
    )


class HelperTests(unittest.TestCase):

    def tearDown(self):
        # unregister any IPaymentGatewayServices
        sm = getSiteManager(None)
        for name, util in getUtilitiesFor(IPaymentGatewayService):
            sm.unregisterUtility(util, name=name)

    def test_get_payment_providers_no_providers(self):
        # we can get a dict of all payment providers
        result = get_payment_providers()
        assert isinstance(result, dict)
        assert result == {}

    def test_get_payment_providers(self):
        # we get any payment providers registered
        sm = getSiteManager(None)

        class FakeUtil(object):
            implements(IPaymentGatewayService)

        fake_util = FakeUtil()
        sm.registerUtility(fake_util, name=u'some_name')
        result = get_payment_providers()
        assert isinstance(result, dict)
        assert result.keys() == ['some_name', ]
        assert result['some_name'] is fake_util


class PaymentTests(unittest.TestCase):

    def test_iface(self):
        # Payments fullfill any interface contracts
        obj = Payment()
        verifyClass(IPayment, Payment)
        verifyObject(IPayment, obj)

    def test_payment_id_unique(self):
        # we get unique payment ids
        p1, p2 = Payment(), Payment()
        id1, id2 = p1.payment_id, p2.payment_id
        assert id1 != id2

    def test_payment_id_format(self):
        # payment ids have a special format: "PAY_<32 hex digits>"
        id1 = Payment().payment_id
        assert isinstance(id1, basestring)
        assert re.match('PAY_[0-9a-f]{32}', id1)

    def test_initial_state_is_unpaid(self):
        # the initial state of payments is <unpaid>
        p1 = Payment()
        assert p1.state == STATE_UNPAID

    def test_approve(self):
        # we can approve payments
        p1 = Payment()
        p1.approve()
        assert p1.state == STATE_PAID
        assert p1.payment_date is not None
        assert isinstance(p1.payment_date, datetime.datetime)

    def test_approve_datetime_given(self):
        # we can give a datetime
        p1 = Payment()
        some_datetime = datetime.datetime(2014, 1, 1, 0, 0, 0)
        p1.approve(payment_date=some_datetime)
        assert p1.payment_date == some_datetime

    def test_approve_datetime_automatic(self):
        # if we do not give a datetime, current one will be used
        current = datetime.datetime.utcnow()
        p1 = Payment()
        p1.approve()
        assert p1.payment_date >= current

    def test_mark_failed(self):
        # we can mark payments as failed
        p1 = Payment()
        p1.mark_failed()
        assert p1.state == STATE_FAILED


class PaymentItemTests(unittest.TestCase):

    def test_iface(self):
        # PaymentItems fullfill any interface contracts
        obj = PaymentItem()
        verifyClass(IPaymentItem, PaymentItem)
        verifyObject(IPaymentItem, obj)
