def create_test_data(setup):
    # type: (SetupTest) -> List[Permission]
    """Sets up a very basic test graph and returns the permission objects.

    Be careful not to include milliseconds in the creation timestamps since this causes different
    behavior on SQLite (which preserves them) and MySQL (which drops them).
    """
    early_date = datetime.utcfromtimestamp(1)
    now_minus_one_second = datetime.utcfromtimestamp(int(time() - 1))
    now = datetime.utcfromtimestamp(int(time()))
    permissions = [
        Permission(name="first-permission",
                   description="first",
                   created_on=now_minus_one_second),
        Permission(name="audited-permission", description="", created_on=now),
        Permission(name="early-permission",
                   description="is early",
                   created_on=early_date),
    ]
    with setup.transaction():
        for permission in permissions:
            setup.create_permission(
                name=permission.name,
                description=permission.description,
                created_on=permission.created_on,
                audited=(permission.name == "audited-permission"),
            )
        setup.create_permission("disabled", enabled=False)
        setup.create_user("*****@*****.**")
    return permissions
Esempio n. 2
0
    def list_permissions(self, pagination, audited_only):
        # type: (Pagination[ListPermissionsSortKey], bool) -> PaginatedList[Permission]
        perm_tuples = self.graph.get_permissions(audited=audited_only)

        # Optionally sort.
        if pagination.sort_key != ListPermissionsSortKey.NONE:
            perm_tuples = sorted(
                perm_tuples,
                key=lambda p: getattr(p, self.SORT_FIELD[pagination.sort_key]),
                reverse=pagination.reverse_sort,
            )

        # Find the total length and then optionally slice.
        total = len(perm_tuples)
        if pagination.limit:
            perm_tuples = perm_tuples[pagination.offset : pagination.offset + pagination.limit]
        elif pagination.offset > 0:
            perm_tuples = perm_tuples[pagination.offset :]

        # Convert to the correct data transfer object.
        permissions = [
            Permission(
                name=p.name,
                description=p.description,
                created_on=p.created_on,
                audited=p.audited,
                enabled=True,
            )
            for p in perm_tuples
        ]
        return PaginatedList[Permission](
            values=permissions, total=total, offset=pagination.offset, limit=pagination.limit
        )
Esempio n. 3
0
 def get_permission(self, name):
     # type: (str) -> Optional[Permission]
     permission = SQLPermission.get(self.session, name=name)
     if not permission:
         return None
     return Permission(
         name=permission.name,
         description=permission.description,
         created_on=permission.created_on,
     )
Esempio n. 4
0
 def _get_permissions(session):
     # type: (Session) -> Dict[str, Permission]
     """Returns all permissions in the graph."""
     permissions = session.query(SQLPermission).filter(SQLPermission.enabled == True)
     out = {}
     for permission in permissions:
         out[permission.name] = Permission(
             name=permission.name,
             description=permission.description,
             created_on=permission.created_on,
             audited=permission.audited,
             enabled=permission.enabled,
         )
     return out