diff options
author | Nicoco <nicoco@nicoco.fr> | 2022-12-09 12:59:46 +0300 |
---|---|---|
committer | Philipp HΓΆrist <philipp@hoerist.com> | 2022-12-09 12:59:46 +0300 |
commit | 8293ab4cbeb94bd25525a119f3a53ef4beb5f80f (patch) | |
tree | 4fe5a9f0988204977fb052c91d2b1df2e3a817f0 /test | |
parent | 6f31072892ff530322f06c7d27ce22473483f932 (diff) |
feat: Add message reactions (XEP-0444) support
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/test_reactions.py | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/test/unit/test_reactions.py b/test/unit/test_reactions.py new file mode 100644 index 0000000..1db2952 --- /dev/null +++ b/test/unit/test_reactions.py @@ -0,0 +1,137 @@ +from test.lib.util import StanzaHandlerTest + +from nbxmpp.structs import Reactions as ReactionStruct +from nbxmpp.modules.reactions import Reactions +from nbxmpp.protocol import Message + + +class MockLog: + @staticmethod + def warning(_): + pass + + +class MockModule: + _log = MockLog + + @staticmethod + def is_emoji(s): + return Reactions.is_emoji(s) + + +class ReactionsTest(StanzaHandlerTest): + def test_reaction_parsing(self): + class P: + reactions: ReactionStruct + + xml = ''' + <message to='romeo@capulet.net/orchard' id='96d73204-a57a-11e9-88b8-4889e7820c76' type='chat'> + <reactions id='744f6e18-a57a-11e9-a656-4889e7820c76' xmlns='urn:xmpp:reactions:0'> + <reaction>π + </reaction> + <reaction>π’</reaction> + </reactions> + <store xmlns='urn:xmpp:hints'/> + </message> + ''' + msg = Message(node=xml) + Reactions._process_message_reaction(MockModule, self, msg, P) + + self.assertEqual(P.reactions.id, '744f6e18-a57a-11e9-a656-4889e7820c76') + self.assertEqual(P.reactions.emojis, {'π', 'π’'}) + + def test_no_reactions(self): + class P: + reactions: ReactionStruct = None + + xml = ''' + <message to='romeo@capulet.net/orchard' id='96d73204-a57a-11e9-88b8-4889e7820c76' type='chat'> + <store xmlns='urn:xmpp:hints'/> + </message> + ''' + msg = Message(node=xml) + Reactions._process_message_reaction(MockModule, self, msg, P) + + self.assertIsNone(P.reactions) + + def test_empty_reactions(self): + class P: + reactions: ReactionStruct + + xml = ''' + <message to='romeo@capulet.net/orchard' id='96d73204-a57a-11e9-88b8-4889e7820c76' type='chat'> + <reactions id='744f6e18-a57a-11e9-a656-4889e7820c76' xmlns='urn:xmpp:reactions:0' /> + <store xmlns='urn:xmpp:hints'/> + </message> + ''' + msg = Message(node=xml) + Reactions._process_message_reaction(MockModule, self, msg, P) + + self.assertEqual(len(P.reactions.emojis), 0) + + def test_invalid_reactions_no_id(self): + class P: + reactions: ReactionStruct + + xml = ''' + <message to='romeo@capulet.net/orchard' id='96d73204-a57a-11e9-88b8-4889e7820c76' type='chat'> + <reactions xmlns='urn:xmpp:reactions:0'> + <reaction>π</reaction> + <reaction>π’</reaction> + </reactions> + <store xmlns='urn:xmpp:hints'/> + </message> + ''' + msg = Message(node=xml) + Reactions._process_message_reaction(MockModule, self, msg, P) + self.assertFalse(hasattr(P, 'reactions')) + + def test_invalid_reactions_empty_id(self): + class P: + reactions: ReactionStruct + + xml = ''' + <message to='romeo@capulet.net/orchard' id='96d73204-a57a-11e9-88b8-4889e7820c76' type='chat'> + <reactions id='' xmlns='urn:xmpp:reactions:0'> + <reaction>π</reaction> + <reaction>π’</reaction> + </reactions> + <store xmlns='urn:xmpp:hints'/> + </message> + ''' + msg = Message(node=xml) + Reactions._process_message_reaction(MockModule, self, msg, P) + self.assertFalse(hasattr(P, 'reactions')) + + def test_invalid_reactions_empty_emoji(self): + class P: + reactions: ReactionStruct + + xml = ''' + <message to='romeo@capulet.net/orchard' id='96d73204-a57a-11e9-88b8-4889e7820c76' type='chat'> + <reactions id='sadfsadf' xmlns='urn:xmpp:reactions:0'> + <reaction></reaction> + <reaction>π’</reaction> + </reactions> + <store xmlns='urn:xmpp:hints'/> + </message> + ''' + msg = Message(node=xml) + Reactions._process_message_reaction(MockModule, self, msg, P) + self.assertEqual(P.reactions.emojis, {'π’'}) + + def test_set_reactions(self): + x = Message() + x.setReactions('id', 'π’') + self.assertEqual(x.getReactions(), ('id', {'π’'})) + + x = Message() + x.setReactions('id', 'π’π') + self.assertEqual(x.getReactions(), ('id', {'π’', 'π'})) + + x = Message() + x.setReactions('id', '') + self.assertEqual(x.getReactions(), ('id', set())) + + x = Message() + self.assertIsNone(x.getReactions()) |