Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(demo-mode): identify demo users via id #83820

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/sentry/utils/demo_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def is_readonly_user(user: User | None) -> bool:
if not user:
return False

email = getattr(user, "email", None)

return email in options.get("demo-mode.users")
return user.id in options.get("demo-mode.users")


def is_demo_org(organization: Organization | None):
Expand All @@ -41,8 +39,8 @@ def get_readonly_user():
if not options.get("demo-mode.enabled"):
return None

email = options.get("demo-mode.users")[0]
return User.objects.get(email=email)
user_id = options.get("demo-mode.users")[0]
return User.objects.get(id=user_id)


def get_readonly_scopes() -> frozenset[str]:
Expand Down
20 changes: 10 additions & 10 deletions tests/sentry/utils/test_demo_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
from sentry.utils.demo_mode import get_readonly_user, is_demo_org, is_readonly_user


@override_options({"demo-mode.enabled": True, "demo-mode.users": ["[email protected]"]})
@override_options({"demo-mode.enabled": True, "demo-mode.users": [1]})
@django_db_all
def test_is_readonly_user_demo_mode_enabled_none():
assert not is_readonly_user(None)


@override_options({"demo-mode.enabled": True, "demo-mode.users": ["[email protected]"]})
@override_options({"demo-mode.enabled": True, "demo-mode.users": [1]})
@django_db_all
def test_is_readonly_user_demo_mode_enabled_readonly_user():
user = Factories.create_user("[email protected]")
user = Factories.create_user(id=1)
assert is_readonly_user(user)


@override_options({"demo-mode.enabled": True, "demo-mode.users": ["[email protected]"]})
@override_options({"demo-mode.enabled": True, "demo-mode.users": [1]})
@django_db_all
def test_is_readonly_user_demo_mode_enabled_non_readonly_user():
user = Factories.create_user("[email protected]")
user = Factories.create_user(id=2)
assert not is_readonly_user(user)


Expand All @@ -35,14 +35,14 @@ def test_is_readonly_user_demo_mode_disabled_none():
@override_options({"demo-mode.enabled": False})
@django_db_all
def test_is_readonly_user_demo_mode_disabled_readonly_user():
user = Factories.create_user("[email protected]")
user = Factories.create_user(id=1)
assert not is_readonly_user(user)


@override_options({"demo-mode.enabled": False})
@django_db_all
def test_is_readonly_user_demo_mode_disabled_non_readonly_user():
user = Factories.create_user("[email protected]")
user = Factories.create_user(id=2)
assert not is_readonly_user(user)


Expand Down Expand Up @@ -79,10 +79,10 @@ def test_get_readonly_user_demo_mode_disabled():
assert get_readonly_user() is None


@override_options({"demo-mode.enabled": True, "demo-mode.users": ["[email protected]"]})
@override_options({"demo-mode.enabled": True, "demo-mode.users": [1]})
@django_db_all
def test_get_readonly_user_demo_mode_enabled():
user = Factories.create_user("[email protected]")
user = Factories.create_user(id=1)
with patch("sentry.utils.demo_mode.User.objects.get", return_value=user) as mock_user_get:
assert get_readonly_user() == user
mock_user_get.assert_called_once_with(email="[email protected]")
mock_user_get.assert_called_once_with(id=1)
Loading