view src/test/helpers.py @ 649:e20c823f23e2

tests: helpers improvments: - FakeParent renamed FakeClient - added FakeRoster class, and an instance of it in FakeClient (FakeClient.roster) - b2s function to convert boolean value to strings used in bridge - some fake methods (FakeClient.send, FakeSat.delContact) to avoid exceptions
author Goffi <goffi@goffi.org>
date Sun, 29 Sep 2013 16:29:36 +0200
parents 63b6e684bbfa
children ffb716804580
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

# SAT: a jabber client
# Copyright (C) 2009, 2010, 2011, 2012, 2013  Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import __builtin__
from twisted.words.protocols.jabber.jid import JID
from wokkel.xmppim import RosterItem
from sat.core.xmpp import SatRosterProtocol
from twisted.trial.unittest import FailTest

TEST_JID_STR = u"test@example.org/SàT"
TEST_JID = JID(u"test@example.org/SàT")
TEST_PROFILE = 'test_profile'

def b2s(value):
    """Convert a bool to a unicode string used in bridge
    @param value: boolean value
    @return: unicode conversion, according to bridge convention

    """
    return  u"True" if value else u"False"

class DifferentArgsException(FailTest):
    pass


class FakeSAT(object):
    """Class to simulate a SAT instance"""

    def __init__(self):
        self.bridge = FakeBridge()
        self.memory = FakeMemory()
        self.trigger = FakeTriggerManager()

    def delContact(self, to, profile_key):
        #TODO
        pass


class FakeBridge(object):
    """Class to simulate and test bridge calls"""

    def expectCall(self, name, *check_args, **check_kwargs):
        def checkCall(*args, **kwargs):
            if args != check_args or kwargs != check_kwargs:
                print "\n\n--------------------"
                print "Args are not equals:"
                print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args)
                print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs)
                print "--------------------\n\n"
                raise DifferentArgsException

        setattr(self, name, checkCall)


class FakeMemory(object):
    """Class to simulate and test memory object"""

    def getProfileName(self, profile_key):
        return profile_key

    def addToHistory(self, from_jid, to_jid, message, _type='chat', timestamp=None, profile=None):
        pass

    def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'):
        pass

    def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'):
        pass

    def addWaitingSub(self, type, contact_jid, profile_key):
        pass

    def delWaitingSub(self, contact_jid, profile_key):
        pass


class FakeTriggerManager(object):

    def add(self, point_name, callback):
        pass

    def point(self, point_name, *args, **kwargs):
        """We always return true to continue the action"""
        return True

class FakeRosterProtocol(SatRosterProtocol):

    def __init__(self, host, parent):
        SatRosterProtocol.__init__(self, host)
        self.parent = parent
        self.addItem(TEST_JID)

    def addItem(self, jid, *args, **kwargs):
        if not args and not kwargs:
            # defaults values setted for the tests only
            kwargs["subscriptionTo"] = True
            kwargs["subscriptionFrom"] = True
        roster_item = RosterItem(jid, *args, **kwargs)
        attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)}
        if roster_item.name:
            attrs['name'] = roster_item.name
        self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) 
        self.onRosterSet(roster_item)


class FakeClient(object):
    def __init__(self, host):
        self.host = host
        self.profile = 'test_profile'
        self.jid = TEST_JID
        self.roster = FakeRosterProtocol(host, self)

    def send(self, obj):
        pass


def _(text):
    return text

__builtin__.__dict__['_'] = _