예제 #1
0
 def test_invalid_v1_schema_data_causes_validation_failure(self, value):
     """
     Test that the schema validation for the v1 schema works even in the combined schema.
     """
     value['version'] = 1
     with self.assertRaises(JSONValidationError):
         theme_schema_validate(value)
예제 #2
0
 def test_invalid_v0_schema_data_causes_validation_failure(self):
     """
     Test that the schema validation for the v0 schema works even in the combined schema.
     """
     with self.assertRaises(JSONValidationError):
         theme_schema_validate({
             'version': 0,
             'main_color': 'red',
             'link_color': '#fff',
             'header_bg_color': '#fff',
             'footer_bg_color': '#fff'
         })
예제 #3
0
 def test_data_in_v0_and_v1_schema_accepted(self):
     """
     Test that the data validating against the v0 or v1 schema is accepted.
     """
     theme_schema_validate({
         'version': 1,
         'main-color': '#fff',
         'link-color': '#000'
     })
     theme_schema_validate({
         'version': 0,
         'main_color': '#000',
         'link_color': '#111',
         'header_bg_color': '#222',
         'footer_bg_color': '#333'
     })
예제 #4
0
    def theme_config(self, request, pk=None):
        """
        Partial update for theme configuration

        This is a custom handler to partially update theme fields.
        TODO: Improve behavior consistency when returning. Instead of setting
        the default value when a required variable is sent empty, error out.
        And to allow reverting back to a default value, add support for a
        `default` keyword to enable the frontend to revert values to the default
        theme colors. Examples:
        PATCH: {"main-color": ""} should error out.
        PATCH" {"main-color": "default"} should revert to the default theme
        base color.
        """
        with transaction.atomic():
            application = BetaTestApplication.objects.select_for_update().get(
                id=pk)
            if not application.draft_theme_config:
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={"non_field_errors": "V1 theme isn't set up yet."})

            # Sanitize inputs
            serializer = ThemeSchemaSerializer(data=request.data)
            if not serializer.is_valid():
                return Response(serializer.errors, status=400)

            # Merges the current application theme draft with the values modified by
            # the user and performs validation.
            # If the key is empty (""), the user reverted the field to the default
            # value, and the key needs to be erased.
            merged_theme = {
                key: value
                for key, value in {
                    **application.draft_theme_config,
                    **serializer.validated_data
                }.items() if value
            }

            # To make sure the required values are always available, merge dict with
            # default values dictionary.
            safe_merged_theme = {**DEFAULT_THEME, **merged_theme}

            # TODO: Needs additional validation/filling up if button customization
            # is enabled to prevent the validation schema from failing

            # Perform validation, handle error if failed, and return updated
            # theme_config if succeeds.
            try:
                theme_schema_validate(safe_merged_theme)
            except JSONSchemaValidationError:
                # TODO: improve schema error feedback. Needs to be done along with
                # multiple frontend changes to allow individual fields feedback.
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={"non_field_errors": "Schema validation failed."})

            application.draft_theme_config = safe_merged_theme
            application.save()

            return Response(status=status.HTTP_200_OK,
                            data=application.draft_theme_config)
예제 #5
0
 def test_null_accepted(self):
     """
     Test that an empty object is accepted as valid.
     """
     theme_schema_validate(None)
예제 #6
0
 def test_invalid_data_types(self, value):
     """
     Test that validation fails when passing invalid data types for the value.
     """
     with self.assertRaisesRegex(JSONValidationError, "is not of type"):
         theme_schema_validate(value)