def test_protocol_relay(self): relay = Mock() relay.relay = Mock() protocol = RelayProtocol({ 'self': { 'id': 'the-user-id', } }) protocol.relay = relay protocol.bot_user_id = 'the-user-id' protocol.onMessage('{"foo": "bar"}', False) relay.relay.assert_called_with('the-user-id', {"foo": "bar"})
def test_disconnect(self): url, r = yield self.mk_relay() protocol = RelayProtocol({'self': {'id': 'the-user-id'}}) protocol.transport = StringTransport() protocol.transport.loseConnection = Mock() protocol.transport.loseConnection.return_value = None r.connections['bot-id'] = protocol yield treq.post('%s/disconnect' % (url, ), auth=('bot-id', 'bot-token'), pool=self.pool) protocol.transport.loseConnection.assert_called_with()
def test_disconnect(self): url, r = yield self.mk_relay() protocol = RelayProtocol({ 'self': { 'id': 'the-user-id' } }) protocol.transport = StringTransport() protocol.transport.loseConnection = Mock() protocol.transport.loseConnection.return_value = None r.connections['bot-id'] = protocol yield treq.post( '%s/disconnect' % (url,), auth=('bot-id', 'bot-token'), pool=self.pool) protocol.transport.loseConnection.assert_called_with()
def test_get_protocol(self): _, r = yield self.mk_relay() mock_proto = RelayProtocol({ 'self': { 'id': 'the-user-id' }, }) r.rtm_start = Mock() r.rtm_start.return_value = succeed(mock_proto) protocol = yield r.get_protocol('bot-id', 'bot-token') self.assertEqual(protocol, mock_proto) self.assertEqual(r.connections, {'bot-id': mock_proto}) r.rtm_start.assert_called_with('bot-token')
def test_protocol_relay(self): relay = Mock() relay.relay = Mock() protocol = RelayProtocol({'self': { 'id': 'the-user-id', }}) protocol.relay = relay protocol.bot_user_id = 'the-user-id' protocol.onMessage('{"foo": "bar"}', False) relay.relay.assert_called_with('the-user-id', {"foo": "bar"})
def test_ping(self): protocol = RelayProtocol({ 'self': { 'id': 'the-user-id', }, }) protocol.clock = Clock() protocol.factory = Mock() protocol.sendMessage = Mock() protocol.onOpen() protocol.clock.advance(3) protocol.sendMessage.assert_has_calls( [call('{"type": "ping"}'), call('{"type": "ping"}')])
def test_protocol_close(self): _, r = yield self.mk_relay() protocol = RelayProtocol({ 'self': { 'id': 'the-user-id', }, }) protocol.factory = Mock() protocol.bot_user_id = 'bot-user-id' protocol.relay = r r.set_protocol('bot-user-id', protocol) protocol.onClose(True, None, None) self.assertEqual(r.connections, {})
def test_connect_patched(self): url, r = yield self.mk_relay() r.get_protocol = Mock() r.get_protocol.return_value = succeed( RelayProtocol({'self': { 'id': 'the-user-id' }})) response = yield treq.post('%s/connect' % (url, ), auth=('bot-id', 'bot-token'), pool=self.pool) data = yield response.json() self.assertEqual(data, { 'self': { 'id': 'the-user-id', }, }) r.get_protocol.assert_called_with(bot_id='bot-id', bot_token='bot-token')
def test_ping(self): protocol = RelayProtocol({ 'self': { 'id': 'the-user-id', }, }) protocol.clock = Clock() protocol.factory = Mock() protocol.sendMessage = Mock() protocol.onOpen() protocol.clock.advance(3) protocol.sendMessage.assert_has_calls([ call('{"type": "ping"}'), call('{"type": "ping"}')])