So to finish that topic, as I set REST_FRAMEWORK that way in my settings

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
'rest_framework.permissions.DjangoModelPermissions'
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication'
),
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
}

I made a permissions.py that overwrite the properties *perms_map* of
*DjangoModelPermissions*

Now everything is ok when the test try to create object with a connected
user.

This, This look like this


class OrotangiTests(APITestCase):

    def setUp(self):
        self.user = User.objects.create_user(username='john',
                                             email='j...@doe.info',
                                             password='doe')

        self.client = APIClient()
        self.client.force_authenticate(user=self.user)


class BooksTests(OrotangiTests):

    def test_get_books(self):
        """
        Ensure we can get the books list.
        """
        url = reverse('books-list')
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_create_book(self):
        """
        Ensure we can create a new book object.
        """

        data = {'name': 'Book1',
                'user': self.user.username}

        url = reverse('books-list')
        response = self.client.post(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Books.objects.count(), 1)
        self.assertEqual(Books.objects.get().name, 'Book1')



Regards


2017-05-02 15:04 GMT+02:00 FoxMaSk <foxmaskh...@gmail.com>:

> Hi,
> I could investigate deeper and find the the cause is due to the settings
> DjangoModelPermissions
> When the user is created, no permissions are set for him.
>
> If I comment this settings that gives :
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.071s
>
> OK
> Destroying test database for alias 'default'...
>
> How do I set permissions to the user that will fit the
> DjangoModelPermissions requirements ??
>
> Regards
>
> Le samedi 29 avril 2017 22:32:46 UTC+2, FoxMaSk a écrit :
>>
>> Hi,
>>
>> As I'm totally new with DRF, and just started my first project with it, I
>> dont see from which side I can take the code to make my unit tests.
>>
>> I read http://www.django-rest-framework.org/api-guide/testing/
>>
>> And if I start from the beginning with
>>
>>
>>    - *APIRequestFactory, *
>>       - with "Creating test requests", as I my model own a FK with the
>>       User model, I dont know how I can use this example
>>       - with "Forcing authentication", the example says to use "user =
>>       User.objects.get(username='olivia')" but how do we create the user
>>       first of all ? Or do I need to care about it and the test does not 
>> need to
>>       care about the existence of the data in the User model ?
>>    - *APIClient*
>>       - with "Making Request", the request is done but what when we have
>>       to connect a user first to then only get the data of that user, how do 
>> we
>>       do ?
>>
>> I feel stuck until I dont have this clarifications.
>> Can someone explain me what I'm missing ?
>>
>> in case you need to see, my code is there https://github.com/orota
>> ngi/orotangi/tree/master/orotangi/api
>>
>> Thanks
>>
>> Regards.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to