|
5 | 5 | from google.protobuf import empty_pb2, wrappers_pb2 |
6 | 6 | from sqlalchemy import func, select, update |
7 | 7 |
|
| 8 | +from couchers.config import config |
8 | 9 | from couchers.db import session_scope |
9 | 10 | from couchers.helpers.badges import user_add_badge |
10 | 11 | from couchers.jobs.handlers import update_badges |
@@ -1573,6 +1574,68 @@ def test_badges(db): |
1573 | 1574 | assert res2.user_ids == [2] |
1574 | 1575 |
|
1575 | 1576 |
|
| 1577 | +def test_hidden_badge_is_awarded_but_not_shown(db): |
| 1578 | + """ |
| 1579 | + With the rollout gate on (tests pass all gates), the moderator badge is awarded to superusers |
| 1580 | + and stored, but not shown to clients or notified. |
| 1581 | + """ |
| 1582 | + # create dummies first so the superuser isn't a static founder/board_member (user ids 1, 2) |
| 1583 | + generate_user(last_donated=None) |
| 1584 | + generate_user(last_donated=None) |
| 1585 | + superuser, token = generate_user(is_superuser=True, last_donated=None) |
| 1586 | + |
| 1587 | + update_badges(empty_pb2.Empty()) |
| 1588 | + |
| 1589 | + with session_scope() as session: |
| 1590 | + # the badge row is still created |
| 1591 | + assert ( |
| 1592 | + session.execute( |
| 1593 | + select(func.count()) |
| 1594 | + .select_from(UserBadge) |
| 1595 | + .where(UserBadge.user_id == superuser.id, UserBadge.badge_id == "moderator") |
| 1596 | + ).scalar() |
| 1597 | + == 1 |
| 1598 | + ) |
| 1599 | + # but the user gets no notification about it |
| 1600 | + assert ( |
| 1601 | + session.execute( |
| 1602 | + select(func.count()).select_from(Notification).where(Notification.user_id == superuser.id) |
| 1603 | + ).scalar() |
| 1604 | + == 0 |
| 1605 | + ) |
| 1606 | + |
| 1607 | + # and it does not appear on the user's profile |
| 1608 | + with api_session(token) as api: |
| 1609 | + assert "moderator" not in api.GetUser(api_pb2.GetUserReq(user=superuser.username)).badges |
| 1610 | + |
| 1611 | + |
| 1612 | +def test_hidden_badge_is_shown_when_rollout_gate_off(db, monkeypatch): |
| 1613 | + """With the rollout gate off, the moderator badge behaves as before: shown and notified.""" |
| 1614 | + # disable experimentation so the global gate evaluates to off (rollback / pre-rollout state) |
| 1615 | + monkeypatch.setitem(config, "EXPERIMENTATION_PASS_ALL_GATES", False) |
| 1616 | + monkeypatch.setitem(config, "EXPERIMENTATION_ENABLED", False) |
| 1617 | + |
| 1618 | + # create dummies first so the superuser isn't a static founder/board_member (user ids 1, 2) |
| 1619 | + generate_user(last_donated=None) |
| 1620 | + generate_user(last_donated=None) |
| 1621 | + superuser, token = generate_user(is_superuser=True, last_donated=None) |
| 1622 | + |
| 1623 | + update_badges(empty_pb2.Empty()) |
| 1624 | + |
| 1625 | + with session_scope() as session: |
| 1626 | + # the badge add is notified |
| 1627 | + assert ( |
| 1628 | + session.execute( |
| 1629 | + select(func.count()).select_from(Notification).where(Notification.user_id == superuser.id) |
| 1630 | + ).scalar() |
| 1631 | + == 1 |
| 1632 | + ) |
| 1633 | + |
| 1634 | + # and it appears on the user's profile |
| 1635 | + with api_session(token) as api: |
| 1636 | + assert "moderator" in api.GetUser(api_pb2.GetUserReq(user=superuser.username)).badges |
| 1637 | + |
| 1638 | + |
1576 | 1639 | def test_user_add_badge_is_idempotent(db): |
1577 | 1640 | """Test that adding a badge a user already has is a no-op and doesn't send a duplicate notification.""" |
1578 | 1641 | user, _ = generate_user() |
|
0 commit comments