Whitebox Tests

This commit is contained in:
2024-06-03 11:51:05 +02:00
parent 9fa7cfe867
commit 2027397e03
5 changed files with 70 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import urllib.error
from datetime import datetime, timedelta
from dateutil.parser import *
@ -6,6 +7,7 @@ import asyncio
import httpx
import icalendar
from dateutil.relativedelta import relativedelta
from icalendar import Calendar, Event
import json
@ -29,6 +31,7 @@ def writeToFile(filename, data):
:param data:
"""
with open(filename, 'w+') as file:
assert "BEGIN:VCALENDAR" in data.text
file.write(data.text)
file.close()
@ -51,7 +54,7 @@ def parseRaplaURL(url: str):
Konvertiert werden: http, www.; page=calendar \n
In: https; page=ical
:param url:
:return str:
:return [Status: int, URL: str]:
"""
rapla = url.find("rapla.")
if rapla == -1:
@ -75,31 +78,44 @@ def parseRaplaURL(url: str):
return 0, 0
async def getNewRapla(url: str):
async def getNewRapla(url: str, testing=None):
"""
Speichert den iCal eines Raplas auf dem Server. \n
Gibt Namen der Datei zurück. \n
TODO: Standort zu Dateibezeichner hinzufügen, um Konflikte zu vermeiden.
:param url:
:param opt. testing:
:return str:
"""
parsed = parseRaplaURL(url)
if testing:
assert "https" in parsed[1]
try:
assert "ical" in parsed[1]
except AssertionError:
assert "key" in parsed[1]
if parsed[0] == 0:
return 0
elif parsed[0] == 1:
url = parsed[1]
elif parsed[0] == 2:
return await buildICALfromKey(parsed[1], onlyUpdate=False)
return await buildICALfromKey(parsed[1], onlyUpdate=False, testing=True)
fileInURL = url.find("file=")
kurs = url[fileInURL + 5:].upper()
if url[-5:] != ".ical":
try:
async with httpx.AsyncClient() as s:
response = await fetchPlan(s, url)
if testing:
assert "Vollmer" in response.text
writeToFile(f"calendars/rapla{kurs}.ical", response)
except urllib.error.URLError:
return -1
writeKursToDB(kurs, url)
if testing:
assert "TINF22B3" in Rapla.query.filter(Rapla.name == "TINF22B3").first().file
return f"rapla{kurs}.ical"
else:
return url
@ -159,11 +175,12 @@ def raplaSchedule():
asyncio.run(refreshRapla())
async def buildICALfromKey(url, onlyUpdate):
async def buildICALfromKey(url, onlyUpdate, testing=True):
"""
Baut eine .ical-Datei aus der mitgegebenen URL, die ein Key-Parameter enthalten muss.
:param url:
:param onlyUpdate:
:param opt. testing:
:return Dateinamen:
"""
async with httpx.AsyncClient() as s:
@ -179,14 +196,18 @@ async def buildICALfromKey(url, onlyUpdate):
return 200
else:
kursname = page.text[page.text.find("<title>") + 7:page.text.find("</title>")]
if testing:
assert "TMT22B1" in kursname
if len(kursname) > 15:
return 0
start = "2024-08-01"
end = "2024-12-31"
start = f'{(datetime.now() - relativedelta(days=183)):%Y-%m-%d}'
end = f'{(datetime.now() + relativedelta(days=183)):%Y-%m-%d}'
payload = {"url": url, "start": start, "end": end}
req = await s.post(url="https://dh-api.paulmartin.cloud/rapla", data=payload,
headers={'Content-Type': 'application/x-www-form-urlencoded'}, timeout=10)
headers={'Content-Type': 'application/x-www-form-urlencoded'}, timeout=15)
jsonresp = json.loads(req.text)
if testing:
assert len(jsonresp) > 0
cal = Calendar()
cal.add('prodid', '-//Rapla//iCal Plugin//EN')
cal.add('version', '2.0')
@ -211,6 +232,8 @@ async def buildICALfromKey(url, onlyUpdate):
f.close()
if not onlyUpdate:
writeKursToDB(kursname, url)
if testing:
assert "TMT22B1" in Rapla.query.filter(Rapla.name == "TMT22B1").first().file
return f"rapla{kursname}.ical"
else:
return 200