Skip to content

Commit

Permalink
Add tests against dropped events and left template strings
Browse files Browse the repository at this point in the history
In one of my previous commits I had a template string in the
description because I forgot to put an f into the f-string. Now
there are tests that make sure this never happens again. It is now
also impossible to drop events in the mapping process as the tests
verify that the new calendar has as many events as the old one.
  • Loading branch information
flofriday committed Aug 25, 2023
1 parent e22010c commit 95039c3
Showing 1 changed file with 155 additions and 50 deletions.
205 changes: 155 additions & 50 deletions tests/test_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask.testing import FlaskClient
from icalendar import Calendar
import re


def get_test_calendar(lang: str = "de"):
Expand All @@ -10,6 +11,10 @@ def get_test_calendar(lang: str = "de"):
return cal


def calendar_event_cnt(cal: Calendar) -> int:
return sum([1 for c in cal.walk() if c.name == "VEVENT"])


def calendar_summaries(cal: Calendar) -> [str]:
return [c.get("summary") for c in cal.walk() if c.name == "VEVENT"]

Expand Down Expand Up @@ -50,100 +55,200 @@ def test_verify_success(client: FlaskClient, mocker):


def test_icalendar_de_success(client: FlaskClient, mocker):
mocker.patch("app.tiss.get_calendar", return_value=get_test_calendar(lang="de"))
testcal = get_test_calendar(lang="de")
mocker.patch("app.tiss.get_calendar", return_value=testcal)

response = client.get(
"/personal.ics?locale=de&token=justATestingTokenObviouslyNotReal"
)
assert response.status_code == 200

cal = Calendar.from_ical(response.data)
assert "PS VU" in calendar_summaries(cal)
assert "SEPS SE" in calendar_summaries(cal)
assert any(["Programmiersprachen" in d for d in calendar_descriptions(cal)])
assert not any(["Programming Languages" in d for d in calendar_descriptions(cal)])
assert all(["Raum:" in d for d in calendar_descriptions(cal)])
assert not any(["Room:" in d for d in calendar_descriptions(cal)])
assert any(["Stock:" in d for d in calendar_descriptions(cal)])
assert not any(["Floor:" in d for d in calendar_descriptions(cal)])
assert not any(["<b>" in d for d in calendar_descriptions(cal)])
summaries = calendar_summaries(cal)
descriptions = calendar_descriptions(cal)

# Number of events
assert calendar_event_cnt(testcal) == calendar_event_cnt(cal)

# Shorthands
assert "PS VU" in summaries
assert "SEPS SE" in summaries

# Correct Language
assert any(["Programmiersprachen" in d for d in descriptions])
assert not any(["Programming Languages" in d for d in descriptions])
assert all(["Raum:" in d for d in descriptions])
assert not any(["Room:" in d for d in descriptions])
assert any(["Stock:" in d for d in descriptions])
assert not any(["Floor:" in d for d in descriptions])

# No HTML
assert not any(["<b>" in d for d in descriptions])

# No left template strings
assert not any(
[
re.fullmatch(".*\\{.*\\}.*", d) is not None in d
for d in calendar_descriptions(cal)
]
)


def test_icalendar_en_success(client: FlaskClient, mocker):
mocker.patch("app.tiss.get_calendar", return_value=get_test_calendar(lang="en"))
testcal = get_test_calendar(lang="en")
mocker.patch("app.tiss.get_calendar", return_value=testcal)

response = client.get(
"/personal.ics?locale=en&token=justATestingTokenObviouslyNotReal"
)
assert response.status_code == 200

cal = Calendar.from_ical(response.data)
assert "PS VU" in calendar_summaries(cal)
assert "SEPS SE" in calendar_summaries(cal)
assert any(["Programming Languages" in d for d in calendar_descriptions(cal)])
assert not any(["Programmiersprachen" in d for d in calendar_descriptions(cal)])
assert any(["Room:" in d for d in calendar_descriptions(cal)])
assert not all(["Raum:" in d for d in calendar_descriptions(cal)])
assert any(["Floor:" in d for d in calendar_descriptions(cal)])
assert not any(["Stock:" in d for d in calendar_descriptions(cal)])
assert not any(["<b>" in d for d in calendar_descriptions(cal)])
summaries = calendar_summaries(cal)
descriptions = calendar_descriptions(cal)

# Number of events
assert calendar_event_cnt(testcal) == calendar_event_cnt(cal)

# Shorthands
assert "PS VU" in summaries
assert "SEPS SE" in summaries

# Correct language
assert any(["Programming Languages" in d for d in descriptions])
assert not any(["Programmiersprachen" in d for d in descriptions])
assert any(["Room:" in d for d in descriptions])
assert not all(["Raum:" in d for d in descriptions])
assert any(["Floor:" in d for d in descriptions])
assert not any(["Stock:" in d for d in descriptions])

# No HTML
assert not any(["<b>" in d for d in descriptions])

# No left template strings
assert not any(
[
re.fullmatch(".*\\{.*\\}.*", d) is not None in d
for d in calendar_descriptions(cal)
]
)


def test_icalendar_forgoogle_de_success(client: FlaskClient, mocker):
mocker.patch("app.tiss.get_calendar", return_value=get_test_calendar(lang="de"))
testcal = get_test_calendar(lang="de")
mocker.patch("app.tiss.get_calendar", return_value=testcal)

response = client.get(
"/personal.ics?locale=de&token=justATestingTokenObviouslyNotReal&google"
)
assert response.status_code == 200

cal = Calendar.from_ical(response.data)
assert "PS VU" in calendar_summaries(cal)
assert "SEPS SE" in calendar_summaries(cal)
assert any(["Programmiersprachen" in d for d in calendar_descriptions(cal)])
assert not any(["Programming Languages" in d for d in calendar_descriptions(cal)])
assert all(["Raum:" in d for d in calendar_descriptions(cal)])
assert not any(["Room:" in d for d in calendar_descriptions(cal)])
assert any(["Stock:" in d for d in calendar_descriptions(cal)])
assert not any(["Floor:" in d for d in calendar_descriptions(cal)])
assert any(["<b>" in d for d in calendar_descriptions(cal)])
summaries = calendar_summaries(cal)
descriptions = calendar_descriptions(cal)

# Number of events
assert calendar_event_cnt(testcal) == calendar_event_cnt(cal)

# Shorthands
assert "PS VU" in summaries
assert "SEPS SE" in summaries

# Correct Language
assert any(["Programmiersprachen" in d for d in descriptions])
assert not any(["Programming Languages" in d for d in descriptions])
assert all(["Raum:" in d for d in descriptions])
assert not any(["Room:" in d for d in descriptions])
assert any(["Stock:" in d for d in descriptions])
assert not any(["Floor:" in d for d in descriptions])

# HTML in description
assert any(["<b>" in d for d in descriptions])

# No left template strings
assert not any(
[
re.fullmatch(".*\\{.*\\}.*", d) is not None in d
for d in calendar_descriptions(cal)
]
)


def test_icalendar_forgoogle_en_success(client: FlaskClient, mocker):
mocker.patch("app.tiss.get_calendar", return_value=get_test_calendar(lang="en"))
testcal = get_test_calendar(lang="en")
mocker.patch("app.tiss.get_calendar", return_value=testcal)

response = client.get(
"/personal.ics?locale=en&token=justATestingTokenObviouslyNotReal&google"
)
assert response.status_code == 200

cal = Calendar.from_ical(response.data)
assert "PS VU" in calendar_summaries(cal)
assert "SEPS SE" in calendar_summaries(cal)
assert any(["Programming Languages" in d for d in calendar_descriptions(cal)])
assert not any(["Programmiersprachen" in d for d in calendar_descriptions(cal)])
assert any(["Room:" in d for d in calendar_descriptions(cal)])
assert not all(["Raum:" in d for d in calendar_descriptions(cal)])
assert any(["Floor:" in d for d in calendar_descriptions(cal)])
assert not any(["Stock:" in d for d in calendar_descriptions(cal)])
assert any(["<b>" in d for d in calendar_descriptions(cal)])
summaries = calendar_summaries(cal)
descriptions = calendar_descriptions(cal)

# Number of events
assert calendar_event_cnt(testcal) == calendar_event_cnt(cal)

# Shorthands
assert "PS VU" in summaries
assert "SEPS SE" in summaries

# Correct language
assert any(["Programming Languages" in d for d in descriptions])
assert not any(["Programmiersprachen" in d for d in descriptions])
assert any(["Room:" in d for d in descriptions])
assert not all(["Raum:" in d for d in descriptions])
assert any(["Floor:" in d for d in descriptions])
assert not any(["Stock:" in d for d in descriptions])

# HTML in description
assert any(["<b>" in d for d in descriptions])

# No left template strings
assert not any(
[
re.fullmatch(".*\\{.*\\}.*", d) is not None in d
for d in calendar_descriptions(cal)
]
)


def test_icalendar_noshorthands_de_success(client: FlaskClient, mocker):
mocker.patch("app.tiss.get_calendar", return_value=get_test_calendar(lang="de"))
testcal = get_test_calendar(lang="de")
mocker.patch("app.tiss.get_calendar", return_value=testcal)

response = client.get(
"/personal.ics?locale=de&token=justATestingTokenObviouslyNotReal&noshorthand"
)
assert response.status_code == 200

cal = Calendar.from_ical(response.data)
assert "PS VU" not in calendar_summaries(cal)
assert "SEPS SE" not in calendar_summaries(cal)
assert not any(["Programmiersprachen" in d for d in calendar_descriptions(cal)])
assert not any(["Programming Languages" in d for d in calendar_descriptions(cal)])
assert all(["Raum:" in d for d in calendar_descriptions(cal)])
assert not any(["Room:" in d for d in calendar_descriptions(cal)])
assert any(["Stock:" in d for d in calendar_descriptions(cal)])
assert not any(["Floor:" in d for d in calendar_descriptions(cal)])
assert not any(["<b>" in d for d in calendar_descriptions(cal)])
summaries = calendar_summaries(cal)
descriptions = calendar_descriptions(cal)

# Number of events
assert calendar_event_cnt(testcal) == calendar_event_cnt(cal)

# No shorthands
assert "PS VU" not in summaries
assert "SEPS SE" not in summaries

# Correct language
assert not any(["Programmiersprachen" in d for d in descriptions])
assert not any(["Programming Languages" in d for d in descriptions])
assert all(["Raum:" in d for d in descriptions])
assert not any(["Room:" in d for d in descriptions])
assert any(["Stock:" in d for d in descriptions])
assert not any(["Floor:" in d for d in descriptions])

# No HTML
assert not any(["<b>" in d for d in descriptions])

# No left template strings
assert not any(
[
re.fullmatch(".*\\{.*\\}.*", d) is not None in d
for d in calendar_descriptions(cal)
]
)

0 comments on commit 95039c3

Please sign in to comment.