Exemple #1
0
def test_party_exists(mock):
    # Should return false if the party does not exist
    party_uri = 'https://example.com#party'
    assert not db_access.party_exists(party_uri)

    # Should return true of the party exists
    db_access.create_party(party_uri)
    assert db_access.party_exists(party_uri)
Exemple #2
0
def test_create_party(mock):
    # Should create a party with the URI, label and comment given
    party_uri = 'https://example.com#party'
    label = 'Party'
    comment = 'This is a test party.'
    db_access.create_party(party_uri, label, comment)
    assert db_access.party_exists(party_uri)

    # Should raise an exception of the party already exists
    with pytest.raises(ValueError):
        db_access.create_party(party_uri)
def view_object():
    # Route for viewing something by URI, regardless of whether it is a licence, action, party, etc.
    object_uri = request.values.get('uri')
    if object_uri is None:
        flash(('Object not found', 'Please supply a URI to view an object.'), category='error')
        return redirect(url_for('controller.home'))
    else:
        if db_access.policy_exists(object_uri):
            return redirect(url_for('controller.licence_routes', uri=object_uri))
        if db_access.action_exists(object_uri):
            return redirect(url_for('controller.action_register', uri=object_uri))
        if db_access.party_exists(object_uri):
            return redirect(url_for('controller.party_register', uri=object_uri))
        abort(404)
Exemple #4
0
def test_delete_party(mock):
    # Should raise an exception if the Party is currently assigned to a Rule
    party_uri = 'https://example.com#party'
    rule_uri = 'https://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    db_access.create_party(party_uri)
    db_access.create_rule(rule_uri, rule_type)
    db_access.add_assignor_to_rule(party_uri, rule_uri)
    with pytest.raises(ValueError):
        db_access.delete_party(party_uri)

    # Should delete the Party if not currently assigned to a Rule (deleting the rule drops the assignment)
    db_access.delete_rule(rule_uri)
    db_access.delete_party(party_uri)
    assert not db_access.party_exists(party_uri)
def create_policy(policy_uri, attributes=None, rules=None):
    """
    Creates an entire policy with the given URI, attributes and Rules.
    Will attempt to use Rule Type and Actions whether they are given by URI or Label.
    If there is an error at any point, will rollback all changes to database to avoid leaving partially created
    policies.

    :param policy_uri:
    :param attributes:  Dictionary of optional attributes of the policy.
        Permitted attributes:   type, label, jurisdiction, legal_code, has_version, language, see_also
                                same_as, comment, logo, status
    :param rules: List of Rules. Each Rule should be a Dictionary containing the following elements:
        TYPE_URI*: string (i.e. 'http://www.w3.org/ns/odrl/2/duty')
        TYPE_LABEL*: string (i.e. 'duty')
        * At least one of TYPE_URI or TYPE_LABEL should be provided, but both are not required
        ASSIGNORS: List of Assignors. Each Assignor should be a Dictionary containing a URI, LABEL and COMMENT.
        ASSIGNEES: List of Assignees.  Each Assignee should be a Dictionary containing a URI, LABEL and COMMENT.
        ACTIONS: List of strings (URIs or labels)
    :return:
    """
    if not is_valid_uri(policy_uri):
        raise ValueError('Not a valid URI: ' + policy_uri)

    permitted_rule_types = []

    try:
        db_access.create_policy(policy_uri)
        if attributes:
            for attr_name, attr_value in attributes.items():
                db_access.set_policy_attribute(policy_uri, attr_name,
                                               attr_value)
        if rules:
            for rule in rules:
                rule_uri = _conf.BASE_URI + 'rules/' + str(uuid4())
                if 'TYPE_URI' in rule:
                    rule_type = rule['TYPE_URI']
                elif 'TYPE_LABEL' in rule:
                    if not permitted_rule_types:
                        permitted_rule_types = db_access.get_permitted_rule_types(
                        )
                    rule_type = get_rule_type_uri(rule['TYPE_LABEL'],
                                                  permitted_rule_types)
                    if not rule_type:
                        raise ValueError('Cannot create policy - Rule type ' +
                                         rule['TYPE_LABEL'] +
                                         ' is not permitted')
                else:
                    raise ValueError(
                        'Cannot create policy - no Rule type provided')
                db_access.create_rule(rule_uri, rule_type)
                for action in rule['ACTIONS']:
                    if action:
                        if is_valid_uri(action):
                            action_uri = action
                        else:
                            permitted_actions = db_access.get_all_actions()
                            action_uri = get_action_uri(
                                action, permitted_actions)
                            if not action_uri:
                                raise ValueError(
                                    'Cannot create policy - Action ' + action +
                                    ' is not permitted')
                        db_access.add_action_to_rule(action_uri, rule_uri)
                if 'ASSIGNORS' in rule:
                    for assignor in rule['ASSIGNORS']:
                        if not db_access.party_exists(assignor['URI']):
                            db_access.create_party(assignor['URI'],
                                                   assignor['LABEL'],
                                                   assignor['COMMENT'])
                        db_access.add_assignor_to_rule(assignor['URI'],
                                                       rule_uri)
                if 'ASSIGNEES' in rule:
                    for assignee in rule['ASSIGNEES']:
                        if not db_access.party_exists(assignee['URI']):
                            db_access.create_party(assignee['URI'],
                                                   assignee['LABEL'],
                                                   assignee['COMMENT'])
                        db_access.add_assignee_to_rule(assignee['URI'],
                                                       rule_uri)
                db_access.add_rule_to_policy(rule_uri, policy_uri)
    except Exception as error:
        db_access.rollback_db()
        raise error
    db_access.commit_db()