示例#1
0
    def test_eq_different(self):
        """
        L{ims.data.Incident.__eq__} between two different incidents.
        """
        incident1 = Incident.from_json_text(incident1_text, 1)
        incident2 = Incident.from_json_text(incident2_text, 2)

        self.assertNotEquals(incident1, incident2)
示例#2
0
    def test_eq_equal(self):
        """
        L{ims.data.Incident.__eq__} between equal incidents.
        """
        incident1a = Incident.from_json_text(incident1_text, 1)
        incident1b = Incident.from_json_text(incident1_text, 1)

        self.assertEquals(incident1a, incident1a)
        self.assertEquals(incident1a, incident1b)
示例#3
0
    def test_to_json_text(self):
        """
        L{ims.data.Incident.to_json_text} produces same incident as json.
        """
        incident1a = Incident.from_json_text(incident1_text, 1)
        incident1a_text = incident1a.to_json_text()
        incident1b = Incident.from_json_text(incident1a_text, 1)

        self.assertEquals(incident1a, incident1b)
示例#4
0
    def test_validate(self):
        """
        L{ims.data.Incident.validate} of valid incident.
        """
        incident = Incident.from_json_text(incident1_text, 1)

        incident.validate()
示例#5
0
    def test_eq_other(self):
        """
        L{ims.data.Incident.__eq__} between incident and other type.
        """
        incident = Incident.from_json_text(incident1_text, 1)

        self.assertNotEquals(incident, object())
示例#6
0
    def new_incident(self, request):
        if self.config.ReadOnly:
            set_response_header(
                request, HeaderName.contentType, ContentType.plain
            )
            request.setResponseCode(http.FORBIDDEN)
            return "Server is in read-only mode."

        incident = Incident.from_json_io(
            request.content, number=self.storage.next_incident_number()
        )

        # Edit report entrys to add author
        for entry in incident.report_entries:
            entry.author = self.avatarId.decode("utf-8")

        self.storage.write_incident(incident)

        request.setResponseCode(http.CREATED)

        request.setHeader(
            HeaderName.incidentNumber.value,
            incident.number
        )
        request.setHeader(
            HeaderName.location.value,
            url_for(request, "get_incident", {"number": incident.number})
        )

        return ""
示例#7
0
    def test_validate_none_rangers(self):
        """
        L{ims.data.Incident.validate} of incident with C{None} Rangers.
        """
        incident = Incident.from_json_text(incident1_text, 1)
        incident.rangers = None

        self.assertRaises(InvalidDataError, incident.validate)
示例#8
0
 def test_str(self):
     """
     L{ims.data.Incident.__str__}
     """
     incident = Incident.from_json_text(incident1_text, 1)
     self.assertEquals(
         str(incident),
         "{i.number}: {i.summary}".format(i=incident)
     )
示例#9
0
    def test_validate_not_unicode_types(self):
        """
        L{ims.data.Incident.validate} of incident with non-unicode
        incident types.
        """
        incident = Incident.from_json_text(incident1_text, 1)
        incident.incident_types.append(b"bytes")

        self.assertRaises(InvalidDataError, incident.validate)
示例#10
0
    def test_validate_not_int_priority(self):
        """
        L{ims.data.Incident.validate} of incident with non-int
        priority.
        """
        incident = Incident.from_json_text(incident1_text, 1)
        incident.priority = "1"

        self.assertRaises(InvalidDataError, incident.validate)
示例#11
0
    def test_validate_not_datetime_closed(self):
        """
        L{ims.data.Incident.validate} of incident with non-datetime
        closed time.
        """
        incident = Incident.from_json_text(incident1_text, 1)
        incident.closed = 0

        self.assertRaises(InvalidDataError, incident.validate)
示例#12
0
    def test_validate_not_unicode_summary(self):
        """
        L{ims.data.Incident.validate} of incident with non-unicode
        summary.
        """
        incident = Incident.from_json_text(incident1_text, 1)
        incident.summary = b"bytes"

        self.assertRaises(InvalidDataError, incident.validate)
示例#13
0
    def test_validate_not_int_priority_bounds(self):
        """
        L{ims.data.Incident.validate} of incident with out-of-bounds
        priority.
        """
        incident = Incident.from_json_text(incident1_text, 1)

        incident.priority = "0"
        self.assertRaises(InvalidDataError, incident.validate)

        incident.priority = "6"
        self.assertRaises(InvalidDataError, incident.validate)
示例#14
0
    def new_incident(self, request):
        incident = Incident.from_json_io(request.content, number=self.storage.next_incident_number())

        # Edit report entrys to add author
        for entry in incident.report_entries:
            entry.author = self.avatarId.decode("utf-8")

        self.storage.write_incident(incident)

        request.setResponseCode(http.CREATED)

        request.setHeader(
            HeaderName.incidentNumber.value,
            incident.number
        )
        request.setHeader(
            HeaderName.location.value,
            url_for(request, "get_incident", {"number": incident.number})
        )

        return "";
示例#15
0
 def test_repr(self):
     """
     L{ims.data.Incident.__repr__}
     """
     incident = Incident.from_json_text(incident1_text, 1)
     self.assertEquals(
         repr(incident),
         "{i.__class__.__name__}("
         "number={i.number!r},"
         "rangers={i.rangers!r},"
         "location={i.location!r},"
         "incident_types={i.incident_types!r},"
         "summary={i.summary!r},"
         "report_entries={i.report_entries!r},"
         "created={i.created!r},"
         "dispatched={i.dispatched!r},"
         "on_scene={i.on_scene!r},"
         "closed={i.closed!r},"
         "priority={i.priority!r})"
         .format(i=incident)
     )
示例#16
0
    def edit_incident(self, request, number):
        number = int(number)
        incident = self.storage.read_incident_with_number(number)

        #
        # Handle the changes requested by the client
        #
        edits_json = from_json_io(request.content)
        edits = Incident.from_json(edits_json, number=number, validate=False)

        user_entries = []

        system_messages = []
        state_changes = []

        def log_edit_value(key, old, new):
            if key is JSON.number:
                return

            if old == new:
                #print "Client submitted unchaged value for {0}: {1}".format(JSON.describe(key), new)
                return

            if key in JSON.states():
                state_changes.append((key, new))
                return

            system_messages.append(u"Changed {0} to: {1}".format(JSON.describe(key), new if new else u"<no value>"))

        def diff_set(key, old, new):
            old = frozenset(old if old else ())
            new = frozenset(new if new else ())
            unchanged = old & new
            removed = old ^ unchanged
            added = new ^ unchanged
            return added, removed

        def log_edit_set(key, added, removed):
            if added:
                system_messages.append(u"Added to {0}: {1}".format(JSON.describe(key), ", ".join(added)))
            if removed:
                system_messages.append(u"Removed from {0}: {1}".format(JSON.describe(key), ", ".join(removed)))

        for key in edits_json.keys():
            key = JSON.lookupByValue(key)

            if key is JSON.report_entries:
                if edits.report_entries is not None:
                    for entry in edits.report_entries:
                        # Edit report entries to add author
                        entry.author = self.avatarId.decode("utf-8")
                        user_entries.append(entry)
            elif key is JSON.location_name:
                if edits.location.name is not None:
                    log_edit_value(key, incident.location.name, edits.location.name)
                    incident.location.name = edits.location.name
            elif key is JSON.location_address:
                if edits.location.address is not None:
                    log_edit_value(key, incident.location.address, edits.location.address)
                    incident.location.address = edits.location.address
            elif key is JSON.ranger_handles:
                if edits.rangers is not None:
                    added, removed = diff_set(key, incident.rangers, edits.rangers)
                    log_edit_set(key, [r.handle for r in added], [r.handle for r in removed])
                    incident.rangers = edits.rangers
            elif key is JSON.incident_types:
                if edits.incident_types is not None:
                    log_edit_set(key, *diff_set(key, incident.incident_types, edits.incident_types))
                    incident.incident_types = edits.incident_types
            else:
                attr_name = key.name
                attr_value = getattr(edits, attr_name)

                if key in (JSON.created, JSON.dispatched, JSON.on_scene, JSON.closed):
                    if edits.created is None:
                        # If created is None, then we aren't editing state.
                        # (It would be weird if others were not None here.)
                        continue
                elif attr_value is None:
                    # None values should not cause edits.
                    continue

                log_edit_value(key, getattr(incident, attr_name), attr_value)

                setattr(incident, attr_name, attr_value)

        #
        # Figure out what to report about state changes
        #
        highest_change = None
        lowest_change = None
        for state_changed, state_time in state_changes:
            if state_time is None:
                if lowest_change is None or JSON.cmpStates(lowest_change, state_changed) > 0:
                    lowest_change = state_changed
            else:
                if highest_change is None or JSON.cmpStates(highest_change[0], state_changed) < 0:
                    highest_change = (state_changed, state_time)

        if highest_change is not None:
            system_messages.append(u"State changed to: {0}".format(JSON.describe(highest_change[0])))
        elif lowest_change is not None:
            # We need one state less than lowest_change
            last = None
            for state in JSON.states():
                if state == lowest_change:
                    break
                last = state
            system_messages.append(u"State changed to: {0}".format(JSON.describe(last)))

        #
        # Add system report entries, then user entries
        #
        if system_messages:
            incident.report_entries.append(
                ReportEntry(
                    author = self.avatarId.decode("utf-8"),
                    text = u"\n".join(system_messages),
                    system_entry = True,
                )
            )
        incident.report_entries.extend(user_entries)

        #
        # Write to disk
        #
        self.storage.write_incident(incident)

        #
        # Respond
        #
        set_response_header(request, HeaderName.contentType, ContentType.JSON)
        request.setResponseCode(http.OK)

        return "";
示例#17
0
 def test_from_json(self):
     """
     Test for L{ims.data.Incident.from_json} with incident data.
     """
     self.equals_1(Incident.from_json(from_json_text(incident1_text), 1))
示例#18
0
 def test_from_json_io(self):
     """
     Test for L{ims.data.Incident.from_json_io} with incident data.
     """
     self.equals_1(Incident.from_json_io(StringIO(incident1_text), 1))
示例#19
0
 def read_incident_with_number(self, number):
     handle = self._open_incident(number, "r")
     try:
         return Incident.from_json_io(handle, number=number)
     finally:
         handle.close()