Fix network tests, add form help and validator.

This commit is contained in:
James Valleroy 2015-03-17 21:13:44 -04:00 committed by Sunil Mohan Adapa
parent 45086ff003
commit 8d9b388375
5 changed files with 67 additions and 26 deletions

View File

@ -16,6 +16,7 @@
#
from django import forms
from django.core import validators
from gettext import gettext as _
from plinth import network
@ -33,11 +34,16 @@ class AddEthernetForm(forms.Form):
name = forms.CharField(label=_('Connection Name'))
zone = forms.ChoiceField(
label=_('Firewall Zone'),
help_text=_('The firewall zone will control which services are \
available over this interfaces. Select Internal only for trusted networks.'),
choices=[('external', 'External'), ('internal', 'Internal')])
ipv4_method = forms.ChoiceField(
label=_('IPv4 Addressing Method'),
choices=[('auto', 'Automatic (DHCP)'), ('manual', 'Manual')])
ipv4_address = forms.CharField(label=_('Address'), required=False)
ipv4_address = forms.CharField(
label=_('Address'),
validators=[validators.validate_ipv4_address],
required=False)
class AddWifiForm(forms.Form):
@ -45,13 +51,22 @@ class AddWifiForm(forms.Form):
name = forms.CharField(label=_('Connection Name'))
zone = forms.ChoiceField(
label=_('Firewall Zone'),
help_text=_('The firewall zone will control which services are \
available over this interfaces. Select Internal only for trusted networks.'),
choices=[('external', 'External'), ('internal', 'Internal')])
ssid = forms.CharField(label=_('SSID'))
ssid = forms.CharField(
label=_('SSID'),
help_text=_('The visible name of the network.'))
auth_mode = forms.ChoiceField(
label=_('Authentication Mode'),
help_text=_('Select WPA if the wireless network is secured and \
requires clients to have the password to connect.'),
choices=[('wpa', 'WPA'), ('open', 'Open')])
passphrase = forms.CharField(label=_('Passphrase'), required=False)
ipv4_method = forms.ChoiceField(
label=_('IPv4 Addressing Method'),
choices=[('auto', 'Automatic (DHCP)'), ('manual', 'Manual')])
ipv4_address = forms.CharField(label=_('Address'), required=False)
ipv4_address = forms.CharField(
label=_('Address'),
validators=[validators.validate_ipv4_address],
required=False)

View File

@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from dbus.exceptions import DBusException
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse_lazy
@ -100,6 +99,11 @@ def edit(request, conn_id):
_('Cannot edit connection %s: '
'Connection type not supported.') % name)
return redirect(reverse_lazy('networks:index'))
else:
return TemplateResponse(request, 'connections_edit.html',
{'title': _('Edit Connection'),
'subsubmenu': subsubmenu,
'form': form})
else:
try:
form_data['zone'] = settings['connection']['zone']

View File

@ -42,16 +42,19 @@
<script type="text/javascript">
(function($) {
$('#id_ipv4_address').prop("readOnly", true);
$('#id_name').focus();
if ($("#id_ipv4_method").prop("value") != "manual") {
$("#id_ipv4_address").prop("readOnly", true);
}
$("#id_name").focus();
$('#id_ipv4_method').change(function() {
if ($('#id_ipv4_method').prop('value') == 'manual') {
$('#id_ipv4_address').prop("readOnly", false);
$('#id_ipv4_address').prop("required", true);
$("#id_ipv4_method").change(function() {
if ($("#id_ipv4_method").prop("value") == 'manual') {
$("#id_ipv4_address").prop("readOnly", false);
$("#id_ipv4_address").prop("required", true);
} else {
$('#id_ipv4_address').prop("readOnly", true);
$('#id_ipv4_address').prop("required", false);
$("#id_ipv4_address").val("");
$("#id_ipv4_address").prop("readOnly", true);
$("#id_ipv4_address").prop("required", false);
}
});

View File

@ -42,18 +42,19 @@
<script type="text/javascript">
(function($) {
if ($('#id_ipv4_method').prop('value') != 'manual') {
$('#id_ipv4_address').prop("readOnly", true);
if ($("#id_ipv4_method").prop("value") != "manual") {
$("#id_ipv4_address").prop("readOnly", true);
}
$('#id_name').focus();
$("#id_name").focus();
$('#id_ipv4_method').change(function() {
if ($('#id_ipv4_method').prop('value') == 'manual') {
$('#id_ipv4_address').prop("readOnly", false);
$('#id_ipv4_address').prop("required", true);
$("#id_ipv4_method").change(function() {
if ($("#id_ipv4_method").prop("value") == "manual") {
$("#id_ipv4_address").prop("readOnly", false);
$("#id_ipv4_address").prop("required", true);
} else {
$('#id_ipv4_address').prop("readOnly", true);
$('#id_ipv4_address').prop("required", false);
$("#id_ipv4_address").val("");
$("#id_ipv4_address").prop("readOnly", true);
$("#id_ipv4_address").prop("required", false);
}
});

View File

@ -27,9 +27,12 @@ class TestNetwork(unittest.TestCase):
@classmethod
def setUpClass(cls):
network.add_ethernet_connection(
'plinth_test_eth', 'auto', '')
'plinth_test_eth', 'internal',
'auto', '')
network.add_wifi_connection(
'plinth_test_wifi', 'plinthtestwifi', 'auto', '')
'plinth_test_wifi', 'external',
'plinthtestwifi', 'open', '',
'auto', '')
@classmethod
def tearDownClass(cls):
@ -39,6 +42,7 @@ class TestNetwork(unittest.TestCase):
def test_get_connection_list(self):
"""Check that we can get a list of available connections."""
connections = network.get_connection_list()
self.assertTrue('plinth_test_eth' in [x['name'] for x in connections])
self.assertTrue('plinth_test_wifi' in [x['name'] for x in connections])
@ -49,6 +53,7 @@ class TestNetwork(unittest.TestCase):
conn.GetSettings()['connection']['id'], 'plinth_test_eth')
conn = network.get_connection('plinth_test_wifi')
self.assertEqual(
conn.GetSettings()['connection']['id'], 'plinth_test_wifi')
@ -56,17 +61,30 @@ class TestNetwork(unittest.TestCase):
"""Check that we can update an ethernet connection."""
conn = network.get_connection('plinth_test_eth')
network.edit_ethernet_connection(
conn, 'plinth_test_eth', 'manual', '169.254.0.1')
conn, 'plinth_test_eth', 'external', 'manual', '169.254.0.1')
conn = network.get_connection('plinth_test_eth')
self.assertEqual(conn.GetSettings()['connection']['zone'], 'external')
self.assertEqual(conn.GetSettings()['ipv4']['method'], 'manual')
def test_edit_wifi_connection(self):
"""Check that we can update a wifi connection."""
conn = network.get_connection('plinth_test_wifi')
network.edit_wifi_connection(
conn, 'plinth_test_wifi', 'plinthtestwifi2', 'auto', '')
conn, 'plinth_test_wifi', 'external',
'plinthtestwifi2', 'wpa', 'secretpassword',
'auto', '')
conn = network.get_connection('plinth_test_wifi')
self.assertEqual(conn.GetSettings()['802-11-wireless']['ssid'], 'plinthtestwifi2')
self.assertEqual(conn.GetSettings()['connection']['zone'], 'external')
self.assertEqual(
conn.GetSettings()['802-11-wireless']['ssid'], 'plinthtestwifi2')
self.assertEqual(
conn.GetSettings()['802-11-wireless-security']['key-mgmt'],
'wpa-psk')
self.assertEqual(
conn.GetSecrets()['802-11-wireless-security']['psk'],
'secretpassword')
if __name__ == "__main__":
unittest.main()