Refactoring

This commit is contained in:
2024-06-02 19:03:00 +02:00
parent 57b5b4ffab
commit 8c11166397
14 changed files with 401 additions and 322 deletions

View File

@ -8,25 +8,34 @@ from tests_examples import login_data
@pytest.fixture()
def app():
"""
Erstellt die App und konfiguriert sie zum Test-Modus
:yield app:
"""
app = init.create(testing=True)
app.config.update({
"TESTING": True,
})
routing.init_routes(app)
routing.initRoutes(app)
yield app
@pytest.fixture()
def client(app):
"""
Liefert einen Test-Client
:param app:
:return client:
"""
return app.test_client(use_cookies=True)
@pytest.fixture()
def runner(app):
return app.test_cli_runner()
def login(client):
"""
Hilfsfunktion, die den Client einloggt
:param client:
:return Bool (true if successful, false otherwise):
"""
client.post('/log-in', data=dict(email=login_data.email, password=login_data.password),
follow_redirects=True)
cookie = client.get_cookie("cnsc")
@ -37,6 +46,10 @@ def login(client):
def test_login(client):
"""
Testet die Login-Funktion
:param client:
"""
loginpage = client.get("/log-in", follow_redirects=True)
assert b"Einloggen" in loginpage.data
assert loginpage.status_code == 200
@ -51,6 +64,10 @@ def test_login(client):
def test_kurssetup(client):
"""
Testet die Konfiguration eines Kurses
:param client:
"""
if login(client):
kurspage = client.get("/set-up", follow_redirects=True)
assert kurspage.status_code == 200
@ -70,14 +87,18 @@ def test_kurssetup(client):
def test_semestersetup(client):
"""
Testet die Konfiguration eines Semesters
:param client:
"""
if login(client):
semesterpage = client.get("/set-up/semester", follow_redirects=True)
assert semesterpage.status_code == 200
semesterpage_html = BeautifulSoup(semesterpage.text, "lxml")
semesterform = semesterpage_html.find("form")
semesterform_action = semesterform.get("action")
semesterform_options = semesterform.find_all("option")
nextpage = client.post(semesterform_action, data=dict(sem=semesterform_options[-1].get("value")),
semesterpageHTML = BeautifulSoup(semesterpage.text, "lxml")
semesterform = semesterpageHTML.find("form")
semesterformAction = semesterform.get("action")
semesterformOptions = semesterform.find_all("option")
nextpage = client.post(semesterformAction, data=dict(sem=semesterformOptions[-1].get("value")),
follow_redirects=True)
assert nextpage.status_code == 200
assert b"Willkommen, " in nextpage.data
@ -86,30 +107,38 @@ def test_semestersetup(client):
def test_noten(client):
"""
Testet das Abrufen der Noten aus zwei verschiedenen Semestern
:param client:
"""
if login(client):
notenpage = client.get("/theorie/noten", follow_redirects=True)
assert notenpage.status_code == 200
notenpage_html = BeautifulSoup(notenpage.text, "lxml")
notenpage_heading = notenpage_html.find("h1")
notenpage_form = notenpage_html.find("form")
notenpage_action = notenpage_form.get("action")
notenpage_selection = notenpage_form.find("select")
notenpage_options = notenpage_selection.find_all("option")
notenpage_semester = "Not found!"
notenpageHTML = BeautifulSoup(notenpage.text, "lxml")
notenpageHeading = notenpageHTML.find("h1")
notenpageForm = notenpageHTML.find("form")
notenpageAction = notenpageForm.get("action")
notenpageSelection = notenpageForm.find("select")
notenpageOptions = notenpageSelection.find_all("option")
notenpageSemester = "Not found!"
nextpage = "Not found!"
for i in notenpage_options:
for i in notenpageOptions:
if i.get("selected") == "":
notenpage_semester = i.text[:-1]
notenpageSemester = i.text[:-1]
else:
nextpage = i.get("value")
assert notenpage_semester.encode("utf-8") in notenpage_heading.encode("utf-8")
nextpage = client.post(notenpage_action, data=dict(sem=nextpage), follow_redirects=True)
assert notenpageSemester.encode("utf-8") in notenpageHeading.encode("utf-8")
nextpage = client.post(notenpageAction, data=dict(sem=nextpage), follow_redirects=True)
assert nextpage.status_code == 200
else:
assert False
def test_logout(client):
"""
Testet die Logout-Funktion
:param client:
"""
if login(client):
loginpage = client.get("/log-out", follow_redirects=True)
assert loginpage.status_code == 200

View File

@ -93,6 +93,7 @@ async def checkUser_async():
"""
# noinspection DuplicatedCode
async with httpx.AsyncClient() as s:
# noinspection DuplicatedCode
content = (f'usrname={fmail}&pass={fpw}&ARGUMENTS=clino%2Cusrname%2Cpass%2Cmenuno%2Cmenu_type%2Cbrowser'
f'%2Cplatform&APPNAME=CampusNet&PRGNAME=LOGINCHECK')
response = await s.post(url=url, headers=headers, content=content)
@ -167,6 +168,7 @@ async def getResults_async(token, cookie, resl):
async with httpx.AsyncClient() as s:
response = await s.get(url=url + "?APPNAME=CampusNet&PRGNAME=COURSERESULTS&ARGUMENTS=-N" + token +
",-N000307," + ",-N" + resl, headers=headers)
# noinspection DuplicatedCode
html = BeautifulSoup(response.content.decode("utf-8"), 'lxml')
table = html.find('table', attrs={"class": "nb list"})
body = table.find("tbody")
@ -183,7 +185,7 @@ async def getResults_async(token, cookie, resl):
col[2] = i
i += 1
vorlist += [col[1:4]]
# noinspection DuplicatedCode
extrakurse = await asyncio.gather(*tasks, return_exceptions=True)
for i in vorlist:
@ -192,6 +194,7 @@ async def getResults_async(token, cookie, resl):
i[e] = extrakurse[i[e]]
return vorlist[:-1]
# noinspection DuplicatedCode
async def getPruefung_async(s, url):
response = await s.get("https://dualis.dhbw.de" + url, headers=headers)