PageRenderTime 86ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pyxmpp2/test/iq.py

https://github.com/kulikov/python-tools
Python | 208 lines | 201 code | 4 blank | 3 comment | 0 complexity | ed9725ccc3b1d81c5c714219b7bfd7ab MD5 | raw file
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. # pylint: disable=C0111
  4. import unittest
  5. from pyxmpp2.etree import ElementTree
  6. from pyxmpp2.iq import Iq
  7. from pyxmpp2.jid import JID
  8. from pyxmpp2.stanzapayload import XMLPayload
  9. from pyxmpp2.error import StanzaErrorElement
  10. IQ1 = """
  11. <iq xmlns="jabber:client" from='source@example.com/res'
  12. to='dest@example.com' type='get' id='1'>
  13. <payload xmlns="http://pyxmpp.jajcus.net/xmlns/test"><abc/></payload>
  14. </iq>"""
  15. IQ2 = """
  16. <iq xmlns="jabber:client" to='source@example.com/res'
  17. from='dest@example.com' type='result' id='1'>
  18. <payload xmlns="http://pyxmpp.jajcus.net/xmlns/test"><abc/></payload>
  19. </iq>"""
  20. IQ3 = """
  21. <iq xmlns="jabber:client" from='source@example.com/res'
  22. to='dest@example.com' type='set' id='2'>
  23. <payload xmlns="http://pyxmpp.jajcus.net/xmlns/test"><abc/></payload>
  24. </iq>"""
  25. IQ4 = """
  26. <iq xmlns="jabber:client" to='source@example.com/res'
  27. from='dest@example.com' type='result' id='2'>
  28. </iq>"""
  29. IQ5 = """
  30. <iq xmlns="jabber:client" to='source@example.com/res'
  31. from='dest@example.com' type='error' id='1'>
  32. <payload xmlns="http://pyxmpp.jajcus.net/xmlns/test"><abc/></payload>
  33. <error type="modify"><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>
  34. </iq>"""
  35. class TestIq(unittest.TestCase):
  36. def check_iq1(self, iq):
  37. self.assertEqual(iq.from_jid, JID("source@example.com/res"))
  38. self.assertEqual(iq.to_jid, JID("dest@example.com"))
  39. self.assertEqual(iq.stanza_type, "get")
  40. self.assertEqual(iq.stanza_id, "1")
  41. payload = iq.get_all_payload()
  42. self.assertTrue(payload)
  43. self.assertEqual(payload[0].xml_element_name,
  44. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  45. self.assertTrue(len(payload[0].element) > 0)
  46. self.assertEqual(payload[0].element[0].tag,
  47. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  48. self.assertFalse(iq.error)
  49. def check_iq2(self, iq):
  50. self.assertEqual(iq.to_jid, JID("source@example.com/res"))
  51. self.assertEqual(iq.from_jid, JID("dest@example.com"))
  52. self.assertEqual(iq.stanza_type, "result")
  53. self.assertEqual(iq.stanza_id, "1")
  54. payload = iq.get_all_payload()
  55. self.assertTrue(payload)
  56. self.assertEqual(payload[0].xml_element_name,
  57. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  58. self.assertTrue(len(payload[0].element) > 0)
  59. self.assertEqual(payload[0].element[0].tag,
  60. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  61. self.assertFalse(iq.error)
  62. def check_iq3(self, iq):
  63. self.assertEqual(iq.from_jid, JID("source@example.com/res"))
  64. self.assertEqual(iq.to_jid, JID("dest@example.com"))
  65. self.assertEqual(iq.stanza_type, "set")
  66. self.assertEqual(iq.stanza_id, "2")
  67. payload = iq.get_all_payload()
  68. self.assertTrue(payload)
  69. self.assertEqual(payload[0].xml_element_name,
  70. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  71. self.assertTrue(len(payload[0].element) > 0)
  72. self.assertEqual(payload[0].element[0].tag,
  73. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  74. self.assertFalse(iq.error)
  75. def check_iq4(self, iq):
  76. self.assertEqual(iq.to_jid, JID("source@example.com/res"))
  77. self.assertEqual(iq.from_jid, JID("dest@example.com"))
  78. self.assertEqual(iq.stanza_type, "result")
  79. self.assertEqual(iq.stanza_id, "2")
  80. payload = iq.get_all_payload()
  81. self.assertFalse(payload)
  82. self.assertFalse(iq.error)
  83. def check_iq5(self, iq):
  84. self.assertEqual(iq.to_jid, JID("source@example.com/res"))
  85. self.assertEqual(iq.from_jid, JID("dest@example.com"))
  86. self.assertEqual(iq.stanza_type, "error")
  87. self.assertEqual(iq.stanza_id, "1")
  88. payload = iq.get_all_payload()
  89. self.assertTrue(payload)
  90. self.assertEqual(payload[0].xml_element_name,
  91. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  92. self.assertTrue(len(payload[0].element) > 0)
  93. self.assertEqual(payload[0].element[0].tag,
  94. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  95. error = iq.error
  96. self.assertIsInstance(error, StanzaErrorElement)
  97. self.assertEqual(error.condition_name, "bad-request")
  98. def test_iq_get_from_xml(self):
  99. iq = Iq(ElementTree.XML(IQ1))
  100. self.check_iq1(iq)
  101. def test_iq_result_full_from_xml(self):
  102. iq = Iq(ElementTree.XML(IQ2))
  103. self.check_iq2(iq)
  104. def test_iq_set_from_xml(self):
  105. iq = Iq(ElementTree.XML(IQ3))
  106. self.check_iq3(iq)
  107. def test_iq_result_empty_from_xml(self):
  108. iq = Iq(ElementTree.XML(IQ4))
  109. self.check_iq4(iq)
  110. def test_iq_get(self):
  111. iq = Iq( from_jid = JID("source@example.com/res"),
  112. to_jid = JID("dest@example.com"),
  113. stanza_type = "get",
  114. stanza_id = 1)
  115. payload = ElementTree.Element(
  116. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  117. ElementTree.SubElement(payload,
  118. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  119. payload = XMLPayload(payload)
  120. iq.add_payload(payload)
  121. self.check_iq1(iq)
  122. xml = iq.as_xml()
  123. self.check_iq1( Iq(xml) )
  124. def test_iq_result_full(self):
  125. iq = Iq( to_jid = JID("source@example.com/res"),
  126. from_jid = JID("dest@example.com"),
  127. stanza_type = "result",
  128. stanza_id = 1)
  129. payload = ElementTree.Element(
  130. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  131. ElementTree.SubElement(payload,
  132. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  133. payload = XMLPayload(payload)
  134. iq.add_payload(payload)
  135. self.check_iq2(iq)
  136. xml = iq.as_xml()
  137. self.check_iq2( Iq(xml) )
  138. def test_iq_set(self):
  139. iq = Iq( from_jid = JID("source@example.com/res"),
  140. to_jid = JID("dest@example.com"),
  141. stanza_type = "set",
  142. stanza_id = 2)
  143. payload = ElementTree.Element(
  144. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  145. ElementTree.SubElement(payload,
  146. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  147. payload = XMLPayload(payload)
  148. iq.add_payload(payload)
  149. self.check_iq3(iq)
  150. xml = iq.as_xml()
  151. self.check_iq3( Iq(xml) )
  152. def test_iq_result_empty(self):
  153. iq = Iq( to_jid = JID("source@example.com/res"),
  154. from_jid = JID("dest@example.com"),
  155. stanza_type = "result",
  156. stanza_id = 2)
  157. self.check_iq4(iq)
  158. xml = iq.as_xml()
  159. self.check_iq4( Iq(xml) )
  160. def test_iq_make_result_response(self):
  161. iq = Iq(ElementTree.XML(IQ1))
  162. iq2 = iq.make_result_response()
  163. payload = ElementTree.Element(
  164. "{http://pyxmpp.jajcus.net/xmlns/test}payload")
  165. ElementTree.SubElement(payload,
  166. "{http://pyxmpp.jajcus.net/xmlns/test}abc")
  167. payload = XMLPayload(payload)
  168. iq2.add_payload(payload)
  169. self.check_iq2(iq2)
  170. def test_iq_make_error_response(self):
  171. iq = Iq(ElementTree.XML(IQ1))
  172. iq5 = iq.make_error_response(u"bad-request")
  173. self.check_iq5(iq5)
  174. xml = iq5.as_xml()
  175. self.check_iq5( Iq(xml) )
  176. # pylint: disable=W0611
  177. from pyxmpp2.test._support import load_tests, setup_logging
  178. def setUpModule():
  179. setup_logging()
  180. if __name__ == "__main__":
  181. unittest.main()