def test_grants_needs(users, roles):
    user = users[0]
    role = roles[0]
    grant1 = Grant(user, "manage")
    grant2 = Grant(role, "view")
    dict_ = {"subject": "sysrole", "id": "system", "level": "view"}
    grant3 = Grant.from_dict(dict_)
    grants = Grants([grant1, grant2, grant3])

    assert len(grants.needs("view")) == 3
def test_grants_dump(users, roles):
    user = users[0]
    role = roles[0]
    grant1 = Grant(user, "manage")
    grant2 = Grant(role, "view")
    dict_ = {"subject": "sysrole", "id": "system", "level": "view"}
    grant3 = Grant.from_dict(dict_)
    grants = Grants([grant1, grant2, grant3])

    dump = grants.dump()
    assert len(dump) == 3
    assert grant1.to_dict() in dump
    assert grant2.to_dict() in dump
    assert grant3.to_dict() in dump
def test_grant_creation(users, roles):
    user = users[0]
    role = roles[0]
    grant = Grant(user, "view")
    assert grant.subject_type == "user"
    assert grant.subject_id == user.id
    assert grant.subject == user

    grant = Grant(role, "view")
    assert grant.subject_type == "role"
    assert grant.subject_id == role.id
    assert grant.subject == role

    grant = Grant(None, "view", subject_type="sysrole", subject_id="system")
    assert grant.subject_type == "sysrole"
    assert grant.subject_id == "system"
    assert grant.subject is None
def test_grant_to_need(users, roles):
    user = users[0]
    role = roles[0]
    grant = Grant(user, "view")
    need = grant.to_need()
    assert need.method == "id"
    assert need.value == user.id

    grant = Grant(role, "view")
    need = grant.to_need()
    assert need.method == "role"
    assert need.value == role.name

    dict_ = {"subject": "sysrole", "id": "system", "level": "view"}
    grant = Grant.from_dict(dict_)
    need = grant.to_need()
    assert need.method == "system_role"
    assert need.value == "system"