Exemplo n.º 1
0
 async def test_device_name_not_allowed_characters(self):
     assert get_device_name() == 'network name-04030201'
     response = await self.get_request(request_line=(
         b"GET /settings/submit_device_name/?name=Name+with+Spaces! HTTP/1.1"
     ))
     assert self.web_server.message == 'Error: Device name contains not allowed characters!'
     assert get_device_name() == 'network name-04030201'
     self.assert_response_parts(
         response,
         parts=(
             'HTTP/1.0 200 OK', '<html>',
             '<title>network name-04030201 - OFF</title>',
             '<p>Error: Device name contains not allowed characters!</p>',
             '<input type="text" name="name" value="Name-with-Spaces">',
             '</html>'))
def run_all_device_name_tests():
    save_device_name(mock_server, 'Test-1')
    assert get_device_name() == 'Test-1'
    assert mock_server.message == "Device name 'Test-1' saved."

    try:
        save_device_name(mock_server, 'a')
    except ValueError as new_name:
        assert str(new_name) == 'a'
        assert mock_server.message == (
            'Error: New Device name is too short. Enter at leased 3 characters!'
        )
    else:
        raise AssertionError('ValueError not raised')

    try:
        save_device_name(mock_server, 'Foo § Bar')
    except ValueError as new_name:
        assert str(new_name) == 'Foo-Bar'
        assert mock_server.message == 'Error: Device name contains not allowed characters!'
    else:
        raise AssertionError('ValueError not raised')

    try:
        save_device_name(mock_server, '_aäoöuü Cool!')
    except ValueError as new_name:
        assert str(new_name) == 'a-o-u-Cool'
        assert mock_server.message == 'Error: Device name contains not allowed characters!'
    else:
        raise AssertionError('ValueError not raised')
Exemplo n.º 3
0
 async def test_device_name_too_short(self):
     assert get_device_name() == 'network name-04030201'
     response = await self.get_request(
         request_line=(b"GET /settings/submit_device_name/?name=X HTTP/1.1")
     )
     assert self.web_server.message == (
         'Error: New Device name is too short. Enter at leased 3 characters!'
     )
     assert get_device_name() == 'network name-04030201'
     self.assert_response_parts(
         response,
         parts=
         ('HTTP/1.0 200 OK', '<html>',
          '<title>network name-04030201 - OFF</title>',
          '<p>Error: New Device name is too short. Enter at leased 3 characters!</p>',
          '<input type="text" name="name" value="X">', '</html>'))
Exemplo n.º 4
0
def init(context):
    print('Setup WiFi interfaces')

    Pins.power_led.off()

    from device_name import get_device_name
    context.device_name = get_device_name()

    del get_device_name
    del sys.modules['device_name']
    gc.collect()

    access_point = network.WLAN(network.AP_IF)  # access-point interface
    if access_point.active():
        print('deactivate access-point interface...')
        access_point.active(False)

    return ensure_connection(context)
Exemplo n.º 5
0
    async def send_html_page(self, writer, filename, content_iterator=None):
        gc.collect()

        from timezone import localtime_isoformat
        localtime = localtime_isoformat(sep=' ')
        del localtime_isoformat
        del sys.modules['timezone']
        gc.collect()

        from device_name import get_device_name
        device_name = get_device_name()
        del get_device_name
        del sys.modules['device_name']
        gc.collect()

        from power_timer import get_info_text
        power_timer_info_text = get_info_text(context=self.context)
        del get_info_text
        del sys.modules['power_timer']
        gc.collect()

        alloc = gc.mem_alloc() / 1024
        free = gc.mem_free() / 1024

        from template import render
        content = render(filename=filename,
                         context={
                             'version': self.version,
                             'device_name': device_name,
                             'state': Pins.relay.state,
                             'next_switch': power_timer_info_text,
                             'message': self.message,
                             'total': alloc + free,
                             'alloc': alloc,
                             'free': free,
                             'localtime': localtime,
                         },
                         content_iterator=content_iterator)
        for line in content:
            await writer.awrite(line)
        gc.collect()
        assert str(new_name) == 'Foo-Bar'
        assert mock_server.message == 'Error: Device name contains not allowed characters!'
    else:
        raise AssertionError('ValueError not raised')

    try:
        save_device_name(mock_server, '_aäoöuü Cool!')
    except ValueError as new_name:
        assert str(new_name) == 'a-o-u-Cool'
        assert mock_server.message == 'Error: Device name contains not allowed characters!'
    else:
        raise AssertionError('ValueError not raised')


if __name__ == '__main__':
    print('Run tests on device...')
    import sys
    sys.modules.clear()

    import gc
    gc.collect()

    origin_device_name = get_device_name()
    try:
        run_all_device_name_tests()
    finally:
        if origin_device_name is not None:
            save_device_name(mock_server, origin_device_name)

    print('OK')