Commit 942cff7d authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

WebUI component:

- Added safe error_details() helper so non-gRPC exceptions no longer crash on e.details().
- Updated device add/delete/config/update flash error handling to use it.
- Mocked context_client.SelectDevice() so delete tests find the emulated device.
- Mocked webui.service.main.routes.context_client.ListContexts() and ListTopologies() so redirects to / do not try real DNS/gRPC resolution.
parent f8c0e2da
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -31,6 +31,10 @@ device = Blueprint('device', __name__, url_prefix='/device')
context_client = ContextClient()
device_client = DeviceClient()

def error_details(error : Exception) -> str:
    details = getattr(error, 'details', None)
    return details() if callable(details) else str(error)

@device.get('/')
def home():
    if 'context_uuid' not in session or 'topology_uuid' not in session:
@@ -164,7 +168,7 @@ def add():
            flash(f'New device was created with ID "{response.device_uuid.uuid}".', 'success')
            return redirect(url_for('device.home'))
        except Exception as e: # pylint: disable=broad-except
            flash(f'Problem adding the device. {e.details()}', 'danger')
            flash(f'Problem adding the device. {error_details(e)}', 'danger')
        
    return render_template('device/add.html', form=form,
                        submit_text='Add New Device')
@@ -233,7 +237,7 @@ def delete(device_uuid):

        context_client.close()
    except Exception as e: # pylint: disable=broad-except
        flash(f'Problem deleting device "{device_uuid}": {e.details()}', 'danger')
        flash(f'Problem deleting device "{device_uuid}": {error_details(e)}', 'danger')
        current_app.logger.exception(e)
    return redirect(url_for('device.home'))

@@ -260,7 +264,7 @@ def addconfig(device_uuid):
            flash(f'New configuration was created with ID "{response.device_uuid.uuid}".', 'success')
            return redirect(url_for('device.home'))
        except Exception as e: # pylint: disable=broad-except
             flash(f'Problem adding the device. {e.details()}', 'danger')
             flash(f'Problem adding the device. {error_details(e)}', 'danger')

    return render_template('device/addconfig.html', form=form,  submit_text='Add New Configuration')

@@ -299,5 +303,5 @@ def update(device_uuid):
            flash(f'Status of device with ID "{response.device_uuid.uuid}" was updated.', 'success')
            return redirect(url_for('device.home'))
        except Exception as e: # pylint: disable=broad-except
             flash(f'Problem updating the device. {e.details()}', 'danger')  
             flash(f'Problem updating the device. {error_details(e)}', 'danger')
    return render_template('device/update.html', device=response, form=form, submit_text='Update Device')
+23 −1
Original line number Diff line number Diff line
@@ -19,7 +19,10 @@ from flask.testing import FlaskClient
from flask.app import Flask
from flask.helpers import url_for
from common.DeviceTypes import DeviceTypeEnum
from common.proto.context_pb2 import ContextIdList, Empty, DeviceId, DeviceList, TopologyIdList
from common.proto.context_pb2 import (
    ContextIdList, ContextList, Empty, DeviceId, DeviceList,
    TopologyIdList, TopologyList,
)
# from device.client.DeviceClient import DeviceClient
from webui.service import create_app

@@ -33,6 +36,10 @@ class TestWebUI(ClientTestCase):

    def setUp(self, client: FlaskClient) -> None:

        device_list = DeviceList()
        device_obj = device_list.devices.add() # pylint: disable=no-member
        device_obj.device_id.device_uuid.uuid = 'EMULATED'

        self.mocker_delete_device = mock.patch('webui.service.device.routes.device_client.DeleteDevice')
        self.mocker_delete_device.return_value = Empty()
        self.mocker_delete_device.start()
@@ -48,6 +55,11 @@ class TestWebUI(ClientTestCase):
        self.mocker_list_devices.start()
        self.addCleanup(self.mocker_list_devices.stop)

        self.mocker_select_device = mock.patch('webui.service.device.routes.context_client.SelectDevice')
        self.mocker_select_device.return_value = device_list
        self.mocker_select_device.start()
        self.addCleanup(self.mocker_select_device.stop)

        self.mocker_add_device = mock.patch('webui.service.device.routes.device_client.AddDevice')
        self.mocker_add_device.return_value = DeviceId()
        self.mocker_add_device.start()
@@ -58,6 +70,16 @@ class TestWebUI(ClientTestCase):
        self.mocker_list_topology_ids.start()
        self.addCleanup(self.mocker_list_topology_ids.stop)

        self.mocker_main_list_contexts = mock.patch('webui.service.main.routes.context_client.ListContexts')
        self.mocker_main_list_contexts.return_value = ContextList()
        self.mocker_main_list_contexts.start()
        self.addCleanup(self.mocker_main_list_contexts.stop)

        self.mocker_main_list_topologies = mock.patch('webui.service.main.routes.context_client.ListTopologies')
        self.mocker_main_list_topologies.return_value = TopologyList()
        self.mocker_main_list_topologies.start()
        self.addCleanup(self.mocker_main_list_topologies.stop)

        return super().setUp(client)

    def tearDown(self, client: FlaskClient) -> None: