Skip to content

Commit

Permalink
feat(demo-mode): identify demo users via id
Browse files Browse the repository at this point in the history
  • Loading branch information
obostjancic committed Jan 22, 2025
1 parent a13ce7b commit 5cfd725
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
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)

0 comments on commit 5cfd725

Please sign in to comment.