2017-10-10 14:42:10 +00:00
|
|
|
# coding=utf-8
|
|
|
|
"""Unit tests for render.py."""
|
2019-12-25 07:26:58 +00:00
|
|
|
from oauth_dropins.webutil.util import json_dumps
|
2017-10-10 14:42:10 +00:00
|
|
|
|
|
|
|
from models import Response
|
2021-07-07 15:07:20 +00:00
|
|
|
import render
|
2019-12-26 06:20:57 +00:00
|
|
|
from . import testutil
|
2017-10-10 14:42:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RenderTest(testutil.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2021-08-18 14:59:52 +00:00
|
|
|
super().setUp()
|
2017-10-10 14:42:10 +00:00
|
|
|
self.as2 = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Note',
|
2017-10-15 17:50:01 +00:00
|
|
|
'id': 'http://this/reply',
|
2017-10-10 14:42:10 +00:00
|
|
|
'url': 'http://this/reply',
|
2017-10-15 17:50:01 +00:00
|
|
|
'content': 'A ☕ reply',
|
2017-10-10 14:42:10 +00:00
|
|
|
'inReplyTo': 'http://orig/post',
|
|
|
|
}
|
|
|
|
self.mf2 = {
|
|
|
|
'type': ['h-entry'],
|
|
|
|
'properties': {
|
2017-10-15 17:50:01 +00:00
|
|
|
'uid': ['http://this/reply'],
|
2017-10-10 14:42:10 +00:00
|
|
|
'url': ['http://this/reply'],
|
|
|
|
'content': [{'value': 'A ☕ reply'}],
|
|
|
|
'in-reply-to': ['http://orig/post'],
|
|
|
|
},
|
|
|
|
}
|
2017-10-15 17:50:01 +00:00
|
|
|
self.atom = """\
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<entry xmlns="http://www.w3.org/2005/Atom"
|
|
|
|
xmlns:thr="http://purl.org/syndication/thread/1.0">
|
|
|
|
|
|
|
|
<uri>http://this/reply</uri>
|
|
|
|
<thr:in-reply-to href="http://orig/post" />
|
|
|
|
<content>A ☕ reply</content>
|
|
|
|
</entry>
|
|
|
|
"""
|
2017-10-10 14:42:10 +00:00
|
|
|
self.html = """\
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
2019-04-17 15:44:26 +00:00
|
|
|
<head><meta charset="utf-8">
|
|
|
|
<meta http-equiv="refresh" content="0;url=abc"></head>
|
2017-10-10 14:42:10 +00:00
|
|
|
<body>
|
|
|
|
<article class="h-entry">
|
2017-10-15 17:50:01 +00:00
|
|
|
<span class="p-uid">http://this/reply</span>
|
2017-10-10 14:42:10 +00:00
|
|
|
<a class="u-url" href="http://this/reply">http://this/reply</a>
|
|
|
|
<div class="e-content p-name">
|
|
|
|
A ☕ reply
|
|
|
|
</div>
|
|
|
|
<a class="u-in-reply-to" href="http://orig/post"></a>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_render_errors(self):
|
|
|
|
for source, target in ('', ''), ('abc', ''), ('', 'xyz'):
|
2021-08-18 14:59:52 +00:00
|
|
|
resp = self.client.get(f'/render?source={source}&target={target}')
|
2021-07-06 20:45:56 +00:00
|
|
|
self.assertEqual(400, resp.status_code, resp.get_data(as_text=True))
|
2017-10-10 14:42:10 +00:00
|
|
|
|
|
|
|
# no Response
|
2021-08-18 14:59:52 +00:00
|
|
|
resp = self.client.get('/render?source=abc&target=xyz')
|
2021-07-06 20:45:56 +00:00
|
|
|
self.assertEqual(404, resp.status_code)
|
2017-10-10 14:42:10 +00:00
|
|
|
|
|
|
|
# no source data
|
|
|
|
Response(id='abc xyz').put()
|
2021-08-18 14:59:52 +00:00
|
|
|
resp = self.client.get('/render?source=abc&target=xyz')
|
2021-07-06 20:45:56 +00:00
|
|
|
self.assertEqual(404, resp.status_code)
|
2017-10-10 14:42:10 +00:00
|
|
|
|
|
|
|
def test_render_as2(self):
|
2019-12-25 07:26:58 +00:00
|
|
|
Response(id='abc xyz', source_as2=json_dumps(self.as2)).put()
|
2021-08-18 14:59:52 +00:00
|
|
|
resp = self.client.get('/render?source=abc&target=xyz')
|
2021-07-06 20:45:56 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
self.assert_multiline_equals(self.html, resp.get_data(as_text=True),
|
2017-10-10 14:42:10 +00:00
|
|
|
ignore_blanks=True)
|
|
|
|
|
|
|
|
def test_render_mf2(self):
|
2019-12-25 07:26:58 +00:00
|
|
|
Response(id='abc xyz', source_mf2=json_dumps(self.mf2)).put()
|
2021-08-18 14:59:52 +00:00
|
|
|
resp = self.client.get('/render?source=abc&target=xyz')
|
2021-07-06 20:45:56 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
self.assert_multiline_equals(self.html, resp.get_data(as_text=True),
|
2017-10-10 14:42:10 +00:00
|
|
|
ignore_blanks=True)
|
2017-10-15 17:50:01 +00:00
|
|
|
|
|
|
|
def test_render_atom(self):
|
|
|
|
Response(id='abc xyz', source_atom=self.atom).put()
|
2021-08-18 14:59:52 +00:00
|
|
|
resp = self.client.get('/render?source=abc&target=xyz')
|
2021-07-06 20:45:56 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
self.assert_multiline_equals(self.html, resp.get_data(as_text=True),
|
2017-10-15 17:50:01 +00:00
|
|
|
ignore_blanks=True)
|