How to test GET and POST api which requires user logged in pytest Django?

I have created a fixture to create user in conftest.py

@pytest.fixture
def test_password():
   return 'strong-test-pass'




@pytest.fixture(scope='session')
def create_user(db, test_password):
    def make_user(**kwargs):
        employee = e_ge_employee.objects.create()
        kwargs['password'] = test_password
        if 'username' not in kwargs:
            kwargs['username'] = str(uuid.uuid4())
        if 'employee' not in kwargs:
            kwargs['employee'] = employee
        return e_ge_user.objects.create(**kwargs)
    return make_user

and then wrote a testcase to check login in test_urls.py

@pytest.mark.django_db
def test_auth_view(client, create_user, test_password):
    user = create_user()
    url = '/accounts/login/'
    client.login(
        username=user.username, password=test_password
    )
    response = client.get(url)
    print("RESPONSE - ",response)
    assert response.status_code == 200

Now I want to write a test case to test GET and POST API and the APIs will only work if the user is authenticated, Can someone please provide me with the solution.....Thanks in Advance

Back to Top