## $Id: test_browser.py 12777 2015-03-16 16:33:56Z henrik $
##
## Copyright (C) 2015 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
##
"""
Test the payments-related UI components.
"""

import os
from decimal import Decimal
from waeup.ikoba.testing import FunctionalLayer
from waeup.ikoba.payments.payment import Payment
from waeup.ikoba.payments.interfaces import IPayer, IPayable
from waeup.ikoba.products.productoptions import ProductOption
from waeup.ikoba.customers.tests.test_browser import CustomersFullSetup


class PaymentsUITests(CustomersFullSetup):
    # Tests for payments related views and pages

    layer = FunctionalLayer

    def setup_payment(self):
        payer = IPayer(self.customer)
        payable = IPayable(self.contract)
        self.payment = Payment(payer, payable)
        self.payment.gateway_service = 'demo_creditcard'
        self.app['payments'][self.payment.payment_id] = self.payment
        self.payments_path = 'http://localhost/app/payments'

    def add_product_option(self, contract):
        prodoption = ProductOption()
        prodoption.title = u'Any product option'
        prodoption.fee = Decimal('88.8')
        prodoption.currency = 'EUR'
        contract.product_options = [prodoption, ]

    def test_manage_payments(self):

        self.add_product_option(self.contract)
        self.setup_payment()
        # Managers can access the pages of payments section
        # and can remove payments
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open('http://localhost/app')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.browser.getLink("Payments").click()
        self.assertEqual(self.browser.url, self.payments_path)
        self.browser.getLink("Manage").click()
        self.assertEqual(self.browser.url, self.payments_path + '/manage')
        # Payment can be found
        self.browser.getControl("Find payment(s)").click()
        self.assertTrue('Empty search string' in self.browser.contents)
        self.browser.getControl(name="searchtype").value = ['payment_id']
        self.browser.getControl(
            name="searchterm").value = self.payment.payment_id
        self.browser.getControl("Find payment(s)").click()
        self.assertTrue(
            '<a href="http://localhost/app/customers/K1000000/contracts/CON1">'
            in self.browser.contents)
        # Payments can be removed
        ctrl = self.browser.getControl(name='entries')
        ctrl.getControl(value=self.payment.payment_id).selected = True
        self.browser.getControl("Remove selected", index=0).click()
        self.assertTrue('Successfully removed' in self.browser.contents)
        # All actions are being logged
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'payments.log')
        logcontent = open(logfile).read()
        self.assertTrue(
            ('INFO - zope.mgr - payments.browser.'
             'PaymentsContainerManagePage - removed: %s')
            % self.payment.payment_id in logcontent)
