예제 #1
0
    def test_insert(self):
        col = Collaboration.get()
        org = Organization(name="unit_organization",
                           domain="testers.com",
                           address1="memorylane 1",
                           zipcode="bla",
                           country="RAM",
                           collaborations=col)
        org.save()

        db_org = Organization.get_by_name("unit_organization")
        self.assertEqual(db_org, org)
예제 #2
0
 def test_insert(self):
     task = Task(name="unit_task")
     result = Result(task=task,
                     organization=Organization.get()[0],
                     input="something")
     result.save()
     self.assertEqual(result, result)
예제 #3
0
    def get(self, id=None):

        # obtain requisters organization
        if g.user:
            auth_org = g.user.organization
        elif g.node:
            auth_org = g.node.organization
        else:  # g.container
            auth_org = Organization.get(g.container['organization_id'])

        if id:
            result = db_Result.get(id)
            if not result:
                return {'msg': f'Result id={id} not found!'}, \
                    HTTPStatus.NOT_FOUND
            if not self.r.v_glo.can():
                c_orgs = result.task.collaboration.organizations
                if not (self.r.v_org.can() and auth_org in c_orgs):
                    return {'msg': 'You lack the permission to do that!'}, \
                        HTTPStatus.UNAUTHORIZED
        else:

            session = Database().Session
            q = session.query(db_Result)

            if request.args.get('state') == 'open':
                q = q.filter(db_Result.finished_at == None)

            # q = q.join(db_Result)
            if request.args.get('task_id'):
                q = q.filter_by(task_id=request.args.get('task_id'))

            q = q.join(Organization).join(Node).join(Task, db_Result.task)\
                .join(Collaboration)
            if request.args.get('node_id'):
                q = q.filter(db.Node.id == request.args.get('node_id'))\
                    .filter(db.Collaboration.id == db.Node.collaboration_id)

            result = q.all()

            # filter results based on permissions
            if not self.r.v_glo.can():
                if self.r.v_org.can():
                    filtered_result = []
                    for res in result:
                        if res.task.collaboration in auth_org.collaborations:
                            filtered_result.append(res)
                    result = filtered_result
                else:
                    return {'msg': 'You lack the permission to do that!'}, \
                        HTTPStatus.UNAUTHORIZED

        if request.args.get('include') == 'task':
            s = result_inc_schema
        else:
            s = result_schema

        return s.dump(result, many=not id).data, HTTPStatus.OK
예제 #4
0
    def post(self):
        """Create a new role

        You can only assign rules that you own. You need permission to create
        roles, and you can only assign roles to other organizations if you
        have gobal permission.

        """
        parser = reqparse.RequestParser()
        parser.add_argument("name", type=str, required=True)
        parser.add_argument("description", type=str, required=True)
        parser.add_argument("rules", type=int, action='append', required=True)
        parser.add_argument("organization_id", type=int, required=False)
        data = parser.parse_args()

        # obtain the requested rules from the DB.
        rules = []
        for rule_id in data["rules"]:
            rule = Rule.get(rule_id)
            if not rule:
                return {"msg": f"Rule id={rule_id} not found."}, \
                    HTTPStatus.NOT_FOUND
            rules.append(rule)

        # And check that this used has the rules he is trying to assign
        denied = self.permissions.verify_user_rules(rules)
        if denied:
            return denied, HTTPStatus.UNAUTHORIZED

        # if trying to create a role for another organization
        if data["organization_id"] and not self.r.c_glo.can():
            return {'msg': 'You cannot create roles for other organizations'},\
                HTTPStatus.UNAUTHORIZED
        elif data["organization_id"] and self.r.c_glo.can():
            organization_id = data["organization_id"]

            # verify that the organization exists
            if not Organization.get(organization_id):
                return {
                    'msg': f'organization "{organization_id}" does not '
                    'exist!'
                }, HTTPStatus.NOT_FOUND
        elif (not data['organization_id'] and self.r.c_glo.can()) or \
                self.r.c_org.can():
            organization_id = g.user.organization_id
        else:
            return {'msg': 'You lack the permission to create roles!'}, \
                HTTPStatus.UNAUTHORIZED

        # create the actual role
        role = db_Role(name=data["name"],
                       description=data["description"],
                       rules=rules,
                       organization_id=organization_id)
        role.save()

        return self.role_schema.dump(role, many=False).data, HTTPStatus.CREATED
예제 #5
0
 def test_relations(self):
     for organization in Organization.get():
         for node in organization.nodes:
             self.assertIsInstance(node, Node)
         for collaboration in organization.collaborations:
             self.assertIsInstance(collaboration, Collaboration)
         for user in organization.users:
             self.assertIsInstance(user, User)
         for result in organization.results:
             self.assertIsInstance(result, Result)
예제 #6
0
 def test_insert(self):
     db_organization = Organization.get(1)
     user = User(username="******",
                 firstname="un",
                 lastname="it",
                 roles="admin",
                 organization=db_organization,
                 email="*****@*****.**")
     user.set_password("unit_pass")
     user.save()
     db_user = User.get_by_username("unit")
     self.assertEqual(db_user, user)
예제 #7
0
    def test_read(self):
        for organization in self.entities.get("organizations"):
            org = Organization.get_by_name(organization.get("name"))
            self.assertEqual(org.name, organization.get("name"))
            self.assertEqual(org.domain, organization.get("domain"))
            self.assertEqual(org.address1, organization.get("address1"))
            self.assertEqual(org.address2, organization.get("address2"))
            self.assertEqual(org.zipcode, str(organization.get("zipcode")))
            self.assertEqual(org.country, organization.get("country"))

            for user in organization.get("users"):
                db_user = User.get_by_username(user.get("username"))
                self.assertIsNotNone(db_user)
                self.assertEqual(db_user.organization.name,
                                 organization.get("name"))
예제 #8
0
 def test_methods(self):
     name = self.entities.get("organizations")[0].get("name")
     self.assertIsNotNone(Organization.get_by_name(name))