<commit_before>from multimail.models import EmailAddress
from multimail.views import Verify
from mock import Mock, patch
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth.models import User
from django.test.client import RequestFactory
from django.utils import timezone
import datetime, django, multimail, unittest

class EmailAddressTest(unittest.TestCase):

    def setUp(self):
        u = User(username='testuser')
        u.save()
        self.obj_ut = EmailAddress(user=u)
        self.obj_ut.save()

    def tearDown(self):
        User.objects.filter(username='testuser').delete()

    def test_is_verified(self):
        addr = EmailAddress()
        self.assertFalse(addr.is_verified())
        addr.verified_at = timezone.now()
        self.assertTrue(addr.is_verified())

    @patch.object(multimail.models.EmailAddress, 'send_verification')
    def test_save(self, mock_send_verification):
        addr = self.obj_ut
        self.assertEquals(30, len(addr.verif_key))
        addr.save()
        self.assertTrue(mock_send_verification.not_called)
        addr2 = EmailAddress(user=addr.user)
        addr2.save()
        self.assertTrue(mock_send_verification.called)

    @patch.object(EmailMultiAlternatives, 'send')
    def test_send_verification(self, mock_send):
        addr = self.obj_ut
        addr.send_verification()
        self.assertTrue(mock_send.called)

class VerifyTest(unittest.TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.request = self.factory.get('/verify')
        u = User(username='testuser')
        u.save()
        self.addr = EmailAddress(user=u)
        self.addr.save()

    def tearDown(self):
        User.objects.filter(username='testuser').delete()

    @patch.object(django.contrib.messages, 'success')
    def test_verify__success(self, mock_success):
        response = Verify.as_view()(self.request, self.addr.pk,
            self.addr.verif_key)
        self.assertTrue(mock_success.called)

    @patch.object(django.contrib.messages, 'error')
    def test_verify__not_exist(self, mock_error):
        response = Verify.as_view()(self.request, -1, self.addr.verif_key)
        self.assertTrue(mock_error.called)

    @patch.object(django.contrib.messages, 'error')
    def test_verify__inactive_account(self, mock_error):
        self.addr.user.is_active = False
        self.addr.user.save()
        response = Verify.as_view()(self.request, self.addr.pk,
            self.addr.verif_key)
        self.assertTrue(mock_error.called)

    @patch.object(django.contrib.messages, 'error')
    def test_verify__already_verified(self, mock_error):
        self.addr.verified_at = timezone.now()
        self.addr.save()
        response = Verify.as_view()(self.request, self.addr.pk,
            self.addr.verif_key)
        self.assertTrue(mock_error.called)<commit_msg>Fixes timezone issue in tests. Fall back to datetime to support Django 1.3<commit_after>from multimail.models import EmailAddress
from multimail.views import Verify
from mock import Mock, patch
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth.models import User
from django.test.client import RequestFactory
import django, multimail, unittest

try:
    from django.utils import timezone
    now = lambda: timezone.now()
except ImportError:
    import datetime
    now = lambda: datetime.datetime.now()


class EmailAddressTest(unittest.TestCase):

    def setUp(self):
        u = User(username='testuser')
        u.save()
        self.obj_ut = EmailAddress(user=u)
        self.obj_ut.save()

    def tearDown(self):
        User.objects.filter(username='testuser').delete()

    def test_is_verified(self):
        addr = EmailAddress()
        self.assertFalse(addr.is_verified())
        addr.verified_at = now()
        self.assertTrue(addr.is_verified())

    @patch.object(multimail.models.EmailAddress, 'send_verification')
    def test_save(self, mock_send_verification):
        addr = self.obj_ut
        self.assertEquals(30, len(addr.verif_key))
        addr.save()
        self.assertTrue(mock_send_verification.not_called)
        addr2 = EmailAddress(user=addr.user)
        addr2.save()
        self.assertTrue(mock_send_verification.called)

    @patch.object(EmailMultiAlternatives, 'send')
    def test_send_verification(self, mock_send):
        addr = self.obj_ut
        addr.send_verification()
        self.assertTrue(mock_send.called)

class VerifyTest(unittest.TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.request = self.factory.get('/verify')
        u = User(username='testuser')
        u.save()
        self.addr = EmailAddress(user=u)
        self.addr.save()

    def tearDown(self):
        User.objects.filter(username='testuser').delete()

    @patch.object(django.contrib.messages, 'success')
    def test_verify__success(self, mock_success):
        response = Verify.as_view()(self.request, self.addr.pk,
            self.addr.verif_key)
        self.assertTrue(mock_success.called)

    @patch.object(django.contrib.messages, 'error')
    def test_verify__not_exist(self, mock_error):
        response = Verify.as_view()(self.request, -1, self.addr.verif_key)
        self.assertTrue(mock_error.called)

    @patch.object(django.contrib.messages, 'error')
    def test_verify__inactive_account(self, mock_error):
        self.addr.user.is_active = False
        self.addr.user.save()
        response = Verify.as_view()(self.request, self.addr.pk,
            self.addr.verif_key)
        self.assertTrue(mock_error.called)

    @patch.object(django.contrib.messages, 'error')
    def test_verify__already_verified(self, mock_error):
        self.addr.verified_at = now()
        self.addr.save()
        response = Verify.as_view()(self.request, self.addr.pk,
            self.addr.verif_key)
        self.assertTrue(mock_error.called)
{'fraction_non_alphanumeric': 0.11262856179396392, 'fraction_numerical': 0.0020232675771370764, 'mean_word_length': 3.1774647887323946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3697254', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1803, 'n_words': 305}