<commit_before>from django.test import TestCase

from corehq.apps.app_manager.exceptions import PracticeUserException
from corehq.apps.app_manager.models import Application, BuildProfile
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.ota.utils import turn_on_demo_mode, turn_off_demo_mode
from corehq.apps.users.models import CommCareUser

from .app_factory import AppFactory
from .util import TestXmlMixin


class TestPracticeUserRestore(TestCase, TestXmlMixin):
    """Tests for Practice Mobile Worker feature"""
    file_path = ('data',)

    @classmethod
    def setUpClass(cls):
        super(TestPracticeUserRestore, cls).setUpClass()
        from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users
        delete_all_users()
        cls.domain = "practice-user-domain"

        cls.project = create_domain(cls.domain)
        cls.user = CommCareUser.create(cls.domain, '<EMAIL>', 'secret')

    def setUp(self):
        self.factory = AppFactory(build_version='2.28.0', domain=self.domain)
        module, form = self.factory.new_basic_module('register', 'case')
        form.source = self.get_xml('very_simple_form')
        self.factory.app.save()

    def tearDown(self):
        self.factory.app.delete()
        turn_off_demo_mode(self.user)

    @staticmethod
    def _get_restore_resource(version, build_profile_id=None):
        extra = "?profile={id}".format(id=build_profile_id) if build_profile_id else ""
        return """
        <partial>
        <user-restore>
           <resource id="practice-user-restore" version="{version}" descriptor="practice user restore">
             <location authority="local">./practice_user_restore.xml</location>
             <location authority="remote">./practice_user_restore.xml{extra}</location>
           </resource>
        </user-restore>
        </partial>
        """.format(version=version, extra=extra)

    def test_app_specific(self):
        turn_on_demo_mode(self.user, self.domain)
        app = self.factory.app
        app.practice_mobile_worker_id = self.user._id

        # check suit contains user-restore resource
        self.assertXmlPartialEqual(
            self._get_restore_resource(self.user.demo_restore_id),
            app.create_suite(),
            "./user-restore"
        )
        # check 'files/practice_user_restore.xml' is included in the build files
        app.create_build_files(save=True)
        app.save()
        self.assertTrue(app.lazy_fetch_attachment('files/practice_user_restore.xml'))
<commit_msg>test cleanup<commit_after>from django.test import TestCase

from corehq.apps.app_manager.exceptions import PracticeUserException
from corehq.apps.app_manager.models import Application, BuildProfile
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.ota.utils import turn_on_demo_mode, turn_off_demo_mode
from corehq.apps.users.models import CommCareUser

from .app_factory import AppFactory
from .util import TestXmlMixin


class TestPracticeUserRestore(TestCase, TestXmlMixin):
    """Tests for Practice Mobile Worker feature"""
    file_path = ('data',)

    @classmethod
    def setUpClass(cls):
        super(TestPracticeUserRestore, cls).setUpClass()
        cls.domain = "practice-user-domain"

        cls.project = create_domain(cls.domain)
        cls.user = CommCareUser.create(cls.domain, '<EMAIL>', 'secret')

    def setUp(self):
        self.factory = AppFactory(build_version='2.28.0', domain=self.domain)
        module, form = self.factory.new_basic_module('register', 'case')
        form.source = self.get_xml('very_simple_form')
        self.factory.app.save()

    def tearDown(self):
        self.factory.app.delete()
        turn_off_demo_mode(self.user)

    @classmethod
    def tearDownClass(cls):
        cls.project.delete()

    @staticmethod
    def _get_restore_resource(version, build_profile_id=None):
        extra = "?profile={id}".format(id=build_profile_id) if build_profile_id else ""
        return """
        <partial>
        <user-restore>
           <resource id="practice-user-restore" version="{version}" descriptor="practice user restore">
             <location authority="local">./practice_user_restore.xml</location>
             <location authority="remote">./practice_user_restore.xml{extra}</location>
           </resource>
        </user-restore>
        </partial>
        """.format(version=version, extra=extra)

    def test_app_specific(self):
        turn_on_demo_mode(self.user, self.domain)
        app = self.factory.app
        app.practice_mobile_worker_id = self.user._id

        # check suit contains user-restore resource
        self.assertXmlPartialEqual(
            self._get_restore_resource(self.user.demo_restore_id),
            app.create_suite(),
            "./user-restore"
        )
        # check 'files/practice_user_restore.xml' is included in the build files
        app.create_build_files(save=True)
        app.save()
        self.assertTrue(app.lazy_fetch_attachment('files/practice_user_restore.xml'))

{'fraction_non_alphanumeric': 0.10731899782994674, 'fraction_numerical': 0.001578220556322746, 'mean_word_length': 3.2320534223706177, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '4890047', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1474, 'n_words': 268}