Skip to content

Commit 2ddaf9b

Browse files
authored
Merge pull request #125 from jagerman/global-mod-tests
Add tests for global mods/admins
2 parents ea27471 + 89e7a91 commit 2ddaf9b

File tree

4 files changed

+134
-36
lines changed

4 files changed

+134
-36
lines changed

sogs/__main__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
from . import web
153153
from .model.room import Room, get_rooms
154154
from .model.user import User, SystemUser, get_all_global_moderators
155-
from .model.exc import AlreadyExists, NoSuchRoom
155+
from .model.exc import AlreadyExists, NoSuchRoom, NoSuchUser
156156

157157
web.appdb = db.get_conn()
158158

@@ -322,7 +322,20 @@ def print_room(room: Room):
322322
print(f"{u.session_id} was not a global moderator")
323323
else:
324324
u.remove_moderator(removed_by=sysadmin)
325-
print(f"Removed {sid} as global {'admin' if was_admin else 'moderator'}")
325+
print(f"Removed {u.session_id} as global {'admin' if was_admin else 'moderator'}")
326+
327+
if u.is_blinded and sid.startswith('05'):
328+
try:
329+
u2 = User(session_id=sid, try_blinding=False, autovivify=False)
330+
if u2.global_admin or u2.global_moderator:
331+
was_admin = u2.global_admin
332+
u2.remove_moderator(removed_by=sysadmin)
333+
print(
334+
f"Removed {u2.session_id} as global "
335+
f"{'admin' if was_admin else 'moderator'}"
336+
)
337+
except NoSuchUser:
338+
pass
326339
else:
327340
if args.rooms == ['*']:
328341
rooms = get_rooms()
@@ -334,9 +347,22 @@ def print_room(room: Room):
334347

335348
for sid in args.delete_moderators:
336349
u = User(session_id=sid, try_blinding=True)
350+
u2 = None
351+
if u.is_blinded and sid.startswith('05'):
352+
try:
353+
u2 = User(session_id=sid, try_blinding=False, autovivify=False)
354+
except NoSuchUser:
355+
pass
356+
337357
for room in rooms:
338358
room.remove_moderator(u, removed_by=sysadmin)
339359
print(f"Removed {u.session_id} as moderator/admin of {room.name} ({room.token})")
360+
if u2 is not None:
361+
room.remove_moderator(u2, removed_by=sysadmin)
362+
print(
363+
f"Removed {u2.session_id} as moderator/admin of {room.name} ({room.token})"
364+
)
365+
340366
elif args.list_rooms:
341367
rooms = get_rooms()
342368
if rooms:

sogs/model/room.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,22 +1398,23 @@ def remove_moderator(self, user: User, *, removed_by: User, remove_admin_only: b
13981398
raise BadPermission()
13991399

14001400
with db.transaction():
1401-
query(
1402-
f"""
1403-
UPDATE user_permission_overrides
1404-
SET admin = FALSE
1405-
{', moderator = FALSE, visible_mod = TRUE' if not remove_admin_only else ''}
1406-
WHERE room = :r AND "user" = :u
1407-
""",
1408-
r=self.id,
1409-
u=user.id,
1410-
)
1401+
with user.check_blinding() as u:
1402+
query(
1403+
f"""
1404+
UPDATE user_permission_overrides
1405+
SET admin = FALSE
1406+
{', moderator = FALSE, visible_mod = TRUE' if not remove_admin_only else ''}
1407+
WHERE room = :r AND "user" = :u
1408+
""",
1409+
r=self.id,
1410+
u=user.id,
1411+
)
14111412

1412-
self._refresh()
1413-
if user.id in self._perm_cache:
1414-
del self._perm_cache[user.id]
1413+
self._refresh()
1414+
if user.id in self._perm_cache:
1415+
del self._perm_cache[user.id]
14151416

1416-
app.logger.info(f"{removed_by} removed {user} as mod/admin of {self}")
1417+
app.logger.info(f"{removed_by} removed {u} as mod/admin of {self}")
14171418

14181419
def ban_user(self, to_ban: User, *, mod: User, timeout: Optional[float] = None):
14191420
"""
@@ -1496,24 +1497,28 @@ def unban_user(self, to_unban: User, *, mod: User):
14961497
app.logger.warning(f"Error unbanning {to_unban} from {self} by {mod}: not a moderator")
14971498
raise BadPermission()
14981499

1499-
result = query(
1500-
"""
1501-
UPDATE user_permission_overrides SET banned = FALSE
1502-
WHERE room = :r AND "user" = :unban AND banned
1503-
""",
1504-
r=self.id,
1505-
unban=to_unban.id,
1506-
)
1507-
if result.rowcount > 0:
1508-
app.logger.debug(f"{mod} unbanned {to_unban} from {self}")
1500+
with db.transaction():
1501+
with to_unban.check_blinding() as to_unban:
1502+
result = query(
1503+
"""
1504+
UPDATE user_permission_overrides SET banned = FALSE
1505+
WHERE room = :r AND "user" = :unban AND banned
1506+
""",
1507+
r=self.id,
1508+
unban=to_unban.id,
1509+
)
1510+
if result.rowcount > 0:
1511+
app.logger.debug(f"{mod} unbanned {to_unban} from {self}")
15091512

1510-
if to_unban.id in self._perm_cache:
1511-
del self._perm_cache[to_unban.id]
1513+
if to_unban.id in self._perm_cache:
1514+
del self._perm_cache[to_unban.id]
15121515

1513-
return True
1516+
return True
15141517

1515-
app.logger.debug(f"{mod} unbanned {to_unban} from {self} (but user was already unbanned)")
1516-
return False
1518+
app.logger.debug(
1519+
f"{mod} unbanned {to_unban} from {self} (but user was already unbanned)"
1520+
)
1521+
return False
15171522

15181523
def get_bans(self):
15191524
"""

sogs/routes/legacy.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def handle_legacy_single_delete(msgid):
331331
@legacy.post("/block_list")
332332
def handle_legacy_ban():
333333
user, room = legacy_check_user_room(moderator=True)
334-
ban = User(session_id=request.json['public_key'], autovivify=True)
334+
ban = User(session_id=request.json['public_key'], autovivify=True, try_blinding=True)
335335

336336
room.ban_user(to_ban=ban, mod=user)
337337

@@ -341,7 +341,7 @@ def handle_legacy_ban():
341341
@legacy.post("/ban_and_delete_all")
342342
def handle_legacy_banhammer():
343343
mod, room = legacy_check_user_room(moderator=True)
344-
ban = User(session_id=request.json['public_key'], autovivify=True)
344+
ban = User(session_id=request.json['public_key'], autovivify=True, try_blinding=True)
345345

346346
with db.transaction():
347347
room.ban_user(to_ban=ban, mod=mod)
@@ -353,7 +353,7 @@ def handle_legacy_banhammer():
353353
@legacy.delete("/block_list/<SessionID:session_id>")
354354
def handle_legacy_unban(session_id):
355355
user, room = legacy_check_user_room(moderator=True)
356-
to_unban = User(session_id=session_id, autovivify=False)
356+
to_unban = User(session_id=session_id, autovivify=False, try_blinding=True)
357357
if room.unban_user(to_unban, mod=user):
358358
return jsonify({"status_code": http.OK})
359359

@@ -395,7 +395,7 @@ def handle_legacy_add_admin():
395395
if len(session_id) != 66 or not session_id.startswith("05"):
396396
abort(http.BAD_REQUEST)
397397

398-
mod = User(session_id=session_id, autovivify=True)
398+
mod = User(session_id=session_id, autovivify=True, try_blinding=True)
399399
room.set_moderator(mod, admin=True, visible=True, added_by=user)
400400

401401
return jsonify({"status_code": http.OK})
@@ -408,7 +408,7 @@ def handle_legacy_add_admin():
408408
def handle_legacy_remove_admin(session_id):
409409
user, room = legacy_check_user_room(admin=True)
410410

411-
mod = User(session_id=session_id, autovivify=False)
411+
mod = User(session_id=session_id, autovivify=False, try_blinding=True)
412412
room.remove_moderator(mod, removed_by=user)
413413

414414
return jsonify({"status_code": http.OK})

tests/test_room_routes.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,73 @@ def test_list(client, room, room2, user, user2, admin, mod, global_mod, global_a
185185
assert r.json == {**r4_expected, **r4_exp_defs, **exp_gadmin}
186186

187187

188+
def test_visible_global_mods(client, room, user, mod, global_mod, global_admin):
189+
url_room = '/room/test-room'
190+
expect_room = {
191+
"token": "test-room",
192+
"name": "Test room",
193+
"description": "Test suite testing room",
194+
"info_updates": 3,
195+
"message_sequence": 0,
196+
"created": room.created,
197+
"active_users": 0,
198+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
199+
"moderators": [mod.session_id],
200+
"admins": [],
201+
"read": True,
202+
"write": True,
203+
"upload": True,
204+
}
205+
r = sogs_get(client, url_room, user)
206+
assert r.status_code == 200
207+
assert r.json == expect_room
208+
209+
expected_for_moderator = {
210+
**expect_room,
211+
**{'default_' + x: True for x in ('accessible', 'read', 'write', 'upload')},
212+
'global_moderator': True,
213+
'hidden_admins': [global_admin.session_id],
214+
'hidden_moderators': [global_mod.session_id],
215+
'moderator': True,
216+
'moderators': [mod.session_id],
217+
}
218+
r = sogs_get(client, "/room/test-room/pollInfo/0", global_mod)
219+
assert r.status_code == 200
220+
assert r.json == {
221+
'token': 'test-room',
222+
'active_users': 0,
223+
'details': expected_for_moderator,
224+
'read': True,
225+
'write': True,
226+
'upload': True,
227+
'moderator': True,
228+
'global_moderator': True,
229+
'default_accessible': True,
230+
'default_read': True,
231+
'default_write': True,
232+
'default_upload': True,
233+
}
234+
235+
global_mod.set_moderator(added_by=global_admin, admin=False, visible=True)
236+
global_admin.set_moderator(added_by=global_admin, admin=True, visible=True)
237+
238+
for e in (expect_room, expected_for_moderator):
239+
e["moderators"] = sorted([mod.session_id, global_mod.session_id])
240+
e["admins"] = [global_admin.session_id]
241+
e["info_updates"] += 2
242+
del expected_for_moderator["hidden_admins"]
243+
del expected_for_moderator["hidden_moderators"]
244+
245+
r = sogs_get(client, url_room, user)
246+
assert r.status_code == 200
247+
assert r.json == expect_room
248+
249+
r = sogs_get(client, url_room, mod)
250+
assert r.status_code == 200
251+
del expected_for_moderator["global_moderator"]
252+
assert r.json == expected_for_moderator
253+
254+
188255
def test_updates(client, room, user, user2, mod, admin, global_mod, global_admin):
189256
url_room = '/room/test-room'
190257
r = sogs_get(client, url_room, user)

0 commit comments

Comments
 (0)