old_contents: import pytest
  from unittest.mock import patch
  
  from django.core.urlresolvers import reverse
  
  
  def test_profile_page_redirects_when_called_without_user(client):
      response = client.get(reverse('profile:edit_view'))
      assert response.status_code == 302
      assert response.url == '/accounts/login/?next=/profile/edit/'
  
  
  @pytest.mark.django_db()
  def test_new_registration_with_email_moves_email_address_correctly(authenticated_client):
      user = authenticated_client.user
      original_email = authenticated_client.user.email
  
      assert user.email != ''
  
      response = authenticated_client.get(reverse('profile:edit_view'))
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert 'To activate your email, click the link in the confirmation email.' in response.content.decode()
      assert user.email == ''
      assert user.profile.unverified_email == original_email
  
  
  @pytest.mark.django_db()
  def test_registration_with_email_doesnt_move_email_when_already_authorized(authorized_client):
      user = authorized_client.user
      original_email = user.email
  
      assert user.email != ''
  
      response = authorized_client.get(reverse('profile:edit_view'))
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert user.email == original_email
      assert user.profile.unverified_email == original_email
      assert 'To activate your email, click the link in the confirmation email.' not in response.content.decode()
  
  
  @pytest.mark.django_db()
  def test_email_change(authorized_client):
      authorized_client.get(reverse('profile:edit_view'))  # create user before posting
      user = authorized_client.user
      original_email = user.email
      new_email = 'someone@example.com'
  
      response = authorized_client.post(reverse('profile:edit_view'), {'unverified_email': new_email}, follow=True)
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert user.email == original_email
      assert user.profile.unverified_email == new_email
      assert 'To activate your email, click the link in the confirmation email.' in response.content.decode()
  
  
  @pytest.mark.django_db()
  def test_mail_sent_only_once(authenticated_client):
      with patch('osmaxx.profile.views.send_mail') as send_mail:
          assert send_mail.call_count == 0
          authenticated_client.get(reverse('profile:edit_view'))
          assert send_mail.call_count == 1
          authenticated_client.get(reverse('profile:edit_view'))
          authenticated_client.get(reverse('profile:edit_view'))
          authenticated_client.get(reverse('profile:edit_view'))
          assert send_mail.call_count == 1
  
new_contents: import pytest
  from unittest.mock import patch
  
  from django.core.urlresolvers import reverse
  
  
  def test_profile_page_redirects_when_called_without_user(client):
      response = client.get(reverse('profile:edit_view'))
      assert response.status_code == 302
      assert response.url == '/accounts/login/?next=/profile/edit/'
  
  
  @pytest.mark.django_db()
  def test_new_registration_with_email_moves_email_address_correctly(authenticated_client):
      user = authenticated_client.user
      original_email = authenticated_client.user.email
  
      assert user.email != ''
  
      response = authenticated_client.get(reverse('profile:edit_view'))
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert 'To activate your email, click the link in the confirmation email.' in response.content.decode()
      assert user.email == ''
      assert user.profile.unverified_email == original_email
  
  
  @pytest.mark.django_db()
  def test_new_registration_without_email_primes_the_user_to_add_one(authenticated_client):
      user = authenticated_client.user
      user.email = ''
      user.save()
  
      response = authenticated_client.get(reverse('profile:edit_view'))
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert 'You have not set an email. You must set a valid email to use OSMaxx.' in response.content.decode()
      assert user.email == ''
  
  
  @pytest.mark.django_db()
  def test_registration_with_email_doesnt_move_email_when_already_authorized(authorized_client):
      user = authorized_client.user
      original_email = user.email
  
      assert user.email != ''
  
      response = authorized_client.get(reverse('profile:edit_view'))
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert user.email == original_email
      assert user.profile.unverified_email == original_email
      assert 'To activate your email, click the link in the confirmation email.' not in response.content.decode()
  
  
  @pytest.mark.django_db()
  def test_email_change(authorized_client):
      authorized_client.get(reverse('profile:edit_view'))  # create user before posting
      user = authorized_client.user
      original_email = user.email
      new_email = 'someone@example.com'
  
      response = authorized_client.post(reverse('profile:edit_view'), {'unverified_email': new_email}, follow=True)
  
      user.refresh_from_db()
  
      assert response.status_code == 200
      assert user.email == original_email
      assert user.profile.unverified_email == new_email
      assert 'To activate your email, click the link in the confirmation email.' in response.content.decode()
  
  
  @pytest.mark.django_db()
  def test_mail_sent_only_once(authenticated_client):
      with patch('osmaxx.profile.views.send_mail') as send_mail:
          assert send_mail.call_count == 0
          authenticated_client.get(reverse('profile:edit_view'))
          assert send_mail.call_count == 1
          authenticated_client.get(reverse('profile:edit_view'))
          authenticated_client.get(reverse('profile:edit_view'))
          authenticated_client.get(reverse('profile:edit_view'))
          assert send_mail.call_count == 1
  
subject: test also the case with missing email on registration