mardi 17 novembre 2020

How should I test this Custom Permissions

I'm confused about the procedure to test this permissions.

Should I run this tests testing the login inheriting just the permissions?

Couldn't just test this permissions by itsef?

How must be done a test like this?

        def has_permission(self, request, view):
            user = request.user
            if not user:
                user = get_user(request)

            # -- Default user from allowed hosts bypasses authentication.
            if API_DEFAULT_USER == user.username:
                return True
            # -- Admin users have permission.
            if user.is_staff or user.is_superuser:
                return True
            # -- suscribed users have permission.
            if not user.is_anonymous:
                return True

            return False


    class SuscribedUserApiPerm(BasePermission):

        def has_permission(self, request, view):
            user = request.user

            # -- suscribed users only have permission.
            if not user.is_anonymous and user.username is None:
                return True

            return False


    class SubscriptionApiPerm(SuscribedUserApiPerm):

        def has_permission(self, request, view):
            user = request.user
            
            if super().has_permission(request, view):
                # Checking current user's subscription
                return user.owns_api()

            return False


    class SubscriptionSimulatorPerm(SuscribedUserApiPerm):

        def has_permission(self, request, view):
            user = request.user
            
            if super().has_permission(request, view):
                # Checking current user's subscription
                return user.owns_simulator()

            return False

Aucun commentaire:

Enregistrer un commentaire