#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_pycfdi
----------------------------------

Tests for `pycfdi` module.
"""

import pytest

from contextlib import contextmanager
from click.testing import CliRunner

from pycfdi import pycfdi
from pycfdi import cli

from .utils import SAMPLE_CFDI_DOC, SAMPLE_BASE64_CERT

import os
import random


__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))


@pytest.fixture
def response():
    """Sample pytest fixture.
    See more at: http://doc.pytest.org/en/latest/fixture.html
    """
    # import requests
    # return requests.get('https://github.com/audreyr/cookiecutter-pypackage')

def test_valid_cfdi_3_2(response):
    '''
        Assert a cfdi doc is valid
    '''
    cfdi = pycfdi.Cfdi(SAMPLE_CFDI_DOC, version='3.2')
    assert cfdi.is_valid() is True, 'CFDI should be valid'

def test_invalid_cfdi_3_2(response):
    '''
        Assert some cfdis are invalid
    '''
    cfdi = pycfdi.Cfdi({}, version='3.2')
    assert cfdi.is_valid() is False, 'CFDI should be invalid'
    assert cfdi.errors == {'Comprobante': ['required field']}

    cfdi = pycfdi.Cfdi({'Comprobante': {}}, version='3.2')
    assert cfdi.is_valid() is False, 'CFDI should be invalid'
    assert cfdi.errors == {
                'Comprobante': [
                    {
                        'Conceptos': ['required field'],
                        'Emisor': ['required field'],
                        'LugarExpedicion': ['required field'],
                        'Receptor': ['required field'],
                        'fecha': ['required field'],
                        'formaDePago': ['required field'],
                        'metodoDePago': ['required field'],
                        'subTotal': ['required field'],
                        'tipoDeComprobante': ['required field'],
                        'total': ['required field']
                    }
                ]
            }

def test_certificate_not_set():
    '''
        Assert a certificate is not set
    '''
    cfdi = pycfdi.Cfdi({})
    with pytest.raises(pycfdi.InvalidCertificateError) as excinfo:
        cfdi._get_base64_certificate()

def test_certificate_invalid():
    '''
        Assert a certificate is not a valid file
    '''
    cfdi = pycfdi.Cfdi({}, cer_filepath='/a/fake/path/{}.cer'.format(random.randint(1, 1000)))
    with pytest.raises(pycfdi.InvalidCertificateError) as excinfo:
        cfdi._get_base64_certificate()

def test_certificate_valid():
    '''
        Assert a certificate valid and correct
    '''
    base64_cert = ''.join(SAMPLE_BASE64_CERT.split())
    cer_filepath = os.path.join(__location__, 'sample_data/certificates/AAA010101AAA.cer')
    cfdi = pycfdi.Cfdi(SAMPLE_CFDI_DOC, cer_filepath=cer_filepath)
    assert cfdi._get_base64_certificate() == base64_cert, 'CFDI should be invalid'

def test_command_line_interface():
    pass

@@ -2848,27 +2848,34 @@
 e64_cert, 'C
-FDI
+ertificate
  should be i

correct assert message in text_certificate_valid