def test_database(self) -> None:
        appInfo = AppInfo()
        databaseFilename = os.path.join(appInfo.path,
                                        ".temp/situationboard.sqlite")
        maxLastEvents = 10

        # check that creating a database works and yields an empty database
        db = Database(databaseFilename, reset=True)
        assert (db.getEventCount(textOnly=False) == 0)
        assert (db.getEventCount(textOnly=True) == 0)
        assert (db.getEvent(1302) is None)
        assert (len(db.getEvents(textOnly=False)) == 0)
        assert (len(db.getEvents(textOnly=True)) == 0)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 0)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 0)

        # check for valid stats
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 0)

        # check that inserting a binary event succeeds
        newEvent1 = self.__createBinaryEvent()
        newEventTemp1 = copy.deepcopy(newEvent1)
        assert (db.updateEvent(newEventTemp1) != 0)
        eventID1 = db.addEvent(newEventTemp1)
        assert (eventID1 != AlarmEvent.NO_ID)
        assert (newEventTemp1.eventID == eventID1)
        assert (db.getEventCount(textOnly=False) == 1)
        assert (db.getEventCount(textOnly=True) == 0)
        assert (len(db.getEvents(textOnly=False)) == 1)
        assert (len(db.getEvents(textOnly=True)) == 0)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 1)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 0)
        assert (db.getEvent(eventID1) is not None)

        # check for valid stats
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 0)

        # check that inserting a text event succeeds
        newEvent2 = self.__createTextEvent()
        newEvent2Temp = copy.deepcopy(newEvent2)
        eventID2 = db.addEvent(newEvent2Temp)
        assert (eventID2 != AlarmEvent.NO_ID)
        assert (newEvent2Temp.eventID == eventID2)
        assert (db.getEventCount(textOnly=False) == 2)
        assert (db.getEventCount(textOnly=True) == 1)
        assert (len(db.getEvents(textOnly=False)) == 2)
        assert (len(db.getEvents(textOnly=True)) == 1)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 2)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 1)
        assert (db.getEvent(eventID2) is not None)

        # check for valid stats
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 2)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 2)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 2)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 2)
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 1)

        db.commitAndClose()

        # check that reloading a database succeeds and yields existing data
        dbr = Database(databaseFilename, reset=False)
        assert (dbr.getEventCount(textOnly=False) == 2)
        assert (dbr.getEventCount(textOnly=True) == 1)
        assert (len(dbr.getEvents(textOnly=False)) == 2)
        assert (len(dbr.getEvents(textOnly=True)) == 1)
        assert (len(dbr.getLastEvents(maxLastEvents, textOnly=False)) == 2)
        assert (len(dbr.getLastEvents(maxLastEvents, textOnly=True)) == 1)

        # check that retrieving an event succeeds after reload succeeds and yields the correct data
        event1 = dbr.getEvent(eventID1)
        assert (event1 is not None)
        assert (isinstance(event1, AlarmEvent))
        assert (event1.eventID == eventID1)
        self.__compareEvents(event1, newEvent1)

        # check that retrieving an event succeeds after reload succeeds and yields the correct data
        event2 = dbr.getEvent(eventID2)
        assert (event2 is not None)
        assert (isinstance(event2, AlarmEvent))
        assert (event2.eventID == eventID2)
        self.__compareEvents(event2, newEvent2)

        # check that updating an event succeeds
        newEvent2Updated = copy.deepcopy(newEvent2Temp)
        self.__updateTextEvent(newEvent2Updated)
        newEvent2UpdatedTemp = copy.deepcopy(newEvent2Updated)
        assert (dbr.updateEvent(newEvent2UpdatedTemp) == 0)
        event2Updated = dbr.getEvent(eventID2)
        assert (event2Updated is not None)
        assert (isinstance(event2Updated, AlarmEvent))
        assert (event2Updated.eventID == eventID2)
        self.__compareEvents(event2Updated, newEvent2Updated)

        # check that deleting an event by ID succeeds
        assert (dbr.removeEventID(eventID1) == 0)
        assert (dbr.getEventCount(textOnly=False) == 1)
        assert (dbr.getEventCount(textOnly=True) == 1)
        assert (dbr.getEvent(eventID1) is None)
        assert (len(dbr.getEvents(textOnly=False)) == 1)
        assert (len(dbr.getEvents(textOnly=True)) == 1)
        assert (len(dbr.getLastEvents(maxLastEvents, textOnly=False)) == 1)
        assert (len(dbr.getLastEvents(maxLastEvents, textOnly=True)) == 1)

        # check that deleting an event succeeds
        assert (dbr.removeEvent(newEvent2Updated) == 0)
        assert (dbr.getEventCount(textOnly=False) == 0)
        assert (dbr.getEventCount(textOnly=True) == 0)
        assert (dbr.getEvent(eventID2) is None)
        assert (len(dbr.getEvents(textOnly=False)) == 0)
        assert (len(dbr.getEvents(textOnly=True)) == 0)
        assert (len(dbr.getLastEvents(maxLastEvents, textOnly=False)) == 0)
        assert (len(dbr.getLastEvents(maxLastEvents, textOnly=True)) == 0)

        # check that deleting non-existent events fails
        assert (dbr.removeEvent(event1) != 0)
        assert (dbr.removeEventID(eventID2) != 0)

        dbr.commitAndClose()
class Test_WebSocket:

    def setup_class(self) -> None:
        #pylint: disable=W0201
        self.appInfo = AppInfo()
        settingsFilenameOrig = os.path.join(self.appInfo.path, "misc/setup/situationboard_default.conf")
        settingsFilename = os.path.join(self.appInfo.path, ".temp/situationboard.conf")
        databaseFilename = os.path.join(self.appInfo.path, ".temp/situationboard.sqlite")

        shutil.copy(settingsFilenameOrig, settingsFilename)

        self.database = Database(databaseFilename, reset = True)

        settings = Settings(settingsFilename, self.appInfo.path)
        settings.setFrontendHeader("header")
        settings.setFrontendNews("news")
        settings.setBoolean(Settings.SECTION_BACKEND, "web_api", True)

        displayPowerManager = DisplayPowerManager(settings)

        self.webSocket = WebSocket(self.appInfo, settings, self.database)

        pluginManager = PluginManager(settings, self.database, self.webSocket, displayPowerManager)

        self.webSocket.init(pluginManager)

        self.appClient = self.webSocket.app_test_client()
        self.socketClient = self.webSocket.socket_test_client(self.appClient)

    def teardown_class(self) -> None:
        self.database.commitAndClose()

    def test_get_last_alarm_events(self) -> None:
        self.__emit("get_last_alarm_events", {'count': 10})
        (event, args) = self.__getReceived()
        assert(event == "last_alarm_events")
        assert(args['total_events'] == 0)
        assert(args['alarm_events'] == "[]")

    def test_get_stats(self) -> None:
        self.__emit("get_stats")
        (event, args) = self.__getReceived()
        assert(event == "stats")
        assert(args is not None)
        assert(args['total'] == 0)
        assert(args['year'] == 0)
        assert(args['month'] == 0)
        assert(args['today'] == 0)

    def test_get_header(self) -> None:
        self.__emit("get_header")
        (event, args) = self.__getReceived()
        assert(event == "header")
        assert(args is not None)
        assert(args['header'] == "header")

    def test_get_news(self) -> None:
        self.__emit("get_news")
        (event, args) = self.__getReceived()
        assert(event == "news")
        assert(args is not None)
        assert(args['news'] == "news")

    def test_get_state(self) -> None:
        self.__emit("get_state")
        (event, args) = self.__getReceived()
        assert(event == "state")
        assert(args is not None)
        assert(args['version'] == self.appInfo.version)
        assert(args['start_timestamp'] > 0)
        assert(args['source_state'] == SourceState.OK)

    def test_broadcast_alarm_event(self) -> None:
        alarmEvent = AlarmEvent(1302)
        self.webSocket.broadcastAlarmEvent(alarmEvent)
        (event, args) = self.__getReceived()
        assert(event == "alarm_event")
        assert(args is not None)
        assert(args['eventID'] == 1302)

    def test_broadcast_header(self) -> None:
        self.webSocket.broadcastHeader("TEST\nHEADER")
        (event, args) = self.__getReceived()
        assert(event == "header")
        assert(args is not None)
        assert(args['header'] == "TEST\nHEADER")

    def test_broadcast_news(self) -> None:
        self.webSocket.broadcastNews("TEST\nNEWS")
        (event, args) = self.__getReceived()
        assert(event == "news")
        assert(args is not None)
        assert(args['news'] == "TEST\nNEWS")

    def test_broadcast_database_changed(self) -> None:
        self.webSocket.broadcastDatabaseChanged()
        (event, _) = self.__getReceived()
        assert(event == "database_changed")

    def test_broadcast_calendar_changed(self) -> None:
        self.webSocket.broadcastCalendarChanged()
        (event, _) = self.__getReceived()
        assert (event == "calendar_changed")

    def test_static(self) -> None:
        response = self.appClient.get('/css/situationboard.css')
        assert(response.status_code == 200)

    def test_index(self) -> None:
        response = self.appClient.get('/')
        assert(response.status_code == 200)

    def test_javascript_frontend(self) -> None:
        response = self.appClient.get('/js/situationboard.js')
        assert(response.status_code == 200)

    def test_javascript_settings(self) -> None:
        response = self.appClient.get('/js/frontend/util/settings.js')
        assert(response.status_code == 200)

    def test_api_stats(self) -> None:
        response = self.appClient.get('/api/v1/stats')
        assert(response.status_code == 200)
        assert(response.json['result'] == "ok")
        stats = response.json['stats']
        assert(stats['total'] == 0)
        assert(stats['year'] == 0)
        assert(stats['month'] == 0)
        assert(stats['today'] == 0)

    def test_api_state(self) -> None:
        response = self.appClient.get('/api/v1/state')
        assert(response.status_code == 200)
        assert(response.json['result'] == "ok")
        state = response.json['state']
        assert(state['version'] == self.appInfo.version)
        assert(state['start_timestamp'] > 0)
        assert(state['source_state'] == SourceState.OK)

    def __emit(self, event: str, args: Optional[Dict[str, Any]] = None) -> None:
        if args is not None:
            self.socketClient.emit(event, args, namespace=WebSocket.NS)
        else:
            self.socketClient.emit(event, namespace=WebSocket.NS)

    def __getReceived(self) -> Tuple[Any, Any]:
        response = self.socketClient.get_received(WebSocket.NS)
        assert(len(response) == 1)
        result = response[0]
        event = result['name']
        argsList = result['args']
        asgsListLen = len(argsList)
        assert(asgsListLen <= 1)
        if asgsListLen == 1:
            args = argsList[0]
            return (event, args)

        return (event, None)
Exemple #3
0
    def test_handle_event(self) -> None:
        #pylint: disable=W0201
        appInfo = AppInfo()
        settingsFilenameOrig = os.path.join(
            appInfo.path, "misc/setup/situationboard_default.conf")
        settingsFilename = os.path.join(appInfo.path,
                                        ".temp/situationboard.conf")
        databaseFilename = os.path.join(appInfo.path,
                                        ".temp/situationboard.sqlite")

        shutil.copy(settingsFilenameOrig, settingsFilename)

        settings = Settings(settingsFilename, appInfo.path)

        maxLastEvents = 10

        displayPowerManager = DisplayPowerManager(settings)

        db = Database(databaseFilename, reset=True)

        webSocket = WebSocket(appInfo, settings, db)

        pluginManager = PluginManager(settings, db, webSocket,
                                      displayPowerManager)

        webSocket.init(pluginManager)

        # check that creating a database works and yields an empty database
        assert (db.getEventCount(textOnly=False) == 0)
        assert (db.getEventCount(textOnly=True) == 0)
        assert (db.getEvent(1302) is None)
        assert (len(db.getEvents(textOnly=False)) == 0)
        assert (len(db.getEvents(textOnly=True)) == 0)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 0)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 0)

        # check for valid stats
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 0)
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 0)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 0)

        action = ActionUpdateDatabase("", settings, db, webSocket)

        # check that inserting an alarm event succeeds
        newEvent = self.__createEvent()
        action.handleEvent(newEvent)
        assert (not newEvent.noID)
        assert (db.getEventCount(textOnly=False) == 1)
        assert (db.getEventCount(textOnly=True) == 1)
        assert (len(db.getEvents(textOnly=False)) == 1)
        assert (len(db.getEvents(textOnly=True)) == 1)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 1)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 1)
        newEventID = newEvent.eventID
        assert (db.getEvent(newEventID) is not None)

        # check for valid stats
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 1)

        # check that updating an alarm event succeeds
        self.__updateEvent(newEvent)
        action.handleEvent(newEvent)
        assert (newEvent.eventID == newEventID)
        assert (db.getEventCount(textOnly=False) == 1)
        assert (db.getEventCount(textOnly=True) == 1)
        assert (len(db.getEvents(textOnly=False)) == 1)
        assert (len(db.getEvents(textOnly=True)) == 1)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 1)
        assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 1)
        retrievedEvent = db.getEvent(newEventID)
        assert (retrievedEvent is not None)
        assert (retrievedEvent.comment ==
                Test_ActionUpdateDatabase.UPDATED_COMMENT)

        # check for valid stats
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 1)
        assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 1)
        assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 1)

        db.commitAndClose()
class Test_ActionUpdateFrontend:
    def setup_class(self) -> None:
        #pylint: disable=W0201
        appInfo = AppInfo()
        settingsFilenameOrig = os.path.join(
            appInfo.path, "misc/setup/situationboard_default.conf")
        settingsFilename = os.path.join(appInfo.path,
                                        ".temp/situationboard.conf")
        databaseFilename = os.path.join(appInfo.path,
                                        ".temp/situationboard.sqlite")

        shutil.copy(settingsFilenameOrig, settingsFilename)

        self.database = Database(databaseFilename, reset=True)

        settings = Settings(settingsFilename, appInfo.path)
        settings.setFrontendHeader("header")
        settings.setFrontendNews("news")

        displayPowerManager = DisplayPowerManager(settings)

        self.webSocket = WebSocket(appInfo, settings, self.database)

        pluginManager = PluginManager(settings, self.database, self.webSocket,
                                      displayPowerManager)

        self.webSocket.init(pluginManager)

        appClient = self.webSocket.app_test_client()
        self.socketClient = self.webSocket.socket_test_client(appClient)

        self.action = ActionUpdateFrontend("", settings, self.webSocket)

    def teardown_class(self) -> None:
        self.database.commitAndClose()

    def test_handle_event_alarm(self) -> None:
        alarmEvent = AlarmEvent(1302)
        self.action.handleEvent(alarmEvent)
        (event, args) = self.__getReceived()
        assert (event == "alarm_event")
        assert (args is not None)
        assert (args['eventID'] == 1302)

    def test_handle_event_setting_header(self) -> None:
        settingEvent = SettingEvent()
        settingEvent.key = "header"
        settingEvent.value = "new header"
        self.action.handleEvent(settingEvent)
        (event, args) = self.__getReceived()
        assert (event == "header")
        assert (args is not None)
        assert (args['header'] == "new header")

    def test_handle_event_setting_news(self) -> None:
        settingEvent = SettingEvent()
        settingEvent.key = "news"
        settingEvent.value = "new news"
        self.action.handleEvent(settingEvent)
        (event, args) = self.__getReceived()
        assert (event == "news")
        assert (args is not None)
        assert (args['news'] == "new news")

    def __getReceived(self) -> Tuple[Any, Any]:
        response = self.socketClient.get_received(WebSocket.NS)
        assert (len(response) == 1)
        result = response[0]
        event = result['name']
        argsList = result['args']
        asgsListLen = len(argsList)
        assert (asgsListLen <= 1)
        if asgsListLen == 1:
            args = argsList[0]
            return (event, args)

        return (event, None)