Home > database >  test return redirect("account_login") in django use pytest
test return redirect("account_login") in django use pytest

Time:07-21

i using pytest and coverage for testing django project But I don't know how to test the else block that has return redirect("account_login") and fix the red part that says coverage.

In your opinion, how can I write a test for that else piece and fix the red part that says coverage?

enter image description here

views.py

@verified_email_required
def profile_view(request, username):
    # We check if the user is logged in?
    if request.user.is_authenticated:
        try:
            profiles = Profile.objects.all()[:4]
            # get user profile by username
            profile = Profile.objects.get(user__username__iexact=username)
            user = User.objects.get(username=username)
            user_following = following(user)
            user_followers = followers(user)
            logged_in_user_following = following(request.user)
            logged_in_user_followers = followers(request.user)
            # if user not found raise 404 error
        except ObjectDoesNotExist:
            raise Http404
        # context data
        context = {
            "user": user,
            "profile": profile,
            "profiles": profiles,
            "following": user_following,
            "followers": user_followers,
        }
        return render(request, "user_profile/profile.html", context)

    # if a user is not login in redirecting to login page
    else:
        return redirect("account_login")

test_view.py

@pytest.mark.django_db
class TestProfileView:
    @pytest.fixture
    def user(self):
        user, created = User.objects.get_or_create(  # created user
            username="test_username",
            first_name="test_first_name",
            last_name="test_last_name",
            email="[email protected]",
            is_active=True,
            is_superuser=False,
        )

        (
            user_email_address,
            created,
        ) = EmailAddress.objects.get_or_create(  # user email confirmation
            email=user.email, user=user
        )
        user_email_address.verified = True
        user_email_address.primary = True
        user_email_address.save()

        return user

    def test_profile_view_when_user_is_authenticated_return_correct_profile(
        self, user, client
    ):
        client.force_login(user)  # user logged in
        response = client.get(user.profile.get_absolute_url())
        content = response.content.decode(response.charset)
        assert response.status_code == 200
        assert (
            '<h1 >Test_first_name test_last_name</h1>'
            in str(content)
        )
        response = client.get("/user_not_exist", follow=True)
        assert response.status_code == 404

        response = client.post("/accounts/logout/")
        assert response.status_code == 302

    def test_profile_view_when_user_is_anonymous_return_login_page(self, user, client):
        response = client.get("/test_username/", follow=True)
        content = response.content.decode(response.charset)
        assertRedirects(response, "/accounts/login/?next=/test_username/")
        assert response.status_code == 200
        assert response.template_name[0] == "account/login.html"
        assert '<h1 >Login</h1>' in str(
            content
        )
        assert response.resolver_match.url_name == "account_login"
        assert response.resolver_match.view_name == "account_login"

CodePudding user response:

I suspect you won't be able to as your decorator @verified_email_required will prevent any unlogged in people from reaching that line of code.

My assumption here is that you are using allauth as it provides a decorator of that precise name. See https://django-allauth.readthedocs.io/en/stable/decorators.html which states: If the user isn’t logged in, it acts identically to the login_required decorator.

  • Related