示例#1
0
def test_main_get_with_valid_values(capsys, reset_globals):
    """Test --get with valid values (with string, number, boolean)"""
    sys.argv = [
        '', '--get', 'ls_secs', '--get', 'wifi_ssid', '--get', 'fixed_position'
    ]
    Globals.getInstance().set_args(sys.argv)

    with patch('meshtastic.serial_interface.SerialInterface') as mo:

        # kind of cheating here, we are setting up the node
        mocked_node = MagicMock(autospec=Node)
        anode = mocked_node()
        anode.radioConfig.preferences.wifi_ssid = 'foo'
        anode.radioConfig.preferences.ls_secs = 300
        anode.radioConfig.preferences.fixed_position = False
        Globals.getInstance().set_target_node(anode)

        main()

        mo.assert_called()

        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'ls_secs: 300', out, re.MULTILINE)
        assert re.search(r'wifi_ssid: foo', out, re.MULTILINE)
        assert re.search(r'fixed_position: False', out, re.MULTILINE)
        assert err == ''
示例#2
0
def test_main_pos_fields_valid_values(capsys, reset_globals):
    """Test --pos-fields with valid values"""
    sys.argv = ['', '--pos-fields', 'POS_GEO_SEP', 'POS_ALT_MSL']
    Globals.getInstance().set_args(sys.argv)

    pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags)

    with patch('meshtastic.serial_interface.SerialInterface') as mo:
        with patch('meshtastic.radioconfig_pb2.PositionFlags',
                   return_value=pos_flags) as mrc:

            mrc.Value.side_effect = [4, 2]

            main()

            mrc.Value.assert_called()
            mo.assert_called()

            out, err = capsys.readouterr()
            assert re.search(r'Connected to radio', out, re.MULTILINE)
            assert re.search(r'Setting position fields to 6', out,
                             re.MULTILINE)
            assert re.search(r'Set position_flags to 6', out, re.MULTILINE)
            assert re.search(r'Writing modified preferences to device', out,
                             re.MULTILINE)
            assert err == ''
示例#3
0
def test_main_set_ham_to_KI123(capsys, reset_globals):
    """Test --set-ham KI123"""
    sys.argv = ['', '--set-ham', 'KI123']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    def mock_turnOffEncryptionOnPrimaryChannel():
        print('inside mocked turnOffEncryptionOnPrimaryChannel')

    def mock_setOwner(name, is_licensed):
        print('inside mocked setOwner')

    mocked_node.turnOffEncryptionOnPrimaryChannel.side_effect = mock_turnOffEncryptionOnPrimaryChannel
    mocked_node.setOwner.side_effect = mock_setOwner

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Setting HAM ID to KI123', out, re.MULTILINE)
        assert re.search(r'inside mocked setOwner', out, re.MULTILINE)
        assert re.search(r'inside mocked turnOffEncryptionOnPrimaryChannel',
                         out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#4
0
def test_main_pos_fields_no_args(capsys, reset_globals):
    """Test --pos-fields no args (which shows settings)"""
    sys.argv = ['', '--pos-fields']
    Globals.getInstance().set_args(sys.argv)

    pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags)

    with patch('meshtastic.serial_interface.SerialInterface') as mo:
        with patch('meshtastic.radioconfig_pb2.PositionFlags',
                   return_value=pos_flags) as mrc:
            # kind of cheating here, we are setting up the node
            mocked_node = MagicMock(autospec=Node)
            anode = mocked_node()
            anode.radioConfig.preferences.position_flags = 35
            Globals.getInstance().set_target_node(anode)

            mrc.values.return_value = [0, 1, 2, 4, 8, 16, 32, 64, 128, 256]
            # Note: When you use side_effect and a list, each call will use a value from the front of the list then
            # remove that value from the list. If there are three values in the list, we expect it to be called
            # three times.
            mrc.Name.side_effect = [
                'POS_ALTITUDE', 'POS_ALT_MSL', 'POS_BATTERY'
            ]

            main()

            mrc.Name.assert_called()
            mrc.values.assert_called()
            mo.assert_called()

            out, err = capsys.readouterr()
            assert re.search(r'Connected to radio', out, re.MULTILINE)
            assert re.search(r'POS_ALTITUDE POS_ALT_MSL POS_BATTERY', out,
                             re.MULTILINE)
            assert err == ''
示例#5
0
def test_main_no_proto(capsys, reset_globals):
    """Test --noproto (using --info for output)"""
    sys.argv = ['', '--info', '--noproto']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=SerialInterface)

    def mock_showInfo():
        print('inside mocked showInfo')

    iface.showInfo.side_effect = mock_showInfo

    # Override the time.sleep so there is no loop
    def my_sleep(amount):
        sys.exit(0)

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface):
        with patch('time.sleep', side_effect=my_sleep):
            with pytest.raises(SystemExit) as pytest_wrapped_e:
                main()
            assert pytest_wrapped_e.type == SystemExit
            assert pytest_wrapped_e.value.code == 0
            out, err = capsys.readouterr()
            assert re.search(r'Connected to radio', out, re.MULTILINE)
            assert re.search(r'inside mocked showInfo', out, re.MULTILINE)
            assert err == ''
示例#6
0
def test_main_set_team_valid(capsys, reset_globals):
    """Test --set-team"""
    sys.argv = ['', '--set-team', 'CYAN']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    def mock_setOwner(team):
        print('inside mocked setOwner')

    mocked_node.setOwner.side_effect = mock_setOwner

    iface = MagicMock(autospec=SerialInterface)
    iface.localNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        with patch('meshtastic.mesh_pb2.Team') as mm:
            mm.Name.return_value = 'FAKENAME'
            mm.Value.return_value = 'FAKEVAL'
            main()
            out, err = capsys.readouterr()
            assert re.search(r'Connected to radio', out, re.MULTILINE)
            assert re.search(r'Setting team to', out, re.MULTILINE)
            assert err == ''
            mo.assert_called()
            mm.Name.assert_called()
            mm.Value.assert_called()
示例#7
0
def test_main_ch_add_invalid_name_too_long(capsys, reset_globals):
    """Test --ch-add with invalid channel name, name too long"""
    sys.argv = ['', '--ch-add', 'testingtestingtesting']
    Globals.getInstance().set_args(sys.argv)

    mocked_channel = MagicMock(autospec=Channel)
    # TODO: figure out how to get it to print the channel name instead of MagicMock

    mocked_node = MagicMock(autospec=Node)
    # set it up so we do not already have a channel named this
    mocked_node.getChannelByName.return_value = False
    # set it up so we have free channels
    mocked_node.getDisabledChannel.return_value = mocked_channel

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            main()
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Warning: Channel name must be shorter', out,
                         re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#8
0
def test_main_ch_add_valid(capsys, reset_globals):
    """Test --ch-add with valid channel name, and that channel name does not already exist"""
    sys.argv = ['', '--ch-add', 'testing']
    Globals.getInstance().set_args(sys.argv)

    mocked_channel = MagicMock(autospec=Channel)
    # TODO: figure out how to get it to print the channel name instead of MagicMock

    mocked_node = MagicMock(autospec=Node)
    # set it up so we do not already have a channel named this
    mocked_node.getChannelByName.return_value = False
    # set it up so we have free channels
    mocked_node.getDisabledChannel.return_value = mocked_channel

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Writing modified channels to device', out,
                         re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#9
0
def test_main_ch_add_but_no_more_channels(capsys, reset_globals):
    """Test --ch-add with but there are no more channels"""
    sys.argv = ['', '--ch-add', 'testing']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)
    # set it up so we do not already have a channel named this
    mocked_node.getChannelByName.return_value = False
    # set it up so we have free channels
    mocked_node.getDisabledChannel.return_value = None

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            main()
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Warning: No free channels were found', out,
                         re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#10
0
def test_main_onReceive_empty(reset_globals):
    """Test onReceive"""
    sys.argv = ['']
    Globals.getInstance().set_args(sys.argv)
    iface = MagicMock(autospec=SerialInterface)
    packet = {'decoded': 'foo'}
    onReceive(packet, iface)
示例#11
0
def test_main_pos_fields_arg_of_zero(capsys, reset_globals):
    """Test --pos-fields an arg of 0 (which shows list)"""
    sys.argv = ['', '--pos-fields', '0']
    Globals.getInstance().set_args(sys.argv)

    pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags)

    with patch('meshtastic.serial_interface.SerialInterface') as mo:
        with patch('meshtastic.radioconfig_pb2.PositionFlags',
                   return_value=pos_flags) as mrc:

            def throw_value_error_exception(exc):
                raise ValueError()

            mrc.Value.side_effect = throw_value_error_exception
            mrc.keys.return_value = [
                'POS_UNDEFINED', 'POS_ALTITUDE', 'POS_ALT_MSL', 'POS_GEO_SEP',
                'POS_DOP', 'POS_HVDOP', 'POS_BATTERY', 'POS_SATINVIEW',
                'POS_SEQ_NOS', 'POS_TIMESTAMP'
            ]

            main()

            mrc.Value.assert_called()
            mrc.keys.assert_called()
            mo.assert_called()

            out, err = capsys.readouterr()
            assert re.search(r'Connected to radio', out, re.MULTILINE)
            assert re.search(r'ERROR: supported position fields are:', out,
                             re.MULTILINE)
            assert re.search(r"['POS_UNDEFINED', 'POS_ALTITUDE', 'POS_ALT_MSL', 'POS_GEO_SEP',"\
                              "'POS_DOP', 'POS_HVDOP', 'POS_BATTERY', 'POS_SATINVIEW', 'POS_SEQ_NOS',"\
                              "'POS_TIMESTAMP']", out, re.MULTILINE)
            assert err == ''
示例#12
0
def test_main_setalt(capsys, reset_globals):
    """Test --setalt"""
    sys.argv = ['', '--setalt', '51']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    def mock_writeConfig():
        print('inside mocked writeConfig')

    mocked_node.writeConfig.side_effect = mock_writeConfig

    iface = MagicMock(autospec=SerialInterface)

    def mock_sendPosition(lat, lon, alt):
        print('inside mocked sendPosition')

    iface.sendPosition.side_effect = mock_sendPosition
    iface.localNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Fixing altitude', out, re.MULTILINE)
        assert re.search(r'Setting device position', out, re.MULTILINE)
        assert re.search(r'inside mocked sendPosition', out, re.MULTILINE)
        # TODO: Why does this not work? assert re.search(r'inside mocked writeConfig', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#13
0
def test_main_init_parser_no_args(capsys, reset_globals):
    """Test no arguments"""
    sys.argv = ['']
    Globals.getInstance().set_args(sys.argv)
    initParser()
    out, err = capsys.readouterr()
    assert out == ''
    assert err == ''
示例#14
0
def test_main_main_no_args(reset_globals):
    """Test with no args"""
    sys.argv = ['']
    Globals.getInstance().set_args(sys.argv)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1
示例#15
0
def test_main_test_one_port(patched_find_ports, reset_globals):
    """Test --test with one fake port"""
    sys.argv = ['', '--test']
    Globals.getInstance().set_args(sys.argv)

    assert Globals.getInstance().get_target_node() is None
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1
    patched_find_ports.assert_called()
示例#16
0
def test_main_test_two_ports_fails(patched_find_ports, patched_test_all,
                                   reset_globals):
    """Test --test two fake ports and testAll() is a simulated failure"""
    sys.argv = ['', '--test']
    Globals.getInstance().set_args(sys.argv)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1
    # TODO: why does this fail? patched_find_ports.assert_called()
    patched_test_all.assert_called()
示例#17
0
def test_main_main_version(capsys, reset_globals):
    """Test --version"""
    sys.argv = ['', '--version']
    Globals.getInstance().set_args(sys.argv)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 0
    out, err = capsys.readouterr()
    assert re.match(r'[0-9]+\.[0-9]+\.[0-9]', out)
    assert err == ''
示例#18
0
def test_main_seturl(capsys, reset_globals):
    """Test --seturl (url used below is what is generated after a factory_reset)"""
    sys.argv = ['', '--seturl', 'https://www.meshtastic.org/d/#CgUYAyIBAQ']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=SerialInterface)
    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#19
0
def test_main_setchan(capsys, reset_globals):
    """Test --setchan (deprecated)"""
    sys.argv = ['', '--setchan', 'a', 'b']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=SerialInterface)

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface):
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            main()
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
示例#20
0
def test_main_set_owner_to_bob(capsys, reset_globals):
    """Test --set-owner bob"""
    sys.argv = ['', '--set-owner', 'bob']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=SerialInterface)
    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Setting device owner to bob', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#21
0
def test_main_ch_index_no_devices(patched_find_ports, capsys, reset_globals):
    """Test --ch-index 1"""
    sys.argv = ['', '--ch-index', '1']
    Globals.getInstance().set_args(sys.argv)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main()
    assert Globals.getInstance().get_channel_index() == 1
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1
    out, err = capsys.readouterr()
    assert re.search(r'Warning: No Meshtastic devices detected', out,
                     re.MULTILINE)
    assert err == ''
    patched_find_ports.assert_called()
示例#22
0
def test_main_support(capsys, reset_globals):
    """Test --support"""
    sys.argv = ['', '--support']
    Globals.getInstance().set_args(sys.argv)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 0
    out, err = capsys.readouterr()
    assert re.search(r'System', out, re.MULTILINE)
    assert re.search(r'Platform', out, re.MULTILINE)
    assert re.search(r'Machine', out, re.MULTILINE)
    assert re.search(r'Executable', out, re.MULTILINE)
    assert err == ''
示例#23
0
def test_main_onConnection(reset_globals, capsys):
    """Test onConnection"""
    sys.argv = ['']
    Globals.getInstance().set_args(sys.argv)
    iface = MagicMock(autospec=SerialInterface)

    class TempTopic:
        """ temp class for topic """
        def getName(self):
            """ return the fake name of a topic"""
            return 'foo'

    mytopic = TempTopic()
    onConnection(iface, mytopic)
    out, err = capsys.readouterr()
    assert re.search(r'Connection changed: foo', out, re.MULTILINE)
    assert err == ''
示例#24
0
def test_main_qr(capsys, reset_globals):
    """Test --qr"""
    sys.argv = ['', '--qr']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=SerialInterface)
    # TODO: could mock/check url
    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Primary channel URL', out, re.MULTILINE)
        # if a qr code is generated it will have lots of these
        assert re.search(r'\[7m', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#25
0
def test_main_ch_del(capsys, reset_globals):
    """Test --ch-del with valid secondary channel to be deleted"""
    sys.argv = ['', '--ch-del', '--ch-index', '1']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Deleting channel', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#26
0
def test_main_set_valid(capsys, reset_globals):
    """Test --set with valid field"""
    sys.argv = ['', '--set', 'wifi_ssid', 'foo']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Set wifi_ssid to foo', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#27
0
def test_main_ch_disable_valid_secondary_channel(capsys, reset_globals):
    """Test --ch-disable with --ch-index"""
    sys.argv = ['', '--ch-disable', '--ch-index', '1']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Writing modified channels', out, re.MULTILINE)
        assert err == ''
        assert Globals.getInstance().get_channel_index() == 1
        mo.assert_called()
示例#28
0
def test_main_info_with_ble_interface(capsys, reset_globals):
    """Test --info"""
    sys.argv = ['', '--info', '--ble', 'foo']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=BLEInterface)

    def mock_showInfo():
        print('inside mocked showInfo')

    iface.showInfo.side_effect = mock_showInfo
    with patch('meshtastic.ble_interface.BLEInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'inside mocked showInfo', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#29
0
def test_main_nodes(capsys, reset_globals):
    """Test --nodes"""
    sys.argv = ['', '--nodes']
    Globals.getInstance().set_args(sys.argv)

    iface = MagicMock(autospec=SerialInterface)

    def mock_showNodes():
        print('inside mocked showNodes')

    iface.showNodes.side_effect = mock_showNodes
    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        main()
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'inside mocked showNodes', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()
示例#30
0
def test_main_ch_del_no_ch_index_specified(capsys, reset_globals):
    """Test --ch-del without a valid ch-index"""
    sys.argv = ['', '--ch-del']
    Globals.getInstance().set_args(sys.argv)

    mocked_node = MagicMock(autospec=Node)

    iface = MagicMock(autospec=SerialInterface)
    iface.getNode.return_value = mocked_node

    with patch('meshtastic.serial_interface.SerialInterface',
               return_value=iface) as mo:
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            main()
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
        out, err = capsys.readouterr()
        assert re.search(r'Connected to radio', out, re.MULTILINE)
        assert re.search(r'Warning: Need to specify', out, re.MULTILINE)
        assert err == ''
        mo.assert_called()