[Django] #33236: assertHTMLEqual shows incorrect diff

22 views
Skip to first unread message

Django

unread,
Oct 28, 2021, 2:50:33 PM10/28/21
to django-...@googlegroups.com
#33236: assertHTMLEqual shows incorrect diff
---------------------------------------------+------------------------
Reporter: Pratyush Mittal | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
---------------------------------------------+------------------------
The diff shown in the error message of assertHTMLEqual seems to be
converting escaped HTML text to unescaped text.

This makes it hard to write tests when testing XSS vulnerabilities in our
tags and filters. Though the assertions work correct, the error messages
don't show the correct differences.

**Steps to reproduce**

{{{
from django.test import TestCase

class UtilsTestCase(TestCase):
def test_assersion(self):
escaped = "<p>&lt;foo&gt;</p>"
raw = "<p><foo></p>"
self.assertHTMLEqual(escaped, raw)
}}}

**Expected Output**

{{{
AssertionError: <p>
&lt;foo&gt;
</p> != <p>
<foo>
</p>
<p>
- &lt;foo&gt;
+ <foo>
</p>
}}}

**Actual Output**

{{{
AssertionError: <p>
<foo>
</p> != <p>
<foo>
</p>
<p>
<foo>
</p>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33236>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 28, 2021, 3:02:45 PM10/28/21
to django-...@googlegroups.com
#33236: assertHTMLEqual shows incorrect diff
-----------------------------------+--------------------------------------

Reporter: Pratyush Mittal | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Pratyush Mittal):

The bug is probably caused because the `__str__` method in the `Element`
class treats all its children the same. The children are either a tree or
string. In the case of a string, the Python's HTMLParser unescapes the
contents. For their string representation, we probably need to escape them
back.

I have tried to fix this in this pull-request:
https://github.com/django/django/pull/15033

--
Ticket URL: <https://code.djangoproject.com/ticket/33236#comment:1>

Django

unread,
Oct 29, 2021, 12:40:03 AM10/29/21
to django-...@googlegroups.com
#33236: assertHTMLEqual() shows a confusing error with escaped HTML.
-------------------------------------+-------------------------------------
Reporter: Pratyush Mittal | Owner: Pratyush
| Mittal
Type: Bug | Status: assigned

Component: Testing framework | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Pratyush Mittal
* needs_better_patch: 0 => 1
* status: new => assigned
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/33236#comment:2>

Django

unread,
Oct 29, 2021, 5:27:14 AM10/29/21
to django-...@googlegroups.com
#33236: assertHTMLEqual() shows a confusing error with escaped HTML.
-------------------------------------+-------------------------------------
Reporter: Pratyush Mittal | Owner: Pratyush
| Mittal
Type: Bug | Status: assigned
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33236#comment:3>

Django

unread,
Oct 29, 2021, 6:46:21 AM10/29/21
to django-...@googlegroups.com
#33236: assertHTMLEqual() shows a confusing error with escaped HTML.
-------------------------------------+-------------------------------------
Reporter: Pratyush Mittal | Owner: Pratyush
| Mittal
Type: Bug | Status: closed

Component: Testing framework | Version: 3.2
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f38458fe56bf8850da72a924bd2e8ff59c6adf06" f38458fe]:
{{{
#!CommitTicketReference repository=""
revision="f38458fe56bf8850da72a924bd2e8ff59c6adf06"
Fixed #33236 -- Fixed assertHTMLEqual() error messages for escaped HTML.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33236#comment:4>

Reply all
Reply to author
Forward
0 new messages