Exemplo n.º 1
0
    def run(self):
        '''
        Finds all the tests modules in tests/, and runs them, exiting after they are all done
        '''
        from tests.server import TestServer
        from tests.test_core import WebserviceTest

        log.set_verbosity(self.verbose)
        if self.verbose >= 2:
            self.announce('Setting log level to DEBUG ({0})'.format(logging.DEBUG), level = 2)
            logging.basicConfig(level = logging.DEBUG)

        testfiles = [ ]
        if self.testmodule is None:
            for t in glob(pjoin(self._dir, 'tests', self.test_prefix + '*.py')):
                if not t.endswith('__init__.py'):
                    testfiles.append('.'.join(['tests', splitext(basename(t))[0]]))
        else:
            testfiles.append(self.testmodule)

        server = TestServer(daemonise = True, silent = (self.verbose < 3))
        server.start()
        WebserviceTest.TEST_PORT = server.port

        self.announce("Waiting for test server to start on port " + str(server.port), level=2)
        time.sleep(1)

        self.announce("Test files:" + str(testfiles), level=2)
        tests = TestLoader().loadTestsFromNames(testfiles)
        t = TextTestRunner(verbosity = self.verbose)
        result = t.run(tests)
        failed, errored = map(len, (result.failures, result.errors))
        exit(failed + errored)
Exemplo n.º 2
0
    def run(self):
        '''
        Finds all the tests modules in tests/, and runs them, exiting after they are all done
        '''
        from tests.server import TestServer
        from tests.test_core import WebserviceTest

        log.set_verbosity(self.verbose)
        if self.verbose >= 2:
            self.announce('Setting log level to DEBUG ({0})'.format(logging.DEBUG), level = 2)
            logging.basicConfig(level = logging.DEBUG)

        testfiles = [ ]
        if self.testmodule is None:
            for t in glob(pjoin(self._dir, 'tests', self.test_prefix + '*.py')):
                if not t.endswith('__init__.py'):
                    testfiles.append('.'.join(['tests', splitext(basename(t))[0]]))
        else:
            testfiles.append(self.testmodule)

        server = TestServer(daemonise = True, silent = (self.verbose < 3))
        server.start()
        WebserviceTest.TEST_PORT = server.port

        self.announce("Waiting for test server to start on port " + str(server.port), level=2)
        time.sleep(1)

        self.announce("Test files:" + str(testfiles), level=2)
        tests = TestLoader().loadTestsFromNames(testfiles)
        t = TextTestRunner(verbosity = self.verbose)
        result = t.run(tests)
        failed, errored = map(len, (result.failures, result.errors))
        exit(failed + errored)
class websocketTests(unittest.TestCase):
    def callback(self, message):
        self.message = message
    
    def setUp(self):
        self.server = TestServer(port=8080)
        self.ws = websocket(eventCallback = self.callback)       
    def test_timeout(self):
        with self.assertRaises(ValueError):
            self.ws.setTimeout(0)
    def test_sendWithAnswer(self):
        answer = self.ws.send("+;NOOP")
        self.assertTrue(answer.isValid())  
    def test_sendWithoutAnswer(self, ):
        self.ws.setTimeout(2)
        with self.assertRaises(VSCPException):
            self.ws.send("^C;NOOP")
    def test_receiveSeed(self):
        self.ws.received_message(createAnswer("+;AUTH0;d002c278b35c152eed9ee2f475a561f1"))
        self.assertEqual(self.ws.seed, "d002c278b35c152eed9ee2f475a561f1")   
    def test_receiveAnswer(self, ):
        self.ws.received_message(createAnswer("+;NOOP"))
        self.assertIsInstance(self.ws.answer, answer)   
    def test_receiveEvent(self, ):
        self.message = None
        self.ws.received_message(createAnswer("E;0,9,1,1,523627200,FF:FF:FF:FF:FF:FF:FF:FE:00:26:55:CA:00:06:00:00,0,1,2,3"))
        self.assertIsInstance(self.message, answer)
    def tearDown(self):
        self.ws.close()
        self.server.shutdown()
 def test_ConnectionLost(self, ):
     server = TestServer(port=8081)
     ws = websocket(port=8081, eventCallback = self.callback, timeout=1)
     server.shutdown()
     with self.assertRaises(VSCPNoCommException):
         ws.send("C;NOOP")
     sleep(ws.timeout+0.1)         
     ws.close()
class vscplibFunctionalTests(unittest.TestCase):
    def setUp(self, ):
        self.server = TestServer(port=8080,
                                 welcomeMessage = "+;AUTH0;d002c278b35c152eed9ee2f475a561f1|+;AUTH1")
        self.client = vscp(user='******', password='******', domain='mydomain.com')
        
    def test_checkAuthenticated(self, ):
        self.assertTrue(self.client.authenticated)
        
    def test_checkSeedAndKey(self, ):
        self.assertEqual(self.client.ws.seed, "d002c278b35c152eed9ee2f475a561f1")
        self.assertEqual(self.client.calculateKey('admin', 'secret', 'mydomain.com'),
                         '1aaabe6d6af390f9729618ad3af4782f')
    
    
    def tearDown(self, ):
        self.client.ws.close() 
        self.server.shutdown()
 def test_seedNotSent(self):
     a = TestServer(port=8080)
     with self.assertRaises(VSCPNoCommException):
         vscp()
     a.shutdown()
 def setUp(self, ):
     self.server = TestServer(port=8080,
                              welcomeMessage = "+;AUTH0;d002c278b35c152eed9ee2f475a561f1|+;AUTH1")
     self.client = vscp(user='******', password='******', domain='mydomain.com')
Exemplo n.º 8
0
class vscplibFunctionalTests(unittest.TestCase):
    def setUp(self, ):
        self.server = TestServer(port=8080,
                                 welcomeMessage = "+;AUTH0;d002c278b35c152eed9ee2f475a561f1|+;AUTH1")
        self.client = vscp(user='******', password='******', domain='mydomain.com')
        
    def __receiveMessage(self):
        self.receivedMessage = True
    
    def test_checkAuthenticated(self, ):
        self.assertTrue(self.client.authenticated)
        
    def test_checkSeedAndKey(self, ):
        self.assertEqual(self.client.ws.seed, "d002c278b35c152eed9ee2f475a561f1")
        self.assertEqual(self.client.calculateKey('admin', 'secret', 'mydomain.com'),
                         '1aaabe6d6af390f9729618ad3af4782f')
    
    def test_setResponseTimeout(self):
        self.client.setResponseTimeOut(1)
        self.assertEqual(self.client.ws.timeout, 1)
        with self.assertRaises(ValueError):
            self.client.setResponseTimeOut(0)
    
    def test_setHandler(self):
        self.assertIsNone(self.client.handler)
        with self.assertRaises(ValueError):
            self.client.setHandler("Malformed")
        self.client.setHandler(self.__receiveMessage)
        event = "E;0,9,1,1,523627200,FF:FF:FF:FF:FF:FF:FF:FE:00:26:55:CA:00:06:00:00,0,1,2,3"
        self.client.ws.send(event, False)
        sleep(0.1)
        self.assertTrue(self.receivedMessage)

    def test_doCommand(self, ):
        self.client.ws.send("^+;NOOP", False)
        self.assertEqual(self.client.isConnected(),
                         constant.VSCP_ERROR_SUCCESS)
        self.assertEqual(self.client.doCommand(),
                         constant.VSCP_ERROR_SUCCESS)
        self.client.ws.send("^-;2;Unkown command",False)
        self.assertEqual(self.client.doCommand(),
                         constant.VSCP_ERROR_ERROR)
        self.client.ws.connected = False
        self.assertEqual(self.client.doCommand(),
                         constant.VSCP_ERROR_CONNECTION)
        self.client.ws.connected = True
    
    def test_sendEvent(self, ):
        event = vscpEvent(vscp_class = 2,
                          vscp_type = 0,
                          vscp_data = [1])
        with self.assertRaises(ValueError):
            self.client.sendEvent("Malformed Arg")
        self.client.ws.connected = False
        self.assertEqual(self.client.doCommand(),
                         constant.VSCP_ERROR_CONNECTION)
        self.client.ws.connected = True
        self.client.ws.send("^+;EVENT", False)
        self.assertEqual(self.client.isConnected(),
                         constant.VSCP_ERROR_SUCCESS)
        self.assertEqual(self.client.sendEvent(event),
                         constant.VSCP_ERROR_SUCCESS)
        self.client.ws.send("^-;2;Unkown command",False)
        self.assertEqual(self.client.sendEvent(event),
                         constant.VSCP_ERROR_ERROR)
        
        
    def test_ReceiveLoop(self, ):
        self.assertFalse(self.client.eventStreaming)
        self.client.ws.send("^+;OPEN|+;CLOSE", False)
        self.client.enterReceiveLoop()
        self.assertTrue(self.client.eventStreaming)
        self.client.quitReceiveLoop()
        self.assertFalse(self.client.eventStreaming)
        
    def test_receiveData(self, ):
        GUID = "FF:FF:FF:FF:FF:FF:FF:FE:00:26:55:CA:00:06:00:00"
        event = "E;0,9,1,2,523627200,"+GUID+",0,1,2,3"
        self.assertFalse(self.client.isDataAvailable())
        self.assertEqual(self.client.receiveEvent(), None)
        self.client.ws.send(event, False)
        sleep(0.05)
        self.assertTrue(self.client.isDataAvailable())
        self.assertIsInstance(self.client.receiveEvent(), vscpEvent)
        
    def test_blockingReceiveData(self, ):
        event = "E;0,9,1,1,523627200,FF:FF:FF:FF:FF:FF:FF:FE:00:26:55:CA:00:06:00:00,0,1,2,3"
        with self.assertRaises(VSCPException):
            self.client.blockingReceiveEvent()
        self.client.eventStreaming = True
        self.client.authenticated = False
        self.assertFalse(self.client.isDataAvailable())
        with self.assertRaises(VSCPException):
            self.client.blockingReceiveEvent()
        self.client.authenticated = True
        self.client.ws.connected = True
        self.client.ws.send(event, False)
        self.assertIsInstance(self.client.blockingReceiveEvent(),
                              vscpEvent)
    
    
    def tearDown(self, ):
        self.client.ws.close() 
        self.server.shutdown()
 def setUp(self):
     self.server = TestServer(port=8080)
     self.ws = websocket(eventCallback = self.callback)