diff --git a/src/authentication.py b/src/authentication.py index 205c8af..b96a512 100644 --- a/src/authentication.py +++ b/src/authentication.py @@ -1,5 +1,5 @@ from src.database import Users, IntegrityError, DoesNotExist -from src.helper import string_in_range +from src.helpers import string_in_range from src.error import InputError from src.type_structure import * import hashlib diff --git a/src/config.py b/src/config.py index 5b870fc..f6b1b16 100644 --- a/src/config.py +++ b/src/config.py @@ -1,5 +1,5 @@ port = 8000 -base_url = f"0.0.0.0" +base_url = f"localhost" full_url = f"http://{base_url}:{port}/" diff --git a/src/constants.py b/src/constants.py index 6baa4a6..0e9bdc7 100644 --- a/src/constants.py +++ b/src/constants.py @@ -7,3 +7,8 @@ xsltproc = proc.new_xslt30_processor() SYNTAX_EXECUTABLE = xsltproc.compile_stylesheet(stylesheet_file="src/validation_artefacts/AUNZ-UBL-validation.xslt") PEPPOL_EXECUTABLE = xsltproc.compile_stylesheet(stylesheet_file="src/validation_artefacts/AUNZ-PEPPOL-validation.xslt") + +# The token below is a temporary token for Sprint 1. +# It will be replaced with a token that is generated by Auth endpoints. +ADMIN_TOKEN = "UG#&*GFUBIFBIUEB#&*FUB" + diff --git a/src/database.py b/src/database.py index 621c9b4..dd2e927 100644 --- a/src/database.py +++ b/src/database.py @@ -1,5 +1,6 @@ from peewee import * import os +from src.constants import ADMIN_TOKEN db = None if 'RDS_DB_NAME' in os.environ: @@ -58,7 +59,7 @@ class Reports(BaseModel): def to_json(self): return { "report_id": self.id, # type: ignore - "date_generated": self.date_generated, + "date_generated": str(self.date_generated), "invoice_name": self.invoice_name, "invoice_text": self.invoice_text, "invoice_hash": self.invoice_hash, @@ -109,7 +110,10 @@ def create_tables(): with db: db.create_tables(tables) -def clear_v1(): +def clear_v1(token: str): + if not token == ADMIN_TOKEN: + raise Exception("Only admins can clear the database") + with db: db.drop_tables(tables) db.create_tables(tables) diff --git a/src/export.py b/src/export.py index 6ae0e69..c09703a 100644 --- a/src/export.py +++ b/src/export.py @@ -1,13 +1,12 @@ from src.type_structure import * from src.database import Reports from bs4 import BeautifulSoup -from copy import copy, deepcopy -import json from html import escape from peewee import DoesNotExist from weasyprint import HTML -from io import BytesIO, StringIO +from io import StringIO, BytesIO import csv +from zipfile import ZipFile, ZIP_DEFLATED def export_json_report_v1(report_id: int): @@ -16,9 +15,9 @@ def export_json_report_v1(report_id: int): except DoesNotExist: raise Exception(f"Report with id {report_id} not found") - return report.to_json() + return Report(**report.to_json()) -def export_pdf_report_v1(report_id: int): +def export_pdf_report_v1(report_id: int) -> bytes: html = export_html_report_v1(report_id) pdf_bytes = HTML(string=html).write_pdf() @@ -161,12 +160,18 @@ def export_csv_report_v1(report_id: int): return csv_contents -def report_bulk_export_v1(report_ids, report_format) -> List: - report_format = report_format.lower() - print("Exporting reports") - if report_format == "json": - return [export_json_report_v1(report_id) for report_id in report_ids] - elif report_format == "html": - return [export_html_report_v1(report_id) for report_id in report_ids] - else: - raise Exception("Unknown report format") +def report_bulk_export_json_v1(report_ids) -> List: + return { + "reports": [export_json_report_v1(report_id) for report_id in report_ids] + } + +def report_bulk_export_pdf_v1(report_ids) -> BytesIO: + reports = BytesIO() + + with ZipFile(reports, 'w', ZIP_DEFLATED) as f: + for report_id in report_ids: + f.writestr(f"invoice_validation_report_{report_id}.pdf", export_pdf_report_v1(report_id)) + + reports.seek(0) + + return reports diff --git a/src/helper.py b/src/helper.py deleted file mode 100644 index 5bf511c..0000000 --- a/src/helper.py +++ /dev/null @@ -1,20 +0,0 @@ -import secrets -import sys -from PIL import Image - - -def string_in_range(min_len:int, max_len:int, input_str:str) -> bool: - ''' - This function checks if a string is within the ranges of min and max length. - - Arguments: - min_len (int) - Minimum length of string - max_len (int) - Maximum length of string - input_str (str) - Input string to check length - - Return Value: - Returns boolean to whether string is within range or not - ''' - - return len(input_str) >= min_len and len(input_str) <= max_len - diff --git a/src/helpers.py b/src/helpers.py index 580a9e0..85d3107 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -1,6 +1,5 @@ from tempfile import NamedTemporaryFile from src.type_structure import * -import requests def create_temp_file(invoice_text: str) -> str: tmp = NamedTemporaryFile(mode='w', delete=False) @@ -9,16 +8,17 @@ def create_temp_file(invoice_text: str) -> str: return tmp.name -def extract_text_from_invoice(invoice: Invoice) -> str: - if invoice.source == "url": - response = requests.get(invoice.data) - if response.status_code != 200: - raise Exception("Could not retrieve file from url") +def string_in_range(min_len:int, max_len:int, input_str:str) -> bool: + ''' + This function checks if a string is within the ranges of min and max length. - data = response.text - elif invoice.source == "text": - data = invoice.data - else: - raise Exception("Invalid source, please enter url or text") + Arguments: + min_len (int) - Minimum length of string + max_len (int) - Maximum length of string + input_str (str) - Input string to check length - return data + Return Value: + Returns boolean to whether string is within range or not + ''' + + return len(input_str) >= min_len and len(input_str) <= max_len diff --git a/src/invoice.py b/src/invoice.py index f10212d..81e05aa 100644 --- a/src/invoice.py +++ b/src/invoice.py @@ -1,4 +1,3 @@ -from typing import Dict from src.type_structure import * from src.database import Reports, DoesNotExist import requests @@ -6,10 +5,8 @@ def invoice_upload_text_v1(invoice_name: str, invoice_text: str): - report_id = generate_report(invoice_name, invoice_text) - return { - "report_id": report_id + "report_id": generate_report(invoice_name, invoice_text) } @@ -27,11 +24,9 @@ def invoice_upload_url_v1(invoice_name: str, invoice_url: str): } -def invoice_upload_file_v1(invoice_name: str, invoice_file): - report_id = generate_report(invoice_name, invoice_file.decode("utf-8")) - +def invoice_upload_file_v1(invoice_name: str, invoice_text: str): return { - "report_id": report_id + "report_id": generate_report(invoice_name, invoice_text) } def invoice_check_validity_v1(report_id: int) -> CheckValidReturn: @@ -42,8 +37,8 @@ def invoice_check_validity_v1(report_id: int) -> CheckValidReturn: return CheckValidReturn(is_valid=report.is_valid) -def invoice_generate_hash_v1(invoice: Invoice) -> str: - return "hash" +def invoice_generate_hash_v1(invoice: TextInvoice) -> str: + return {} -def invoice_file_upload_bulk_v1(invoices: List[Invoice]) -> List[int]: - return [generate_report(invoice.name, invoice.data) for invoice in invoices] +def invoice_upload_bulk_text_v1(invoices: List[TextInvoice]) -> ReportIDs: + return ReportIDs(report_ids=[generate_report(invoice.name, invoice.text) for invoice in invoices]) diff --git a/src/main.py b/src/main.py index 1586088..d208382 100644 --- a/src/main.py +++ b/src/main.py @@ -13,7 +13,30 @@ from io import BytesIO import uvicorn -app = FastAPI() + +description = """ +CHURROS VALIDATION API helps you validate **any** invoice! 🚀🚀 +""" + +tags_metadata = [ + { + "name": "invoice", + "description": "Uploading invoices to the API and generating reports." + }, + { + "name": "export", + "description": "Exporting reports to JSON, PDF, HTML or CSV." + }, + { + "name": "report", + "description": "Generate individual evaluations and manage your reports." + }, +] + +app = FastAPI(title="CHURROS VALIDATION API", + description=description, + version="0.0.1", + openapi_tags=tags_metadata) @app.exception_handler(500) async def validation_exception_handler(request: Request, exc: Exception): @@ -36,34 +59,45 @@ async def welcome(): async def health_check(): return health_check_v1() -@app.get("/auth_login/v1") -async def auth_login(email: str, password: str): - return auth_login_v1(email, password) +@app.post("/invoice/upload_file/v1", tags=["invoice"]) +async def invoice_upload_file(file: UploadFile = File(...)) -> ReportID: + invoice_text = await file.read() + return invoice_upload_file_v1(invoice_name=file.filename, invoice_text=invoice_text.decode("utf-8")) -@app.get("/auth_register/v1") -async def auth_register(email: str, password: str): - return auth_register_v1(email, password) +@app.post("/invoice/bulk_upload_file/v1", tags=["invoice"]) +async def invoice_bulk_upload_file(files: List[UploadFile] = File(...)) -> ReportIDs: + invoices = [] + + for file in files: + invoice_text = await file.read() + invoice = TextInvoice(name=file.filename, text=invoice_text.decode("utf-8")) + invoices.append(invoice) + + return invoice_upload_bulk_text_v1(invoices) -@app.post("/invoice/upload_text/v1") -async def invoice_upload_text(invoice: Invoice) -> Dict: - return invoice_upload_text_v1(invoice_name=invoice.name, invoice_text=invoice.data) +@app.post("/invoice/upload_text/v1", tags=["invoice"]) +async def invoice_upload_text(invoice: TextInvoice) -> ReportID: + return invoice_upload_text_v1(invoice_name=invoice.name, invoice_text=invoice.text) -@app.post("/invoice/upload_url/v1") -async def invoice_upload_url(invoice_name: str, invoice_url: str) -> Dict: - return invoice_upload_url_v1(invoice_name=invoice_name, invoice_url=invoice_url) +@app.post("/invoice/bulk_upload_text/v1", tags=["invoice"]) +async def invoice_upload_bulk_text(invoices: List[TextInvoice]) -> ReportIDs: + return invoice_upload_bulk_text_v1(invoices) -@app.post("/invoice/upload_file/v1") -async def invoice_upload_file(file: UploadFile = File(...)) -> Dict: - file_data = await file.read() - return invoice_upload_file_v1(invoice_name=file.filename, invoice_file=file_data) # type: ignore +@app.post("/invoice/upload_url/v1", tags=["invoice"]) +async def invoice_upload_url(invoice: RemoteInvoice) -> ReportID: + return invoice_upload_url_v1(invoice_name=invoice.name, invoice_url=invoice.url) -@app.get("/export/json_report/v1") -async def export_json_report(report_id: int): +@app.get("/export/json_report/v1", tags=["export"]) +async def export_json_report(report_id: int) -> Report: return export_json_report_v1(report_id) -@app.get("/export/pdf_report/v1") -async def export_pdf_report(report_id: int): - pdf_file = BytesIO(export_pdf_report_v1(report_id)) # type: ignore +@app.post("/export/bulk_json_reports/v1", tags=["export"]) +async def report_bulk_export_json(report_ids: List[int]) -> ReportList: + return report_bulk_export_json_v1(report_ids) + +@app.get("/export/pdf_report/v1", tags=["export"]) +async def export_pdf_report(report_id: int) -> StreamingResponse: + pdf_file = BytesIO(export_pdf_report_v1(report_id)) # Return the PDF as a streaming response headers = { @@ -72,72 +106,88 @@ async def export_pdf_report(report_id: int): } return StreamingResponse(pdf_file, headers=headers) -@app.get("/export/html_report/v1", response_class=HTMLResponse) -async def export_html_report(report_id: int): +@app.post("/export/bulk_pdf_reports/v1", tags=["export"]) +async def report_bulk_export_pdf(report_ids: List[int]) -> StreamingResponse: + reports_zip = report_bulk_export_pdf_v1(report_ids) + + return StreamingResponse( + reports_zip, + media_type="application/x-zip-compressed", + headers = { "Content-Disposition": f"attachment; filename=reports.zip"} + ) + +@app.get("/export/html_report/v1", response_class=HTMLResponse, tags=["export"]) +async def export_html_report(report_id: int) -> HTMLResponse: html_content = export_html_report_v1(report_id) return HTMLResponse(content=html_content, status_code=200) -@app.get("/export/csv_report/v1") -async def export_csv_report(report_id: int): +@app.get("/export/csv_report/v1", tags=["export"]) +async def export_csv_report(report_id: int) -> HTMLResponse: csv_contents = export_csv_report_v1(report_id) - response = Response(content=csv_contents, media_type='text/csv') + response = HTMLResponse(content=csv_contents, media_type='text/csv') response.headers['Content-Disposition'] = f'attachment; filename="invoice_validation_report_{report_id}.csv"' return response -@app.post("/report/wellformedness/v1") -async def report_wellformedness(invoice: Invoice) -> Evaluation: - return report_wellformedness_v1(invoice) +@app.post("/report/wellformedness/v1", tags=["report"]) +async def report_wellformedness(file: UploadFile = File(...)) -> Evaluation: + invoice_text = await file.read() + return report_wellformedness_v1(invoice_text=invoice_text.decode("utf-8")) -@app.post("/report/schema/v1") -async def report_schema(invoice: Invoice) -> Evaluation: - return report_schema_v1(invoice) +@app.post("/report/schema/v1", tags=["report"]) +async def report_schema(file: UploadFile = File(...)) -> Evaluation: + invoice_text = await file.read() + return report_schema_v1(invoice_text=invoice_text.decode("utf-8")) -@app.post("/report/syntax/v1") -async def report_syntax(invoice: Invoice) -> Evaluation: - return report_syntax_v1(invoice) +@app.post("/report/syntax/v1", tags=["report"]) +async def report_syntax(file: UploadFile = File(...)) -> Evaluation: + invoice_text = await file.read() + return report_syntax_v1(invoice_text=invoice_text.decode("utf-8")) -@app.post("/report/peppol/v1") -async def report_peppol(invoice: Invoice) -> Evaluation: - return report_peppol_v1(invoice) +@app.post("/report/peppol/v1", tags=["report"]) +async def report_peppol(file: UploadFile = File(...)) -> Evaluation: + invoice_text = await file.read() + return report_peppol_v1(invoice_text=invoice_text.decode("utf-8")) -@app.get("/report/list_all/v1") -async def report_list_all() -> List[int]: +@app.get("/report/list_all/v1", tags=["report"]) +async def report_list_all() -> ReportIDs: return report_list_all_v1() -@app.get("/report/list_by/v1") -async def report_list_by(order_by: OrderBy) -> List[int]: +@app.get("/report/list_by/v1", tags=["report"]) +async def report_list_by(order_by: OrderBy) -> ReportIDs: return report_list_by_v1(order_by) -@app.put("/report/change_name/v1") -async def report_change_name(report_id: int, new_name: str) -> Dict[None, None]: - return report_change_name_v1(report_id, new_name) - -@app.delete("/report/delete/v1") -async def report_delete(report_id: int) -> Dict[None, None]: - return report_delete_v1(report_id) - -@app.get("/report/check_validity/v1") +@app.get("/report/check_validity/v1", tags=["report"]) async def invoice_check_validity(report_id: int) -> CheckValidReturn: return invoice_check_validity_v1(report_id) -@app.post("/invoice/generate_hash/v1") -async def invoice_generate_hash(invoice: Invoice) -> str: - return invoice_generate_hash_v1(invoice) -@app.post("/invoice/file_upload_bulk/v1") -async def invoice_file_upload_bulk(invoices: List[Invoice]) -> List[int]: - return invoice_file_upload_bulk_v1(invoices) +### Below to be replaced with proper authentication system ### + +@app.put("/report/change_name/v2", include_in_schema=False) +async def report_change_name(token: str, report_id: int, new_name: str) -> Dict[None, None]: + return report_change_name_v1(token, report_id, new_name) + +@app.delete("/report/delete/v2", include_in_schema=False) +async def report_delete(token: str, report_id: int) -> Dict[None, None]: + return report_delete_v1(token, report_id) -@app.post("/report/bulk_export/v1") -async def report_bulk_export(report_ids: List[int], report_format: str) -> List: - return report_bulk_export_v1(report_ids, report_format) +@app.get("/auth_login/v2", include_in_schema=False) +async def auth_login(email: str, password: str): + return auth_login_v1(email, password) + +@app.get("/auth_register/v2", include_in_schema=False) +async def auth_register(email: str, password: str): + return auth_register_v1(email, password) +@app.post("/invoice/generate_hash/v2", include_in_schema=False) +async def invoice_generate_hash(invoice_text: str) -> str: + return invoice_generate_hash_v1(invoice_text) -@app.delete("/clear/v1") -async def clear(): - return clear_v1() +@app.delete("/clear/v1", include_in_schema=False) +async def clear(token: str): + return clear_v1(token) # ENDPOINTS ABOVE diff --git a/src/report.py b/src/report.py index 7ea13d4..57bc3ea 100644 --- a/src/report.py +++ b/src/report.py @@ -1,37 +1,33 @@ from src.type_structure import * from typing import Dict -from tempfile import NamedTemporaryFile -from os import unlink -from src.database import Users, Reports, Violations, Evaluations, db -from src.helpers import extract_text_from_invoice +from src.database import Reports from src.generation import generate_xslt_evaluation, generate_schema_evaluation, generate_wellformedness_evaluation from peewee import DoesNotExist +from src.constants import ADMIN_TOKEN -def report_wellformedness_v1(invoice: Invoice) -> Evaluation: - invoice_text = extract_text_from_invoice(invoice) +def report_wellformedness_v1(invoice_text: str) -> Evaluation: evaluation = generate_wellformedness_evaluation(invoice_text) return Evaluation(**evaluation.to_json()) -def report_schema_v1(invoice: Invoice) -> Evaluation: - invoice_text = extract_text_from_invoice(invoice) +def report_schema_v1(invoice_text: str) -> Evaluation: evaluation = generate_schema_evaluation(invoice_text) return Evaluation(**evaluation.to_json()) -def report_syntax_v1(invoice: Invoice) -> Evaluation: - data = extract_text_from_invoice(invoice) - evaluation = generate_xslt_evaluation("syntax", data) + +def report_syntax_v1(invoice_text: str) -> Evaluation: + evaluation = generate_xslt_evaluation("syntax", invoice_text) return Evaluation(**evaluation.to_json()) -def report_peppol_v1(invoice: Invoice) -> Evaluation: - data = extract_text_from_invoice(invoice) - evaluation = generate_xslt_evaluation("peppol", data) + +def report_peppol_v1(invoice_text: str) -> Evaluation: + evaluation = generate_xslt_evaluation("peppol", invoice_text) return Evaluation(**evaluation.to_json()) def report_list_all_v1() -> List[int]: - return [report.id for report in Reports.select()] + return ReportIDs(report_ids=[report.id for report in Reports.select()]) def report_list_by_v1(order_by: OrderBy) -> List[int]: if order_by.is_ascending: @@ -39,9 +35,12 @@ def report_list_by_v1(order_by: OrderBy) -> List[int]: else: order = getattr(Reports, order_by.table).desc() - return [report.id for report in Reports.select().order_by(order)] + return ReportIDs(report_ids=[report.id for report in Reports.select().order_by(order)]) -def report_change_name_v1(report_id: int, new_name: str) -> Dict[None, None]: +def report_change_name_v1(token: str, report_id: int, new_name: str) -> Dict[None, None]: + if not token == ADMIN_TOKEN: + raise Exception("Only admins can change the names of reports at the moment") + try: report = Reports.get_by_id(report_id) except DoesNotExist: @@ -52,7 +51,10 @@ def report_change_name_v1(report_id: int, new_name: str) -> Dict[None, None]: return {} -def report_delete_v1(report_id: int) -> Dict[None, None]: +def report_delete_v1(token: str, report_id: int) -> Dict[None, None]: + if not token == ADMIN_TOKEN: + raise Exception("Only admins can delete reports at the moment") + try: report = Reports.get_by_id(report_id) except DoesNotExist: @@ -62,12 +64,11 @@ def report_delete_v1(report_id: int) -> Dict[None, None]: return {} -def report_bulk_generate_v1(invoices: List[Invoice]) -> List[Report]: +def report_bulk_generate_v1(invoices: List[TextInvoice]) -> List[Report]: report = Report( report_id=0, date_generated="", invoice_name="", - invoice_text="", invoice_hash="", is_valid=True, total_warnings=0, @@ -80,7 +81,7 @@ def report_bulk_generate_v1(invoices: List[Invoice]) -> List[Report]: reports = [report] return reports -def report_bulk_export_v1(report_ids, report_format) -> List[ReportExport]: +def report_bulk_json_export_v1(report_ids) -> List[ReportExport]: export = ReportExport(url="", invoice_hash="") exports = [export for _ in report_ids] return exports diff --git a/src/type_structure.py b/src/type_structure.py index 75a7824..6bf2282 100644 --- a/src/type_structure.py +++ b/src/type_structure.py @@ -7,10 +7,13 @@ class OrderBy(BaseModel): table: Literal["date_generated", "invoice_name", "total_errors", "total_warnings"] is_ascending: bool -class Invoice(BaseModel): +class TextInvoice(BaseModel): name: str - source: str - data: str + text: str + +class RemoteInvoice(BaseModel): + name: str + url: str class Location(BaseModel): type: Literal["xpath", "line"] @@ -39,7 +42,6 @@ class Report(BaseModel): report_id: int date_generated: str invoice_name: str - invoice_text: str invoice_hash: str is_valid: bool total_warnings: int @@ -49,8 +51,14 @@ class Report(BaseModel): syntax_evaluation: Union[Evaluation, None] peppol_evaluation: Union[Evaluation, None] +class ReportList(BaseModel): + reports: List[Report] + class ReportID(BaseModel): report_id: int + +class ReportIDs(BaseModel): + report_ids: List[int] class ReportExport(BaseModel): url: str diff --git a/tests/authentication/login_test.py b/tests/authentication/login_test.py index c662770..a764556 100644 --- a/tests/authentication/login_test.py +++ b/tests/authentication/login_test.py @@ -1,4 +1,4 @@ -from tests.server_calls import auth_login_v1, auth_register_v1, clear_v1 +from tests.server_calls import auth_login_v2, auth_register_v2, clear_v1 """ ============================================================== @@ -10,18 +10,18 @@ def test_login_success(): clear_v1() # Register and login functions should return same id for same user - reg_return_value = auth_register_v1("test@test.com", "password") - login_return_value = auth_login_v1("test@test.com", "password") + reg_return_value = auth_register_v2("test@test.com", "password") + login_return_value = auth_login_v2("test@test.com", "password") print(login_return_value) assert reg_return_value["auth_user_id"] == login_return_value["auth_user_id"] def test_login_multiple_success(): clear_v1() - reg_return_value_1 = auth_register_v1("test@test.com", "password")["auth_user_id"] + reg_return_value_1 = auth_register_v2("test@test.com", "password")["auth_user_id"] # First user registered and logged in assert reg_return_value_1 - reg_return_value_2 = auth_register_v1("test1@test.com", "password")["auth_user_id"] + reg_return_value_2 = auth_register_v2("test1@test.com", "password")["auth_user_id"] # Second user registered and logged in assert reg_return_value_2 @@ -30,20 +30,20 @@ def test_login_multiple_success(): def test_login_incorrect_email(): clear_v1() - auth_register_v1("test@test.com", "password") - assert auth_login_v1("test2@test.com", "password")['detail'] == "Invalid input: No user with email test2@test.com." + auth_register_v2("test@test.com", "password") + assert auth_login_v2("test2@test.com", "password")['detail'] == "Invalid input: No user with email test2@test.com." def test_login_incorrect_email_and_password(): clear_v1() - auth_register_v1("test@test.com", "password") - assert auth_login_v1("test@test.com", "efef")['detail'] == "Invalid input: Incorrect password." + auth_register_v2("test@test.com", "password") + assert auth_login_v2("test@test.com", "efef")['detail'] == "Invalid input: Incorrect password." def test_login_incorrect_password(): clear_v1() # Password is incorrect - auth_register_v1("test@test.com", "password") - assert auth_login_v1("test@test.com", "eeffef")['detail'] == "Invalid input: Incorrect password." + auth_register_v2("test@test.com", "password") + assert auth_login_v2("test@test.com", "eeffef")['detail'] == "Invalid input: Incorrect password." # Password is incorrect (and empty) - auth_register_v1("test1@test.com", "password") - assert auth_login_v1("test1@test.com", "")['detail'] == "Invalid input: Incorrect password." \ No newline at end of file + auth_register_v2("test1@test.com", "password") + assert auth_login_v2("test1@test.com", "")['detail'] == "Invalid input: Incorrect password." \ No newline at end of file diff --git a/tests/authentication/register_test.py b/tests/authentication/register_test.py index d8748a4..99b7d4a 100644 --- a/tests/authentication/register_test.py +++ b/tests/authentication/register_test.py @@ -1,4 +1,4 @@ -from tests.server_calls import clear_v1, auth_register_v1 +from tests.server_calls import clear_v1, auth_register_v2 """ ============================================================== @@ -9,8 +9,8 @@ # Test single registers with valid emails def test_register_unique_id_valid(): clear_v1() - auth_user1 = auth_register_v1("test@test.com", "luciddreams14") - auth_user2 = auth_register_v1("test1@test.com", "luciddreams14") + auth_user1 = auth_register_v2("test@test.com", "luciddreams14") + auth_user2 = auth_register_v2("test1@test.com", "luciddreams14") print(auth_user1) # Testing if user ID is unique assert auth_user1["auth_user_id"] != auth_user2["auth_user_id"] @@ -19,44 +19,44 @@ def test_register_unique_id_valid(): # Test multiple registers def test_register_multiple_success(): clear_v1() - auth_user1 =auth_register_v1("test@test.com", "www.www")["auth_user_id"] - auth_user2 =auth_register_v1("test1@test.com", "lisbon2424")["auth_user_id"] - auth_user3 =auth_register_v1("test2@test.com", "janedoe")["auth_user_id"] - auth_user4 =auth_register_v1("test3@test.com", "knittingislife")["auth_user_id"] + auth_user1 =auth_register_v2("test@test.com", "www.www")["auth_user_id"] + auth_user2 =auth_register_v2("test1@test.com", "lisbon2424")["auth_user_id"] + auth_user3 =auth_register_v2("test2@test.com", "janedoe")["auth_user_id"] + auth_user4 =auth_register_v2("test3@test.com", "knittingislife")["auth_user_id"] assert auth_user1 != auth_user2 != auth_user3 != auth_user4 # Test Input errors for invalid email - failing regex match def test_register_invalid_email(): clear_v1() # Not matching regex after first character class - assert auth_register_v1("chocoalate", "covered")['detail'] == "Email is invalid!" + assert auth_register_v2("chocoalate", "covered")['detail'] == "Email is invalid!" # Doesn't have an @ - assert auth_register_v1("waterfordsgmail.com", "dasani2048")['detail'] == "Email is invalid!" + assert auth_register_v2("waterfordsgmail.com", "dasani2048")['detail'] == "Email is invalid!" # Fails last part of the regex - missing 2 alphabetic characters after '.' - assert auth_register_v1("test@test.c", "paperboy")['detail'] == "Email is invalid!" + assert auth_register_v2("test@test.c", "paperboy")['detail'] == "Email is invalid!" # No email given - assert auth_register_v1("", "paperboy")['detail'] == "Email is invalid!" + assert auth_register_v2("", "paperboy")['detail'] == "Email is invalid!" # Email is not unique - Raise Input Error def test_register_duplicate_email(): clear_v1() # Duplicate email example 1 - auth_register_v1("test@test.com", "iloveyou") - assert auth_register_v1("test@test.com", "iloveyou")['detail'] == "Invalid input: Email test@test.com is already taken." + auth_register_v2("test@test.com", "iloveyou") + assert auth_register_v2("test@test.com", "iloveyou")['detail'] == "Invalid input: Email test@test.com is already taken." # Duplicate email example 2 - only email is duplicated - auth_register_v1("test1@test.com", "dasani2048") - assert auth_register_v1("test1@test.com", "dasani")['detail'] == "Invalid input: Email test1@test.com is already taken." + auth_register_v2("test1@test.com", "dasani2048") + assert auth_register_v2("test1@test.com", "dasani")['detail'] == "Invalid input: Email test1@test.com is already taken." # Length of password is less than 6 characters def test_register_short_passwords(): clear_v1() - assert auth_register_v1("test@test.com", "2048")['detail'] == "Invalid input: Password is too short." + assert auth_register_v2("test@test.com", "2048")['detail'] == "Invalid input: Password is too short." - assert auth_register_v1("test1@test.com", "LoVer")['detail'] == "Invalid input: Password is too short." + assert auth_register_v2("test1@test.com", "LoVer")['detail'] == "Invalid input: Password is too short." - assert auth_register_v1("test2@test.com", "")['detail'] == "Invalid input: Password is too short." + assert auth_register_v2("test2@test.com", "")['detail'] == "Invalid input: Password is too short." diff --git a/tests/bulk/bulk_export_test.py b/tests/bulk/bulk_export_test.py index c1f3bf9..ce30432 100644 --- a/tests/bulk/bulk_export_test.py +++ b/tests/bulk/bulk_export_test.py @@ -1,7 +1,7 @@ from src.type_structure import * from tests.constants import VALID_INVOICE_TEXT from tests.helpers import invalidate_invoice, replace_part_of_string -from tests.server_calls import invoice_file_upload_bulk_v1, report_bulk_export_v1 +from tests.server_calls import export_bulk_json_reports_v1, invoice_upload_text_v1 """ ===================================== @@ -11,27 +11,27 @@ def test_bulk_export_valid(): data = VALID_INVOICE_TEXT - invoice_valid = Invoice(name="My Invoice", source="text", data=data) + invoice_valid = TextInvoice(name="My Invoice", source="text", text=data) data = invalidate_invoice(VALID_INVOICE_TEXT, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) data = invalidate_invoice(data, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) - invoice_schema = Invoice(name="My Invoice", source="text", data=data) + invoice_schema = TextInvoice(name="My Invoice", source="text", text=data) data = invalidate_invoice(VALID_INVOICE_TEXT, 'content', 'cbc:EndpointID', '', 'Not an ABN', 1) - invoice_peppol = Invoice(name="My Invoice", source="text", data=data) + invoice_peppol = TextInvoice(name="My Invoice", source="text", text=data) data = invalidate_invoice(VALID_INVOICE_TEXT, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 1) - invoice_syntax = Invoice(name="My Invoice", source="text", data=data) + invoice_syntax = TextInvoice(name="My Invoice", source="text", text=data) data = replace_part_of_string(VALID_INVOICE_TEXT, 2025, 2027, "id") - invoice_wellformedness = Invoice(name="My Invoice", source="text", data=data) + invoice_wellformedness = TextInvoice(name="My Invoice", source="text", text=data) - invoices = [invoice_valid, invoice_schema, invoice_peppol, invoice_syntax, invoice_wellformedness] + report_ids = [] + for invoice in [invoice_valid, invoice_schema, invoice_peppol, invoice_syntax, invoice_wellformedness]: + report_ids.append(invoice_upload_text_v1(invoice.name, invoice.text)["report_id"]) - report_ids = invoice_file_upload_bulk_v1(invoices) - - exports = report_bulk_export_v1(report_ids, "json") # type: ignore + invoices = export_bulk_json_reports_v1(report_ids)["reports"] # type: ignore # Checking that the number of exports returned is the same as the number of invoices inputted assert len(report_ids) == len(invoices) diff --git a/tests/bulk/bulk_upload_test.py b/tests/bulk/bulk_upload_test.py index 754903a..84bb0e5 100644 --- a/tests/bulk/bulk_upload_test.py +++ b/tests/bulk/bulk_upload_test.py @@ -1,7 +1,7 @@ from src.type_structure import * from tests.constants import VALID_INVOICE_TEXT from tests.helpers import invalidate_invoice, replace_part_of_string -from tests.server_calls import invoice_file_upload_bulk_v1 +from tests.server_calls import invoice_bulk_upload_file_v1 """ ===================================== @@ -9,28 +9,29 @@ ===================================== """ -def test_bulk_upload_valid(): - data = VALID_INVOICE_TEXT - invoice_valid = Invoice(name="My Invoice", source="text", data=data) +# def test_bulk_upload_valid(): +# data = VALID_INVOICE_TEXT +# invoice_valid = TextInvoice(name="My Invoice", source="text", text=data) - data = invalidate_invoice(VALID_INVOICE_TEXT, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) - data = invalidate_invoice(data, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) - invoice_schema = Invoice(name="My Invoice", source="text", data=data) +# data = invalidate_invoice(VALID_INVOICE_TEXT, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) +# data = invalidate_invoice(data, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) +# invoice_schema = TextInvoice(name="My Invoice", source="text", text=data) - data = invalidate_invoice(VALID_INVOICE_TEXT, 'content', 'cbc:EndpointID', '', 'Not an ABN', 1) - invoice_peppol = Invoice(name="My Invoice", source="text", data=data) +# data = invalidate_invoice(VALID_INVOICE_TEXT, 'content', 'cbc:EndpointID', '', 'Not an ABN', 1) +# invoice_peppol = TextInvoice(name="My Invoice", source="text", text=data) - data = invalidate_invoice(VALID_INVOICE_TEXT, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 1) - invoice_syntax = Invoice(name="My Invoice", source="text", data=data) +# data = invalidate_invoice(VALID_INVOICE_TEXT, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 1) +# invoice_syntax = TextInvoice(name="My Invoice", source="text", text=data) - data = replace_part_of_string(VALID_INVOICE_TEXT, 2025, 2027, "id") +# data = replace_part_of_string(VALID_INVOICE_TEXT, 2025, 2027, "id") - invoice_wellformedness = Invoice(name="My Invoice", source="text", data=data) +# invoice_wellformedness = TextInvoice(name="My Invoice", source="text", text=data) - invoices = [invoice_valid, invoice_schema, invoice_peppol, invoice_syntax, invoice_wellformedness] +# invoices = [invoice_valid, invoice_schema, invoice_peppol, invoice_syntax, invoice_wellformedness] - report_ids = invoice_file_upload_bulk_v1(invoices) +# # report_ids = invoice_bulk_upload_file_v1(invoices) +# report_ids = invoice_bulk_upload_file_v1("tests/example_files/AUInvoice.xml", "tests/example_files/AUInvoice.xml") - # checking that the number of report_ids returned is the same as the number of invoices inputted - assert len(report_ids) == len(invoices) +# # checking that the number of report_ids returned is the same as the number of invoices inputted +# assert len(report_ids) == 2 diff --git a/tests/export/csv_report_test.py b/tests/export/csv_report_test.py index 1e54078..058588b 100644 --- a/tests/export/csv_report_test.py +++ b/tests/export/csv_report_test.py @@ -11,9 +11,9 @@ # Testing that the report was generated properly and matches input data def test_csv_valid_invoice(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) + invoice = TextInvoice(name="My Invoice", text=VALID_INVOICE_TEXT) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report_bytes = export_csv_report_v1(report_id) assert report_bytes diff --git a/tests/export/html_report_test.py b/tests/export/html_report_test.py index 9a91cb1..db7a41d 100644 --- a/tests/export/html_report_test.py +++ b/tests/export/html_report_test.py @@ -11,9 +11,9 @@ # Testing that the report was generated properly and matches input data def test_html_valid_invoice(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) + invoice = TextInvoice(name="My Invoice", source="text", text=VALID_INVOICE_TEXT) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report_bytes = export_html_report_v1(report_id) assert report_bytes @@ -21,8 +21,8 @@ def test_html_valid_invoice(): def test_html_text_invalid_peppol_invoice(): data = invalidate_invoice(VALID_INVOICE_TEXT, 'content', 'cbc:EndpointID', '', 'Not an ABN', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + invoice = TextInvoice(name="My Invoice", source="text", text=data) + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report_bytes = export_html_report_v1(report_id) @@ -31,8 +31,8 @@ def test_html_text_invalid_peppol_invoice(): def test_html_text_invalid_wellformedness_invoice(): data = replace_part_of_string(VALID_INVOICE_TEXT, 2025, 2027, "id") - invoice = Invoice(name="My Invoice", source="text", data=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + invoice = TextInvoice(name="My Invoice", source="text", text=data) + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report_bytes = export_html_report_v1(report_id) assert report_bytes diff --git a/tests/export/json_report_test.py b/tests/export/json_report_test.py index 90f08fd..e90d524 100644 --- a/tests/export/json_report_test.py +++ b/tests/export/json_report_test.py @@ -13,9 +13,9 @@ def test_json_valid_invoice(): data = VALID_INVOICE_TEXT - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report = export_json_report_v1(report_id) report = Report(**report) @@ -70,14 +70,14 @@ def test_json_unique_id(): data = VALID_INVOICE_TEXT # Creating 2 invoices - invoice1 = Invoice(name="Invoice01", source="text", data=data) - invoice2 = Invoice(name="Invoice02", source="text", data=data) + invoice1 = TextInvoice(name="Invoice01", source="text", text=data) + invoice2 = TextInvoice(name="Invoice02", source="text", text=data) # Creating 2 reports - report_id1 = invoice_upload_text_v1(invoice1.name, invoice1.data)["report_id"] + report_id1 = invoice_upload_text_v1(invoice1.name, invoice1.text)["report_id"] report1 = export_json_report_v1(report_id1) report1 = Report(**report1) - report_id2 = invoice_upload_text_v1(invoice2.name, invoice2.data)["report_id"] + report_id2 = invoice_upload_text_v1(invoice2.name, invoice2.text)["report_id"] report2 = export_json_report_v1(report_id2) report2 = Report(**report2) @@ -98,9 +98,9 @@ def test_json_single_violation(): # Invalidating the currency code data = invalidate_invoice(data, "attrib", "cbc:Amount", "currencyID", "TEST", 1) - invoice = Invoice(name="Invoice Test", source="text", data=data) + invoice = TextInvoice(name="Invoice Test", source="text", text=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report = export_json_report_v1(report_id) report = Report(**report) @@ -150,9 +150,9 @@ def test_json_multiple_violations_same_rule(): data = invalidate_invoice(data, "content", "cbc:EndpointID", "", "Not an ABN 1", 1) data = invalidate_invoice(data, "content", "cbc:EndpointID", "", "Not an ABN 2", 2) - invoice = Invoice(name="Invoice Test", source="text", data=data) + invoice = TextInvoice(name="Invoice Test", source="text", text=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report = export_json_report_v1(report_id) report = Report(**report) @@ -202,9 +202,9 @@ def test_json_multiple_violations_different_rules(): data = invalidate_invoice(data, 'content', 'cbc:IdentificationCode', '', 'TEST', 1) data = invalidate_invoice(data, 'content', 'cbc:IdentificationCode', '', 'TEST', 2) - invoice = Invoice(name="Invoice Test", source="text", data=data) + invoice = TextInvoice(name="Invoice Test", source="text", text=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report = export_json_report_v1(report_id) report = Report(**report) @@ -250,9 +250,9 @@ def test_json_invalid_wellformedness(): # Removing a closing tag data = remove_part_of_string(data, 11530, 11540) - invoice = Invoice(name="Invoice Test", source="text", data=data) + invoice = TextInvoice(name="Invoice Test", source="text", text=data) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report = export_json_report_v1(report_id) report = Report(**report) diff --git a/tests/export/pdf_report_test.py b/tests/export/pdf_report_test.py index 6652f50..8262144 100644 --- a/tests/export/pdf_report_test.py +++ b/tests/export/pdf_report_test.py @@ -1,7 +1,6 @@ from src.type_structure import * from tests.server_calls import export_pdf_report_v1, invoice_upload_text_v1 from tests.constants import VALID_INVOICE_TEXT -from tests.helpers import remove_part_of_string, invalidate_invoice, clear_database """ ===================================== @@ -11,9 +10,9 @@ # Testing that the report was generated properly and matches input data def test_pdf_valid_invoice(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) + invoice = TextInvoice(name="My Invoice", source="text", text=VALID_INVOICE_TEXT) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report_bytes = export_pdf_report_v1(report_id) assert report_bytes diff --git a/tests/invoice/upload_file.py b/tests/invoice/upload_file_test.py similarity index 84% rename from tests/invoice/upload_file.py rename to tests/invoice/upload_file_test.py index 099d687..9b7ebdf 100644 --- a/tests/invoice/upload_file.py +++ b/tests/invoice/upload_file_test.py @@ -10,6 +10,6 @@ """ def test_upload_file_valid_invoice(): - response = invoice_upload_file_v1("My Invoice", "src/AUInvoice.xml") + response = invoice_upload_file_v1("tests/example_files/AUInvoice.xml") assert response['report_id'] >= 0 diff --git a/tests/invoice/upload_text_test.py b/tests/invoice/upload_text_test.py index c792709..17a2c41 100644 --- a/tests/invoice/upload_text_test.py +++ b/tests/invoice/upload_text_test.py @@ -10,8 +10,8 @@ """ def test_upload_text_valid_invoice(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) - response = invoice_upload_text_v1(invoice.name, invoice.data) + invoice = TextInvoice(name="My Invoice", source="text", text=VALID_INVOICE_TEXT) + response = invoice_upload_text_v1(invoice.name, invoice.text) assert response['report_id'] >= 0 def test_upload_text_invalid_invoice(): @@ -19,8 +19,8 @@ def test_upload_text_invalid_invoice(): # Invalidating the ABN, changing the content of the ABN data = invalidate_invoice(data, 'content', 'cbc:EndpointID', '', 'Not an ABN', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) - response = invoice_upload_text_v1(invoice.name, invoice.data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) + response = invoice_upload_text_v1(invoice.name, invoice.text) assert response['report_id'] >= 0 diff --git a/tests/invoice/upload_url_test.py b/tests/invoice/upload_url_test.py index 976270a..1d34c81 100644 --- a/tests/invoice/upload_url_test.py +++ b/tests/invoice/upload_url_test.py @@ -11,8 +11,8 @@ def test_upload_url_valid_invoice(): - invoice = Invoice(name="My Invoice", source="url", data="https://raw.githubusercontent.com/A-NZ-PEPPOL/A-NZ-PEPPOL-BIS-3.0/master/Message%20examples/AU%20Invoice.xml") - response = invoice_upload_url_v1(invoice.name, invoice.data) + invoice = RemoteInvoice(name="My Invoice", url="https://raw.githubusercontent.com/A-NZ-PEPPOL/A-NZ-PEPPOL-BIS-3.0/master/Message%20examples/AU%20Invoice.xml") + response = invoice_upload_url_v1(invoice.name, invoice.url) assert response['report_id'] >= 0 diff --git a/tests/report/change_name_test.py b/tests/report/change_name_test.py index 6b9bac8..5fb82a1 100644 --- a/tests/report/change_name_test.py +++ b/tests/report/change_name_test.py @@ -1,5 +1,5 @@ from src.type_structure import * -from tests.server_calls import report_change_name_v1, export_json_report_v1, invoice_upload_text_v1 +from tests.server_calls import report_change_name_v2, export_json_report_v1, invoice_upload_text_v1 from tests.constants import VALID_INVOICE_TEXT from tests.helpers import invalidate_invoice, remove_part_of_string @@ -11,15 +11,15 @@ # Testing that the report was generated properly and matches input data def test_change_name(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) - report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"] + invoice = TextInvoice(name="My Invoice", source="text", text=VALID_INVOICE_TEXT) + report_id = invoice_upload_text_v1(invoice.name, invoice.text)["report_id"] report = Report(**export_json_report_v1(report_id)) # Checking for the old name of the invoice assert report.invoice_name == "My Invoice" - report_change_name_v1(report_id, "New Name") + report_change_name_v2(report_id, "New Name") report = Report(**export_json_report_v1(report_id)) # Checking for the new name of the invoice diff --git a/tests/report/delete_test.py b/tests/report/delete_test.py index a97afb7..51b62f1 100644 --- a/tests/report/delete_test.py +++ b/tests/report/delete_test.py @@ -1,17 +1,17 @@ from src.type_structure import * -from tests.server_calls import report_delete_v1, export_json_report_v1, invoice_upload_text_v1 +from tests.server_calls import report_delete_v2, export_json_report_v1, invoice_upload_text_v1 from tests.constants import VALID_INVOICE_TEXT from tests.helpers import invalidate_invoice, remove_part_of_string """ ===================================== -/report/delete/v1 TESTS +/report/delete/v2 TESTS ===================================== """ def test_delete(): report_id = invoice_upload_text_v1("invoice", VALID_INVOICE_TEXT)["report_id"] - report_delete_v1(report_id) + report_delete_v2(report_id) assert export_json_report_v1(report_id)["code"] == 500 diff --git a/tests/report/list_all_test.py b/tests/report/list_all_test.py index fddf928..7b50c3c 100644 --- a/tests/report/list_all_test.py +++ b/tests/report/list_all_test.py @@ -5,15 +5,15 @@ """ ===================================== -/report/list_all/v1 TESTS +/report/list_all/v2 TESTS ===================================== """ def test_list_all_one_report(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) - invoice_upload_text_v1(invoice.name, invoice.data) + invoice = TextInvoice(name="My Invoice", source="text", text=VALID_INVOICE_TEXT) + invoice_upload_text_v1(invoice.name, invoice.text) - report_ids = report_list_all_v1() + report_ids = report_list_all_v1()["report_ids"] report = export_json_report_v1(report_ids[0]) report = Report(**report) @@ -23,12 +23,12 @@ def test_list_all_one_report(): def test_list_all_many_reports(): - invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT) - invoice_upload_text_v1(invoice.name, invoice.data) - invoice_upload_text_v1(invoice.name, invoice.data) - invoice_upload_text_v1(invoice.name, invoice.data) + invoice = TextInvoice(name="My Invoice", source="text", text=VALID_INVOICE_TEXT) + invoice_upload_text_v1(invoice.name, invoice.text) + invoice_upload_text_v1(invoice.name, invoice.text) + invoice_upload_text_v1(invoice.name, invoice.text) - report_ids = report_list_all_v1() + report_ids = report_list_all_v1()["report_ids"] assert len(report_ids) == 3 diff --git a/tests/report/list_by_test.py b/tests/report/list_by_test.py index 73ad066..7370757 100644 --- a/tests/report/list_by_test.py +++ b/tests/report/list_by_test.py @@ -5,7 +5,7 @@ """ ===================================== -/report/list_by/v1 TESTS +/report/list_by/v2 TESTS ===================================== """ @@ -14,8 +14,8 @@ def test_list_by_many_reports(): invoice_upload_text_v1("invoice1", VALID_INVOICE_TEXT) invoice_upload_text_v1("invoice3", VALID_INVOICE_TEXT) - report_ids1 = report_list_by_v1(OrderBy(table="invoice_name", is_ascending=True)) - report_ids2 = report_list_by_v1(OrderBy(table="invoice_name", is_ascending=False)) + report_ids1 = report_list_by_v1(OrderBy(table="invoice_name", is_ascending=True))["report_ids"] + report_ids2 = report_list_by_v1(OrderBy(table="invoice_name", is_ascending=False))["report_ids"] assert len(report_ids1) == len(report_ids2) == 3 assert report_ids1[0] == report_ids2[-1] diff --git a/tests/report/peppol_test.py b/tests/report/peppol_test.py index 5d7d6e2..867710a 100644 --- a/tests/report/peppol_test.py +++ b/tests/report/peppol_test.py @@ -13,7 +13,7 @@ def test_peppol_valid_invoice(): data = VALID_INVOICE_TEXT - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) peppol_evaluation = report_peppol_v1(invoice) peppol_evaluation = Evaluation(**peppol_evaluation) @@ -35,7 +35,7 @@ def test_peppol_single_violation(): # Invalidating the ABN, changing the content of the ABN data = invalidate_invoice(data, 'content', 'cbc:EndpointID', '', 'Not an ABN', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) peppol_evaluation = report_peppol_v1(invoice) peppol_evaluation = Evaluation(**peppol_evaluation) @@ -76,7 +76,7 @@ def test_peppol_multiple_violations_same_rule(): data = invalidate_invoice(data, 'content', 'cbc:EndpointID', '', 'Not an ABN 1', 1) data = invalidate_invoice(data, 'content', 'cbc:EndpointID', '', 'Not an ABN 2', 2) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) peppol_evaluation = report_peppol_v1(invoice) peppol_evaluation = Evaluation(**peppol_evaluation) @@ -111,7 +111,7 @@ def test_peppol_multiple_violations_different_rules(): data = invalidate_invoice(data, 'content', 'cbc:IssueDate', '', 'bad-date', 1) data = invalidate_invoice(data, 'content', 'cbc:IssueDate', '', 'bad-date', 2) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) peppol_evaluation = report_peppol_v1(invoice) peppol_evaluation = Evaluation(**peppol_evaluation) @@ -149,7 +149,7 @@ def test_peppol_warning_doesnt_invalidate_report(): # Invalidating the ABN data = invalidate_invoice(data, 'content', 'cbc:EndpointID', '', 'Not an ABN 1', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) peppol_evaluation = report_peppol_v1(invoice) peppol_evaluation = Evaluation(**peppol_evaluation) @@ -168,7 +168,7 @@ def test_peppol_fatal_error_invalidates_report(): # Changing the start date year to 2029 data = invalidate_invoice(data, 'content', 'cbc:IssueDate', '', 'bad-date', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) peppol_evaluation = report_peppol_v1(invoice) peppol_evaluation = Evaluation(**peppol_evaluation) diff --git a/tests/report/schema_test.py b/tests/report/schema_test.py index cf62bcd..e7db995 100644 --- a/tests/report/schema_test.py +++ b/tests/report/schema_test.py @@ -1,4 +1,4 @@ -from src.type_structure import Invoice, Evaluation +from src.type_structure import * from tests.server_calls import report_schema_v1 from tests.constants import VALID_INVOICE_TEXT from tests.helpers import * @@ -12,7 +12,7 @@ def test_schema_valid(): # Replacing the tags but making sure they are valid data = VALID_INVOICE_TEXT - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) schema_evaluation = report_schema_v1(invoice) schema_evaluation = Evaluation(**schema_evaluation) @@ -31,7 +31,7 @@ def test_schema_tag_name_invalid(): data = invalidate_invoice(VALID_INVOICE_TEXT, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) data = invalidate_invoice(data, "tag", "cac:BillingReference", "", "cac:BillingReferencee", 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) schema_evaluation = report_schema_v1(invoice) schema_evaluation = Evaluation(**schema_evaluation) @@ -61,7 +61,7 @@ def test_schema_tag_order_invalid(): # Invalidating the date data = invalidate_invoice(VALID_INVOICE_TEXT, "tag", "cbc:IssueDate", "", "cbc:DueDate", 1) data = invalidate_invoice(data, "tag", "cbc:DueDate", "", "cbc:IssueDate", 2) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) schema_evaluation = report_schema_v1(invoice) schema_evaluation = Evaluation(**schema_evaluation) @@ -90,7 +90,7 @@ def test_schema_tag_order_invalid(): def test_schema_date_type_invalid(): # Invalidating the date data = invalidate_invoice(VALID_INVOICE_TEXT, "content", "cbc:IssueDate", "", "totallyADate", 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) schema_evaluation = report_schema_v1(invoice) schema_evaluation = Evaluation(**schema_evaluation) @@ -122,7 +122,7 @@ def test_schema_tags_revalid(): data = invalidate_invoice(data, "content", "cbc:CopyIndicator", "", "true", 1) data = invalidate_invoice(data, "tag", "cbc:DueDate", "", "cbc:IssueDate", 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) schema_evaluation = report_schema_v1(invoice) schema_evaluation = Evaluation(**schema_evaluation) @@ -141,7 +141,7 @@ def test_schema_tags_multiple_errors_invalid(): # Also expects the following tag to be different data = invalidate_invoice(VALID_INVOICE_TEXT, "tag", "cbc:IssueDate", "", "cbc:CopyIndicator", 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) schema_evaluation = report_schema_v1(invoice) schema_evaluation = Evaluation(**schema_evaluation) diff --git a/tests/report/syntax_test.py b/tests/report/syntax_test.py index a1c08eb..707a34a 100644 --- a/tests/report/syntax_test.py +++ b/tests/report/syntax_test.py @@ -13,7 +13,7 @@ def test_syntax_valid_invoice(): data = VALID_INVOICE_TEXT - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) syntax_evaluation = report_syntax_v1(invoice) syntax_evaluation = Evaluation(**syntax_evaluation) @@ -34,7 +34,7 @@ def test_syntax_single_violation(): # Invalidating the currency code data = invalidate_invoice(data, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) syntax_evaluation = report_syntax_v1(invoice) syntax_evaluation = Evaluation(**syntax_evaluation) @@ -74,7 +74,7 @@ def test_syntax_multiple_violations_same_rule(): data = invalidate_invoice(data, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 1) data = invalidate_invoice(data, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 2) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) syntax_evaluation = report_syntax_v1(invoice) syntax_evaluation = Evaluation(**syntax_evaluation) @@ -109,7 +109,7 @@ def test_syntax_multiple_violations_different_rules(): data = invalidate_invoice(data, 'content', 'cbc:IdentificationCode', '', 'TEST', 1) data = invalidate_invoice(data, 'content', 'cbc:IdentificationCode', '', 'TEST', 2) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) syntax_evaluation = report_syntax_v1(invoice) syntax_evaluation = Evaluation(**syntax_evaluation) @@ -147,7 +147,7 @@ def test_syntax_warning_doesnt_invalidate_report(): # Violates [UBL-CR-003]-A UBL invoice should not include the ProfileExecutionID data = invalidate_invoice(data, 'tag', 'cbc:Note', '', 'cbc:ProfileExecutionID', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) syntax_evaluation = report_syntax_v1(invoice) syntax_evaluation = Evaluation(**syntax_evaluation) @@ -166,7 +166,7 @@ def test_syntax_fatal_error_invalidates_report(): # Adding "D" to the currency code to make it invalid data = invalidate_invoice(data, 'attrib', 'cbc:Amount', 'currencyID', 'TEST', 1) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) syntax_evaluation = report_syntax_v1(invoice) syntax_evaluation = Evaluation(**syntax_evaluation) diff --git a/tests/report/wellformedness_test.py b/tests/report/wellformedness_test.py index eb9e052..2408f83 100644 --- a/tests/report/wellformedness_test.py +++ b/tests/report/wellformedness_test.py @@ -11,7 +11,7 @@ def test_wellformedness_valid_invoice(): data = VALID_INVOICE_TEXT - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -30,7 +30,7 @@ def test_wellformedness_case_sensitive_tags_invalid(): # Invalidating the tags so that only one of the tags is capitalised data = replace_part_of_string(VALID_INVOICE_TEXT, 2025, 2027, "id") - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -60,7 +60,7 @@ def test_wellformedness_case_sensitive_tags_valid(): data = replace_part_of_string(VALID_INVOICE_TEXT, 2025, 2027, "id") data = replace_part_of_string(data, 2045, 2047, "id") - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -76,7 +76,7 @@ def test_wellformedness_case_sensitive_tags_valid(): def test_wellformedness_two_root_elements_invalid(): data = VALID_INVOICE_TEXT data = append_to_string(data, """Second root at the end""") - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -101,7 +101,7 @@ def test_wellformedness_two_root_elements_invalid(): def test_wellformedness_no_closing_tag_invalid(): data = VALID_INVOICE_TEXT data = remove_part_of_string(data, 11530, 11540) - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -128,7 +128,7 @@ def test_wellformedness_wrong_nesting_invalid(): data = VALID_INVOICE_TEXT data = remove_part_of_string(data, 11512, 11530) data = append_to_string(data, """""") - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -154,7 +154,7 @@ def test_wellformedness_wrong_nesting_invalid(): def test_wellformedness_valid_version_number_error(): data = VALID_INVOICE_TEXT - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformedness_evaluation = report_wellformedness_v1(invoice) wellformedness_evaluation = Evaluation(**wellformedness_evaluation) @@ -176,7 +176,7 @@ def test_wellformedness_invalid_version_number_error(): data = VALID_INVOICE_TEXT data = replace_part_of_string(data, 15, 16, '5') - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformedness_evaluation = report_wellformedness_v1(invoice) wellformedness_evaluation = Evaluation(**wellformedness_evaluation) @@ -204,7 +204,7 @@ def test_wellformedness_invalid_version_number_error(): def test_wellformedness_no_escape_for_special_char_invalid(): data = VALID_INVOICE_TEXT data = replace_part_of_string(data, 694, 695, "<") - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) @@ -234,7 +234,7 @@ def test_wellformedness_no_escape_for_special_char_invalid(): def test_wellformedness_escape_for_special_char_valid(): data = VALID_INVOICE_TEXT data = replace_part_of_string(data, 694, 695, "<") - invoice = Invoice(name="My Invoice", source="text", data=data) + invoice = TextInvoice(name="My Invoice", source="text", text=data) wellformed_evaluation = report_wellformedness_v1(invoice) wellformed_evaluation = Evaluation(**wellformed_evaluation) diff --git a/tests/server_calls.py b/tests/server_calls.py index 97e2295..a34473a 100644 --- a/tests/server_calls.py +++ b/tests/server_calls.py @@ -1,66 +1,44 @@ +from io import BytesIO import requests import json from src.config import full_url from src.type_structure import * +from src.constants import ADMIN_TOKEN -# Authentication endpoints - -def auth_register_v1(email: str, password: str) -> Server_call_return: - payload = { - "email": email, - "password": password - } - response = requests.get(full_url + 'auth_register/v1', params=payload) - - return json.loads(response.text) +# Invoice Endpoints +def invoice_upload_file_v1(invoice_filename: str) -> Server_call_return: + files = {"file": (invoice_filename, open(invoice_filename, 'rb'))} -def auth_login_v1(email: str, password: str) -> Server_call_return: - payload = { - "email": email, - "password": password - } - response = requests.get(full_url + 'auth_login/v1', params=payload) + response = requests.post(full_url + 'invoice/upload_file/v1', files=files) return json.loads(response.text) +def invoice_bulk_upload_file_v1(invoice_filename1: str, invoice_filename2: str) -> Server_call_return: + files = {"file1": (invoice_filename1, open(invoice_filename1, 'rb')), + "file2": (invoice_filename2, open(invoice_filename2, 'rb'))} -def auth_logout_v1(): - response = requests.get(full_url + 'auth_logout/v1') + response = requests.post(full_url + 'invoice/bulk_upload_file/v1', files=files) return json.loads(response.text) -# Invoice Endpoints - def invoice_upload_text_v1(invoice_name: str, invoice_text: str) -> Server_call_return: - payload = Invoice(name=invoice_name, source="text", data=invoice_text).dict() + payload = TextInvoice(name=invoice_name, text=invoice_text).dict() response = requests.post(full_url + 'invoice/upload_text/v1', json=payload) return json.loads(response.text) -def invoice_upload_url_v1(invoice_name: str, invoice_url: str) -> Server_call_return: +def invoice_bulk_upload_text_v1(invoices: List[TextInvoice]) -> Server_call_return: payload = { - "invoice_name": invoice_name, - "invoice_url": invoice_url + "invoices": [invoice.dict() for invoice in invoices] } - response = requests.post(full_url + 'invoice/upload_url/v1', params=payload) + response = requests.post(full_url + 'invoice/bulk_upload_text/v1', json=payload) return json.loads(response.text) -def invoice_upload_file_v1(invoice_name: str, invoice_filename) -> Server_call_return: - headers = { - "invoice_name": invoice_name - } - - files = {"file": (invoice_filename, open(invoice_filename, 'rb'))} - - response = requests.post(full_url + 'invoice/upload_file/v1', files=files, headers=headers) - - return json.loads(response.text) - -def invoice_file_upload_bulk_v1(invoices: List[Invoice]) -> Server_call_return: - payload = [invoice.dict() for invoice in invoices] - response = requests.post(full_url + 'invoice/file_upload_bulk/v1', json=payload) +def invoice_upload_url_v1(invoice_name: str, invoice_url: str) -> Server_call_return: + payload = RemoteInvoice(name=invoice_name, url=invoice_url).dict() + response = requests.post(full_url + 'invoice/upload_url/v1', json=payload) return json.loads(response.text) @@ -74,6 +52,12 @@ def export_json_report_v1(report_id: int) -> Server_call_return: return json.loads(response.text) +def export_bulk_json_reports_v1(report_ids) -> Server_call_return: + payload = report_ids + response = requests.post(full_url + 'export/bulk_json_reports/v1', json=payload) + + return json.loads(response.text) + def export_pdf_report_v1(report_id: int): payload = { "report_id": report_id @@ -82,6 +66,14 @@ def export_pdf_report_v1(report_id: int): return response.content +def export_bulk_pdf_reports_v1(report_ids) -> Server_call_return: + payload = { + "report_ids": report_ids + } + response = requests.get(full_url + 'export/bulk_pdf_reports/v1', params=payload) + + return json.loads(response.text) + def export_html_report_v1(report_id: int): payload = { "report_id": report_id @@ -100,30 +92,34 @@ def export_csv_report_v1(report_id: int): # Report Endpoints -def report_wellformedness_v1(invoice: Invoice) -> Server_call_return: - payload = invoice.dict() - response = requests.post(full_url + 'report/wellformedness/v1', json=payload) +def report_wellformedness_v1(invoice: TextInvoice) -> Server_call_return: + files = {"file": (invoice.name, BytesIO(invoice.text.encode()))} + + response = requests.post(full_url + 'report/wellformedness/v1', files=files) return json.loads(response.text) -def report_schema_v1(invoice: Invoice) -> Server_call_return: - payload = invoice.dict() - response = requests.post(full_url + 'report/schema/v1', json=payload) +def report_schema_v1(invoice: TextInvoice) -> Server_call_return: + files = {"file": (invoice.name, BytesIO(invoice.text.encode()))} + + response = requests.post(full_url + 'report/schema/v1', files=files) return json.loads(response.text) -def report_syntax_v1(invoice: Invoice) -> Server_call_return: - payload = invoice.dict() - response = requests.post(full_url + 'report/syntax/v1', json=payload) +def report_syntax_v1(invoice: TextInvoice) -> Server_call_return: + files = {"file": (invoice.name, BytesIO(invoice.text.encode()))} + + response = requests.post(full_url + 'report/syntax/v1', files=files) return json.loads(response.text) -def report_peppol_v1(invoice: Invoice) -> Server_call_return: - payload = invoice.dict() - response = requests.post(full_url + 'report/peppol/v1', json=payload) +def report_peppol_v1(invoice: TextInvoice) -> Server_call_return: + files = {"file": (invoice.name, BytesIO(invoice.text.encode()))} + + response = requests.post(full_url + 'report/peppol/v1', files=files) return json.loads(response.text) @@ -138,40 +134,61 @@ def report_list_by_v1(order_by: OrderBy) -> Server_call_return: return json.loads(response.text) -def report_delete_v1(report_id: int) -> Server_call_return: +def report_check_validity_v1(report_id: int) -> Server_call_return: + payload = { + "report_id": report_id + } + response = requests.get(full_url + 'report/check_validity/v1', params=payload) + + return json.loads(response.text) + +### Other Endpoints + +def report_delete_v2(report_id: int) -> Server_call_return: payload = { + "token": ADMIN_TOKEN, "report_id": report_id } - response = requests.delete(full_url + 'report/delete/v1', params=payload) + response = requests.delete(full_url + 'report/delete/v2', params=payload) return json.loads(response.text) -def report_change_name_v1(report_id: int, new_name: str) -> Server_call_return: +def report_change_name_v2(report_id: int, new_name: str) -> Server_call_return: payload = { + "token": ADMIN_TOKEN, "report_id": report_id, "new_name": new_name } - response = requests.put(full_url + 'report/change_name/v1', params=payload) + response = requests.put(full_url + 'report/change_name/v2', params=payload) return json.loads(response.text) -def report_check_validity_v1(report_id: int) -> Server_call_return: +# Authentication endpoints + +def auth_register_v2(email: str, password: str) -> Server_call_return: payload = { - "report_id": report_id + "email": email, + "password": password } - response = requests.get(full_url + 'report/check_validity/v1', params=payload) + response = requests.get(full_url + 'auth_register/v2', params=payload) return json.loads(response.text) -def report_bulk_export_v1(report_ids: List[int], report_format: str) -> Server_call_return: + +def auth_login_v2(email: str, password: str) -> Server_call_return: payload = { - "report_ids": report_ids, - "report_format": report_format + "email": email, + "password": password } - response = requests.get(full_url + 'report/bulk_export/v1', json=payload) + response = requests.get(full_url + 'auth_login/v2', params=payload) + return json.loads(response.text) -# Other Endpoints + +def auth_logout_v2(): + response = requests.get(full_url + 'auth_logout/v2') + + return json.loads(response.text) def health_check_v1(): response = requests.get(full_url + 'health_check/v1') @@ -180,7 +197,10 @@ def health_check_v1(): def clear_v1(): - response = requests.delete(full_url + 'clear/v1') + params = { + "token": ADMIN_TOKEN + } + response = requests.delete(full_url + 'clear/v1', params=params) return json.loads(response.text)