<commit_before>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

import datetime

from django.test import TestCase, override_settings
from django.urls import reverse
from django.core.management import call_command

from utils.testing import helpers
from core import models


class CoreTests(TestCase):
    """
    Regression tests for the core application.
    """

    @override_settings(URL_CONFIG="domain")
    def test_create_user_form(self):

        data = {
            'email': '<EMAIL>',
            'is_active': True,
            'password_1': '<PASSWORD>',
            'password_2': '<PASSWORD>',
            'salutation': 'Prof.',
            'first_name': 'Martin',
            'last_name': 'Eve',
            'department': 'English & Humanities',
            'institution': 'Birkbeck, University of London',
            'country': 235,
        }

        self.client.force_login(self.admin_user)
        response = self.client.post(reverse('core_add_user'), data)

        try:
            models.Account.objects.get(email='<EMAIL>')
        except models.Account.DoesNotExist:
            self.fail('User account has not been saved.')

    @override_settings(URL_CONFIG="domain")
    def test_create_user_form_normalise_email(self):

        data = {
            'email': '<EMAIL>',
            'is_active': True,
            'password_1': '<PASSWORD>',
            'password_2': '<PASSWORD>',
            'salutation': 'Prof.',
            'first_name': 'Martin',
            'last_name': 'Eve',
            'department': 'English & Humanities',
            'institution': 'Birkbeck, University of London',
            'country': 235,
        }

        self.client.force_login(self.admin_user)
        response = self.client.post(reverse('core_add_user'), data)

        try:
            models.Account.objects.get(email='<EMAIL>')
        except models.Account.DoesNotExist:
            self.fail('User account has not been saved.')

    def setUp(self):
        self.press = helpers.create_press()
        self.press.save()
        self.journal_one, self.journal_two = helpers.create_journals()
        helpers.create_roles(["editor", "author", "reviewer", "proofreader", "production", "copyeditor", "typesetter",
                            "proofing_manager", "section-editor"])

        self.regular_user = helpers.create_user("<EMAIL>")
        self.regular_user.is_active = True
        self.regular_user.save()

        self.second_user = helpers.create_user("<EMAIL>", ["reviewer"], journal=self.journal_one)
        self.second_user.is_active = True
        self.second_user.save()

        self.admin_user = helpers.create_user("<EMAIL>")
        self.admin_user.is_staff = True
        self.admin_user.is_active = True
        self.admin_user.save()

        self.journal_one.name = 'Journal One'
        self.journal_two.name = 'Journal Two'
        call_command('install_plugins')<commit_msg>#1713: Adds test that detects missing email subjects<commit_after>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

import datetime

from django.test import TestCase, override_settings
from django.urls import reverse
from django.core.management import call_command

from utils.testing import helpers
from core import models


class CoreTests(TestCase):
    """
    Regression tests for the core application.
    """

    @override_settings(URL_CONFIG="domain")
    def test_create_user_form(self):

        data = {
            'email': '<EMAIL>',
            'is_active': True,
            'password_1': '<PASSWORD>',
            'password_2': '<PASSWORD>',
            'salutation': 'Prof.',
            'first_name': 'Martin',
            'last_name': 'Eve',
            'department': 'English & Humanities',
            'institution': 'Birkbeck, University of London',
            'country': 235,
        }

        self.client.force_login(self.admin_user)
        response = self.client.post(reverse('core_add_user'), data)

        try:
            models.Account.objects.get(email='<EMAIL>')
        except models.Account.DoesNotExist:
            self.fail('User account has not been saved.')

    @override_settings(URL_CONFIG="domain")
    def test_create_user_form_normalise_email(self):

        data = {
            'email': '<EMAIL>',
            'is_active': True,
            'password_1': '<PASSWORD>',
            'password_2': '<PASSWORD>',
            'salutation': 'Prof.',
            'first_name': 'Martin',
            'last_name': 'Eve',
            'department': 'English & Humanities',
            'institution': 'Birkbeck, University of London',
            'country': 235,
        }

        self.client.force_login(self.admin_user)
        response = self.client.post(reverse('core_add_user'), data)

        try:
            models.Account.objects.get(email='<EMAIL>')
        except models.Account.DoesNotExist:
            self.fail('User account has not been saved.')

    def test_email_subjects(self):
        email_settings= models.Setting.objects.filter(
            group__name="email",
        ).values_list("name", flat=True)
        subject_settings = models.Setting.objects.filter(
            group__name="email_subject",
        ).values_list("name", flat=True)
        missing = (
            set(email_settings)
            - {s[len("subject_"):] for s in subject_settings}
        )
        self.assertFalse(
            missing,
            msg="Found emails that don't have a subject setting")


    def setUp(self):
        self.press = helpers.create_press()
        self.press.save()
        self.journal_one, self.journal_two = helpers.create_journals()
        helpers.create_roles(["editor", "author", "reviewer", "proofreader", "production", "copyeditor", "typesetter",
                            "proofing_manager", "section-editor"])

        self.regular_user = helpers.create_user("<EMAIL>")
        self.regular_user.is_active = True
        self.regular_user.save()

        self.second_user = helpers.create_user("<EMAIL>", ["reviewer"], journal=self.journal_one)
        self.second_user.is_active = True
        self.second_user.save()

        self.admin_user = helpers.create_user("<EMAIL>")
        self.admin_user.is_staff = True
        self.admin_user.is_active = True
        self.admin_user.save()

        self.journal_one.name = 'Journal One'
        self.journal_two.name = 'Journal Two'
        call_command('install_plugins')
        call_command('load_default_settings')
{'fraction_non_alphanumeric': 0.1239620403321471, 'fraction_numerical': 0.0050415183867141165, 'mean_word_length': 2.4893947232281426, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '2458032', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1921, 'n_words': 392}