# 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.user_verified.save()

        # User who is not verified yet on the site
        self.user_not_verified_email = 'user_not_verified@mail.com'
        self.user_not_verified_pw = 'user_not_verified'
        self.user_not_verified = get_user_model().objects.create_user(self.user_not_verified_email, 'pw')
        self.user_not_verified.save()

        # Email address available
        self.available_email = 'available@mail.com'

    def test_email_change_no_auth_token(self):
        url = reverse('authemail-email-change')
        payload = {
            'email': self.user_to_change_email,
        }
        response = self.client.post(url, payload)

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.data['detail'],
                         'Authentication credentials were not provided.')

    def test_email_change_invalid_auth_token(self):
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + 'XXX')
        url = reverse('authemail-email-change')
        response = self.client.post(url)

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.data['detail'], 'Invalid token.')


# 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.user_verified.save()

        # User who is not verified yet on the site
        self.user_not_verified_email = 'user_not_verified@mail.com'
        self.user_not_verified_pw = 'user_not_verified'
        self.user_not_verified = get_user_model().objects.create_user(self.user_not_verified_email, 'pw')
        self.user_not_verified.save()

        # Email address available
        self.available_email = 'available@mail.com'

    def test_email_change_no_auth_token(self):
        url = reverse('authemail-email-change')
        payload = {
            'email': self.user_to_change_email,
        }
        response = self.client.post(url, payload)

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.data['detail'],
                         'Authentication credentials were not provided.')

    def test_email_change_invalid_auth_token(self):
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + 'XXX')
        url = reverse('authemail-email-change')
        response = self.client.post(url)

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.data['detail'], 'Invalid token.')


    def test_email_change_serializer_errors(self):