示例#1
0
    def test_get(self, rf, mocker):  # pylint: disable=no-self-use disable=invalid-name
        """
        test create
        """
        url = reverse('profilepermission-list')

        profile_test = Profile()
        profile_test.name = "profile_test_viewset"
        profile_test.key = "test"
        profile_test.description = "test"
        profile_test.save()

        permission_test = Permission()
        permission_test.name = "permission_test_viewset"
        permission_test.key = "test"
        permission_test.description = "test"
        permission_test.save()

        data = {"profile": profile_test.id, "permission": permission_test.id}

        request = rf.post(url,
                          content_type='application/json',
                          data=json.dumps(data))

        mocker.patch.object(ProfilePermission, 'save')
        # Renderizamos la vista con nuestro request.
        response = ProfilePermissionViewSet.as_view({'post': 'create'
                                                     })(request).render()
        print(response.content)
        profile = Profile.objects.get(
            id=json.loads(response.content).get('profile'))
        assert response.status_code == 201
        assert str(profile) == 'profile_test_viewset'
        # Verificamos si efectivamente se llamo el metodo save
        assert ProfilePermission.save.called
示例#2
0
    def test_get(self, rf, mocker):  # pylint: disable=no-self-use disable=invalid-name
        """
        test create
        """
        url = reverse('profilepermission-list')

        permission_test = Permission()
        permission_test.name = "user_permission_viewset"
        permission_test.key = "test"
        permission_test.description = "test"
        permission_test.save()

        user_test = ApiUser()
        user_test.username = "******"
        user_test.password = "******"
        user_test.email = "*****@*****.**"
        user_test.save()

        data = {"permission": permission_test.id, "user": user_test.id}

        request = rf.post(url,
                          content_type='application/json',
                          data=json.dumps(data))

        mocker.patch.object(UserPermission, 'save')
        # Renderizamos la vista con nuestro request.
        response = UserPermissionViewSet.as_view({'post':
                                                  'create'})(request).render()
        permission = Permission.objects.get(
            id=json.loads(response.content).get('permission'))
        assert response.status_code == 201
        assert str(permission) == 'user_permission_viewset'
        # Verificamos si efectivamente se llamo el metodo save
        assert UserPermission.save.called
示例#3
0
    def test_update(
        self,
        user_permission_factory,
    ):  # pylint: disable=no-self-use
        """
        test updated user_permission
        """
        permission_test = Permission()
        permission_test.name = "user_permission_test2"
        permission_test.key = "test"
        permission_test.description = "test"
        permission_test.save()

        user_permission_created = user_permission_factory()
        user_permission = UserPermission.objects.get(
            id=user_permission_created.id)

        user_permission.permission = permission_test
        user_permission.save()

        assert user_permission.permission.name == "user_permission_test2", 'name should be user_permission_test2'
示例#4
0
 def create(self, validated_data):
     instance = self.Meta.model(**validated_data)
     instance.save()
     TYPE_CHOICE_LIST = [{
         'id': 1,
         'type': 'GET'
     }, {
         'id': 2,
         'type': 'LIST'
     }, {
         'id': 3,
         'type': 'POST'
     }, {
         'id': 4,
         'type': 'PUT'
     }, {
         'id': 5,
         'type': 'PATCH'
     }, {
         'id': 6,
         'type': 'DELETE'
     }]
     for item in TYPE_CHOICE_LIST:
         item_id = item['id']
         item_type = item['type']
         permission = Permission()
         permission.name = "{} | {} | {}".format(item_type,
                                                 instance.origin.name,
                                                 instance.api_path)
         permission.key = "{}{}{}".format(item_type.lower(),
                                          instance.origin.host,
                                          instance.api_path)
         permission.description = "Autogenerate permission type {} for {} from {}:{}".format(
             item_type, instance.api_path, instance.origin.name,
             instance.origin.port)
         permission.type = item_id
         permission.api = instance
         permission.save()
     return instance