<commit_before>from django.test import TestCase
import os
import time
from django.test.utils import override_settings
from casexml.apps.phone.caselogic import BatchedCaseSyncOperation
from casexml.apps.phone.tests.utils import generate_restore_payload
from couchforms.tests.testutils import post_xform_to_couch
from casexml.apps.case.models import CommCareCase
from casexml.apps.case.tests.util import check_xml_line_by_line, delete_all_cases, delete_all_sync_logs
from casexml.apps.case.xform import process_cases
from datetime import datetime, date
from casexml.apps.phone.models import User, SyncLog
from casexml.apps.phone import xml
from django.contrib.auth.models import User as DjangoUser
from casexml.apps.phone.tests import const
from casexml.apps.case import const as case_const
from casexml.apps.phone.tests.dummy import dummy_restore_xml, dummy_user,\
    dummy_user_xml, DUMMY_USERNAME
from corehq import toggles
from toggle.shortcuts import update_toggle_cache, clear_toggle_cache


@override_settings(CASEXML_FORCE_DOMAIN_CHECK=False)
class OtaRestoreTest(TestCase):
    """Tests OTA Restore"""

    @classmethod
    def setUpClass(cls):
        delete_all_cases()
        delete_all_sync_logs()

    def tearDown(self):
        delete_all_cases()
        delete_all_sync_logs()

    def testFromDjangoUser(self):
        django_user = DjangoUser(username="foo", password="<PASSWORD>", date_joined=datetime(2011, 6, 9))
        django_user.save()
        user = User.from_django_user(django_user)
        self.assertEqual(str(django_user.pk), user.user_id)
        self.assertEqual("foo", user.username)
        self.assertEqual("secret", user.password)
        self.assertEqual(datetime(2011, 6, 9), user.date_joined)
        self.assertFalse(bool(user.user_data))

    def testRegistrationXML(self):
        check_xml_line_by_line(self, dummy_user_xml(),
                               xml.get_registration_xml(dummy_user()))

    def testUserRestore(self):
        self.assertEqual(0, SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,<commit_msg>Delete cache on teardown<commit_after>from django.test import TestCase
import os
import time
from django.test.utils import override_settings
from casexml.apps.phone.caselogic import BatchedCaseSyncOperation
from casexml.apps.phone.tests.utils import generate_restore_payload
from couchforms.tests.testutils import post_xform_to_couch
from casexml.apps.case.models import CommCareCase
from casexml.apps.case.tests.util import check_xml_line_by_line, delete_all_cases, delete_all_sync_logs
from casexml.apps.phone.restore import RestoreConfig
from casexml.apps.case.xform import process_cases
from datetime import datetime, date
from casexml.apps.phone.models import User, SyncLog
from casexml.apps.phone import xml
from django.contrib.auth.models import User as DjangoUser
from casexml.apps.phone.tests import const
from casexml.apps.case import const as case_const
from casexml.apps.phone.tests.dummy import dummy_restore_xml, dummy_user,\
    dummy_user_xml, DUMMY_USERNAME
from corehq import toggles
from toggle.shortcuts import update_toggle_cache, clear_toggle_cache


@override_settings(CASEXML_FORCE_DOMAIN_CHECK=False)
class OtaRestoreTest(TestCase):
    """Tests OTA Restore"""

    @classmethod
    def setUpClass(cls):
        delete_all_cases()
        delete_all_sync_logs()

    def tearDown(self):
        delete_all_cases()
        delete_all_sync_logs()
        restore_config = RestoreConfig(dummy_user())
        restore_config.cache.delete(restore_config._initial_cache_key())

    def testFromDjangoUser(self):
        django_user = DjangoUser(username="foo", password="<PASSWORD>", date_joined=datetime(2011, 6, 9))
        django_user.save()
        user = User.from_django_user(django_user)
        self.assertEqual(str(django_user.pk), user.user_id)
        self.assertEqual("foo", user.username)
        self.assertEqual("<PASSWORD>", user.password)
        self.assertEqual(datetime(2011, 6, 9), user.date_joined)
        self.assertFalse(bool(user.user_data))

    def testRegistrationXML(self):
        check_xml_line_by_line(self, dummy_user_xml(),
                               xml.get_registration_xml(dummy_user()))

    def testUserRestore(self):
        self.assertEqual(0, SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
{'fraction_non_alphanumeric': 0.0826879271070615, 'fraction_numerical': 0.00592255125284738, 'mean_word_length': 4.303140096618358, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 37}, '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': '2769014', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1302, 'n_words': 273}