Merge pull request #123 from lrnselfreliance/feature/55-hotspot-password

Hotspot SSID/password can be changed
This commit is contained in:
lrnselfreliance 2022-05-01 08:42:19 -06:00 • committed by GitHub
commit 8ab61bd8a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 124 additions and 12 deletions

View file

@ -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",

View file

@ -7,7 +7,9 @@ import {
Divider,
Form,
Header,
Icon,
Loader,
Modal,
Placeholder,
Table
} from "semantic-ui-react";
@ -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: '',
qrOpen: false,
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,7 @@ class Settings extends React.Component {
}
this.handleSubmit = this.handleSubmit.bind(this);
this.handleHotspotChange = this.handleHotspotChange.bind(this);
}
async componentDidMount() {
@ -60,11 +69,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.handleHotspotChange);
}
async handleSubmit(e) {
@ -72,19 +83,24 @@ 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,
}
await saveSettings(settings);
}
handleTimezoneChange = async (timezone) => {
this.setState({timezone: timezone});
handleInputChange = async (e, name, value) => {
if (e) {e.preventDefault()}
this.setState({[name]: value});
}
handleInputChange = async (e, name, checked) => {
e.preventDefault();
this.setState({[name]: checked});
handleHotspotChange = 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});
}
render() {
@ -92,7 +108,16 @@ class Settings extends React.Component {
return <Loader active inline='centered'/>
}
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 (
<Container fluid>
@ -138,11 +163,41 @@ class Settings extends React.Component {
onChange={(e, d) => this.handleInputChange(e, 'throttle_on_startup', d.checked)}
/>
<Form.Group inline>
<Form.Input
label='Hotspot SSID'
value={hotspot_ssid}
disabled={disabled || hotspot_ssid === null}
onChange={(e, d) =>
this.setState({hotspot_ssid: d.value}, this.handleHotspotChange)}
/>
<Form.Input
label='Hotspot Password'
disabled={disabled || hotspot_password === null}
value={hotspot_password}
onChange={(e, d) =>
this.setState({hotspot_password: d.value}, this.handleHotspotChange)}
/>
<Modal closeIcon
onClose={() => this.setState({qrOpen: false})}
onOpen={() => this.setState({qrOpen: true})}
open={this.state.qrOpen}
trigger={<Button icon><Icon name='qrcode' size='big'/></Button>}
>
<Modal.Header>
Scan this code to join the hotspot
</Modal.Header>
<Modal.Content>
<QRCode value={qrCodeValue} size={300}/>
</Modal.Content>
</Modal>
</Form.Group>
<Form.Field>
<label>Timezone</label>
<TimezoneSelect
value={timezone}
onChange={this.handleTimezoneChange}
onChange={(v) => this.handleInputChange(null, 'timezone', v)}
/>
</Form.Field>

View file

@ -2,7 +2,7 @@ import enum
import subprocess
from wrolpi.cmd import which
from wrolpi.common import logger
from wrolpi.common import logger, WROLPI_CONFIG
from wrolpi.vars import DEFAULT_CPU_FREQUENCY, DOCKERIZED
logger = logger.getChild(__name__)
@ -59,7 +59,7 @@ def enable_hotspot():
elif status == HotspotStatus.disconnected:
# Radio is on, but not connected. Good, turn it into a hotspot.
cmd = (SUDO, NMCLI, 'device', 'wifi', 'hotspot', 'ifname', 'wlan0',
'ssid', 'WROLPi', 'password', 'wrolpi hotspot')
'ssid', WROLPI_CONFIG.hotspot_ssid, 'password', WROLPI_CONFIG.hotspot_password)
subprocess.check_call(cmd)
return True
elif status == HotspotStatus.unavailable:

View file

@ -262,6 +262,8 @@ class WROLPiConfig(ConfigFile):
default_config = dict(
download_on_startup=True,
hotspot_on_startup=True,
hotspot_password='wrolpi hotspot',
hotspot_ssid='WROLPi',
throttle_on_startup=False,
timezone=str(DEFAULT_TIMEZONE),
wrol_mode=False,
@ -283,6 +285,22 @@ class WROLPiConfig(ConfigFile):
def hotspot_on_startup(self, value: bool):
self.update({'hotspot_on_startup': value})
@property
def hotspot_password(self) -> 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']

View file

@ -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.
"""

View file

@ -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,

View file

@ -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

View file

@ -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')):

View file

@ -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):

View file

@ -1,4 +1,4 @@
__version__ = '0.4.5-beta'
__version__ = '0.4.6-beta'
import subprocess