Ejemplo n.º 1
0
    def test_operations_add_results(self):
        self.operation.add_results(Probe("39232d2", 1), "Traceroute results",
                                   "an unique code")

        self.assertEqual(self.operation.results[0]["probe"],
                         Probe("39232d2", 1))
        self.assertEqual(self.operation.results[0]["results"],
                         "Traceroute results")
        self.assertEqual(self.operation.results[0]["unique_code"],
                         "an unique code")
Ejemplo n.º 2
0
    def test_encodes_probes(self):
        probe_a = Probe("fake_id", 1)
        probe_b = Probe("another_fake_id", 1)

        probes = [probe_a, probe_b]

        b = json.dumps(probes, cls=JsonEncoder)

        c = json.loads(b)

        self.assertEqual(c[0]["identifier"], "fake_id")
Ejemplo n.º 3
0
 def setUp(self):
     self.operation = Operation(
         _id="operation_id",
         operation="Traceroute",
         params={
             "ips": ["192.168.0.0", "192.168.0.1"],
             "confidence": 0.95,
         },
         probes=[Probe("39232d2", 1),
                 Probe("43i4iec", 1)],
         credits_=10,
         result_format="json")
Ejemplo n.º 4
0
    def test_encodes_single_probe(self):
        probe_a = Probe("fake_id", 1)

        b = json.dumps(probe_a, cls=JsonEncoder)

        c = json.loads(b)

        self.assertEqual(c["identifier"], "fake_id")
Ejemplo n.º 5
0
    def find_probe(self, identifier):
        try:
            probe = self.probesCol.find_one({"_id": ObjectId(identifier)})
            location = self.from_geo_json(probe["location"])

            return Probe(str(probe["_id"]), str(probe["owner"]), location)
        except:
            return None
Ejemplo n.º 6
0
    def find_all_probes(self):
        probes = []

        for probe in self.probesCol.find():
            location = self.from_geo_json(probe["location"])
            probes.append(
                Probe(str(probe["_id"]), str(probe["owner"]), location))

        return probes
Ejemplo n.º 7
0
    def find_by_user(self, user_id):
        probes = []

        user_query = {"owner": ObjectId(user_id)}

        for probe in self.probesCol.find(user_query):
            location = self.from_geo_json(probe["location"])
            probes.append(
                Probe(str(probe["_id"]), str(probe["owner"]), location))

        return probes
Ejemplo n.º 8
0
    def create_probe(self, user, location):
        data = {
            "owner": ObjectId(user._id),
            "location": self.to_geo_json(location)
        }

        _id = self.probesCol.insert_one(data)

        probe = Probe(str(_id.inserted_id), user._id, location)

        return probe
Ejemplo n.º 9
0
    def deserialize_operation(self, raw_operation):
        operation = Operation(
            str(raw_operation["_id"]), raw_operation["operation"],
            raw_operation["params"], [
                Probe(str(probe), str(raw_operation["owner"]), None)
                for probe in raw_operation["probes"]
            ], raw_operation["credits"], raw_operation["result_format"])

        if "results" in raw_operation:
            for result in raw_operation["results"]:
                operation.add_results(str(result["probe"]), result["results"],
                                      result["unique_code"])

        return operation
Ejemplo n.º 10
0
class ProbeTest(unittest.TestCase):
    def setUp(self):
        self.probe = Probe(
            identifier="3ap394c",
            owner_id=4
        )

    def test_probe_includes_identifier(self):
        self.assertEqual("3ap394c", self.probe.identifier)

    def test_probe_has_public_data(self):
        self.assertEqual("identifier" in self.probe.public_data(), True)

    def test_probe_includes_owner(self):
        self.assertEqual(self.probe.owner_id, 4)
Ejemplo n.º 11
0
    def find_selected_probes(self, identifiers):
        probes = []

        try:
            probes_ids = [ObjectId(probe) for probe in identifiers]
        except:
            raise InvalidProbe()

        for probe in self.probesCol.find({"_id": {"$in": probes_ids}}):
            location = self.from_geo_json(probe["location"])
            probes.append(
                Probe(str(probe["_id"]), str(probe["owner"]), location))

        if len(probes_ids) != len(probes):
            raise InvalidProbe()

        return probes
Ejemplo n.º 12
0
    def find_operation(self, identifier):
        try:
            op = self.operationsCol.find_one({"_id": ObjectId(identifier)})

            operation = Operation(str(op["_id"]), op["operation"],
                                  op["params"], [
                                      Probe(str(probe), str(op["owner"]), None)
                                      for probe in op["probes"]
                                  ], op["credits"], op["result_format"])

            if "results" in op:
                for result in op["results"]:
                    operation.add_results(str(result["probe"]),
                                          result["results"],
                                          result["unique_code"])

            return operation
        except:
            raise InvalidOperation()
Ejemplo n.º 13
0
    def test_operation_public_data_doesnt_include_results_unique_codes(self):
        self.operation.add_results(Probe("39232d2", 1), "Traceroute results",
                                   "an unique code")

        self.assertEqual(
            "unique_code" in self.operation.public_data()["results"][0], False)
Ejemplo n.º 14
0
    def test_operations_check_unexisting_unique_code(self):
        self.operation.add_results(Probe("39232d2", 1), "Traceroute results",
                                   "an unique code")

        self.assertEqual(self.operation.code_exists("another unique code"),
                         False)
Ejemplo n.º 15
0
 def setUp(self):
     self.probe = Probe(
         identifier="3ap394c",
         owner_id=4
     )