def test_utils_with_enterprise_disabled(self, mock_enterprise_enabled):
     """
     Test that the enterprise app not being available causes
     the utilities to return the expected default values.
     """
     mock_enterprise_enabled.return_value = False
     self.assertFalse(data_sharing_consent_requested(None))
     self.assertFalse(data_sharing_consent_required_at_login(None))
     self.assertEqual(data_sharing_consent_requirement_at_login(None), None)
     self.assertEqual(insert_enterprise_fields(None, None), None)
     self.assertEqual(insert_enterprise_pipeline_elements(None), None)
Ejemplo n.º 2
0
 def test_utils_with_enterprise_disabled(self, mock_enterprise_enabled):
     """
     Test that the enterprise app not being available causes
     the utilities to return the expected default values.
     """
     mock_enterprise_enabled.return_value = False
     self.assertFalse(data_sharing_consent_requested(None))
     self.assertFalse(data_sharing_consent_required_at_login(None))
     self.assertEqual(data_sharing_consent_requirement_at_login(None), None)
     self.assertEqual(insert_enterprise_fields(None, None), None)
     self.assertEqual(insert_enterprise_pipeline_elements(None), None)
Ejemplo n.º 3
0
 def test_insert_enterprise_fields(self, mock_config_helpers, mock_get_ec, mock_get_ec2):
     """
     Test that the insertion of the enterprise fields is processed as expected.
     """
     request = mock.MagicMock(session={"partial_pipeline": "thing"})
     mock_ec = mock.MagicMock(
         enforces_data_sharing_consent=mock.MagicMock(return_value=True), requests_data_sharing_consent=True
     )
     # Name values in a MagicMock constructor don't fill a `name` attribute
     mock_ec.name = "MassiveCorp"
     mock_get_ec.return_value = mock_ec
     mock_get_ec2.return_value = mock_ec
     mock_config_helpers.get_value.return_value = "OpenEdX"
     form_desc = mock.MagicMock()
     form_desc.add_field.return_value = None
     expected_label = (
         "I agree to allow OpenEdX to share data about my enrollment, "
         "completion and performance in all OpenEdX courses and programs "
         "where my enrollment is sponsored by MassiveCorp."
     )
     expected_err_msg = "To link your account with MassiveCorp, you are required to consent to data sharing."
     insert_enterprise_fields(request, form_desc)
     mock_ec.enforces_data_sharing_consent.return_value = False
     insert_enterprise_fields(request, form_desc)
     calls = [
         mock.call(
             "data_sharing_consent",
             label=expected_label,
             field_type="checkbox",
             default=False,
             required=True,
             error_messages={"required": expected_err_msg},
         ),
         mock.call(
             "data_sharing_consent",
             label=expected_label,
             field_type="checkbox",
             default=False,
             required=False,
             error_messages={"required": expected_err_msg},
         ),
     ]
     form_desc.add_field.assert_has_calls(calls)
     form_desc.add_field.reset_mock()
     mock_ec.requests_data_sharing_consent = False
     insert_enterprise_fields(request, form_desc)
     form_desc.add_field.assert_not_called()
 def test_insert_enterprise_fields(self, mock_config_helpers, mock_get_ec,
                                   mock_get_ec2):
     """
     Test that the insertion of the enterprise fields is processed as expected.
     """
     request = mock.MagicMock(session={'partial_pipeline': 'thing'})
     mock_ec = mock.MagicMock(
         enforces_data_sharing_consent=mock.MagicMock(return_value=True),
         requests_data_sharing_consent=True,
     )
     # Name values in a MagicMock constructor don't fill a `name` attribute
     mock_ec.name = 'MassiveCorp'
     mock_get_ec.return_value = mock_ec
     mock_get_ec2.return_value = mock_ec
     mock_config_helpers.get_value.return_value = 'OpenEdX'
     form_desc = mock.MagicMock()
     form_desc.add_field.return_value = None
     expected_label = (
         "I agree to allow OpenEdX to share data about my enrollment, "
         "completion and performance in all OpenEdX courses and programs "
         "where my enrollment is sponsored by MassiveCorp.")
     expected_err_msg = (
         "To link your account with MassiveCorp, you are required to consent to data sharing."
     )
     insert_enterprise_fields(request, form_desc)
     mock_ec.enforces_data_sharing_consent.return_value = False
     insert_enterprise_fields(request, form_desc)
     calls = [
         mock.call('data_sharing_consent',
                   label=expected_label,
                   field_type='checkbox',
                   default=False,
                   required=True,
                   error_messages={'required': expected_err_msg}),
         mock.call('data_sharing_consent',
                   label=expected_label,
                   field_type='checkbox',
                   default=False,
                   required=False,
                   error_messages={'required': expected_err_msg})
     ]
     form_desc.add_field.assert_has_calls(calls)
     form_desc.add_field.reset_mock()
     mock_ec.requests_data_sharing_consent = False
     insert_enterprise_fields(request, form_desc)
     form_desc.add_field.assert_not_called()
Ejemplo n.º 5
0
    def get(self, request):
        """Return a description of the registration form.

        This decouples clients from the API definition:
        if the API decides to modify the form, clients won't need
        to be updated.

        This is especially important for the registration form,
        since different edx-platform installations might
        collect different demographic information.

        See `user_api.helpers.FormDescription` for examples
        of the JSON-encoded form description.

        Arguments:
            request (HttpRequest)

        Returns:
            HttpResponse

        """
        form_desc = FormDescription("post", reverse("user_api_registration"))
        self._apply_third_party_auth_overrides(request, form_desc)

        # Default fields are always required
        for field_name in self.DEFAULT_FIELDS:
            self.field_handlers[field_name](form_desc, required=True)

        # Custom form fields can be added via the form set in settings.REGISTRATION_EXTENSION_FORM
        custom_form = get_registration_extension_form()

        if custom_form:
            for field_name, field in custom_form.fields.items():
                restrictions = {}
                if getattr(field, 'max_length', None):
                    restrictions['max_length'] = field.max_length
                if getattr(field, 'min_length', None):
                    restrictions['min_length'] = field.min_length
                field_options = getattr(getattr(custom_form, 'Meta',
                                                None), 'serialization_options',
                                        {}).get(field_name, {})
                field_type = field_options.get(
                    'field_type',
                    FormDescription.FIELD_TYPE_MAP.get(field.__class__))
                if not field_type:
                    raise ImproperlyConfigured(
                        "Field type '{}' not recognized for registration extension field '{}'."
                        .format(field_type, field_name))
                form_desc.add_field(
                    field_name,
                    label=field.label,
                    default=field_options.get('default'),
                    field_type=field_options.get(
                        'field_type',
                        FormDescription.FIELD_TYPE_MAP.get(field.__class__)),
                    placeholder=field.initial,
                    instructions=field.help_text,
                    required=field.required,
                    restrictions=restrictions,
                    options=getattr(field, 'choices', None),
                    error_messages=field.error_messages,
                    include_default_option=field_options.get(
                        'include_default_option'),
                )

        # Extra fields configured in Django settings
        # may be required, optional, or hidden
        for field_name in self.EXTRA_FIELDS:
            if self._is_field_visible(field_name):
                self.field_handlers[field_name](
                    form_desc, required=self._is_field_required(field_name))

        # Add any Enterprise fields if the app is enabled
        insert_enterprise_fields(request, form_desc)

        return HttpResponse(form_desc.to_json(),
                            content_type="application/json")
Ejemplo n.º 6
0
    def get(self, request):
        """Return a description of the registration form.

        This decouples clients from the API definition:
        if the API decides to modify the form, clients won't need
        to be updated.

        This is especially important for the registration form,
        since different edx-platform installations might
        collect different demographic information.

        See `user_api.helpers.FormDescription` for examples
        of the JSON-encoded form description.

        Arguments:
            request (HttpRequest)

        Returns:
            HttpResponse

        """
        form_desc = FormDescription("post", reverse("user_api_registration"))
        self._apply_third_party_auth_overrides(request, form_desc)

        # Default fields are always required
        for field_name in self.DEFAULT_FIELDS:
            self.field_handlers[field_name](form_desc, required=True)

        # Custom form fields can be added via the form set in settings.REGISTRATION_EXTENSION_FORM
        custom_form = get_registration_extension_form()

        if custom_form:
            for field_name, field in custom_form.fields.items():
                restrictions = {}
                if getattr(field, 'max_length', None):
                    restrictions['max_length'] = field.max_length
                if getattr(field, 'min_length', None):
                    restrictions['min_length'] = field.min_length
                field_options = getattr(
                    getattr(custom_form, 'Meta', None), 'serialization_options', {}
                ).get(field_name, {})
                field_type = field_options.get('field_type', FormDescription.FIELD_TYPE_MAP.get(field.__class__))
                if not field_type:
                    raise ImproperlyConfigured(
                        "Field type '{}' not recognized for registration extension field '{}'.".format(
                            field_type,
                            field_name
                        )
                    )
                form_desc.add_field(
                    field_name, label=field.label,
                    default=field_options.get('default'),
                    field_type=field_options.get('field_type', FormDescription.FIELD_TYPE_MAP.get(field.__class__)),
                    placeholder=field.initial, instructions=field.help_text, required=field.required,
                    restrictions=restrictions,
                    options=getattr(field, 'choices', None), error_messages=field.error_messages,
                    include_default_option=field_options.get('include_default_option'),
                )

        # Extra fields configured in Django settings
        # may be required, optional, or hidden
        for field_name in self.EXTRA_FIELDS:
            if self._is_field_visible(field_name):
                self.field_handlers[field_name](
                    form_desc,
                    required=self._is_field_required(field_name)
                )

        # Add any Enterprise fields if the app is enabled
        insert_enterprise_fields(request, form_desc)

        return HttpResponse(form_desc.to_json(), content_type="application/json")