def login_with_guest(sio: ServerApp, encrypted_login_request: bytes): if sio.guest_encrypt is None: raise NotAuthorizedForAction() try: login_request_bytes = sio.guest_encrypt.decrypt(encrypted_login_request) except cryptography.fernet.InvalidToken: raise NotAuthorizedForAction() try: login_request = json.loads(login_request_bytes.decode("utf-8")) name = login_request["name"] date = datetime.datetime.fromisoformat(login_request["date"]) except (UnicodeDecodeError, json.JSONDecodeError, KeyError, ValueError) as e: raise InvalidAction(str(e)) if _get_now() - date > datetime.timedelta(days=1): raise NotAuthorizedForAction() user: User = User.create(name=f"Guest: {name}") with sio.session() as session: session["user-id"] = user.id return _create_client_side_session(sio, user)
def _verify_has_admin(sio: ServerApp, session_id: int, admin_user_id: Optional[int], *, allow_when_no_admins: bool = False) -> None: """ Checks if the logged user can do admin operations to the given session, :param session_id: The GameSessions id :param admin_user_id: An user id that is exceptionally authorized for this :param allow_when_no_admins: This action is authorized for non-admins if there are no admins. :return: """ current_user = sio.get_current_user() try: current_membership = GameSessionMembership.get_by_ids( current_user.id, session_id) except peewee.DoesNotExist: raise NotAuthorizedForAction() if not (current_membership.admin or (admin_user_id is not None and current_user.id == admin_user_id)): if allow_when_no_admins and GameSessionMembership.select().where( GameSessionMembership.session == session_id, GameSessionMembership.admin == True).count() == 0: return raise NotAuthorizedForAction()
def _download_layout_description(sio: ServerApp, session: GameSession): try: # You must be a session member to do get the spoiler GameSessionMembership.get_by_ids(sio.get_current_user().id, session.id) except peewee.DoesNotExist: raise NotAuthorizedForAction() if session.layout_description_json is None: raise InvalidAction("Session does not contain a game") if not session.layout_description.permalink.spoiler: raise InvalidAction("Session does not contain a spoiler") return session.layout_description_json
# Run wrapped = qt_network_client.handle_network_errors(callee) result = await wrapped(qapp, "foo", data) # Assert callee.assert_awaited_once_with(qapp, "foo", data) assert result is callee.return_value @pytest.mark.parametrize(["exception", "title", "message"], [ (InvalidAction("something"), "Invalid action", "Invalid Action: something"), (ServerError(), "Server error", "An error occurred on the server while processing your request."), (NotAuthorizedForAction(), "Unauthorized", "You're not authorized to perform that action."), (NotLoggedIn(), "Unauthenticated", "You must be logged in."), (RequestTimeout("5s timeout"), "Connection Error", "<b>Timeout while communicating with the server:</b><br /><br />Request timed out: 5s timeout<br />" "Further attempts will wait for longer."), ]) async def test_handle_network_errors_exception(skip_qtbot, qapp, mocker, exception, title, message): mock_dialog = mocker.patch("randovania.gui.lib.async_dialog.warning", new_callable=AsyncMock) callee = AsyncMock() callee.side_effect = exception data = MagicMock() # Run