Ignore:
Timestamp:
Jan 8, 2012, 9:26:02 AM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Use symbolic constants instead of integers MUC status code.

Instead of using normal constant values for representing MUC status codes,
UserPresence now uses twisted.python.constants to define these. This
makes code better to understand and helps debugging.

If a user presence includes one or more status codes, they are stored in the
mucStatuses attribute, as an instance of Statuses. This replaces the
former statusCodes attribute.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/test/test_muc.py

    r157 r160  
    4343
    4444
     45class StatusCodeTest(unittest.TestCase):
     46    """
     47    Tests for L{muc.STATUS_CODE}.
     48    """
     49
     50    def test_lookupByValue(self):
     51        """
     52        The registered MUC status codes map to STATUS_CODE value constants.
     53
     54        Note: the identifiers used in the dictionary of status codes are
     55        borrowed from U{XEP-0306<http://xmpp.org/extensions/xep-0306.html>}
     56        that defines Extensible Status Conditions for Multi-User Chat. If this
     57        specification is implemented itself, the dictionary could move there.
     58        """
     59        codes = {
     60            100: 'realjid-public',
     61            101: 'affiliation-changed',
     62            102: 'unavailable-shown',
     63            103: 'unavailable-not-shown',
     64            104: 'configuration-changed',
     65            110: 'self-presence',
     66            170: 'logging-enabled',
     67            171: 'logging-disabled',
     68            172: 'non-anonymous',
     69            173: 'semi-anonymous',
     70            174: 'fully-anonymous',
     71            201: 'room-created',
     72            210: 'nick-assigned',
     73            301: 'banned',
     74            303: 'new-nick',
     75            307: 'kicked',
     76            321: 'removed-affiliation',
     77            322: 'removed-membership',
     78            332: 'removed-shutdown',
     79        }
     80
     81        for code, condition in codes.iteritems():
     82            constantName = condition.replace('-', '_').upper()
     83            self.assertEqual(getattr(muc.STATUS_CODE, constantName),
     84                             muc.STATUS_CODE.lookupByValue(code))
     85
     86
     87
     88class StatusesTest(unittest.TestCase):
     89    """
     90    Tests for L{muc.Statuses}.
     91    """
     92
     93    def setUp(self):
     94        self.mucStatuses = muc.Statuses()
     95        self.mucStatuses.add(muc.STATUS_CODE.SELF_PRESENCE)
     96        self.mucStatuses.add(muc.STATUS_CODE.ROOM_CREATED)
     97
     98
     99    def test_interface(self):
     100        """
     101        Instances of L{Statuses} provide L{iwokkel.IMUCStatuses}.
     102        """
     103        verify.verifyObject(iwokkel.IMUCStatuses, self.mucStatuses)
     104
     105
     106    def test_contains(self):
     107        """
     108        The status contained are 'in' the container.
     109        """
     110        self.assertIn(muc.STATUS_CODE.SELF_PRESENCE, self.mucStatuses)
     111        self.assertIn(muc.STATUS_CODE.ROOM_CREATED, self.mucStatuses)
     112        self.assertNotIn(muc.STATUS_CODE.NON_ANONYMOUS, self.mucStatuses)
     113
     114
     115    def test_iter(self):
     116        """
     117        All statuses can be iterated over.
     118        """
     119        statuses = set()
     120        for status in self.mucStatuses:
     121            statuses.add(status)
     122
     123        self.assertEqual(set([muc.STATUS_CODE.SELF_PRESENCE,
     124                              muc.STATUS_CODE.ROOM_CREATED]), statuses)
     125
     126
     127    def test_len(self):
     128        """
     129        The number of items in this container is returned by C{__len__}.
     130        """
     131        self.assertEqual(2, len(self.mucStatuses))
     132
     133
     134
    45135class GroupChatTest(unittest.TestCase):
    46136    """
    47     Tests for {muc.GroupChat}.
     137    Tests for L{muc.GroupChat}.
    48138    """
    49139
     
    121211
    122212
     213
    123214class UserPresenceTest(unittest.TestCase):
    124215    """
     
    126217    """
    127218
    128 
    129     def test_toElementUnknownChild(self):
     219    def test_fromElementNoUserElement(self):
     220        """
     221        Without user element, all associated attributes are None.
     222        """
     223        xml = """
     224            <presence from='coven@chat.shakespeare.lit/thirdwitch'
     225                      id='026B3509-2CCE-4D69-96D6-25F41FFDC408'
     226                      to='hag66@shakespeare.lit/pda'>
     227            </presence>
     228        """
     229
     230        element = parseXml(xml)
     231        presence = muc.UserPresence.fromElement(element)
     232
     233        self.assertIdentical(None, presence.affiliation)
     234        self.assertIdentical(None, presence.role)
     235        self.assertIdentical(None, presence.entity)
     236        self.assertIdentical(None, presence.nick)
     237        self.assertEqual(0, len(presence.mucStatuses))
     238
     239
     240    def test_fromElementUnknownChild(self):
    130241        """
    131242        Unknown child elements are ignored.
     
    144255        presence = muc.UserPresence.fromElement(element)
    145256
    146         self.assertIdentical(None, presence.statusCodes)
    147 
    148 
    149     def test_toElementStatusOne(self):
     257        self.assertEqual(0, len(presence.mucStatuses))
     258
     259
     260    def test_fromElementStatusOne(self):
    150261        """
    151262        Status codes are extracted.
     
    165276        presence = muc.UserPresence.fromElement(element)
    166277
    167         self.assertIn(110, presence.statusCodes)
    168 
    169 
    170     def test_toElementStatusMultiple(self):
     278        self.assertIn(muc.STATUS_CODE.SELF_PRESENCE, presence.mucStatuses)
     279
     280
     281    def test_fromElementStatusMultiple(self):
    171282        """
    172283        Multiple status codes are all extracted.
     
    187298        presence = muc.UserPresence.fromElement(element)
    188299
    189         self.assertIn(110, presence.statusCodes)
    190         self.assertIn(100, presence.statusCodes)
    191 
    192 
    193     def test_toElementStatusEmpty(self):
     300        self.assertIn(muc.STATUS_CODE.SELF_PRESENCE, presence.mucStatuses)
     301        self.assertIn(muc.STATUS_CODE.REALJID_PUBLIC, presence.mucStatuses)
     302
     303
     304    def test_fromElementStatusEmpty(self):
    194305        """
    195306        Empty status elements are ignored.
     
    209320        presence = muc.UserPresence.fromElement(element)
    210321
    211         self.assertIdentical(None, presence.statusCodes)
    212 
    213 
    214     def test_toElementStatusBad(self):
     322        self.assertEqual(0, len(presence.mucStatuses))
     323
     324
     325    def test_fromElementStatusBad(self):
    215326        """
    216327        Bad status codes are ignored.
     
    230341        presence = muc.UserPresence.fromElement(element)
    231342
    232         self.assertIdentical(None, presence.statusCodes)
    233 
    234 
    235     def test_toElementStatusUnknown(self):
    236         """
    237         Unknown status codes are still recorded in C{statusCodes}.
     343        self.assertEqual(0, len(presence.mucStatuses))
     344
     345
     346    def test_fromElementStatusUnknown(self):
     347        """
     348        Unknown status codes are not recorded in C{mucStatuses}.
    238349        """
    239350        xml = """
     
    251362        presence = muc.UserPresence.fromElement(element)
    252363
    253         self.assertIn(999, presence.statusCodes)
    254 
    255 
    256     def test_toElementItem(self):
     364        self.assertEqual(0, len(presence.mucStatuses))
     365
     366
     367    def test_fromElementItem(self):
    257368        """
    258369        Item attributes are parsed properly.
Note: See TracChangeset for help on using the changeset viewer.