<commit_before>import json
from urllib.parse import urlencode

from django.test import TestCase
from django.urls import reverse

from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.userreports.const import UCR_NAMED_EXPRESSION
from corehq.apps.userreports.models import UCRExpression
from corehq.apps.users.models import HQApiKey, WebUser
from corehq.form_processor.tests.utils import FormProcessorTestUtils
from corehq.motech.generic_inbound.models import ConfigurableAPI


class TestGenericInboundAPIView(TestCase):
    domain_name = 'ucr-api-test'

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.domain = create_domain(cls.domain_name)
        cls.user = WebUser.create(cls.domain_name, '<EMAIL>', 'secret', None, None)
        cls.addClassCleanup(cls.domain.delete)

        cls.api_key, _ = HQApiKey.objects.get_or_create(user=cls.user.get_django_user())

    def _make_api(self, property_expressions):
        return ConfigurableAPI.objects.create(
            domain=self.domain_name,
            transform_expression=self._make_expression(property_expressions)
        )

    def _make_expression(self, property_expressions):
        return UCRExpression.objects.create(
            name='create_sport',
            domain=self.domain_name,
            expression_type=UCR_NAMED_EXPRESSION,
            definition=self._get_ucr_case_expression(property_expressions)
        )

    def _get_ucr_case_expression(self, property_expressions):
        return {
            'type': 'dict',
            'properties': {
                'create': True,
                'case_type': 'sport',
                'case_name': {
                    'type': 'jsonpath',
                    'jsonpath': 'body.name',
                },
                'owner_id': {
                    'type': 'jsonpath',
                    'jsonpath': 'user.uuid'
                },
                'properties': {
                    'type': 'dict',
                    'properties': property_expressions
                }
            }
        }

    @classmethod
    def tearDownClass(cls):
        FormProcessorTestUtils.delete_all_cases_forms_ledgers(cls.domain_name)

    def test_post_denied(self):
        generic_api = self._make_api({})
        url = reverse('generic_inbound_api', args=[self.domain_name, generic_api.url_key])
        response = self.client.post(url, data={})
        self.assertEqual(response.status_code, 401)

    def test_post_not_json(self):
        generic_api = self._make_api({})
        url = reverse('generic_inbound_api', args=[self.domain_name, generic_api.url_key])
        response = self.client.post(
            url, data={}, HTTP_AUTHORIZATION=f"apikey {self.user.username}:{self.api_key.key}"
        )
        self.assertEqual(response.status_code, 400)

    def test_post(self):
        response_json = self._test_generic_api({
            'is_team_sport': {<commit_msg>add missing class teardown<commit_after>import json
from urllib.parse import urlencode

from django.test import TestCase
from django.urls import reverse

from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.userreports.const import UCR_NAMED_EXPRESSION
from corehq.apps.userreports.models import UCRExpression
from corehq.apps.users.models import HQApiKey, WebUser
from corehq.form_processor.tests.utils import FormProcessorTestUtils
from corehq.motech.generic_inbound.models import ConfigurableAPI


class TestGenericInboundAPIView(TestCase):
    domain_name = 'ucr-api-test'

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.domain = create_domain(cls.domain_name)
        cls.user = WebUser.create(cls.domain_name, '<EMAIL>', 'secret', None, None)
        cls.addClassCleanup(cls.domain.delete)

        cls.api_key, _ = HQApiKey.objects.get_or_create(user=cls.user.get_django_user())

    def _make_api(self, property_expressions):
        return ConfigurableAPI.objects.create(
            domain=self.domain_name,
            transform_expression=self._make_expression(property_expressions)
        )

    def _make_expression(self, property_expressions):
        return UCRExpression.objects.create(
            name='create_sport',
            domain=self.domain_name,
            expression_type=UCR_NAMED_EXPRESSION,
            definition=self._get_ucr_case_expression(property_expressions)
        )

    def _get_ucr_case_expression(self, property_expressions):
        return {
            'type': 'dict',
            'properties': {
                'create': True,
                'case_type': 'sport',
                'case_name': {
                    'type': 'jsonpath',
                    'jsonpath': 'body.name',
                },
                'owner_id': {
                    'type': 'jsonpath',
                    'jsonpath': 'user.uuid'
                },
                'properties': {
                    'type': 'dict',
                    'properties': property_expressions
                }
            }
        }

    @classmethod
    def tearDownClass(cls):
        FormProcessorTestUtils.delete_all_cases_forms_ledgers(cls.domain_name)
        super().tearDownClass()

    def test_post_denied(self):
        generic_api = self._make_api({})
        url = reverse('generic_inbound_api', args=[self.domain_name, generic_api.url_key])
        response = self.client.post(url, data={})
        self.assertEqual(response.status_code, 401)

    def test_post_not_json(self):
        generic_api = self._make_api({})
        url = reverse('generic_inbound_api', args=[self.domain_name, generic_api.url_key])
        response = self.client.post(
            url, data={}, HTTP_AUTHORIZATION=f"apikey {self.user.username}:{self.api_key.key}"
        )
        self.assertEqual(response.status_code, 400)

    def test_post(self):
        response_json = self._test_generic_api({
            'is_team_sport': {
{'fraction_non_alphanumeric': 0.10045431600201918, 'fraction_numerical': 0.0020191822311963654, 'mean_word_length': 2.4923619271445356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1362225', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1712, 'n_words': 280}