From 3d50627cefbbcc11155cf95217bdf69a8eb0c701 Mon Sep 17 00:00:00 2001 From: Debin Li Date: Fri, 13 May 2022 13:40:20 -0400 Subject: [PATCH] Adding unit tests and refactoring code logic --- api/tests/send_email_test.py | 35 +++++++++++++++++++++++++++++++++++ api/yelp_beans/send_email.py | 14 ++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/api/tests/send_email_test.py b/api/tests/send_email_test.py index 499fb9ff..8d09c984 100644 --- a/api/tests/send_email_test.py +++ b/api/tests/send_email_test.py @@ -1,8 +1,14 @@ +import datetime +from urllib.parse import parse_qs +from urllib.parse import urlparse + import pytest +import pytz from yelp_beans.logic.meeting_spec import get_specs_for_current_week from yelp_beans.matching.match import generate_meetings from yelp_beans.models import User from yelp_beans.models import UserSubscriptionPreferences +from yelp_beans.send_email import create_google_calendar_invitation_link from yelp_beans.send_email import send_batch_initial_opt_in_email from yelp_beans.send_email import send_batch_meeting_confirmation_email from yelp_beans.send_email import send_batch_unmatched_email @@ -58,3 +64,32 @@ def test_send_batch_meeting_confirmation_email(database, session): def test_send_batch_unmatched_email(database, fake_user): matches, unmatched = generate_meetings([fake_user], database.specs[0]) send_batch_unmatched_email(unmatched) + +@pytest.mark.parametrize( + "test_datetime,expected_link_ctz", + [ + ( + datetime.datetime(2022, 5, 13, 23, 34, 45, tzinfo=pytz.timezone('America/Chicago')), + "America/Chicago" + ), + ( + datetime.datetime(2022, 5, 13, 23, 34, 45, tzinfo=pytz.timezone('America/Los_Angeles')), + "America/Los_Angeles" + ), + ( + datetime.datetime(2022, 5, 13, 23, 34, 45, tzinfo=pytz.timezone('Europe/London')), + "Europe/London" + ), + ( + datetime.datetime(2022, 5, 13, 23, 34, 45), + None + ), + ] +) +def test_create_google_calendar_invitation_link(test_datetime, expected_link_ctz): + generated_calendar_url = create_google_calendar_invitation_link([], "title", "office", "location", test_datetime, + test_datetime) + parsed_url = urlparse(generated_calendar_url) + ctz_param = parse_qs(parsed_url.query).get("ctz", None) + ctz_value = ctz_param[0] if ctz_param else None + assert ctz_value == expected_link_ctz diff --git a/api/yelp_beans/send_email.py b/api/yelp_beans/send_email.py index 48721d3a..6a722833 100644 --- a/api/yelp_beans/send_email.py +++ b/api/yelp_beans/send_email.py @@ -183,14 +183,12 @@ def create_google_calendar_invitation_link(user_list, title, office, location, m 'location': office + " " + location, 'add': ','.join([user.email for user in user_list]) } - try: - if meeting_datetime.strftime("%Z") and "ctz" not in url_params: - # If the meeting time have a timezone specified - # and Calendar URL link doesn't contain timezone - # Add the "ctz" parameter to Google's Calendar template link - url_params["ctz"] = meeting_datetime.tzinfo.zone - except Exception: - logging.exception("Failed to add timezone information to Google Calendar link") + if meeting_datetime.tzinfo and meeting_datetime.tzinfo.zone and "ctz" not in url_params: + # If the meeting time have a timezone specified + # and Calendar URL link doesn't contain timezone + # Add the "ctz" parameter to Google's Calendar template link + url_params["ctz"] = meeting_datetime.tzinfo.zone + invite_url += urllib.parse.urlencode(url_params) return invite_url