Home > Enterprise >  TypeError: Field 'id' expected a number but got <django.contrib.auth.models.AnonymousUs
TypeError: Field 'id' expected a number but got <django.contrib.auth.models.AnonymousUs

Time:02-21

first time I ask on the website.

I was testing a Django Restframework app. The is to check an unauthenticated user that wants to create a review. I got this error: TypeError: Field 'id' expected a number but got <django.contrib.auth.models.AnonymousUser object at 0x7f5e95756920>.

this is the class test: class ReviewTestCase(APITestCase):

def setUp(self):
    self.user = User.objects.create_user(
        username="example", password="Password@123")
    self.token = Token.objects.get(user__username=self.user)
    self.client.credentials(HTTP_AUTHORIZATION='Token '   self.token.key)

    self.stream = models.StreamPlatform.objects.create(
        name="Netflix",
        about="#1 Platform",
        website="https://www.netflix.com"
    )
    self.watchlist = models.WatchList.objects.create(
        platform=self.stream, title="Example Movie",
        storyline="Example Movie",
        active=True
    )
    self.watchlist2 = models.WatchList.objects.create(platform=self.stream, title="Example Movie",
                                                      storyline="Example Movie", active=True)
    self.review = models.Review.objects.create(review_user=self.user, rating=5, description="Great Movie",
                                               watchlist=self.watchlist2, active=True)

def test_review_create(self):
    data = {
        "review_user": self.user,
        "rating": 5,
        "description": "Great Movie!",
        "watchlist": self.watchlist,
        "active": True
    }

    response = self.client.post(
        reverse('reviewcreate', args=(self.watchlist.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    self.assertEqual(models.Review.objects.count(), 2)

    response = self.client.post(
        reverse('reviewcreate', args=(self.watchlist.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
**#The test who cause the error**
def test_review_create_unauth(self):
    data = {
        "review_user": self.user,
        "rating": 5,
        "description": "Great Movie!",
        "watchlist": self.watchlist,
        "active": True
    }

    self.client.force_authenticate(user=None, token=None)
    response = self.client.post(
        reverse('reviewcreate', args=(self.watchlist.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_review_update(self):
    data = {
        "review_user": self.user,
        "rating": 4,
        "description": "Great Movie! - Updated",
        "watchlist": self.watchlist,
        "active": False
    }
    response = self.client.put(
        reverse('review-detail', args=(self.review.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_review_list(self):
    response = self.client.get(
        reverse('movie-review', args=(self.watchlist.id,)))
    self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_review_ind(self):
    response = self.client.get(
        reverse('review-detail', args=(self.review.id,)))
    self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_review_ind_delete(self):
    response = self.client.delete(
        reverse('review-detail', args=(self.review.id,)))
    self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

def test_review_user(self):
    response = self.client.get(
        '/watch/review/?username'   self.user.username)
    self.assertEqual(response.status_code, status.HTTP_200_OK)

CodePudding user response:

In data items, you are passing an object, and you have to pasa an id. Change this:

review_user: self.user.id

CodePudding user response:

The error is with this:

self.client.force_authenticate(user=None, token=None)

You've set the user=None, so their is no user id. Try setting it like this:

self.client.force_authenticate(user=self.user, token=None)

Note that according to the docs you may need to refresh the user from the database if using the same user on multiple tests:

Note: force_authenticate directly sets request.user to the in-memory user instance. If you are re-using the same user instance across multiple tests that update the saved user state, you may need to call refresh_from_db() between tests.

Or, perhaps create another user to test on:

self.user = User.objects.create_user(
        username="another_user", password="Password@123")
self.client.force_authenticate(user=self.user, token=None)
  • Related