Esempio n. 1
0
 def connect_ws(self):
     log.msg(
         'WebSocket protocol has been selected. URL: {0}; HOST: {1}; PORT: {2};'
         .format(self.ws_url, self.ws_host, self.ws_port))
     factory = WebSocketFactory(self)
     factory.timestamp = self.server_time
     reactor.connectDeviceHive(self.ws_url, factory)
Esempio n. 2
0
 def connect_ws(self):
     log.msg('WebSocket protocol has been selected. URL: {0}; HOST: {1}; PORT: {2};'.format(self.ws_url, self.ws_host, self.ws_port))
     factory = WebSocketFactory(self)
     factory.url  = self.ws_url
     factory.host = self.ws_host
     factory.port = self.ws_port
     factory.timestamp = self.server_time
     reactor.connectTCP(factory.host, factory.port, factory)
 def setUp(self):
     self.transport = StringTransport()
     self.handler = Handler()
     self.factory = WebSocketFactory(self.handler)
     self.proto = self.factory.buildProtocol(None)
     self.proto.makeConnection(self.transport)
     self.proto.dataReceived('HTTP/1.1 101 OK\r\n\r\n')
     self.proto.socket.rand = Random(1)
     self.transport.clear()
 def setUp(self):
     self.transport = StringTransport()
     self.handler = Handler()
     self.factory = WebSocketFactory(self.handler)
     self.proto = self.factory.buildProtocol(None)
     self.proto.makeConnection(self.transport)
     self.proto.dataReceived('HTTP/1.1 101 OK\r\n\r\n')
     self.proto.socket.rand = Random(1)
     self.transport.clear()
Esempio n. 5
0
class WsClientSendingTestCase(unittest.TestCase):
    def setUp(self):
        self.transport = StringTransport()
        self.handler = Handler()
        self.factory = WebSocketFactory(self.handler)
    
    def test_buildProtocol(self):
        proto = self.factory.buildProtocol(None)
        self.assertIsInstance(proto, WebSocketDeviceHiveProtocol)
    
    def test_send_message(self):
        # testing headers sending
        # this should result in connected event

        proto = self.factory.buildProtocol(None)
        proto.makeConnection(self.transport)

        origin = ''.join((
            'GET /device HTTP/1.1\r\n',
            'Host: localhost\r\n',
            'Upgrade: websocket\r\n',
            'Connection: Upgrade\r\n',
            'Sec-WebSocket-Key: {0}\r\n'.format(proto.socket.security_key),
            'Origin: http://localhost\r\n',
            'Sec-WebSocket-Protocol: device-hive, devicehive\r\n',
            'Sec-WebSocket-Version: 13\r\n\r\n',
        ))
        self.assertEquals(origin, self.transport.value())

        proto.dataReceived('HTTP/1.1 101 OK\r\n\r\n')
        self.assertTrue(self.handler.is_connected)

        self.transport.clear()
        # testing message sending
        proto.socket.rand = Random(1)
        defer = self.factory.send_message({'test': True})
        self.assertIsInstance(defer, Deferred)
        self.assertEquals('{{"test": true, "requestId": {0}}}'.format(max(proto.msg_callbacks.keys())), decode_ws_message(self.transport.value()))
        self.assertFalse(defer.called)
        # testing message response
        request_id = max(proto.msg_callbacks.keys())
        proto.socket.frame_received(WS_OPCODE_TEXT_FRAME, '{{"requestId":{0},"done":1}}'.format(request_id))
        self.assertTrue(defer.called)
 def setUp(self):
     self.transport = StringTransport()
     self.handler = Handler()
     self.factory = WebSocketFactory(self.handler)
class WsClientMethodsTestCase(unittest.TestCase):
    def setUp(self):
        self.transport = StringTransport()
        self.handler = Handler()
        self.factory = WebSocketFactory(self.handler)
        self.proto = self.factory.buildProtocol(None)
        self.proto.makeConnection(self.transport)
        self.proto.dataReceived('HTTP/1.1 101 OK\r\n\r\n')
        self.proto.socket.rand = Random(1)
        self.transport.clear()

    def test_notify(self):
        self.transport.clear()
        defer = self.factory.notify('nt', {
            'a': 1,
            'b': 2
        },
                                    device_id='123',
                                    device_key='321')
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'notification/insert',
                u'notification': {
                    u'notification': u'nt',
                    u'parameters': {
                        u'a': 1,
                        u'b': 2
                    }
                },
                u'deviceKey': u'321',
                u'deviceId': u'123',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))

    def test_subscribe(self):
        self.transport.clear()
        defer = self.factory.subscribe('123', '321')
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'command/subscribe',
                u'deviceId': u'123',
                u'deviceKey': u'321',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))

    def test_unsubscribe(self):
        self.transport.clear()
        defer = self.factory.unsubscribe('123', '312')
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'command/unsubscribe',
                u'deviceKey': u'312',
                u'deviceId': u'123',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))

    def test_authenticate(self):
        self.transport.clear()
        defer = self.factory.authenticate('123', '321')
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'authenticate',
                u'deviceId': u'123',
                u'deviceKey': u'321',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))

    def test_device_save(self):
        class TestDev(object):
            implements(IDeviceInfo)
            id = 'td_id'
            key = 'td_key'
            name = 'td_name'
            equipment = []
            status = None
            network = None
            device_class = None

        self.transport.clear()
        # minimal message
        info = TestDev()
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'device/save',
                u'device': {
                    u'equipment': [],
                    u'name': u'td_name',
                    u'key': u'td_key'
                },
                u'deviceKey': u'td_key',
                u'deviceId': u'td_id',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))
        # with equipment
        self.transport.clear()
        info.equipment = [
            devicehive.Equipment(name='en', code='cd', type='tp', data=None)
        ]
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'device/save',
                u'device': {
                    u'equipment': [{
                        u'name': u'en',
                        u'code': u'cd',
                        u'type': u'tp'
                    }],
                    u'name':
                    u'td_name',
                    u'key':
                    u'td_key'
                },
                u'deviceKey': u'td_key',
                u'deviceId': u'td_id',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))
        # equipment with data
        self.transport.clear()
        info.equipment = [
            devicehive.Equipment(name='en', code='cd', type='tp', data='dt')
        ]
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'device/save',
                u'device': {
                    u'equipment': [{
                        u'name': u'en',
                        u'code': u'cd',
                        u'type': u'tp',
                        u'data': u'dt'
                    }],
                    u'name':
                    u'td_name',
                    u'key':
                    u'td_key'
                },
                u'deviceKey': u'td_key',
                u'deviceId': u'td_id',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))
        # with network
        self.transport.clear()
        info.network = devicehive.Network(id='nid',
                                          key='nkey',
                                          name='nname',
                                          descr='ndesr')
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals(
            {
                u'action': u'device/save',
                u'device': {
                    u'equipment': [{
                        u'name': u'en',
                        u'code': u'cd',
                        u'type': u'tp',
                        u'data': u'dt'
                    }],
                    u'name':
                    u'td_name',
                    u'key':
                    u'td_key',
                    u'network': {
                        u'id': u'nid',
                        u'name': u'nname',
                        u'key': u'nkey',
                        u'description': 'ndesr'
                    }
                },
                u'deviceKey': u'td_key',
                u'deviceId': u'td_id',
                u'requestId': max(self.proto.msg_callbacks.keys())
            }, json.loads(s))
 def setUp(self):
     self.transport = StringTransport()
     self.handler = Handler()
     self.factory = WebSocketFactory(self.handler)
class WsClientMethodsTestCase(unittest.TestCase):
    def setUp(self):
        self.transport = StringTransport()
        self.handler = Handler()
        self.factory = WebSocketFactory(self.handler)
        self.proto = self.factory.buildProtocol(None)
        self.proto.makeConnection(self.transport)
        self.proto.dataReceived('HTTP/1.1 101 OK\r\n\r\n')
        self.proto.socket.rand = Random(1)
        self.transport.clear()
    
    def test_notify(self):
        self.transport.clear()
        defer = self.factory.notify('nt', {'a':1,'b':2}, device_id='123', device_key='321')
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'notification/insert',
                           u'notification': {u'notification': u'nt',
                                             u'parameters': {u'a': 1, u'b': 2}},
                           u'deviceKey': u'321',
                           u'deviceId': u'123',
                           u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
    
    def test_subscribe(self):
        self.transport.clear()
        defer = self.factory.subscribe('123', '321')
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'command/subscribe',
                           u'deviceId': u'123',
                           u'deviceKey': u'321',
                           u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
    
    def test_unsubscribe(self):
        self.transport.clear()
        defer = self.factory.unsubscribe('123', '312')
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'command/unsubscribe',
                           u'deviceKey': u'312',
                           u'deviceId': u'123',
                           u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
    
    def test_authenticate(self):
        self.transport.clear()
        defer = self.factory.authenticate('123', '321')
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'authenticate',
                           u'deviceId': u'123',
                           u'deviceKey': u'321',
                           u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
    
    def test_device_save(self):
        class TestDev(object):
            implements(IDeviceInfo)
            id = 'td_id'
            key = 'td_key'
            name = 'td_name'
            equipment = []
            status = None
            network = None
            device_class = None
        self.transport.clear()
        # minimal message
        info = TestDev()
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'device/save',
                           u'device':
                                {u'equipment': [],
                                 u'name': u'td_name',
                                 u'key': u'td_key'},
                            u'deviceKey': u'td_key',
                            u'deviceId': u'td_id',
                            u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
        # with equipment
        self.transport.clear()
        info.equipment = [devicehive.Equipment(name = 'en', code='cd', type='tp', data = None)]
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'device/save',
                           u'device':
                                {u'equipment': [{u'name': u'en', u'code': u'cd', u'type': u'tp'}],
                                 u'name': u'td_name',
                                 u'key': u'td_key'},
                            u'deviceKey': u'td_key',
                            u'deviceId': u'td_id',
                            u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
        # equipment with data
        self.transport.clear()
        info.equipment = [devicehive.Equipment(name = 'en', code='cd', type='tp', data = 'dt')]
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'device/save',
                           u'device':
                                {u'equipment': [{u'name': u'en', u'code': u'cd', u'type': u'tp', u'data': u'dt'}],
                                 u'name': u'td_name',
                                 u'key': u'td_key'},
                            u'deviceKey': u'td_key',
                            u'deviceId': u'td_id',
                            u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))
        # with network
        self.transport.clear()
        info.network = devicehive.Network(id = 'nid', key = 'nkey', name = 'nname', descr = 'ndesr')
        self.factory.device_save(info)
        s = decode_ws_message(self.transport.value())
        self.assertEquals({u'action': u'device/save',
                           u'device':
                                {u'equipment': [{u'name': u'en', u'code': u'cd', u'type': u'tp', u'data': u'dt'}],
                                 u'name': u'td_name',
                                 u'key': u'td_key',
                                 u'network': {u'id': u'nid',
                                              u'name': u'nname',
                                              u'key': u'nkey',
                                              u'description': 'ndesr'}},
                            u'deviceKey': u'td_key',
                            u'deviceId': u'td_id',
                            u'requestId': max(self.proto.msg_callbacks.keys())}, json.loads(s))