def test_revert_add_user_tags(session, user, sticker_set, tags): """Test that reverting the tags of a user works.""" ban_user = user_factory(session, 3, "testuser2") for sticker in sticker_set.stickers: # Create a new tag for each sticker tag_sticker(session, f"tag-banned-{sticker.file_id}", sticker, ban_user) session.commit() # Revert the changes of malicious user revert_user_changes(session, ban_user) # Ensure that the mallicious user's tags have been removed and the old tags are in place for sticker in sticker_set.stickers: assert len(sticker.tags) == 1 assert sticker.tags[0].name == f"tag-{sticker.file_id}" for change in ban_user.changes: assert change.reverted # Undo the revert undo_user_changes_revert(session, ban_user) # Ensure that the mallicious user's tags have been removed and the old tags are in place for sticker in sticker_set.stickers: assert_sticker_contains_tags( sticker, [f"tag-{sticker.file_id}", f"tag-banned-{sticker.file_id}"] ) for change in ban_user.changes: assert not change.reverted
def handle_private_text(bot, update, session, chat, user): """Read all messages and handle the tagging of stickers.""" # Handle the name of a sticker set to initialize full sticker set tagging if chat.tag_mode in [TagMode.sticker_set.value, TagMode.random.value]: # Try to tag the sticker. Return early if it didn't work. tag_sticker( session, update.message.text, chat.current_sticker, user, tg_chat=update.message.chat, chat=chat, message_id=update.message.message_id, ) session.commit() handle_next(session, bot, chat, update.message.chat, user) elif chat.tag_mode == TagMode.single_sticker.value: tag_sticker( session, update.message.text, chat.current_sticker, user, tg_chat=update.message.chat, chat=chat, message_id=update.message.message_id, ) chat.cancel(bot) return "Sticker tags adjusted."
def test_add_tags(session, user, sticker_set): """Add new tags to a sticker.""" for sticker in sticker_set.stickers: # Create a new tag for each sticker tag_sticker(session, f"tag-{sticker.file_id}", sticker, user) session.commit() # Ensure that the mallicious user actually replaced the tag for sticker in sticker_set.stickers: assert sticker.tags[0].name == f"tag-{sticker.file_id}" # User got a new change assert len(user.changes) == len(sticker_set.stickers) for sticker in sticker_set.stickers: # Create a new tag for each sticker tag_sticker(session, f"tag-2-{sticker.file_id}", sticker, user) session.commit() # Ensure that the mallicious user actually replaced the tag for sticker in sticker_set.stickers: assert_sticker_contains_tags( sticker, [f"tag-{sticker.file_id}", f"tag-2-{sticker.file_id}"]) assert len(user.changes) == len(sticker_set.stickers) * 2
def test_original_emoji_stays_on_replace(session, user, sticker_set): """Original emojis will remain if tags are added in replace mode.""" sticker = sticker_set.stickers[0] # Add an original emoji tag to the sticker tag = Tag("😲", False, True) sticker.tags.append(tag) sticker.original_emojis.append(tag) session.commit() # Now tag the sticker in replace mode tag_sticker(session, "new-tag", sticker, user, replace=True) assert_sticker_contains_tags(sticker, ["new-tag", "😲"]) assert len(sticker.tags) == 2 assert sticker.original_emojis[0] in sticker.tags
def test_replace_sticker_tags(session, user, sticker_set, tags): """Replace tags of a sticker.""" for sticker in sticker_set.stickers: # Replace the existing tag tag_sticker(session, f"new-tag-{sticker.file_id}", sticker, user, replace=True) session.commit() # Ensure the tag has been replaced for sticker in sticker_set.stickers: assert len(sticker.tags) == 1 assert sticker.tags[0].name == f"new-tag-{sticker.file_id}" assert len(user.changes) == len(sticker_set.stickers) * 2
def replace_single(bot, update, session, chat, user): """Tag the last sticker send to this chat.""" # The replace command has been replied to another message # If it's a sticker, replace the tags of this specific sticker if (update.message.reply_to_message is not None and update.message.reply_to_message.sticker is not None): tg_sticker = update.message.reply_to_message.sticker sticker = session.query(Sticker).get(tg_sticker.file_unique_id) if sticker is None: return "This sticker has not yet been added." is_single_sticker = True # The replace command has been normally called elif chat.current_sticker: sticker = chat.current_sticker is_single_sticker = chat.tag_mode not in [ TagMode.sticker_set.value, TagMode.random.value, ] else: return "No sticker for replacement selected" # Remove the /replace command text = update.message.text[8:] if text.strip() == "": return 'You need to add some tags to the /replace command. E.g. "/replace meme prequel obi wan"' tag_sticker( session, text, sticker, user, tg_chat=update.message.chat, chat=chat, message_id=update.message.message_id, single_sticker=is_single_sticker, replace=True, ) if not is_single_sticker: handle_next(session, bot, chat, update.message.chat, user) else: return "Sticker tags replaced."
def test_add_duplicate_sticker_tags_in_other_language(session, user, sticker_set): """Add the same tag to a sticker, but in different languages. The tag should be converted from international to default, if somebody tags in default, but not the other way around. """ # User should tag in not default language first user.international = True sticker = sticker_set.stickers[0] tag_sticker(session, "language-test-tag", sticker, user) session.commit() tag = session.query(Tag).get("language-test-tag") assert tag.international # Add same tag to sticker, but this time in default language user.international = False tag_sticker(session, "language-test-tag", sticker, user) assert not tag.international assert len(user.changes) == 1 # Now tag in the not default language again. This shouldn't change anything now user.international = True tag_sticker(session, "language-test-tag", sticker, user) assert not tag.international assert len(user.changes) == 1
def handle_edited_messages(bot, update, session, chat, user): """Read edited messages and check whether the user corrected some tags.""" message = update.edited_message # Try to find a Change with this message change = (session.query(Change).filter(Change.chat == chat).filter( Change.message_id == message.message_id).order_by( Change.created_at.desc()).limit(1).one_or_none()) if change is None: return tag_sticker( session, message.text, change.sticker, user, tg_chat=message.chat, chat=chat, message_id=message.message_id, single_sticker=True, ) return "Sticker tags edited."
def tags(session, sticker_set, user): """Create tags for all stickers.""" for sticker in sticker_set.stickers: # Create a new tag for each sticker tag_sticker(session, f"tag-{sticker.file_id}", sticker, user)