class TestBotutils(unittest.TestCase):
    def setUp(self):
        self.message = MagicMock()
        self.prop = PropertyMock()

    def test_is_channel_valid_empty_config(self):
        self.prop.return_value = 'can be anything'
        type(self.message.channel).name = self.prop

        ret_val = botutils.is_channel_valid({}, 'anything', self.message)
        self.assertTrue(ret_val)
        self.prop.assert_not_called()

    def test_is_channel_valid_valid_config(self):
        self.prop.return_value = 'valid channel name'
        type(self.message.channel).name = self.prop

        ret_val = botutils.is_channel_valid(
            {'attribute': ['valid channel name']}, 'attribute', self.message)
        self.assertTrue(ret_val)
        self.prop.assert_called_once_with()

    # Helper
    def has_prefix_helper(self, config_prefix, actual_prefix, expected):
        self.prop.return_value = f'{actual_prefix}foo bar it'
        type(self.message).content = self.prop

        ret_val = botutils.has_prefix({'prefix': config_prefix}, self.message)
        self.assertEqual(ret_val, expected)
        self.prop.assert_called_once_with()

    def test_has_prefix_valid_len_one(self):
        self.has_prefix_helper('!', '!', True)

    def test_has_prefix_valid_len_random(self):
        prefix = '!' * random.randint(0, 100)
        self.has_prefix_helper(prefix, prefix, True)

    def test_has_prefix_valid_invalid(self):
        self.has_prefix_helper('a', 'b', False)

    # Helper
    def get_content_without_prefix_helper(self, prefix, message, expected):
        self.prop.return_value = message
        type(self.message).content = self.prop

        ret_val = botutils.get_content_without_prefix({'prefix': prefix},
                                                      self.message)
        self.assertEqual(ret_val, expected)
        self.prop.assert_called_once_with()

    def test_get_content_without_prefix_prefix_len_one(self):
        self.get_content_without_prefix_helper('!', '!one two three',
                                               'one two three')

    def test_get_content_without_prefix_prefix_len_random(self):
        prefix = '!' * random.randint(0, 100)
        self.get_content_without_prefix_helper(prefix,
                                               f'{prefix}one two three',
                                               'one two three')
Example #2
0
async def test_messages_from_bot_should_not_be_processed():
    mock_message = Mock()
    mock_message.author = client
    type(mock_message).channel = PropertyMock(DMChannel)
    content_mock = PropertyMock()
    type(mock_message).content = content_mock

    await handle_message(mock_message)

    content_mock.assert_not_called()
Example #3
0
async def test_text_channel_should_not_be_processed():
    mock_message = Mock()
    channel_mock = PropertyMock(TextChannel)
    type(mock_message).channel = channel_mock
    content_mock = PropertyMock()
    type(mock_message).content = content_mock

    await handle_message(mock_message)

    content_mock.assert_not_called()
Example #4
0
 def test_url(self, mock__auth: PropertyMock) -> None:
     """Testign getter of the proxy URL"""
     mock__auth.side_effect = lambda: 'L:P@'
     proxy: Proxy = Proxy()
     proxy.ip = ''
     proxy.port = 81
     proxy.protocol = 'HTTP'
     self.assertEqual(proxy.url, '')
     mock__auth.assert_not_called()
     proxy.ip = 'IP'
     self.assertEqual(proxy.url, 'HTTP://*****:*****@IP:81')
     mock__auth.assert_called_once_with()
    def test_does_not_write_unknown_devices(self, lcd_mock_class):
        lcd_mock = lcd_mock_class.return_value

        cursor_mock = PropertyMock()
        type(lcd_mock).cursor_pos = cursor_mock

        display = Display(["vessel"], ["heater", "cooler"])

        display.handle_switch("inbetweener", True)
        display.handle_temperature("tropics", 42.3, 3)

        cursor_mock.assert_not_called()
        lcd_mock.write_string.assert_not_called()
Example #6
0
    def test_zap_ajax_spider_raises_exception_if_not_started(self):
        """AJAX Spider raises exception if not started."""
        zap = Mock()
        zap.ajaxSpider.scan.return_value = "url_not_in_context"
        status = PropertyMock()
        type(zap.ajaxSpider).status = status
        target = "http://target.example.com"
        max_time = None

        with self.assertRaises(zap_common.ScanNotStartedException):
            zap_common.zap_ajax_spider(zap, target, max_time)

        zap.ajaxSpider.scan.assert_called_once_with(target, contextname=None)
        status.assert_not_called()
Example #7
0
    def test_creates_and_caches_proxy_objects(self):
        connection = Mock()
        p = PropertyMock(return_value='Account')
        type(connection).Account = p

        oc = amaxa.Operation(connection)

        proxy = oc.get_proxy_object('Account')

        self.assertEqual('Account', proxy)
        p.assert_called_once_with()

        p.reset_mock()
        proxy = oc.get_proxy_object('Account')

        # Proxy should be cached
        self.assertEqual('Account', proxy)
        p.assert_not_called()
Example #8
0
    def test_postprocess_testrun(self):
        test_1 = Mock()
        test_1_name = PropertyMock(return_value="test_1")
        type(test_1).name = test_1_name
        test_1_log = PropertyMock()
        type(test_1).log = test_1_log

        test_2 = Mock()
        test_2_name = PropertyMock(return_value="test_2")
        type(test_2).name = test_2_name
        test_2_log = PropertyMock()
        type(test_2).log = test_2_log

        test_3 = Mock()
        test_3_name = PropertyMock(return_value="test_3")
        type(test_3).name = test_3_name
        test_3_log = PropertyMock()
        type(test_3).log = test_3_log

        testrun = Mock()
        testrun_log = PropertyMock(return_value=TEST_LOG)
        type(testrun).log_file = testrun_log

        testrun.tests = Mock()
        testrun.tests.filter.return_value = [test_1, test_2, test_3]

        self.plugin.postprocess_testrun(testrun)
        testrun.tests.filter.assert_called_with(result=False)
        testrun_log.assert_called_with()

        test_1_name.assert_called_with()
        # uncomment when running with python3.6
        #test_1_log.assert_called()
        test_1.save.assert_called_with()

        # test_2 not present in the log
        test_2_name.assert_called_with()
        test_2_log.assert_not_called()
        test_2.save.assert_not_called()

        test_3_name.assert_called_with()
        # uncomment when running with python3.6
        #test_3_log.assert_called()
        test_3.save.assert_called_with()
    def test_filtered_state(self):
        """`filtered_state` calls thru to SessionState, unless disconnected."""
        for disconnected in (False, True):
            with self.subTest(f"disconnected={disconnected}"):
                safe_state, mock_state = _create_state_spy({}, disconnected)

                filtered_state_mock = PropertyMock(return_value={
                    "foo": 10,
                    "bar": 20
                })
                type(mock_state).filtered_state = filtered_state_mock

                result = safe_state.filtered_state
                if disconnected:
                    filtered_state_mock.assert_not_called()
                    self.assertEqual({}, result)
                else:
                    filtered_state_mock.assert_called_once()
                    self.assertEqual({"foo": 10, "bar": 20}, result)
Example #10
0
 def test_download_results_expired_url(self, get_mock):
     requests_result_mock = Mock()
     status_code_mock = PropertyMock(return_value=404)
     type(requests_result_mock).status_code = status_code_mock
     content_mock = PropertyMock(return_value=bytes())
     type(requests_result_mock).content = content_mock
     url_mock = PropertyMock(return_value="http://foo.bar.com/file.tar.xz")
     type(requests_result_mock).url = url_mock
     headers_mock = PropertyMock(
         return_value={"Content-Type": "application/x-tar"})
     type(requests_result_mock).headers = headers_mock
     get_mock.return_value = requests_result_mock
     results = self.plugin._download_results(RESULT_DICT)
     status_code_mock.assert_called_once_with()
     content_mock.assert_not_called()
     self.assertEqual(self.plugin.tradefed_results_url, RESULT_URL)
     self.assertIsNone(results.test_results)
     self.assertIsNone(results.tradefed_stdout)
     self.assertIsNone(results.tradefed_logcat)
Example #11
0
async def test_audio_input_sensor(hass, async_autosetup_sonos, soco, tv_event,
                                  no_media_event):
    """Test audio input sensor."""
    entity_registry = ent_reg.async_get(hass)

    subscription = soco.avTransport.subscribe.return_value
    sub_callback = subscription.callback
    sub_callback(tv_event)
    await hass.async_block_till_done()

    audio_input_sensor = entity_registry.entities[
        "sensor.zone_a_audio_input_format"]
    audio_input_state = hass.states.get(audio_input_sensor.entity_id)
    assert audio_input_state.state == "Dolby 5.1"

    # Set mocked input format to new value and ensure poll success
    no_input_mock = PropertyMock(return_value="No input")
    type(soco).soundbar_audio_input_format = no_input_mock

    async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
    await hass.async_block_till_done()

    no_input_mock.assert_called_once()
    audio_input_state = hass.states.get(audio_input_sensor.entity_id)
    assert audio_input_state.state == "No input"

    # Ensure state is not polled when source is not TV and state is already "No input"
    sub_callback(no_media_event)
    await hass.async_block_till_done()

    unpolled_mock = PropertyMock(return_value="Will not be polled")
    type(soco).soundbar_audio_input_format = unpolled_mock

    async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
    await hass.async_block_till_done()

    unpolled_mock.assert_not_called()
    audio_input_state = hass.states.get(audio_input_sensor.entity_id)
    assert audio_input_state.state == "No input"