Skip to content

Commit

Permalink
Merge pull request #74 from akretion/fingerprint
Browse files Browse the repository at this point in the history
add fingerprint test for new xsd releases
  • Loading branch information
rvalyi committed Jun 27, 2023
2 parents 6d43af5 + bde8920 commit 9f42442
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ test =
pytest
pytest-cov
tox
requests
beautifulsoup4

[flake8]
exclude = tests/*
Expand Down
14 changes: 14 additions & 0 deletions tests/fingerprint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"nfe": [
"https://www.nfe.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=BMPFMBoln3w=",
"c97f204b214a7ef70003dbce455dbac6"
],
"cte": [
"https://www.cte.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=0xlG1bdBass=",
"8931c120a3a6713e3b9235793b59da7e"
],
"nfse": [
"https://www.gov.br/nfse/pt-br/documentacao-tecnica",
"ef832452035e8b58e5519909bdd52f61"
]
}
1 change: 0 additions & 1 deletion tests/nfe/test_nfe_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import os
import sys
from os import path
from xmldiff import main
from nfelib.v4_00 import leiauteNFe_sub as nfe_sub
from nfelib.v4_00 import retEnviNFe as nfe
Expand Down
1 change: 0 additions & 1 deletion tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os
import sys
from os import path
import importlib
import inspect
from enum import EnumMeta
Expand Down
69 changes: 69 additions & 0 deletions tests/test_fingerprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import hashlib
import json
import logging
from os import environ, path
from pathlib import Path

import requests
from bs4 import BeautifulSoup

_logger = logging.getLogger(__name__)

HEADERS = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
}

PAGES = {
"nfe": (
"https://www.nfe.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=BMPFMBoln3w=",
"div",
"id",
"conteudoDinamico",
),
# "nfe_pynfe_webservices": ("https://raw.githubusercontent.com/TadaSoftware/PyNFe/main/pynfe/utils/webservices.py",),
# "nfe_pynfe_comunicacao": ("https://raw.githubusercontent.com/TadaSoftware/PyNFe/main/pynfe/processamento/comunicacao.py"),
"cte": (
"https://www.cte.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=0xlG1bdBass=",
"div",
"id",
"conteudoDinamico",
),
"nfse": (
"https://www.gov.br/nfse/pt-br/documentacao-tecnica",
"div",
"id",
"content-core",
),
# TODO MDF-e content seems loaded with XHR
# "mdfe": ("https://portal.fazenda.sp.gov.br/servicos/mdfe/Paginas/Downloads.aspx", "div", "class", "content"),
}


def test_fingerprint():
if environ.get("SKIP_FINGERPRINT"):
_logger.info("Skipping fingerprint test")
return True
fingerprint = {}
for code, scrap_params in PAGES.items():
url = scrap_params[0]
_logger.info("Fetching %s ..." % (url,))
if len(scrap_params) > 1:
page = requests.get(url, headers=HEADERS)
soup = BeautifulSoup(page.text, "html.parser")
if scrap_params[2] == "id":
fragment = soup.find(
scrap_params[1], {"id": scrap_params[3]}
).text.encode("utf-8")
else:
fragment = b"TODO" # MDF-e
else:
fragment = requests.get(url, headers=HEADERS).content # .decode('utf-8')
md5 = hashlib.md5(fragment).hexdigest()
fingerprint[code] = (url, md5)

_logger.info(fingerprint)
json_string = json.dumps(fingerprint, indent=4)
target = Path("tests/fingerprint.txt").read_text()
with open("tests/fingerprint.txt", "w") as outfile:
outfile.write(json_string)
assert target.strip() == json_string.strip()

0 comments on commit 9f42442

Please sign in to comment.