==================================
EDocument Peppol Peppyrus Scenario
==================================

Imports::

    >>> import os
    >>> import time
    >>> from decimal import Decimal
    >>> from unittest.mock import patch

    >>> from proteus import Model
    >>> from trytond.modules.account.tests.tools import create_chart, create_tax
    >>> from trytond.modules.company.tests.tools import create_company, get_company
    >>> from trytond.modules.edocument_peppol.edocument import Peppol
    >>> from trytond.tests.tools import activate_modules, assertTrue
    >>> from trytond.tools import file_open

    >>> FETCH_SLEEP, MAX_SLEEP = 1, 200

Patch Peppol::

    >>> _validate = patch.object(Peppol, '_validate').start()
    >>> render = patch.object(Peppol, 'render').start()
    >>> participant_scheme, participant_id = (
    ...     os.getenv('PEPPYRUS_PARTICIPANT_ID').split(':', 1))
    >>> participant_vat = os.getenv('PEPPYRUS_PARTICIPANT_VAT')
    >>> with file_open('edocument_peppol_peppyrus/tests/invoice.xml', mode='r') as fp:
    ...     render.return_value = (
    ...         fp.read()
    ...         .replace('PARTICIPANT_SCHEME', participant_scheme)
    ...         .replace('PARTICIPANT_ID', participant_id)
    ...         .replace('PARTICIPANT_VAT', participant_vat.upper())
    ...         .encode())

Activate modules::

    >>> config = activate_modules(
    ...     'edocument_peppol_peppyrus', create_company, create_chart)

    >>> Cron = Model.get('ir.cron')
    >>> Peppol = Model.get('edocument.peppol')
    >>> PeppolService = Model.get('edocument.peppol.service')

Setup company::

    >>> company = get_company()
    >>> _ = company.party.identifiers.new(type='eu_vat', code=participant_vat)
    >>> company.party.save()
    >>> company.currency.code = 'EUR'
    >>> company.currency.save()

Setup accounting::

    >>> tax = create_tax(Decimal('.1'))
    >>> tax.unece_code = 'VAT'
    >>> tax.unece_category_code = 'S'
    >>> tax.save()

Create a service::

    >>> peppol_service = PeppolService(sequence=1)
    >>> peppol_service.types = ['bis-billing-3']
    >>> peppol_service.service = 'peppyrus'
    >>> peppol_service.peppyrus_server = 'testing'
    >>> peppol_service.peppyrus_api_key = os.getenv('PEPPYRUS_API_KEY')
    >>> peppol_service.save()

Setup webhook::

    >>> peppol_service.click('peppyrus_new_identifier')
    >>> assertTrue(peppol_service.peppyrus_incoming_webhook)
    >>> assertTrue(peppol_service.peppyrus_outgoing_webhook)

Send out a Peppol document::

    >>> peppol = Peppol(direction='out')
    >>> peppol.type = 'bis-billing-3'
    >>> peppol.service = peppol_service
    >>> peppol.click('submit')
    >>> peppol.state
    'processing'
    >>> bool(peppol.transmission_id)
    True

Check Peppol status::

    >>> while peppol.state == 'processing':
    ...     peppol.click('update_status')
    >>> peppol.state
    'succeeded'

Fetch messages::

    >>> for _ in range(MAX_SLEEP):
    ...     cron, = Cron.find([
    ...             ('method', '=', 'edocument.peppol.service|peppyrus_fetch'),
    ...             ], limit=1)
    ...     cron.click('run_once')
    ...     if bool(Peppol.find([('direction', '=', 'in')])):
    ...         break
    ...     time.sleep(FETCH_SLEEP)
    >>> peppol, = Peppol.find([('direction', '=', 'in')], limit=1)
    >>> peppol.state
    'succeeded'
