Skip to content

Commit

Permalink
Validate schedule from Google
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Jun 10, 2020
1 parent d5ef9dc commit 4afd8ac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
32 changes: 21 additions & 11 deletions backend/atlas/utils/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
Team,
User,
)
from atlas.schema import Pronouns
from atlas.schema import DaySchedule, Pronouns

ENUMS = {"pronouns": Pronouns}

logger = logging.getLogger("atlas")

Expand Down Expand Up @@ -63,6 +65,14 @@ def find(iterable, func):
return None


def coerce_day_schedule(value):
if not value:
return ""
if not hasattr(DaySchedule, value):
return ""
return value


def refresh_token(identity: Identity) -> Identity:
req = requests.post(
"https://accounts.google.com/o/oauth2/token",
Expand Down Expand Up @@ -465,10 +475,10 @@ def sync_user( # NOQA
value = DEFAULT_VALUES[attribute_name]
elif attribute_name.startswith(BOOLEAN_PREFIXES):
value = bool(value)
elif attribute_name == "pronouns" and value:
elif attribute_name in ENUMS and value:
value = (
getattr(Pronouns, value).value
if getattr(Pronouns, value, None)
getattr(ENUMS[attribute_name], value).value
if getattr(ENUMS[attribute_name], value, None)
else None
)
elif attribute_name == "referred_by" and value is not None:
Expand All @@ -480,13 +490,13 @@ def sync_user( # NOQA
if settings.GOOGLE_SCHEDULE_FIELD in schemas:
value = schemas[settings.GOOGLE_SCHEDULE_FIELD]
profile_fields["schedule"] = [
value.get("Sunday") or "",
value.get("Monday") or "",
value.get("Tuesday") or "",
value.get("Wednesday") or "",
value.get("Thursday") or "",
value.get("Friday") or "",
value.get("Saturday") or "",
coerce_day_schedule(value.get("Sunday")),
coerce_day_schedule(value.get("Monday")),
coerce_day_schedule(value.get("Tuesday")),
coerce_day_schedule(value.get("Wednesday")),
coerce_day_schedule(value.get("Thursday")),
coerce_day_schedule(value.get("Friday")),
coerce_day_schedule(value.get("Saturday")),
]
if data["customSchemas"] != profile.config:
profile_fields["config"] = data["customSchemas"]
Expand Down
28 changes: 28 additions & 0 deletions backend/atlas/utils/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,34 @@ def test_sync_user_valid_pronouns(
assert user.profile.pronouns == "HE_HIM"


def test_sync_user_invalid_schedule(
responses, default_superuser, user_payload, design_department
):
user_payload["customSchemas"][settings.GOOGLE_SCHEDULE_FIELD] = {
"Monday": "In Office"
}
result = sync_user(data=user_payload)
assert result.created
assert result.updated

user = result.user
assert user.profile.schedule == ["", "", "", "", "", "", ""]


def test_sync_user_valid_schedule(
responses, default_superuser, user_payload, design_department
):
user_payload["customSchemas"][settings.GOOGLE_SCHEDULE_FIELD] = {
"Monday": "INOFFICE"
}
result = sync_user(data=user_payload)
assert result.created
assert result.updated

user = result.user
assert user.profile.schedule == ["", "INOFFICE", "", "", "", "", ""]


def test_sync_user_new_account_updated_department_without_cost_center(
responses, default_superuser, design_department, user_payload
):
Expand Down

0 comments on commit 4afd8ac

Please sign in to comment.