test: store the constants in constants.py + better PEP8 compliance

This commit is contained in:
souliane 2014-01-04 15:45:08 +01:00
commit a21e3c2783
4 changed files with 67 additions and 36 deletions

38
src/test/constants.py Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Primitivus: a SAT frontend
# 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/>.
from sat.core.i18n import _, D_
from twisted.words.protocols.jabber.jid import JID
class Const(object):
TEST_JID_STR = u"test@example.org/SàT"
TEST_JID = JID(TEST_JID_STR)
TEST_JID_2_STR = u"sender@example.net/house"
TEST_JID_2 = JID(TEST_JID_2_STR)
TEST_PROFILE = 'test_profile'
# To test frontend parameters
APP_NAME = "dummy_frontend"
SECURITY_LIMIT = 0
ENABLE_UNIBOX_KEY = D_("Composition")
ENABLE_UNIBOX_PARAM = D_("Enable unibox")

View file

@ -18,8 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _
import __builtin__
from twisted.words.protocols.jabber.jid import JID
from constants import Const
from wokkel.xmppim import RosterItem
from sat.core.xmpp import SatRosterProtocol
from twisted.trial.unittest import FailTest
@ -27,9 +26,6 @@ from twisted.trial import unittest
from xml.etree import cElementTree as etree
import re
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
@ -39,12 +35,15 @@ def b2s(value):
"""
return u"True" if value else u"False"
class DifferentArgsException(FailTest):
pass
class DifferentXMLException(FailTest):
pass
class FakeSAT(object):
"""Class to simulate a SAT instance"""
@ -91,7 +90,7 @@ class FakeMemory(object):
def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'):
pass
def addWaitingSub(self, type, contact_jid, profile_key):
def addWaitingSub(self, type_, contact_jid, profile_key):
pass
def delWaitingSub(self, contact_jid, profile_key):
@ -109,12 +108,13 @@ class FakeTriggerManager(object):
"""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)
self.addItem(Const.TEST_JID)
def addItem(self, jid, *args, **kwargs):
if not args and not kwargs:
@ -133,7 +133,7 @@ class FakeClient(object):
def __init__(self, host):
self.host = host
self.profile = 'test_profile'
self.jid = TEST_JID
self.jid = Const.TEST_JID
self.roster = FakeRosterProtocol(host, self)
def send(self, obj):
@ -142,11 +142,11 @@ class FakeClient(object):
class SatTestCase(unittest.TestCase):
def assertEqualXML(self, xml, expected, ignore_blank = False):
def assertEqualXML(self, xml, expected, ignore_blank=False):
def equalElt(got_elt, exp_elt):
if ignore_blank:
for elt in got_elt, exp_elt:
for attr in ('text','tail'):
for attr in ('text', 'tail'):
value = getattr(elt, attr)
try:
value = value.strip() or None
@ -176,7 +176,7 @@ class SatTestCase(unittest.TestCase):
return True
def remove_blank(xml):
lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+',' ',xml).split('\n')]
lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+', ' ', xml).split('\n')]
return '\n'.join([line for line in lines if line])
xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml)
@ -189,9 +189,3 @@ class SatTestCase(unittest.TestCase):
print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8')
print "---"
raise DifferentXMLException
def _(text):
return text
__builtin__.__dict__['_'] = _

View file

@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.test import helpers
from constants import Const
from twisted.trial import unittest
from sat.core.sat_main import SAT
from sat.core import xmpp
@ -67,13 +68,13 @@ class SatRosterProtocolTest(unittest.TestCase):
self.roster.parent = helpers.FakeClient(self.host)
def test_onRosterSet(self):
roster_item = RosterItem(helpers.TEST_JID)
roster_item = RosterItem(Const.TEST_JID)
roster_item.name = u"Test Man"
roster_item.subscriptionTo = True
roster_item.subscriptionFrom = True
roster_item.ask = False
roster_item.groups = set([u"Test Group 1", u"Test Group 2", u"Test Group 3"])
self.host.bridge.expectCall("newContact", helpers.TEST_JID_STR, {'to': 'True', 'from': 'True', 'ask': 'False', 'name': u'Test Man'}, set([u"Test Group 1", u"Test Group 2", u"Test Group 3"]), "test_profile")
self.host.bridge.expectCall("newContact", Const.TEST_JID_STR, {'to': 'True', 'from': 'True', 'ask': 'False', 'name': u'Test Man'}, set([u"Test Group 1", u"Test Group 2", u"Test Group 3"]), "test_profile")
self.roster.onRosterSet(roster_item)
@ -85,29 +86,29 @@ class SatPresenceProtocolTest(unittest.TestCase):
self.presence.parent = helpers.FakeClient(self.host)
def test_availableReceived(self):
self.host.bridge.expectCall("presenceUpdate", helpers.TEST_JID_STR, "xa", 15, {'default': "test status", 'fr': 'statut de test'}, helpers.TEST_PROFILE)
self.presence.availableReceived(helpers.TEST_JID, 'xa', {None: "test status", 'fr': 'statut de test'}, 15)
self.host.bridge.expectCall("presenceUpdate", Const.TEST_JID_STR, "xa", 15, {'default': "test status", 'fr': 'statut de test'}, Const.TEST_PROFILE)
self.presence.availableReceived(Const.TEST_JID, 'xa', {None: "test status", 'fr': 'statut de test'}, 15)
def test_availableReceived_empty_statuses(self):
self.host.bridge.expectCall("presenceUpdate", helpers.TEST_JID_STR, "xa", 15, {}, helpers.TEST_PROFILE)
self.presence.availableReceived(helpers.TEST_JID, 'xa', None, 15)
self.host.bridge.expectCall("presenceUpdate", Const.TEST_JID_STR, "xa", 15, {}, Const.TEST_PROFILE)
self.presence.availableReceived(Const.TEST_JID, 'xa', None, 15)
def test_unavailableReceived(self):
self.host.bridge.expectCall("presenceUpdate", helpers.TEST_JID_STR, "unavailable", 0, {}, helpers.TEST_PROFILE)
self.presence.unavailableReceived(helpers.TEST_JID, None)
self.host.bridge.expectCall("presenceUpdate", Const.TEST_JID_STR, "unavailable", 0, {}, Const.TEST_PROFILE)
self.presence.unavailableReceived(Const.TEST_JID, None)
def test_subscribedReceived(self):
self.host.bridge.expectCall("subscribe", "subscribed", helpers.TEST_JID.userhost(), helpers.TEST_PROFILE)
self.presence.subscribedReceived(helpers.TEST_JID)
self.host.bridge.expectCall("subscribe", "subscribed", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
self.presence.subscribedReceived(Const.TEST_JID)
def test_unsubscribedReceived(self):
self.host.bridge.expectCall("subscribe", "unsubscribed", helpers.TEST_JID.userhost(), helpers.TEST_PROFILE)
self.presence.unsubscribedReceived(helpers.TEST_JID)
self.host.bridge.expectCall("subscribe", "unsubscribed", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
self.presence.unsubscribedReceived(Const.TEST_JID)
def test_subscribeReceived(self):
self.host.bridge.expectCall("subscribe", "subscribe", helpers.TEST_JID.userhost(), helpers.TEST_PROFILE)
self.presence.subscribeReceived(helpers.TEST_JID)
self.host.bridge.expectCall("subscribe", "subscribe", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
self.presence.subscribeReceived(Const.TEST_JID)
def test_unsubscribeReceived(self):
self.host.bridge.expectCall("subscribe", "unsubscribe", helpers.TEST_JID.userhost(), helpers.TEST_PROFILE)
self.presence.unsubscribeReceived(helpers.TEST_JID)
self.host.bridge.expectCall("subscribe", "unsubscribe", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
self.presence.unsubscribeReceived(Const.TEST_JID)

View file

@ -29,7 +29,6 @@ class SanitisationTest(helpers.SatTestCase):
self.host = helpers.FakeSAT()
self.text_syntaxes = plugin_misc_text_syntaxes.TextSyntaxes(self.host)
def test_xhtml_sanitise(self):
evil_html = """
<html>
@ -56,7 +55,7 @@ class SanitisationTest(helpers.SatTestCase):
<a href="evil-site">spam spam SPAM!</a>
<image src="evil!">
</body>
</html>""" # example from lxml: /usr/share/doc/python-lxml-doc/html/lxmlhtml.html#cleaning-up-html
</html>""" # example from lxml: /usr/share/doc/python-lxml-doc/html/lxmlhtml.html#cleaning-up-html
expected = """<div>
<style>/* deleted */</style>
@ -77,7 +76,6 @@ class SanitisationTest(helpers.SatTestCase):
d.addCallback(self.assertEqualXML, expected, ignore_blank=True)
return d
def test_styles_sanitise(self):
evil_html = """<p style='display: None; test: blah; background: url(: alert()); color: blue;'>test <strong>retest</strong><br><span style="background-color: (alert('bouh')); titi; color: #cf2828; font-size: 3px; direction: !important; color: red; color: red !important; font-size: 100px !important; font-size: 100px ! important; font-size: 100%; font-size: 100ox; font-size: 100px; font-size: 100;;;; font-size: 100 %; color: 100 px 1.7em; color: rgba(0, 0, 0, 0.1); color: rgb(35,79,255); background-color: no-repeat; background-color: :alert(1); color: (alert('XSS')); color: (window.location='http://example.org/'); color: url(:window.location='http://example.org/'); "> toto </span></p>"""