Search code examples
djangoseleniumtestingtdd

Unit Testing A @verified_email_required View


Im trying to unit test one of my views that is decorated with @verified_email_required. I am unable to find out how to set the user as having their email verified so that it will allow them to view the page and assert that it uses the correct template (creating a superuser doesnt help).

This is the error im getting

AssertionError: False is not true : Template 'enrolment/index.html' was not a template used to render the response. Actual template(s) used: account/verified_email_required.html, account/base.html, base.html

And this is my test

def test_verified_user_uses_correct_template(self):
    user = User.objects.create_superuser('username')
    self.client.force_login(user)
    response = self.client.get('/enrolment/')
    self.assertTemplateUsed(response, 'enrolment/index.html')

Thank you.


Solution

  • This is not standard Django authentication but one from django-allauth package.

    To verify email address you have to create an EmailAdress object with verified=True

     EmailAddress.objects.create(user=user, email="example@example.com", primary=True, verified=True)
    

    You can see following model in source of django-allauth also some of package tests