django-rules
django-rules copied to clipboard
Unit tests fail with new model class.
Adding a model class in the unit tests, users seem to get permissions they should not have.
See the following commit on my forked project: https://github.com/rjekker/django-rules/commit/27179a1303074fce675e2bc2558c0cdd0b70d6f7
I create a new Model class with permissions in tests/testapp/models.py:
@rules.predicate
def is_car_owner(user, car):
return car.owner == user
class Car(RulesModel):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
class Meta:
rules_permissions = {"wash": rules.always_allow,
"drive": is_car_owner,
"crash": rules.always_deny
I create two car instances with different owners:
if sys.version_info.major >= 3:
from testapp.models import Car
Car.objects.create(
owner=adrian
)
Car.objects.create(
owner=martin
)
Then the following test fails:
def test_cars_owner(self):
# adrian can *not* drive martins car
car1 = Car.objects.get(pk=1)
assert car1.owner.username == "adrian"
car2 = Car.objects.get(pk=2)
assert car2.owner.username == "martin"
adrian = get_user_model().objects.get(pk=1)
martin = get_user_model().objects.get(pk=2)
assert adrian == car1.owner
assert martin == car2.owner
assert not adrian.has_perm(Car.get_perm("drive"), car2) # <---- Fails here
assert not martin.has_perm(Car.get_perm("drive"), car1)
The unit test seems to decide that adrian can drive martins car, even though he should not.