From b1b7aa51a45863672dcef6614a68132e73baf59d Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Mon, 12 Mar 2018 13:42:20 +0100 Subject: [PATCH] Added tests to make sure content is saved with leading whitespaces in the db. --- dpaste/tests/test_api.py | 10 ++++++++++ dpaste/tests/test_snippet.py | 9 +++++++++ dpaste/views.py | 7 ++----- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/dpaste/tests/test_api.py b/dpaste/tests/test_api.py index a086bd4..063d58e 100644 --- a/dpaste/tests/test_api.py +++ b/dpaste/tests/test_api.py @@ -239,3 +239,13 @@ class SnippetAPITestCase(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(Snippet.objects.count(), 1) self.assertEqual(Snippet.objects.all()[0].lexer, 'php') + + def test_leading_white_is_retained(self): + """ + Leading Whitespace is retained in the db. + """ + content = u' one\n two\n three\n four' + self.client.post(self.api_url, {'content': content}) + self.assertEqual(Snippet.objects.all()[0].content, content) + + diff --git a/dpaste/tests/test_snippet.py b/dpaste/tests/test_snippet.py index 1b2b055..cd598df 100644 --- a/dpaste/tests/test_snippet.py +++ b/dpaste/tests/test_snippet.py @@ -401,3 +401,12 @@ class SnippetTestCase(TestCase): slug_list = Snippet.objects.values_list( 'secret_id', flat=True).order_by('published') self.assertEqual(len(set(slug_list)), 100) + + def test_leading_white_is_retained_in_db(self): + """ + Leading Whitespace is retained in the db. + """ + content = u' one\n two\n three\n four' + data = self.valid_form_data(content=content) + self.client.post(self.new_url, data, follow=True) + self.assertEqual(Snippet.objects.all()[0].content, content) diff --git a/dpaste/views.py b/dpaste/views.py index e6009b0..214d3ec 100644 --- a/dpaste/views.py +++ b/dpaste/views.py @@ -6,15 +6,13 @@ import json from django.conf import settings from django.core.exceptions import ObjectDoesNotExist -from django.urls import reverse from django.db.models import Count from django.http import (Http404, HttpResponse, HttpResponseBadRequest, - HttpResponseRedirect) + HttpResponseRedirect) from django.shortcuts import get_object_or_404 -from django.utils.decorators import method_decorator +from django.urls import reverse from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy as _ -from django.views.decorators.csrf import csrf_exempt from django.views.defaults import page_not_found as django_page_not_found, \ server_error as django_server_error from django.views.generic import FormView @@ -112,7 +110,6 @@ class SnippetDetailView(SnippetView, DetailView): def highlight_snippet(self): snippet = self.get_object() h = pygmentize(snippet.content, snippet.lexer) - h = h.replace(u' ', u'  ') h = h.replace(u'\t', '    ') return h