Skip to content

Commit

Permalink
fix: Fix 'EMBED_MENTION_RE' expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Kondrahin committed Aug 25, 2021
1 parent f32ef18 commit cdf4632
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
8 changes: 4 additions & 4 deletions botx/models/messages/sending/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
"{0} can not be passed along with manual validated_values for it"
)

EMBED_MENTION_TEMPATE = (
EMBED_MENTION_TEMPLATE = (
"<embed_mention:{mention_type}:"
"{mentioned_entity_id}:{mention_id}:{mention_name}>" # noqa: WPS326
)
EMBED_MENTION_RE: Final = re.compile(
"<embed_mention:(?P<mention_type>.+?):(?P<mentioned_entity_id>.+?)"
":(?P<mention_id>.+?):(?P<name>.+?)?>", # noqa: WPS326 C812
r"<embed_mention:(?P<mention_type>.+?):(?P<mentioned_entity_id>[0-9a-f\-]+?):"
r"(?P<mention_id>[0-9a-f\-]+?):(?P<name>.+?)??>", # noqa: WPS326 C812
)


Expand Down Expand Up @@ -175,7 +175,7 @@ def make_mention_embeddable(cls, mention: Mention) -> str:

mention_name = mention.mention_data.name or ""

return EMBED_MENTION_TEMPATE.format(
return EMBED_MENTION_TEMPLATE.format(
mention_type=mention.mention_type,
mentioned_entity_id=mentioned_entity_id,
mention_id=mention.mention_id,
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.22.1 (Aug 23, 2021)

### Fixed

* Add `embed_mentions` argument in `SendingMessage.from_message` method.
* Fix `EMBED_MENTION_RE` expression.


## 0.22.0 (Aug 19, 2021)

Tested on BotX 1.44.0-rc2
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "botx"
version = "0.22.0"
version = "0.22.1"
description = "A little python framework for building bots for eXpress"
license = "MIT"
authors = [
Expand Down
74 changes: 74 additions & 0 deletions tests/test_models/test_messages/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ def incoming_message() -> IncomingMessage:
)


@pytest.fixture()
def embed_mention_with_name() -> str:
user_huid = uuid.uuid4()
return f"<embed_mention:contact:{user_huid}:{user_huid}:Test User>"


@pytest.fixture()
def embed_mention_without_name() -> str:
user_huid = uuid.uuid4()
return f"<embed_mention:contact:{user_huid}:{user_huid}:>"


@pytest.fixture()
def embed_mention_with_wrong_type() -> str:
user_huid = uuid.uuid4()
return f"<embed_mention:wrong_type:{user_huid}:{user_huid}:Test User>"


def test_setting_ui_flag_property_for_common_message() -> None:
msg = Message.from_dict(
{
Expand Down Expand Up @@ -508,6 +526,62 @@ def test_build_embeddable_channel_mention(self) -> None:
f"<embed_mention:channel:{channel_id}:",
)

class TestParseEmbedMentions:
def test_replace_embed_mention_with_name(
self,
sending_message: SendingMessage,
embed_mention_with_name: str,
) -> None:
_, found_mentions = sending_message._find_and_replace_embed_mentions(
embed_mention_with_name,
)

assert len(found_mentions) == 1

def test_replace_embed_mention_without_name(
self,
sending_message: SendingMessage,
embed_mention_without_name: str,
) -> None:
_, found_mentions = sending_message._find_and_replace_embed_mentions(
embed_mention_without_name,
)

assert len(found_mentions) == 1

@pytest.mark.parametrize(
("embed_mention_with_name", "embed_mention_without_name"),
[
(embed_mention_with_name, embed_mention_without_name),
(embed_mention_without_name, embed_mention_with_name),
],
indirect=True,
)
def test_replace_group_embed_mentions(
self,
sending_message: SendingMessage,
embed_mention_with_name: str,
embed_mention_without_name: str,
) -> None:
embed_mentions = ", ".join(
[embed_mention_with_name, embed_mention_without_name],
)
_, found_mentions = sending_message._find_and_replace_embed_mentions(
embed_mentions,
)

assert len(found_mentions) == 2

def test_replace_embed_mention_with_wrong_type(
self,
sending_message: SendingMessage,
embed_mention_with_wrong_type: str,
) -> None:
with pytest.raises(ValueError):
_, found_mentions = sending_message._find_and_replace_embed_mentions(
embed_mention_with_wrong_type,
)

class TestAddingRecipients:
def test_adding_recipients_separately(
self,
Expand Down

1 comment on commit cdf4632

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://pybotx.netlify.app as production
🚀 Deployed on https://61260c1b8822829b54ff648d--pybotx.netlify.app

Please sign in to comment.