<commit_before>from django.test import TestCase

from corehq.apps.api.exceptions import UpdateUserException
from corehq.apps.api.user_updates import update
from corehq.apps.custom_data_fields.models import (
    PROFILE_SLUG,
    CustomDataFieldsDefinition,
    CustomDataFieldsProfile,
    Field,
)
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.groups.models import Group
from corehq.apps.user_importer.helpers import UserChangeLogger
from corehq.apps.users.audit.change_messages import (
    GROUPS_FIELD,
    PASSWORD_FIELD,
    ROLE_FIELD,
)
from corehq.apps.users.models import CommCareUser, Permissions
from corehq.apps.users.models_role import UserRole
from corehq.apps.users.views.mobile import UserFieldsView
from corehq.const import USER_CHANGE_VIA_API


class TestUpdateUserMethods(TestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.domain = 'test-domain'
        cls.domain_obj = create_domain(cls.domain)
        cls.addClassCleanup(cls.domain_obj.delete)

    def setUp(self) -> None:
        super().setUp()
        self.user = CommCareUser.create('test-domain', 'test-username', 'qwer1234', None, None)
        self.addCleanup(self.user.delete, self.domain, deleted_by=None)

    def test_update_password_without_strong_passwords_succeeds(self):
        self.domain_obj.strong_mobile_passwords = False
        self.domain_obj.save()

        try:
            update(self.user, 'password', '<PASSWORD>')
        except UpdateUserException:
            self.fail('Unexpected ValidationError raised.')

    def test_update_password_with_strong_passwords_raises_exception(self):
        self.domain_obj.strong_mobile_passwords = True
        self.domain_obj.save()

        with self.assertRaises(UpdateUserException) as cm:
            update(self.user, 'password', '<PASSWORD>')

        self.assertEqual(cm.exception.message, "Password is not strong enough.")

    def test_update_password_with_strong_passwords_succeeds(self):
        self.domain_obj.strong_mobile_passwords = True
        self.domain_obj.save()

        try:
            update(self.user, 'password', '<PASSWORD>')
        except UpdateUserException:
            self.fail('Unexpected ValidationError raised.')

    def test_update_email_succeeds(self):
        self.user.email = '<EMAIL>'
        update(self.user, 'email', '<EMAIL>')
        self.assertEqual(self.user.email, '<EMAIL>')

    def test_update_first_name_succeeds(self):
        self.user.first_name = 'Initial'
        update(self.user, 'first_name', 'Updated')
        self.assertEqual(self.user.first_name, 'Updated')

    def test_update_last_name_succeeds(self):
        self.user.last_name = 'Initial'
        update(self.user, 'last_name', 'Updated')
        self.assertEqual(self.user.last_name, 'Updated')

    def test_update_language_succeeds(self):
        self.user.language = 'in'
        update(self.user, 'language', 'up')
        self.assertEqual(self.user.language, 'up')

    def test_update_default_phone_number_succeeds(self):
        self.user.set_default_phone_number('50253311398')<commit_msg>simplify tests that assert exception is not raised<commit_after>from django.test import TestCase

from corehq.apps.api.exceptions import UpdateUserException
from corehq.apps.api.user_updates import update
from corehq.apps.custom_data_fields.models import (
    PROFILE_SLUG,
    CustomDataFieldsDefinition,
    CustomDataFieldsProfile,
    Field,
)
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.groups.models import Group
from corehq.apps.user_importer.helpers import UserChangeLogger
from corehq.apps.users.audit.change_messages import (
    GROUPS_FIELD,
    PASSWORD_FIELD,
    ROLE_FIELD,
)
from corehq.apps.users.models import CommCareUser, Permissions
from corehq.apps.users.models_role import UserRole
from corehq.apps.users.views.mobile import UserFieldsView
from corehq.const import USER_CHANGE_VIA_API


class TestUpdateUserMethods(TestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.domain = 'test-domain'
        cls.domain_obj = create_domain(cls.domain)
        cls.addClassCleanup(cls.domain_obj.delete)

    def setUp(self) -> None:
        super().setUp()
        self.user = CommCareUser.create('test-domain', 'test-username', 'qwer1234', None, None)
        self.addCleanup(self.user.delete, self.domain, deleted_by=None)

    def test_update_password_without_strong_passwords_succeeds(self):
        self.domain_obj.strong_mobile_passwords = False
        self.domain_obj.save()

        update(self.user, 'password', '<PASSWORD>')  # should not raise

    def test_update_password_with_strong_passwords_raises_exception(self):
        self.domain_obj.strong_mobile_passwords = True
        self.domain_obj.save()

        with self.assertRaises(UpdateUserException) as cm:
            update(self.user, 'password', '<PASSWORD>')

        self.assertEqual(cm.exception.message, "Password is not strong enough.")

    def test_update_password_with_strong_passwords_succeeds(self):
        self.domain_obj.strong_mobile_passwords = True
        self.domain_obj.save()

        update(self.user, 'password', '<PASSWORD>')  # should not raise

    def test_update_email_succeeds(self):
        self.user.email = '<EMAIL>'
        update(self.user, 'email', '<EMAIL>')
        self.assertEqual(self.user.email, '<EMAIL>')

    def test_update_first_name_succeeds(self):
        self.user.first_name = 'Initial'
        update(self.user, 'first_name', 'Updated')
        self.assertEqual(self.user.first_name, 'Updated')

    def test_update_last_name_succeeds(self):
        self.user.last_name = 'Initial'
        update(self.user, 'last_name', 'Updated')
        self.assertEqual(self.user.last_name, 'Updated')

    def test_update_language_succeeds(self):
        self.user.language = 'in'
        update(self.user, 'language', 'up')
        self.assertEqual(self.user.language, 'up')

    def test_update_default_phone_number_succeeds(self):
        self.user.set_default_phone_number('50253311398')
{'fraction_non_alphanumeric': 0.10187755102040816, 'fraction_numerical': 0.004897959183673469, 'mean_word_length': 3.6906584992343032, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '7380077', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1878, 'n_words': 326}