Search code examples
djangopytestdjango-authentication

how to make query in django pytest test user


I have a django code which is for reset password and if user is authenticated the process goes on as change password. I write a test in pytest for this code, and i use a test user to auth and check change password stage, in code for process we need user national id and when user is already online we take national id from profile. so is provided national id in test user as well but the problem is that it's not wrking and my test user not return national id. does anybody has any idea whats problem? andis this any oher way to make test user for this test.

views.py

@method_decorator(csrf_protect, name='dispatch')
class ResetPassword(APIView):
    permission_classes = (permissions.AllowAny, )
        try:
            # read input
            nid = request.data.get('nationalId')
            #....take more input to conside stage based on them ....
            if not request.user.is_anonymous and request.user.is_authenticated:
                user = request.user
                nid = user.profile.identity_number 

tests.py

@pytest.fixture
def test_user_nid():
    user = User.objects.create_user(
        username="8835246563",
        password="upass123",
    )
    profile = Profile.objects.update(user=user, identity_number="8835246563")
    return user

@pytest.mark.django_db
 class Test:   

        def test_change_password_stages(self, api_client, test_user_nid):
            api_client.force_authenticate(user=test_user_nid)
            print("User ID:", test_user_nid2.id)
            print("User Profile Identity Number:", 
            test_user_nid2.profile.identity_number)
            data = {
                "tfaCode": 12345
            }
            response = api_client.post(self.url, data, format='json')
            # assert response.status_code == status.HTTP_200_OK
            response_data = response.json()
            print(">>>>", response_data)
    
            # Verify that the profile's identity_number is set correctly
            profile = test_user_nid2.profile
            print("Profile Identity Number:", profile.identity_number)

also this is my print statements results:

tests.py ...............User ID: 12
User Profile Identity Number:
>>>> {'State': 'Error', 'Message': 'WrongInputs', 'Data': {}}
Profile Identity Number:

this is my model for more informations: models.py

class Profile(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    identity_number = models.CharField(max_length=20, blank=True, default='')

Solution

  • i find out the problem here profile = Profile.objects.update(user=user, identity_number="8835246563") it goes and make query on database, we need to set on test database so it shoukd be like this:

    @pytest.fixture
    def test_user_nid2():
        user = User.objects.create_user(
            username="8835246563",
            password="upass123",
        )
        user.profile.identity_number="8835246563"
        user.profile.save()
        return user