<commit_before>import json
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings

from api import test_data
from api.models import ACL, AuthEvent
from api.tests import JClient
from authmethods.models import Code
from captcha.models import Captcha

# Create your tests here.

class TestProcessCaptcha(TestCase):
    fixtures = ['initial.json']
    def setUp(self):
        ae = AuthEvent(auth_method="email",
                auth_method_config=test_data.authmethod_config_email_default,
                extra_fields=test_data.ae_email_fields_captcha['extra_fields'],
                status='started',
                census="open")
        ae.save()
        self.ae = ae
        self.aeid = ae.pk

        u_admin = User(username=test_data.admin['username'])
        u_admin.set_password(test_data.admin['password'])
        u_admin.save()
        u_admin.userdata.event = ae
        u_admin.userdata.save()

        acl = ACL(user=u_admin.userdata, object_type='AuthEvent', perm='edit',
            object_id=self.aeid)
        acl.save()

        acl = ACL(user=u_admin.userdata, object_type='AuthEvent', perm='create',
                object_id=0)
        acl.save()

    def test_create_new_captcha(self):
        c = JClient()
        self.assertEqual(0, Captcha.objects.count())
        response = c.get('/api/captcha/new/', {})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(1, Captcha.objects.count())

    @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                       CELERY_ALWAYS_EAGER=True,
                       BROKER_BACKEND='memory')
    def test_pregenerate_captchas(self):
        from django.conf import settings #import PREGENERATION_CAPTCHA
        self.assertEqual(0, Captcha.objects.count())

        c = JClient()
        c.authenticate(0, test_data.admin)
        response = c.post('/api/auth-event/', test_data.ae_email_fields_captcha)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(settings.PREGENERATION_CAPTCHA, Captcha.objects.filter(used=False).count())

    def test_create_authevent_email_with_captcha(self):
        c = JClient()

        # add census without problem with captcha
        c.authenticate(0, test_data.admin)
        response = c.census(self.aeid, test_data.census_email_default)
        self.assertEqual(response.status_code, 200)
        response = c.get('/api/auth-event/%d/census/' % self.aeid, {})<commit_msg>Removed generated captchas when run tests.<commit_after>import json
import os
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings

from api import test_data
from api.models import ACL, AuthEvent
from api.tests import JClient
from authmethods.models import Code
from captcha.models import Captcha

# Create your tests here.

class TestProcessCaptcha(TestCase):
    fixtures = ['initial.json']
    def setUp(self):
        ae = AuthEvent(auth_method="email",
                auth_method_config=test_data.authmethod_config_email_default,
                extra_fields=test_data.ae_email_fields_captcha['extra_fields'],
                status='started',
                census="open")
        ae.save()
        self.ae = ae
        self.aeid = ae.pk

        u_admin = User(username=test_data.admin['username'])
        u_admin.set_password(test_data.admin['password'])
        u_admin.save()
        u_admin.userdata.event = ae
        u_admin.userdata.save()

        acl = ACL(user=u_admin.userdata, object_type='AuthEvent', perm='edit',
            object_id=self.aeid)
        acl.save()

        acl = ACL(user=u_admin.userdata, object_type='AuthEvent', perm='create',
                object_id=0)
        acl.save()

    def tearDown(self):
        # Removed generated captchas
        captcha_dir = settings.STATIC_ROOT + '/captcha/'
        captchas = [f for f in os.listdir(captcha_dir) if f.endswith('.png') ]
        for c in captchas:
            os.remove(captcha_dir + c)


    def test_create_new_captcha(self):
        c = JClient()
        self.assertEqual(0, Captcha.objects.count())
        response = c.get('/api/captcha/new/', {})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(1, Captcha.objects.count())

    @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                       CELERY_ALWAYS_EAGER=True,
                       BROKER_BACKEND='memory')
    def test_pregenerate_captchas(self):
        self.assertEqual(0, Captcha.objects.count())

        c = JClient()
        c.authenticate(0, test_data.admin)
        response = c.post('/api/auth-event/', test_data.ae_email_fields_captcha)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(settings.PREGENERATION_CAPTCHA, Captcha.objects.filter(used=False).count())

    def test_create_authevent_email_with_captcha(self):
        c = JClient()

        # add census without problem with captcha
        c.authenticate(0, test_data.admin)
        response = c.census(self.aeid, test_data.census_email_default)
        self.assertEqual(response.status_code, 200)
        response = c.get('/api/auth-event/%d/census/' % self.aeid, {})
{'fraction_non_alphanumeric': 0.10607776519441299, 'fraction_numerical': 0.0056625141562853904, 'mean_word_length': 2.828757225433526, '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': '7426518', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1600, 'n_words': 289}