# Path: authemail/models.py
# class SignupCode(AbstractBaseCode):
#     ipaddr = models.GenericIPAddressField(_('ip address'))
# 
#     objects = SignupCodeManager()
# 
#     def send_signup_email(self):
#         prefix = 'signup_email'
#         self.send_email(prefix)
# 
# class PasswordResetCode(AbstractBaseCode):
#     objects = PasswordResetCodeManager()
# 
#     def send_password_reset_email(self):
#         prefix = 'password_reset_email'
#         self.send_email(prefix)
# 
# Path: authemail/models.py
# class EmailChangeCode(AbstractBaseCode):
#     email = models.EmailField(_('email address'), max_length=255)
# 
#     objects = EmailChangeCodeManager()
# 
#     def send_email_change_emails(self):
#         prefix = 'email_change_notify_previous_email'
#         self.send_email(prefix)
# 
#         prefix = 'email_change_confirm_new_email'
#         ctxt = {
#             'email': self.email,
#             'code': self.code
#         }
# 
#         send_multi_format_email(prefix, ctxt, target_email=self.email)


import re
from datetime import timedelta
from django.core import mail
from django.contrib.auth import get_user_model
from django.test.utils import override_settings
from django.urls import reverse
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from authemail.models import SignupCode, PasswordResetCode
from authemail.models import EmailChangeCode
        self.assertEqual(email_change_code.user.email, self.user_to_change_email)
        self.assertEqual(email_change_code.email, self.user_not_verified_email)

        # Confirm that email address in response
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['email'], payload['email'])

        # Confirm that two emails sent and that Subject correct
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].subject, 'Notify Previous Email Address')
        self.assertEqual(mail.outbox[1].subject, 'Confirm New Email Address')

    def test_email_change_no_other_user_code_created_and_emails_sent(self):
        # Create two past change codes that aren't used
        email_change_code_old1 = EmailChangeCode.objects.create_email_change_code(
            self.user_to_change, self.user_not_verified_email)
        email_change_code_old2 = EmailChangeCode.objects.create_email_change_code(
            self.user_to_change, self.user_not_verified_email)
        count = EmailChangeCode.objects.filter(user=self.user_to_change).count()
        self.assertEqual(count, 2)

        # Send Email Change request
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        url = reverse('authemail-email-change')
        payload = {
            'email': self.available_email,
        }
        response = self.client.post(url, payload)

        # Get email change code

# Path: authemail/models.py
# class SignupCode(AbstractBaseCode):
#     ipaddr = models.GenericIPAddressField(_('ip address'))
# 
#     objects = SignupCodeManager()
# 
#     def send_signup_email(self):
#         prefix = 'signup_email'
#         self.send_email(prefix)
# 
# class PasswordResetCode(AbstractBaseCode):
#     objects = PasswordResetCodeManager()
# 
#     def send_password_reset_email(self):
#         prefix = 'password_reset_email'
#         self.send_email(prefix)
# 
# Path: authemail/models.py
# class EmailChangeCode(AbstractBaseCode):
#     email = models.EmailField(_('email address'), max_length=255)
# 
#     objects = EmailChangeCodeManager()
# 
#     def send_email_change_emails(self):
#         prefix = 'email_change_notify_previous_email'
#         self.send_email(prefix)
# 
#         prefix = 'email_change_confirm_new_email'
#         ctxt = {
#             'email': self.email,
#             'code': self.code
#         }
# 
#         send_multi_format_email(prefix, ctxt, target_email=self.email)


# Path: authemail/tests.py
import re
from datetime import timedelta
from django.core import mail
from django.contrib.auth import get_user_model
from django.test.utils import override_settings
from django.urls import reverse
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from authemail.models import SignupCode, PasswordResetCode
from authemail.models import EmailChangeCode

        self.assertEqual(email_change_code.user.email, self.user_to_change_email)
        self.assertEqual(email_change_code.email, self.user_not_verified_email)

        # Confirm that email address in response
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['email'], payload['email'])

        # Confirm that two emails sent and that Subject correct
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].subject, 'Notify Previous Email Address')
        self.assertEqual(mail.outbox[1].subject, 'Confirm New Email Address')

    def test_email_change_no_other_user_code_created_and_emails_sent(self):
        # Create two past change codes that aren't used
        email_change_code_old1 = EmailChangeCode.objects.create_email_change_code(
            self.user_to_change, self.user_not_verified_email)
        email_change_code_old2 = EmailChangeCode.objects.create_email_change_code(
            self.user_to_change, self.user_not_verified_email)
        count = EmailChangeCode.objects.filter(user=self.user_to_change).count()
        self.assertEqual(count, 2)

        # Send Email Change request
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        url = reverse('authemail-email-change')
        payload = {
            'email': self.available_email,
        }
        response = self.client.post(url, payload)

        # Get email change code

        email_change_code = EmailChangeCode.objects.latest('code')