/
test_atom.py
210 lines (197 loc) · 7.27 KB
/
test_atom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import datasette
from datasette.app import Datasette
import urllib.parse
import pytest
import httpx
EXPECTED_ATOM = """
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>:memory:/d765251b024da6156f346354d5ca573b2c8717bc71d82d2e1a2fdd55b9a44215</id>
<title>
select
1 as atom_id,
123 as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'blah <b>Bold</b>' as atom_content,
'Author' as atom_author_name,
'https://www.example.com/' as atom_author_uri
union select
'atom-id-2' as atom_id,
'title 2' as atom_title,
'2019-09-23T21:32:12-07:00' as atom_updated,
'blah' as atom_content,
null as atom_author_name,
null as atom_author_uri;
</title>
<updated>2019-10-23T21:32:12-07:00</updated>
<generator uri="https://github.com/simonw/datasette" version="{version}">Datasette</generator>
<entry>
<id>1</id>
<title>123</title>
<updated>2019-10-23T21:32:12-07:00</updated>
<author>
<name>Author</name>
<uri>https://www.example.com/</uri>
</author>
<content type="text">blah <b>Bold</b></content>
</entry>
<entry>
<id>atom-id-2</id>
<title>title 2</title>
<updated>2019-09-23T21:32:12-07:00</updated>
<content type="text">blah</content>
</entry>
</feed>
""".strip()
EXPECTED_ATOM_WITH_LINK = """
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>:memory:/652f6714d6b9efa3657b50fe0ae8cbac13ccefb2ecbdbafe527c6f6fe97556da</id>
<title>
select
'atom-id' as atom_id,
'title' as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'https://www.niche-museums.com/' as atom_link,
'blah' as atom_content;
</title>
<updated>2019-10-23T21:32:12-07:00</updated>
<generator uri="https://github.com/simonw/datasette" version="{version}">Datasette</generator>
<entry>
<id>atom-id</id>
<title>title</title>
<updated>2019-10-23T21:32:12-07:00</updated>
<content type="text">blah</content>
<link href="https://www.niche-museums.com/" rel="alternate"/>
</entry>
</feed>
""".strip()
EXPECTED_ATOM_WITH_HTML = """
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>:memory:/beb5a312c0daa591d04d7dfb5a79eb8bbbcd6f84ebe90cf0345ed8c24bb2ff22</id>
<title>
select
'atom-id' as atom_id,
'title' as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'https://www.niche-museums.com/' as atom_link,
'<h2>blah</h2><script>alert("bad")</script>' as atom_content_html;
</title>
<updated>2019-10-23T21:32:12-07:00</updated>
<generator uri="https://github.com/simonw/datasette" version="{version}">Datasette</generator>
<entry>
<id>atom-id</id>
<title>title</title>
<updated>2019-10-23T21:32:12-07:00</updated>
<content type="html"><h2>blah</h2>&lt;script&gt;alert("bad")&lt;/script&gt;</content>
<link href="https://www.niche-museums.com/" rel="alternate"/>
</entry>
</feed>
""".strip()
@pytest.mark.asyncio
async def test_incorrect_sql_returns_400():
app = Datasette([], immutables=[], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:.atom?sql=select+sqlite_version()"
)
assert 400 == response.status_code
assert b"SQL query must return columns" in response.content
@pytest.mark.asyncio
async def test_atom_for_valid_query():
sql = """
select
1 as atom_id,
123 as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'blah <b>Bold</b>' as atom_content,
'Author' as atom_author_name,
'https://www.example.com/' as atom_author_uri
union select
'atom-id-2' as atom_id,
'title 2' as atom_title,
'2019-09-23T21:32:12-07:00' as atom_updated,
'blah' as atom_content,
null as atom_author_name,
null as atom_author_uri;
"""
app = Datasette([], immutables=[], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:.atom?" + urllib.parse.urlencode({"sql": sql})
)
assert 200 == response.status_code
assert "application/xml; charset=utf-8" == response.headers["content-type"]
assert (
EXPECTED_ATOM.format(version=datasette.__version__)
== response.content.decode("utf-8").strip()
)
@pytest.mark.asyncio
async def test_atom_with_optional_link():
sql = """
select
'atom-id' as atom_id,
'title' as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'https://www.niche-museums.com/' as atom_link,
'blah' as atom_content;
"""
app = Datasette([], immutables=[], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:.atom?" + urllib.parse.urlencode({"sql": sql})
)
assert 200 == response.status_code
assert "application/xml; charset=utf-8" == response.headers["content-type"]
assert (
EXPECTED_ATOM_WITH_LINK.format(version=datasette.__version__)
== response.content.decode("utf-8").strip()
)
@pytest.mark.asyncio
async def test_atom_with_bad_html():
sql = """
select
'atom-id' as atom_id,
'title' as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'https://www.niche-museums.com/' as atom_link,
'<h2>blah</h2><script>alert("bad")</script>' as atom_content_html;
"""
app = Datasette([], immutables=[], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:.atom?" + urllib.parse.urlencode({"sql": sql})
)
assert 200 == response.status_code
assert "application/xml; charset=utf-8" == response.headers["content-type"]
assert (
EXPECTED_ATOM_WITH_HTML.format(version=datasette.__version__)
== response.content.decode("utf-8").strip()
)
@pytest.mark.asyncio
async def test_atom_link_only_shown_for_correct_queries():
sql = """
select
'atom-id' as atom_id,
'title' as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'https://www.niche-museums.com/' as atom_link,
'<h2>blah</h2><script>alert("bad")</script>' as atom_content_html;
"""
app = Datasette([], immutables=[], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:?" + urllib.parse.urlencode({"sql": sql})
)
assert 200 == response.status_code
assert "text/html; charset=utf-8" == response.headers["content-type"]
assert b'<a href="/:memory:.atom' in response.content
# But with a different query that link is not shown:
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:?"
+ urllib.parse.urlencode({"sql": "select sqlite_version()"})
)
assert b'<a href="/:memory:.json' in response.content
assert b'<a href="/:memory:.atom' not in response.content