def test_invalid_v0_schema_data(self): """ Test that data with invalid data for the v0 schema causes the validation to fail. """ with self.assertRaisesRegex(ValidationError, "is not of type 'string'"): static_content_overrides_schema_validate({ 'version': 0, 'static_template_about_content': [1, 2, 3] })
def test_data_in_v0_schema_accepted(self): """ Test that the data validating against the v0 schema is accepted. """ static_content_overrides_schema_validate({ 'version': 0, 'static_template_about_content': 'Hello world!', 'homepage_overlay_html': 'Welcome to the LMS!' })
def static_content_overrides(self, request, pk=None): """ Partial update for static content overrides configuration """ with transaction.atomic(): application = BetaTestApplication.objects.select_for_update().get( id=pk) serializer = StaticContentOverridesSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=400) if not application.draft_static_content_overrides: application.draft_static_content_overrides = DEFAULT_STATIC_CONTENT_OVERRIDES merged_values = { key: value for key, value in { **application.draft_static_content_overrides, **serializer.validated_data }.items() } if 'homepage_overlay_html' not in merged_values: current_value = '' else: current_value = merged_values['homepage_overlay_html'] merged_values['homepage_overlay_html'] = Template( fill_default_hero_text(current_value)).safe_substitute( instance_name=application.instance_name) try: static_content_overrides_schema_validate(merged_values) except JSONSchemaValidationError: return Response( status=status.HTTP_400_BAD_REQUEST, data={'non_field_errors': "Schema validation failed."}) application.draft_static_content_overrides = merged_values application.save() return Response(status=status.HTTP_200_OK, data=application.draft_static_content_overrides)
def validate(self, value): """ Validate the given value with the v0 static content overrides schema. """ static_content_overrides_schema_validate( value, schema=static_content_overrides_v0_schema)
def test_null_accepted(self): """ Test that an empty object is accepted as valid. """ static_content_overrides_schema_validate(None)
def test_invalid_data_types(self, value): """ Test that validation fails when passing invalid data types for the value. """ with self.assertRaisesRegex(ValidationError, 'is not of type'): static_content_overrides_schema_validate(value)