source: products/PloneSMSCommunicator/tags/0.1.3/tests/test_Communicator.py @ 1591

Last change on this file since 1591 was 1, checked in by myroslav, 19 years ago

Building directory structure

File size: 3.8 KB
Line 
1from Testing import ZopeTestCase
2from Products.CMFPlone.tests import PloneTestCase
3from Products.CMFCore.utils import getToolByName
4from Products.Archetypes.tests import ArchetypesTestCase
5from AccessControl.SecurityManagement import newSecurityManager
6from AccessControl.SecurityManagement import noSecurityManager
7from Products.PloneSMSCommunicator.pyXIAM import SMSsubmitRequest, getAllTextFromTag
8from xml.dom.minidom import *
9from StringIO import StringIO
10import SimpleXiamServer
11import BaseHTTPServer
12import thread
13
14ZopeTestCase.installProduct('PloneSMSCommunicator')
15
16tests=[]
17
18class SmSHandler(SimpleXiamServer.SimpleXiamHandler):
19
20    server_url = None
21    def testCommunicator(self):
22        cl = int(self.headers['content-length'])
23        doc = parseString(self.rfile.read(cl))
24        self.assertEqual(getAllTextFromTag(doc, 'from')[0], '+380979312198')
25        self.assertEqual(getAllTextFromTag(doc, 'to')[0], '+380979987348')
26        self.assertEqual(getAllTextFromTag(doc, 'content')[0], 'hello how are you?')
27
28    def do_POST(self):
29        SimpleXiamServer.SimpleXiamHandler.do_POST(self)
30        self.testCommunicator()
31
32
33class TestPloneSMSCommunicator(ArchetypesTestCase.ArcheSiteTestCase, ZopeTestCase.Functional):
34
35    httpd = BaseHTTPServer.HTTPServer(("", 10010), SmSHandler)
36
37    def createManager(self, portal):
38
39        acl_users = portal.acl_users
40        return acl_users._doAddUser('PortalManager', '', ['Manager'], (), (), )
41
42    def startServer(self):
43
44        print 'serving at port', 10010
45        self.httpd.serve_forever()
46
47    def installProducts(self, portal):
48        #create manager user
49        self.createManager(portal)
50        user = portal.acl_users.getUserById('PortalManager')
51
52        #login as manager
53        newSecurityManager(None, user)
54
55        #install products
56        qi = getToolByName(portal, 'portal_quickinstaller')
57        qi.installProduct('PloneSMSCommunicator')
58
59        #log out
60        noSecurityManager()
61
62    def afterSetUp(self):
63
64        portal = self.portal
65        self.installProducts(portal)
66
67    def test_sendRequest(self):
68        portal = self.portal
69        #start new thread
70        th = thread.start_new_thread(self.startServer, ())
71
72        communicator = portal.portal_smsCommunicator
73        communicator.setLogFlag(False)
74        communicator.setServer('http://localhost:10010/')
75        communicator.send_Request(originator = '+380979312198', destination = ['+380979987348'], body = 'hello how are you?')
76        #destroy thread
77        del th
78
79    def test_Response(self):
80        portal = self.portal
81        communicator = portal.portal_smsCommunicator
82        communicator.setLogFlag(False)
83        response = """<?xml version="1.0" encoding="UTF-8"?>
84                    <!DOCTYPE xiamSMS SYSTEM "xiamSMSMessage.dtd">
85                    <xiamSMS status="OK" statusText="XML contained 1 xir messages">
86                     <submitResponse id="betye54">
87                        <result status="OK" statusText="">+380979987348</result>
88                     </submitResponse>
89                    </xiamSMS>"""
90        out = StringIO()
91        out.write(response)
92        communicator = portal.portal_smsCommunicator
93        data = self.publish(portal.id+'/portal_smsCommunicator/Response', 'mgr:mgrpw', env=None, extra=None, request_method='POST', stdin = out)
94        response = data.getBody()
95        self.assertEqual(response, """<?xml version="1.0" ?>\n<!DOCTYPE  xiamSMS SYSTEM "xiamSMSMessage.dtd" >\n<xiamSMS status="OK"><submitRsponse id="betye54">XML contained your response messages</submitRsponse></xiamSMS>""")
96
97
98tests.append(TestPloneSMSCommunicator)
99
100def test_suite():
101    from unittest import TestSuite, makeSuite
102    suite = TestSuite()
103    suite.addTest(makeSuite(TestPloneSMSCommunicator))
104    return suite
105
106if __name__ == '__main__':
107    framework()
Note: See TracBrowser for help on using the repository browser.