183 lines
6.5 KiB
Python
183 lines
6.5 KiB
Python
import pytest
|
|
from bs4 import BeautifulSoup
|
|
|
|
import fetchRAPLA
|
|
import routing
|
|
import init
|
|
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.initRoutes(app)
|
|
yield app
|
|
|
|
|
|
@pytest.fixture()
|
|
def client(app):
|
|
"""
|
|
Liefert einen Test-Client
|
|
:param app:
|
|
:return client:
|
|
"""
|
|
return app.test_client(use_cookies=True)
|
|
|
|
|
|
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")
|
|
try:
|
|
return len(cookie.value) == 32 # CNSC-Länge: 32 → Wenn der Cookie so lang ist, ist man erfolgreich eingeloggt.
|
|
except AttributeError:
|
|
return False
|
|
|
|
|
|
def test_login_blackbox(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
|
|
loginpage_html = BeautifulSoup(loginpage.text, "lxml")
|
|
login_form = loginpage_html.find("form")
|
|
login_action = login_form.get("action")
|
|
login_request = client.post(login_action, data=dict(email=login_data.email, password=login_data.password),
|
|
follow_redirects=True)
|
|
assert login_request.status_code == 200
|
|
cookie = client.get_cookie("cnsc")
|
|
assert len(cookie.value) == 32 # CNSC-Länge: 32 → Wenn der Cookie so lang ist, ist man erfolgreich eingeloggt.
|
|
|
|
|
|
def test_kurssetup_blackbox(client):
|
|
"""
|
|
Testet die Konfiguration eines Kurses
|
|
:param client:
|
|
"""
|
|
if login(client):
|
|
kurspage = client.get("/set-up", follow_redirects=True)
|
|
assert kurspage.status_code == 200
|
|
kurspage_html = BeautifulSoup(kurspage.text, "lxml")
|
|
kursbutton = kurspage_html.find("form", {"id": "manualForm"})
|
|
kursbutton_action = kursbutton.get("action")
|
|
planpage = client.get(kursbutton_action, follow_redirects=True)
|
|
assert planpage.status_code == 200
|
|
planpage_html = BeautifulSoup(planpage.text, "lxml")
|
|
planpage_form = planpage_html.find("form", {"id": "manualForm"})
|
|
planpage_action = planpage_form.get("action")
|
|
set_request = client.post(planpage_action, data=dict(url=login_data.kurs_url), follow_redirects=True)
|
|
assert set_request.status_code == 200
|
|
assert b"Willkommen, " in set_request.data
|
|
else:
|
|
assert False
|
|
|
|
|
|
def test_semestersetup_blackbox(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
|
|
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
|
|
else:
|
|
assert False
|
|
|
|
|
|
def test_noten_blackbox(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
|
|
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 notenpageOptions:
|
|
if i.get("selected") == "":
|
|
notenpageSemester = i.text[:-1]
|
|
else:
|
|
nextpage = i.get("value")
|
|
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_blackbox(client):
|
|
"""
|
|
Testet die Logout-Funktion
|
|
:param client:
|
|
"""
|
|
if login(client):
|
|
loginpage = client.get("/log-out", follow_redirects=True)
|
|
assert loginpage.status_code == 200
|
|
assert b"Einloggen" in loginpage.data
|
|
cookie = client.get_cookie("cnsc")
|
|
assert len(cookie.value) != 32 # CNSC-Länge: 32 → CNSC darf ausgeloggt nicht gesetzt sein
|
|
else:
|
|
assert False
|
|
|
|
|
|
@pytest.mark.asyncio()
|
|
async def test_url_anweisung_whitebox(app):
|
|
"""
|
|
Testet einen Pfad des URL-Imports
|
|
:param app:
|
|
"""
|
|
with app.app_context():
|
|
raplaPage = await fetchRAPLA.getNewRapla("http://www.rapla.dhbw-karlsruhe.de/rapla?page=calendar&user="
|
|
"vollmer&file=tinf22b3", True) #HTML
|
|
assert "TINF22B3" in raplaPage
|
|
raplaFile = await fetchRAPLA.getNewRapla("http://rapla.dhbw-karlsruhe.de/rapla?page=ical&user=vollmer"
|
|
"&file=tinf22b3", True) #ICAL
|
|
assert "TINF22B3" in raplaFile
|
|
|
|
|
|
@pytest.mark.asyncio()
|
|
async def test_url_entscheidung_whitebox(app):
|
|
"""
|
|
Testet alle Pfade des URL-Imports, die mit einer fehlerfreien Datei enden
|
|
:param app:
|
|
"""
|
|
with app.app_context():
|
|
await test_url_anweisung_whitebox(app)
|
|
raplaFile = await fetchRAPLA.getNewRapla("https://rapla.dhbw-karlsruhe.de/rapla?key=5h7oySnUbC4A59dSScuZ"
|
|
"lPHhaNFS3OaaP-0UTlOEPu-NcWfZ-gMhnSpHZmYCPcIe", True) #ICAL
|
|
assert "TMB22" in raplaFile
|
|
raplaPage = await fetchRAPLA.getNewRapla("http://www.rapla.dhbw-karlsruhe.de/rapla?key=ah9tAVphi"
|
|
"caj4FqCtMVJchAs9fh0Dt89jA8Td4kEi21V0i2mlUEpycpIVw5jSY5T",
|
|
True) #HTML
|
|
assert "TMT22B1" in raplaPage
|