def test_post_invalid_form( self, monkeypatch, client, name: str, contact_email: str, subject: str, message: str, ): """ When invalid forms are submitted, ensures we have a page rediction (return code 302) and that the email function is not called, """ mock_send_mail = Mock() monkeypatch.setattr("main.views.send_mail", mock_send_mail) invalid_form = ContactForm() invalid_form.name = name invalid_form.contact_email = contact_email invalid_form.subject = subject invalid_form.message = message contact_us_url = reverse("contact_us") response = client.post( contact_us_url, data=invalid_form.json(), HTTP_REFERER=contact_us_url, ) mock_send_mail.assert_not_called() assert "main/contact_us.html" in (t.name for t in response.templates) assert response.status_code == 200
def mock_contact_form() -> ContactForm: """Fixture for a valid ContactForm""" mock_form = ContactForm() mock_form.name = "dummy name" mock_form.contact_email = "*****@*****.**" mock_form.subject = "dummy subject" mock_form.message = "dummy content" return mock_form
''' from django.shortcuts import render, get_object_or_404 from django.contrib.auth.models import User from django.db.models import Count from django.core.mail import send_mail from blogi.models import Post from taggit.models import Tag ''' from main.forms import ContactForm import environ contact = ContactForm() contact.message="Hello Hal! It works1" contact.name = "Hal Vong" #contact.send_mail()