Skip to content

Commit 750e00a

Browse files
committed
test: add unit tests for get_extended_profile_model function
1 parent e9c7382 commit 750e00a

1 file changed

Lines changed: 136 additions & 3 deletions

File tree

openedx/core/djangoapps/user_authn/views/tests/test_utils.py

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
"""
22
Tests for user utils functionality.
33
"""
4-
from django.test import TestCase
54
from datetime import datetime
6-
from openedx.core.djangoapps.user_authn.views.utils import get_auto_generated_username, _get_username_prefix
5+
from typing import Optional
6+
from unittest.mock import Mock, patch
7+
78
import ddt
8-
from unittest.mock import patch
9+
from django.db.models import Model
10+
from django.test import TestCase
11+
from django.test.utils import override_settings
12+
13+
from openedx.core.djangoapps.user_authn.views.registration_form import get_extended_profile_model
14+
from openedx.core.djangoapps.user_authn.views.utils import _get_username_prefix, get_auto_generated_username
915

1016

1117
@ddt.ddt
@@ -77,3 +83,130 @@ def test_get_auto_generated_username_no_prefix(self, mock_datetime, mock_choices
7783

7884
username = get_auto_generated_username({})
7985
self.assertEqual(username, expected_username)
86+
87+
@ddt.ddt
88+
class TestGetExtendedProfileModel(TestCase):
89+
"""
90+
Tests for get_extended_profile_model function
91+
"""
92+
93+
@ddt.data(None, "")
94+
@ddt.unpack
95+
def test_get_extended_profile_model_no_setting_or_empty_string(self, setting_value: Optional[str]):
96+
"""
97+
Test when REGISTRATION_EXTENSION_FORM setting is not configured
98+
"""
99+
with override_settings(REGISTRATION_EXTENSION_FORM=setting_value):
100+
result = get_extended_profile_model()
101+
102+
self.assertIsNone(result)
103+
104+
@override_settings(REGISTRATION_EXTENSION_FORM="invalid.module.path")
105+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.logger")
106+
def test_get_extended_profile_model_invalid_module(self, mock_logger: Mock):
107+
"""
108+
Test when the module path is invalid
109+
"""
110+
result = get_extended_profile_model()
111+
112+
self.assertIsNone(result)
113+
mock_logger.warning.assert_called_once()
114+
self.assertIn("Could not load extended profile model", str(mock_logger.warning.call_args))
115+
116+
@override_settings(REGISTRATION_EXTENSION_FORM="django.forms.Form")
117+
def test_get_extended_profile_model_no_meta_class(self):
118+
"""
119+
Test when the form class doesn't have a Meta class
120+
"""
121+
result = get_extended_profile_model()
122+
123+
# Form doesn't have Meta.model, should return None
124+
self.assertIsNone(result)
125+
126+
@override_settings(REGISTRATION_EXTENSION_FORM="invalid_module_path")
127+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.logger")
128+
def test_get_extended_profile_model_malformed_path(self, mock_logger: Mock):
129+
"""
130+
Test when the setting value doesn't have a dot separator
131+
"""
132+
result = get_extended_profile_model()
133+
134+
self.assertIsNone(result)
135+
mock_logger.warning.assert_called_once()
136+
137+
@override_settings(REGISTRATION_EXTENSION_FORM="myapp.forms.CustomExtendedProfileForm")
138+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.import_module")
139+
def test_get_extended_profile_model_custom_form(self, mock_import_module: Mock):
140+
"""
141+
Test loading model from a custom extended profile form
142+
"""
143+
# Create a mock model
144+
mock_model = Mock(spec=Model)
145+
# Create a mock form class with Meta.model
146+
mock_form_class = Mock()
147+
mock_form_class.Meta = Mock()
148+
mock_form_class.Meta.model = mock_model
149+
# Create a mock module with the custom form class
150+
mock_module = Mock()
151+
mock_module.CustomExtendedProfileForm = mock_form_class
152+
mock_import_module.return_value = mock_module
153+
154+
result = get_extended_profile_model()
155+
156+
self.assertEqual(result, mock_model)
157+
mock_import_module.assert_called_once_with("myapp.forms")
158+
159+
@override_settings(REGISTRATION_EXTENSION_FORM="myapp.forms.FormWithoutModel")
160+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.import_module")
161+
def test_get_extended_profile_model_form_without_model(self, mock_import_module: Mock):
162+
"""
163+
Test when form has Meta but no model attribute
164+
"""
165+
# Create a mock form class with Meta but no model
166+
mock_form_class = Mock()
167+
mock_form_class.Meta = Mock(spec=[]) # Meta exists but has no model attribute
168+
# Create a mock module with the form class
169+
mock_module = Mock()
170+
mock_module.FormWithoutModel = mock_form_class
171+
mock_import_module.return_value = mock_module
172+
173+
result = get_extended_profile_model()
174+
175+
self.assertIsNone(result)
176+
177+
@ddt.data(
178+
(ImportError, "Module not found"),
179+
(ModuleNotFoundError, "No module named 'myapp'"),
180+
)
181+
@ddt.unpack
182+
@override_settings(REGISTRATION_EXTENSION_FORM="myapp.forms.ExtendedProfileForm")
183+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.import_module")
184+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.logger")
185+
def test_get_extended_profile_model_import_errors(
186+
self, exception_class: type, error_message: str, mock_logger: Mock, mock_import_module: Mock
187+
):
188+
"""
189+
Test when import_module raises ImportError or ModuleNotFoundError
190+
"""
191+
mock_import_module.side_effect = exception_class(error_message)
192+
193+
result = get_extended_profile_model()
194+
195+
self.assertIsNone(result)
196+
mock_logger.warning.assert_called_once()
197+
self.assertIn("Could not load extended profile model", str(mock_logger.warning.call_args))
198+
199+
@override_settings(REGISTRATION_EXTENSION_FORM="myapp.forms.NonExistentForm")
200+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.import_module")
201+
@patch("openedx.core.djangoapps.user_authn.views.registration_form.logger")
202+
def test_get_extended_profile_model_attribute_error(self, mock_logger: Mock, mock_import_module: Mock):
203+
"""
204+
Test when the form class doesn't exist in the module
205+
"""
206+
mock_module = Mock(spec=[])
207+
mock_import_module.return_value = mock_module
208+
209+
result = get_extended_profile_model()
210+
211+
self.assertIsNone(result)
212+
mock_logger.warning.assert_called_once()

0 commit comments

Comments
 (0)