<commit_before>from unittest import skip

from django_migration_testcase import MigrationTest

from django.contrib.auth import get_user_model
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
from django.urls import reverse
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode

from callisto_core.tests.test_base import (
    ReportFlowHelper as ReportFlowTestCase,
)
from callisto_core.utils.api import NotificationApi

from ..models import Account, BulkAccount

User = get_user_model()


class AccountEmailParsingTest(ReportFlowTestCase):

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        Site.objects.create(id=2)
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )

    def test_case_lowered(self):
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(Account.objects.filter(
            school_email='<EMAIL>').count(), 0)
        self.assertEqual(Account.objects.filter(
            school_email='<EMAIL>').count(), 1)

    def test_empty_emails_not_sent(self):
        bulk_account = BulkAccount.objects.create(
            emails=' , ',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['', '']
        )

    def test_email_address_is_user_name(self):
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )

        self.assertTrue(
            User.objects.filter(username='<EMAIL>').count(),
        )

    def test_spaces_allowed_in_email_list(self):
        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>,  <EMAIL>',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>', '<EMAIL>']
        )

    def test_special_characters_allowed_in_email(self):
        bulk_account = BulkAccount.objects.create(
            emails='first!?#$%^&*<EMAIL>',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['first!?#$%^&*<EMAIL>']
        )

    def test_basic_case(self):
        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>,<EMAIL>,<EMAIL>',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>', '<EMAIL>', '<EMAIL>']
        )

    def test_can_run_multiple_times_on_one_account(self):
        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>']
        )

        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>']
        )


class AccountEmailTest(ReportFlowTestCase):

    @skip('skip pending NotificationApi update')
    def test_gets_account_activation_email(self):
        Site.objects.create(id=2)
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(len(self.cassette), 1)
        self.assertEqual(
            self.cassette.requests[0].uri,
            NotificationApi.mailgun_post_route,
        )

    def test_can_activate_account(self):
        Site.objects.create(id=2)
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        account = Account.objects.get(school_email='<EMAIL>')
        uid = urlsafe_base64_encode(
            force_bytes(account.user.pk)).decode("utf-8")
        token = default_token_generator.make_token(account.user)

        response = self.client.get(
            reverse(
                'activate_account',
                kwargs={'uidb64': uid, 'token': token},
            ),
            follow=True,
        )

        self.assertNotIn('invalid_token', response.context)
        self.assertTemplateUsed(
            response, 'callisto_core/accounts/account_activation_confirm.html')<commit_msg>removed duplicate call to create site<commit_after>from unittest import skip

from django_migration_testcase import MigrationTest

from django.contrib.auth import get_user_model
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
from django.urls import reverse
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode

from callisto_core.tests.test_base import (
    ReportFlowHelper as ReportFlowTestCase,
)
from callisto_core.utils.api import NotificationApi

from ..models import Account, BulkAccount

User = get_user_model()


class AccountEmailParsingTest(ReportFlowTestCase):

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )

    def test_case_lowered(self):
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(Account.objects.filter(
            school_email='<EMAIL>').count(), 0)
        self.assertEqual(Account.objects.filter(
            school_email='<EMAIL>').count(), 1)

    def test_empty_emails_not_sent(self):
        bulk_account = BulkAccount.objects.create(
            emails=' , ',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['', '']
        )

    def test_email_address_is_user_name(self):
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )

        self.assertTrue(
            User.objects.filter(username='<EMAIL>').count(),
        )

    def test_spaces_allowed_in_email_list(self):
        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>,  <EMAIL>',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>', '<EMAIL>']
        )

    def test_special_characters_allowed_in_email(self):
        bulk_account = BulkAccount.objects.create(
            emails='first!?#$%^&*@<EMAIL>',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['first!?#$%^&*@<EMAIL>']
        )

    def test_basic_case(self):
        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>,<EMAIL>,<EMAIL>',
            site_id=2,
        )

        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>', '<EMAIL>', '<EMAIL>']
        )

    def test_can_run_multiple_times_on_one_account(self):
        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>']
        )

        bulk_account = BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(
            bulk_account.parsed_emails,
            ['<EMAIL>']
        )


class AccountEmailTest(ReportFlowTestCase):

    @skip('skip pending NotificationApi update')
    def test_gets_account_activation_email(self):
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        self.assertEqual(len(self.cassette), 1)
        self.assertEqual(
            self.cassette.requests[0].uri,
            NotificationApi.mailgun_post_route,
        )

    def test_can_activate_account(self):
        BulkAccount.objects.create(
            emails='<EMAIL>',
            site_id=2,
        )
        account = Account.objects.get(school_email='<EMAIL>')
        uid = urlsafe_base64_encode(
            force_bytes(account.user.pk)).decode("utf-8")
        token = default_token_generator.make_token(account.user)

        response = self.client.get(
            reverse(
                'activate_account',
                kwargs={'uidb64': uid, 'token': token},
            ),
            follow=True,
        )

        self.assertNotIn('invalid_token', response.context)
        self.assertTemplateUsed(
            response, 'callisto_core/accounts/account_activation_confirm.html')
{'fraction_non_alphanumeric': 0.10928638941398866, 'fraction_numerical': 0.005552930056710775, 'mean_word_length': 2.1340244353942985, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, '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': '7553636', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2538, 'n_words': 353}