def test_add_get_multiple_certification(meta_repo):
    certifications_repo = CertificationsRepo(meta_repo.conn)
    certifications_repo.insert(
        Certification(
            "testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
            "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", 20, 1473108382,
            "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
            22))
    certifications_repo.insert(
        Certification(
            "testcurrency", "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
            "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", 101, 1473108382,
            "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
            105))
    certifications = certifications_repo.get_all(currency="testcurrency")
    assert "testcurrency" in [i.currency for i in certifications]
    assert "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn" in [
        i.certifier for i in certifications
    ]
    assert "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" in [
        i.certifier for i in certifications
    ]
    assert "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn" in [
        i.certified for i in certifications
    ]
    assert "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" in [
        i.certified for i in certifications
    ]
def test_expired(meta_repo):
    certifications_repo = CertificationsRepo(meta_repo.conn)
    not_written_expired = Certification(
        "testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", 20, 1000,
        "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
        0)
    written_expired = attr.assoc(
        not_written_expired,
        certifier="8Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        written_on=10)
    written_not_expired = attr.assoc(
        not_written_expired,
        certifier="9Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        written_on=10,
        timestamp=3200)
    not_written_not_expired = attr.assoc(
        not_written_expired,
        certifier="1Bqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        timestamp=4900)
    for c in (written_expired, written_not_expired, not_written_expired,
              not_written_not_expired):
        certifications_repo.insert(c)

    certs = certifications_repo.expired(
        "testcurrency",
        "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
        current_ts=5000,
        sig_window=500,
        sig_validity=2000)
    assert written_expired in certs
    assert not_written_expired in certs
    assert not_written_not_expired not in certs
    assert written_not_expired not in certs
def test_add_get_drop_blockchain(meta_repo):
    certifications_repo = CertificationsRepo(meta_repo.conn)
    certifications_repo.insert(
        Certification(
            "testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
            "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", 20, 1473108382,
            "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
            0))
    certification = certifications_repo.get_one(
        currency="testcurrency",
        certifier="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        certified="FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
        block=20)
    assert certification.currency == "testcurrency"
    assert certification.certifier == "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ"
    assert certification.certified == "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn"
    assert certification.block == 20
    assert certification.timestamp == 1473108382
    assert certification.signature == "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw=="
    assert certification.written_on == 0
    certifications_repo.drop(certification)
    certification = certifications_repo.get_one(
        currency="testcurrency",
        certifier="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        certified="FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
        block=20)
    assert certification is None
def test_add_update_certification(meta_repo):
    certifications_repo = CertificationsRepo(meta_repo.conn)
    certification = Certification(
        "testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", 20, 1473108382,
        "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
        0)

    certifications_repo.insert(certification)
    certification.written_on = 22
    certifications_repo.update(certification)
    cert2 = certifications_repo.get_one(
        currency="testcurrency",
        certifier="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
        certified="FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
        block=20)
    assert cert2.written_on == 22
Exemple #5
0
    async def load_certified_by(self, identity):
        """
        Request the identity data and save it to written certifications
        It does nothing if the identity is already written and updated with blockchain lookups
        :param sakia.data.entities.Identity identity: the identity
        """
        certifications = []
        try:
            data = await self._bma_connector.get(self.currency,
                                                 bma.wot.certified_by,
                                                 {'search': identity.pubkey})
            for certified_data in data['certifications']:
                cert = Certification(
                    currency=self.currency,
                    certifier=data["pubkey"],
                    certified=certified_data["pubkey"],
                    block=certified_data["cert_time"]["block"],
                    timestamp=certified_data["cert_time"]["medianTime"],
                    signature=certified_data['signature'])
                if certified_data['written']:
                    cert.written_on = certified_data['written']['number']
                certifications.append(cert)
                # We save connections pubkeys
                if identity.pubkey in self._connections_processor.pubkeys():
                    self._certs_processor.insert_or_update_certification(cert)

            identity.written = True
            if identity.pubkey in self._connections_processor.pubkeys():
                self._identities_processor.insert_or_update_identity(identity)
        except errors.DuniterError as e:
            if e.ucode in (errors.NO_MATCHING_IDENTITY,
                           errors.NO_MEMBER_MATCHING_PUB_OR_UID):
                logging.debug("Certified by error : {0}".format(str(e)))
                identity.written = False
                if identity.pubkey in self._connections_processor.pubkeys():
                    self._identities_processor.insert_or_update_identity(
                        identity)
        except NoPeerAvailable as e:
            logging.debug(str(e))
        return certifications
Exemple #6
0
 async def load_certs_in_lookup(self, identity, certifiers, certified):
     """
     :param sakia.data.entities.Identity identity: the identity
     :param list[sakia.data.entities.Certification] certifiers: the list of certifiers got in /wot/certifiers-of
     :param list[sakia.data.entities.Certification] certified: the list of certified got in /wot/certified-by
     """
     try:
         lookup_data = await self._bma_connector.get(
             self.currency, bma.wot.lookup, {'search': identity.pubkey})
         for result in lookup_data['results']:
             if result["pubkey"] == identity.pubkey:
                 for uid_data in result['uids']:
                     if not identity.uid or uid_data["uid"] == identity.uid:
                         if not identity.blockstamp or identity.blockstamp == block_uid(
                                 uid_data["meta"]["timestamp"]):
                             for other_data in uid_data["others"]:
                                 cert = Certification(
                                     currency=self.currency,
                                     certified=identity.pubkey,
                                     certifier=other_data["pubkey"],
                                     block=other_data["meta"]
                                     ["block_number"],
                                     timestamp=0,
                                     signature=other_data['signature'])
                                 if cert not in certifiers:
                                     cert.timestamp = await self._blockchain_processor.timestamp(
                                         self.currency, cert.block)
                                     certifiers.append(cert)
                                     # We save connections pubkeys
                                     if identity.pubkey in self._connections_processor.pubkeys(
                                     ):
                                         self._certs_processor.insert_or_update_certification(
                                             cert)
             for signed_data in result["signed"]:
                 cert = Certification(
                     currency=self.currency,
                     certified=signed_data["pubkey"],
                     certifier=identity.pubkey,
                     block=signed_data["cert_time"]["block"],
                     timestamp=0,
                     signature=signed_data['signature'])
                 if cert not in certified:
                     certified.append(cert)
                     # We save connections pubkeys
                     if identity.pubkey in self._connections_processor.pubkeys(
                     ):
                         cert.timestamp = await self._blockchain_processor.timestamp(
                             self.currency, cert.block)
                         self._certs_processor.insert_or_update_certification(
                             cert)
     except errors.DuniterError as e:
         logging.debug("Certified by error : {0}".format(str(e)))
     except NoPeerAvailable as e:
         logging.debug(str(e))
     return certifiers, certified