diff --git a/app/package.json b/app/package.json index 8b32f9aa..e4229e37 100644 --- a/app/package.json +++ b/app/package.json @@ -10,6 +10,7 @@ "react-dom": "^16.13.1", "react-dropzone": "^11.4.2", "react-keyed-file-browser": "^1.14.0", + "react-qr-code": "^2.0.7", "react-router-dom": "^5.2.0", "react-scripts": "^3.4.1", "react-semantic-toasts": "^0.6.5", diff --git a/app/src/components/Admin.js b/app/src/components/Admin.js index d067846d..4e844d64 100644 --- a/app/src/components/Admin.js +++ b/app/src/components/Admin.js @@ -7,6 +7,8 @@ import { Divider, Form, Header, + Icon, + Input, Loader, Placeholder, Table @@ -33,6 +35,7 @@ import { WROLModeMessage } from "./Common"; import {Route} from "react-router-dom"; +import QRCode from "react-qr-code"; class Settings extends React.Component { @@ -40,10 +43,15 @@ class Settings extends React.Component { super(props); this.state = { disabled: false, + hotspot_encryption: 'WPA', + qrCodeValue: '', + qrHidden: true, ready: false, download_on_startup: null, hotspot_on_startup: null, + hotspot_password: null, + hotspot_ssid: null, hotspot_status: null, throttle_on_startup: null, throttle_status: null, @@ -51,6 +59,8 @@ class Settings extends React.Component { } this.handleSubmit = this.handleSubmit.bind(this); + this.toggleQRCode = this.toggleQRCode.bind(this); + this.handleHotspotPasswordChange = this.handleHotspotPasswordChange.bind(this); } async componentDidMount() { @@ -60,11 +70,13 @@ class Settings extends React.Component { disabled: settings.wrol_mode, download_on_startup: settings.download_on_startup, hotspot_on_startup: settings.hotspot_on_startup, + hotspot_password: settings.hotspot_password, + hotspot_ssid: settings.hotspot_ssid, hotspot_status: settings.hotspot_status, throttle_on_startup: settings.throttle_on_startup, throttle_status: settings.throttle_status, timezone: {value: settings.timezone, label: settings.timezone}, - }); + }, this.handleHotspotPasswordChange); } async handleSubmit(e) { @@ -72,6 +84,8 @@ class Settings extends React.Component { let settings = { download_on_startup: this.state.download_on_startup, hotspot_on_startup: this.state.hotspot_on_startup, + hotspot_password: this.state.hotspot_password, + hotspot_ssid: this.state.hotspot_ssid, throttle_on_startup: this.state.throttle_on_startup, timezone: this.state.timezone.value, } @@ -82,9 +96,23 @@ class Settings extends React.Component { this.setState({timezone: timezone}); } - handleInputChange = async (e, name, checked) => { + handleInputChange = async (e, name, value) => { e.preventDefault(); - this.setState({[name]: checked}); + this.setState({[name]: value}); + } + + handleHotspotPasswordChange = async () => { + let {hotspot_ssid, hotspot_encryption, hotspot_password} = this.state; + // Special string which allows a mobile device to connect to a specific Wi-Fi. + let qrCodeValue = `WIFI:S:${hotspot_ssid};T:${hotspot_encryption};P:${hotspot_password};;`; + this.setState({qrCodeValue}); + } + + toggleQRCode(e) { + if (e !== undefined) { + e.preventDefault(); + } + this.setState({qrHidden: !this.state.qrHidden}); } render() { @@ -92,7 +120,16 @@ class Settings extends React.Component { return } - let {disabled, download_on_startup, hotspot_on_startup, throttle_on_startup, timezone} = this.state; + let { + disabled, + download_on_startup, + hotspot_on_startup, + hotspot_password, + hotspot_ssid, + qrCodeValue, + throttle_on_startup, + timezone, + } = this.state; return ( @@ -138,6 +175,45 @@ class Settings extends React.Component { onChange={(e, d) => this.handleInputChange(e, 'throttle_on_startup', d.checked)} /> + + + Hotspot SSID + + + this.setState({hotspot_ssid: d.value}, this.handleHotspotPasswordChange)} + /> + + + Hotspot Password + + + this.setState({hotspot_password: d.value}, this.handleHotspotPasswordChange)} + /> + + + + this.toggleQRCode(e)}> + + + + + Scan this QR Code to join hotspot + + + + + + + + Timezone str: + return self._config['hotspot_password'] + + @hotspot_password.setter + def hotspot_password(self, value: str): + self.update({'hotspot_password': value}) + + @property + def hotspot_ssid(self) -> str: + return self._config['hotspot_ssid'] + + @hotspot_ssid.setter + def hotspot_ssid(self, value: str): + self.update({'hotspot_ssid': value}) + @property def throttle_on_startup(self) -> bool: return self._config['throttle_on_startup'] diff --git a/wrolpi/conftest.py b/wrolpi/conftest.py index 49bd2d29..d7552df3 100644 --- a/wrolpi/conftest.py +++ b/wrolpi/conftest.py @@ -83,7 +83,7 @@ def test_directory() -> pathlib.Path: @pytest.fixture(autouse=True) -def test_config(test_directory): +def test_config(test_directory) -> pathlib.Path: """ Create a test config based off the example config. """ diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 77e93f71..6b9342e7 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -119,6 +119,8 @@ def get_settings(_: Request): settings = { 'download_on_startup': config.download_on_startup, 'hotspot_on_startup': config.hotspot_on_startup, + 'hotspot_password': config.hotspot_password, + 'hotspot_ssid': config.hotspot_ssid, 'hotspot_status': admin.hotspot_status().name, 'media_directory': get_media_directory(), 'throttle_on_startup': config.throttle_on_startup, diff --git a/wrolpi/schema.py b/wrolpi/schema.py index a1969cdd..4b01b9ef 100644 --- a/wrolpi/schema.py +++ b/wrolpi/schema.py @@ -16,6 +16,8 @@ class SettingsResponse: class SettingsRequest: download_on_startup: Optional[bool] = None hotspot_on_startup: Optional[bool] = None + hotspot_password: Optional[str] = None + hotspot_ssid: Optional[str] = None hotspot_status: Optional[bool] = None media_directory: Optional[str] = None throttle_on: Optional[bool] = None diff --git a/wrolpi/test/test_admin.py b/wrolpi/test/test_admin.py index cc7fd2be..76d52552 100644 --- a/wrolpi/test/test_admin.py +++ b/wrolpi/test/test_admin.py @@ -4,6 +4,7 @@ from unittest.mock import call from wrolpi import admin from wrolpi.admin import HotspotStatus +from wrolpi.common import WROLPI_CONFIG from wrolpi.test.common import skip_circleci @@ -68,6 +69,31 @@ def test_enable_hotspot_connected(): ) +def test_change_hotspot(test_config): + """The hotspot can be configured.""" + with mock.patch('wrolpi.admin.hotspot_status') as mock_hotspot_status, \ + mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \ + mock.patch('wrolpi.admin.NMCLI', PosixPath('/usr/bin/nmcli')): + mock_subprocess.check_output.return_value = 0 + mock_hotspot_status.return_value = HotspotStatus.disconnected + assert admin.enable_hotspot() is True + mock_subprocess.check_call.assert_called_once_with( + (PosixPath('/usr/bin/sudo'), PosixPath('/usr/bin/nmcli'), 'device', 'wifi', 'hotspot', 'ifname', 'wlan0', + 'ssid', 'WROLPi', 'password', 'wrolpi hotspot')) + + WROLPI_CONFIG.hotspot_ssid = 'New SSID' + WROLPI_CONFIG.hotspot_password = '$uper Secure *#' + with mock.patch('wrolpi.admin.hotspot_status') as mock_hotspot_status, \ + mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \ + mock.patch('wrolpi.admin.NMCLI', PosixPath('/usr/bin/nmcli')): + mock_subprocess.check_output.return_value = 0 + mock_hotspot_status.return_value = HotspotStatus.disconnected + assert admin.enable_hotspot() is True + mock_subprocess.check_call.assert_called_once_with( + (PosixPath('/usr/bin/sudo'), PosixPath('/usr/bin/nmcli'), 'device', 'wifi', 'hotspot', 'ifname', 'wlan0', + 'ssid', 'New SSID', 'password', '$uper Secure *#')) + + def test_throttle_on(): with mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \ mock.patch('wrolpi.admin.CPUFREQ_SET', PosixPath('/usr/bin/cpufreq-set')): diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index 45f27bf1..a50dce75 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -200,6 +200,14 @@ def test_hotspot_settings(test_session, test_client, test_config): config = get_config() assert config.hotspot_on_startup is True + # Hotspot password can be changed. + mock_admin.disable_hotspot.return_value = True + content = {'hotspot_password': 'new password', 'hotspot_ssid': 'new ssid'} + request, response = test_client.patch('/api/settings', content=json.dumps(content)) + assert response.status_code == HTTPStatus.NO_CONTENT + assert config.hotspot_password == 'new password' + assert config.hotspot_ssid == 'new ssid' + @skip_circleci def test_throttle_toggle(test_session, test_client, test_config):