summaryrefslogtreecommitdiff
path: root/tests/sleektest.py
blob: eed52ec22a567c72642ba19bfb6ad21dbfecb270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
    This file is part of SleekXMPP.

    See the file license.txt for copying permission.
"""

import unittest
from xml.etree import cElementTree as ET
from sleekxmpp import Message, Iq
from sleekxmpp.stanza.presence import Presence
from sleekxmpp.xmlstream.matcher.stanzapath import StanzaPath


class SleekTest(unittest.TestCase):
    """
    A SleekXMPP specific TestCase class that provides
    methods for comparing message, iq, and presence stanzas.
    """

    def stanzaPlugin(self, stanza, plugin):
        """
        Associate a stanza object as a plugin for another stanza.
        """
        tag = "{%s}%s" % (plugin.namespace, plugin.name)
	stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
	stanza.plugin_tag_map[tag] = plugin

    def Message(self, *args, **kwargs):
        """Create a message stanza."""
        return Message(None, *args, **kwargs)

    def Iq(self, *args, **kwargs):
        """Create an iq stanza."""
        return Iq(None, *args, **kwargs)

    def Presence(self, *args, **kwargs):
        """Create a presence stanza."""
        return Presence(None, *args, **kwargs)

    def checkMessage(self, msg, xml_string, use_values=True):
        """
        Create and compare several message stanza objects to a 
        correct XML string. 

        If use_values is False, the test using getValues() and 
        setValues() will not be used.
        """

        debug = "Given Stanza:\n%s\n" % ET.tostring(msg.xml)

        xml = ET.fromstring(xml_string)
        xml.tag = '{jabber:client}message'
        debug += "XML String:\n%s\n" % ET.tostring(xml)
        
        msg2 = self.Message(xml)
        debug += "Constructed Stanza:\n%s\n" % ET.tostring(msg2.xml)
        
        if use_values:
            # Ugly, but need to make sure the type attribute is set.
            msg['type'] = msg['type']
            if xml.attrib.get('type', None) is None:
                xml.attrib['type'] = 'normal'

            values = msg2.getValues()
            msg3 = self.Message()
            msg3.setValues(values)
            
            debug += "Second Constructed Stanza:\n%s\n" % ET.tostring(msg3.xml)
            debug = "Three methods for creating stanza do not match:\n" + debug
            self.failUnless(self.compare([xml, msg.xml, 
                                          msg2.xml, msg3.xml]), 
                            debug)
        else:
            debug = "Two methods for creating stanza do not match:\n" + debug
            self.failUnless(self.compare([xml, msg.xml, msg2.xml]), debug)

    def checkIq(self, iq, xml_string, use_values=True):
        """
        Create and compare several iq stanza objects to a 
        correct XML string. 

        If use_values is False, the test using getValues() and 
        setValues() will not be used.
        """
        debug = "Given Stanza:\n%s\n" % ET.tostring(iq.xml)

        xml = ET.fromstring(xml_string)
        xml.tag = '{jabber:client}iq'
        debug += "XML String:\n%s\n" % ET.tostring(xml)
        
        iq2 = self.Iq(xml)
        debug += "Constructed Stanza:\n%s\n" % ET.tostring(iq2.xml)
        
        if use_values:
            values = iq.getValues()
            iq3 = self.Iq()
            iq3.setValues(values)

            debug += "Second Constructed Stanza:\n%s\n" % ET.tostring(iq3.xml)
            debug = "Three methods for creating stanza do not match:\n" + debug
            self.failUnless(self.compare([xml, iq.xml, iq2.xml, iq3.xml]), 
                            debug)
        else:
            debug = "Two methods for creating stanza do not match:\n" + debug
            self.failUnless(self.compare([xml, iq.xml, iq2.xml]), debug)

    def checkPresence(self, pres, xml_string, use_values=True):
        """
        Create and compare several presence stanza objects to a 
        correct XML string. 

        If use_values is False, the test using getValues() and 
        setValues() will not be used.
        """
        pass

    def compare(self, xml1, xml2=None):
        """
        Compare XML objects.

        If given a list of XML objects, then
        all of the elements in the list will be
        compared.
        """

        # Compare multiple objects
        if type(xml1) is list:
            xmls = xml1
            xml1 = xmls[0]
            for xml in xmls[1:]:
                xml2 = xml
                if not self.compare(xml1, xml2):
                    return False
            return True

        # Step 1: Check tags
        if xml1.tag != xml2.tag:
            print xml1.tag, xml2.tag
            return False

        # Step 2: Check attributes
        if xml1.attrib != xml2.attrib:
            return False

        # Step 3: Recursively check children
        for child in xml1:
            child2s = xml2.findall("%s" % child.tag)
            if child2s is None:
                return False
            for child2 in child2s:
                if self.compare(child, child2):
                    break
            else:
                return False

        # Everything matches
        return True