Ejemplo n.º 1
0
class TestBase(TestCase):
    def setUp(self):
        self.db_session = DBSession()
        self.connection = engine.connect()

        # # Configure Search DDL triggers.
        Base.metadata.drop_all(self.connection)
        Base.metadata.create_all(self.connection)

        self.client = TestClient(app)

    def tearDown(self):
        self.db_session.rollback()
        self.db_session.close()

    def create_system_admin(self, *args, **kwargs):
        from app.controllers.account import create_account
        from app.schemas.account import AccountCreate

        return create_account(
            self.db_session,
            first_name="Admin",
            last_name="Istrator",
            email="*****@*****.**",
            password="******",
            is_system_admin=True,
            is_active=True,
            send_registration_email=False,
        )

    def auth_headers(self, email="*****@*****.**", password="******"):
        payload = {"username": email, "password": password}
        resp = self.client.post("/auth/token", data=payload)
        return {"Authorization": "Bearer " + resp.json().get("access_token")}
def authorizable_attribute(
        item: AuthorizableAttributeSchema = Body(
            ...,
            example={
                "authorizable_attribute_id":
                "Authorizable Attribute %s" %
                "".join(random.choice(string.hexdigits) for i in range(10)),
                "authorizable_attribute_info": [
                    {
                        "name":
                        "email",
                        "type":
                        "str",
                        "value_set": [
                            "*****@*****.**",
                            "*****@*****.**",
                            "*****@*****.**",
                        ],
                    },
                    {
                        "name":
                        "zip_code",
                        "type":
                        "int",
                        "value_set":
                        ["08001", "08002", "08003", "08004", "08005", "08006"],
                    },
                ],
                "authorizable_attribute_info_optional": [
                    {
                        "name": "age",
                        "type": "str",
                        "value_set": ["0-18", "18-25", "25-45", ">45"],
                        "k": 2,
                    }
                ],
                "reissuable":
                False,
            },
        ),
        token: str = Security(oauth2_scheme),
):
    info = [_.json() for _ in item.authorizable_attribute_info]
    optional = [_.json() for _ in item.authorizable_attribute_info_optional]

    keypair_contract = ZenContract(CONTRACTS.GENERATE_KEYPAIR,
                                   {"issuer_identifier": config.get("uid")})
    keypair = keypair_contract.execute()
    contract = ZenContract(CONTRACTS.PUBLIC_VERIFY,
                           {"issuer_identifier": config.get("uid")})
    contract.keys(keypair)
    verification_key = contract.execute()
    aa = AuthorizableAttribute(
        authorizable_attribute_id=item.authorizable_attribute_id,
        authorizable_attribute_info=json.dumps(info),
        authorizable_attribute_info_optional=json.dumps(optional),
        keypair=keypair,
        verification_key=verification_key,
        reissuable=item.reissuable,
    )

    try:
        DBSession.add(aa)
        DBSession.commit()
    except IntegrityError:
        DBSession.rollback()
        raise HTTPException(status_code=HTTP_409_CONFLICT,
                            detail="Duplicate Authorizable Attribute Id")

    return {
        "verification_key": json.loads(verification_key),
        "credential_issuer_id": config.get("uid"),
        "authorizable_attribute_id": aa.authorizable_attribute_id,
    }