Skip to content

Commit

Permalink
fix: Explicitly call out readonly user fields (#21889)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Apr 26, 2024
1 parent 5963254 commit 2d25f2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 19 additions & 0 deletions posthog/api/test/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,25 @@ def assert_forbidden_url(url):
assert_allowed_url("https://subdomain.otherexample.com")
assert_allowed_url("https://sub.subdomain.otherexample.com")

def test_user_cannot_update_protected_fields(self):
self.user.is_staff = False
self.user.save()
fields = {
"date_joined": "2021-01-01T00:00:00Z",
"uuid": str(uuid.uuid4()),
"distinct_id": "distinct_id",
"pending_email": "[email protected]",
"is_email_verified": True,
}

initial_user = self.client.get("/api/users/@me/").json()

for field, value in fields.items():
response = self.client.patch("/api/users/@me/", {field: value})
assert (
response.json()[field] == initial_user[field]
), f"Updating field '{field}' to '{value}' worked when it shouldn't! Was {initial_user[field]} and is now {response.json()[field]}"


class TestUserSlackWebhook(APIBaseTest):
ENDPOINT: str = "/api/user/test_slack_webhook/"
Expand Down
17 changes: 15 additions & 2 deletions posthog/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class Meta:
"pending_email",
"email_opt_in",
"is_email_verified",
"pending_email",
"notification_settings",
"anonymize_data",
"toolbar_mode",
Expand All @@ -107,8 +106,22 @@ class Meta:
"scene_personalisation",
"theme_mode",
]

read_only_fields = [
"date_joined",
"uuid",
"distinct_id",
"pending_email",
"is_email_verified",
"has_password",
"is_impersonated",
"team",
"organization",
"organizations",
"has_social_auth",
]

extra_kwargs = {
"date_joined": {"read_only": True},
"password": {"write_only": True},
}

Expand Down

0 comments on commit 2d25f2e

Please sign in to comment.