<commit_before>from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.core import mail
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.test.client import RequestFactory

from mock import Mock, patch
import pytest

from ..forms import ContactPasswordResetForm
from ..views.activation import (
    change_password,
    ActivationEmailsView,
    ResetPassword,
    SendActivationEmailView
)

User = get_user_model()


def test_reset_password_subject_contains_site_name():
    assert '{0}: password recovery'.format(settings.SITE_NAME) == ResetPassword().get_subject()


@pytest.mark.django_db
@patch('contacts.views.activation.messages', new=Mock())
def test_reset_password_view_sends_email_when_form_valid():
    user = User.objects.create(business_email='<EMAIL>')
    form = ContactPasswordResetForm(data={'email': '<EMAIL>'})
    form.users_cache = [user]

    request = RequestFactory().get('/')

    mail.outbox = []

    view = ResetPassword()
    view.request = request
    view.form_valid(form)

    assert len(mail.outbox) > 0


@pytest.mark.django_db
def test_reset_password_view_displays_message_when_form_valid():
    user = User.objects.create(business_email='<EMAIL>')
    form = ContactPasswordResetForm(data={'email': '<EMAIL>'})<commit_msg>Add test to ensure password reset emials not sent when form invalid<commit_after>from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.core import mail
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.test.client import RequestFactory

from mock import Mock, patch
import pytest

from ..forms import ContactPasswordResetForm
from ..views.activation import (
    change_password,
    ActivationEmailsView,
    ResetPassword,
    SendActivationEmailView
)

User = get_user_model()


def test_reset_password_subject_contains_site_name():
    assert '{0}: password recovery'.format(settings.SITE_NAME) == ResetPassword().get_subject()


@pytest.mark.django_db
@patch('contacts.views.activation.messages', new=Mock())
def test_reset_password_view_sends_email_when_form_valid():
    user = User.objects.create(business_email='<EMAIL>')
    form = ContactPasswordResetForm(data={'email': '<EMAIL>'})
    form.users_cache = [user]

    request = RequestFactory().get('/')

    mail.outbox = []

    view = ResetPassword()
    view.request = request
    view.form_valid(form)

    assert len(mail.outbox) > 0


@pytest.mark.django_db
@patch('contacts.views.activation.messages', new=Mock())
def test_reset_password_view_doesnt_send_email_when_form_invalid():
    user = User.objects.create(business_email='<EMAIL>')
    form = ContactPasswordResetForm()
    form.users_cache = [user]

    request = RequestFactory().get('/')

    mail.outbox = []

    view = ResetPassword()
    view.request = request
    view.form_invalid(form)

    assert len(mail.outbox) == 0


@pytest.mark.django_db
def test_reset_password_view_displays_message_when_form_valid():
    user = User.objects.create(business_email='<EMAIL>')
    form = ContactPasswordResetForm(data={'email': '<EMAIL>'})
{'fraction_non_alphanumeric': 0.10434032768036793, 'fraction_numerical': 0.0014371945961483186, 'mean_word_length': 5.385321100917431, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6956423', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1021, 'n_words': 212}