/
test_routes.py
410 lines (315 loc) · 12.3 KB
/
test_routes.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import os
from flask.testing import FlaskClient
from flask import request
from flask_login import current_user
from responses import RequestsMock, GET
from archivy.helpers import get_max_id
from archivy.data import get_dirs, create_dir, get_items, get_item
def test_get_index(test_app, client: FlaskClient):
response = client.get("/")
assert response.status_code == 200
def test_get_custom_css(test_app, client: FlaskClient):
test_app.config["THEME_CONF"]["use_custom_css"] = True
css_file = "custom.css"
css_contents = """
body {
color: red
}
"""
os.mkdir(f"{test_app.config['USER_DIR']}/css/")
with open(f"{test_app.config['USER_DIR']}/css/{css_file}", "w") as f:
f.write(css_contents)
test_app.config["THEME_CONF"]["custom_css_file"] = css_file
resp = client.get("/static/custom.css")
assert css_contents.encode("utf-8") in resp.data
test_app.config["THEME_CONF"]["use_custom_css"] = False
def test_get_new_bookmark(test_app, client: FlaskClient):
response = client.get("/bookmarks/new")
assert response.status_code == 200
def test_post_new_bookmark_missing_fields(test_app, client: FlaskClient):
response = client.post("/bookmarks/new", data={"submit": True})
assert response.status_code == 200
assert b"This field is required" in response.data
def test_get_new_note(test_app, client: FlaskClient):
response = client.get("/notes/new")
assert response.status_code == 200
def test_get_dataobj_not_found(test_app, client: FlaskClient):
response = client.get("/dataobj/1")
assert response.status_code == 302
def test_get_dataobj(test_app, client: FlaskClient, note_fixture):
response = client.get("/dataobj/1")
assert response.status_code == 200
def test_get_delete_dataobj_not_found(test_app, client: FlaskClient):
response = client.post("/dataobj/delete/1")
assert response.status_code == 302
def test_get_delete_dataobj(test_app, client: FlaskClient, note_fixture):
response = client.post("/dataobj/delete/1")
assert response.status_code == 302
def test_create_new_bookmark(
test_app, client: FlaskClient, mocked_responses: RequestsMock
):
mocked_responses.add(
GET,
"https://example.com/",
body="""<html>
<head><title>Random</title></head><body><p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit
</p></body></html>
""",
)
bookmark_data = {
"url": "https://example.com",
"tags": "testing,bookmark",
"path": "",
"submit": "true",
}
resp = client.post("/bookmarks/new", data=bookmark_data)
assert resp.status_code == 302
assert not b"invalid" in resp.data
resp = client.post("/bookmarks/new", data=bookmark_data, follow_redirects=True)
assert resp.status_code == 200
assert (
b'<span class="post-tag"><a href="/tags/bookmark">bookmark</a></span>'
in resp.data
)
assert (
b'<span class="post-tag"><a href="/tags/testing">testing</a></span>'
in resp.data
)
assert b"https://example.com" in resp.data
assert b"Random" in resp.data
def test_creating_bookmark_without_passing_path_saves_to_default_dir(
test_app, client, mocked_responses
):
mocked_responses.add(GET, "http://example.org", body="Example\n")
bookmarks_dir = "bookmarks"
test_app.config["DEFAULT_BOOKMARKS_DIR"] = bookmarks_dir
create_dir(bookmarks_dir)
resp = client.post(
"/bookmarks/new",
data={
"url": "http://example.org",
"submit": "true",
},
)
bookmark = get_items(structured=False)[0]
assert (
"bookmarks" in bookmark["path"]
) # verify it was saved to default bookmark dir
def test_create_note(test_app, client: FlaskClient):
note_data = {
"title": "Testing the create route",
"tags": "testing,note",
"path": "",
"submit": "true",
}
resp = client.post("/notes/new", data=note_data)
assert resp.status_code == 302
assert not b"invalid" in resp.data
resp = client.post("/notes/new", data=note_data, follow_redirects=True)
assert resp.status_code == 200
assert b'<span class="post-tag"><a href="/tags/note">note</a></span>' in resp.data
assert (
b'<span class="post-tag"><a href="/tags/testing">testing</a></span>'
in resp.data
)
assert b"Testing the create route" in resp.data
def test_logging_in(test_app, client: FlaskClient):
resp = client.post(
"/login",
data={"username": "halcyon", "password": "password"},
follow_redirects=True,
)
assert resp.status_code == 200
assert resp.request.path == "/"
assert current_user
def test_logging_in_with_invalid_creds(test_app, client: FlaskClient):
resp = client.post(
"/login",
data={"username": "invalid", "password": "dasdasd"},
follow_redirects=True,
)
assert resp.status_code == 200
assert resp.request.path == "/login"
assert b"Invalid credentials" in resp.data
def test_edit_user(test_app, client: FlaskClient):
"""Tests editing a user's info, logging out and then logging in with new info."""
new_user = "new_halcyon"
new_pass = "password2"
resp = client.post(
"/user/edit",
data={"username": new_user, "password": new_pass},
follow_redirects=True,
)
assert resp.request.path == "/"
client.delete("/logout")
resp = client.post(
"/login",
data={"username": new_user, "password": new_pass},
follow_redirects=True,
)
assert resp.status_code == 200
assert resp.request.path == "/"
# check information has updated.
def test_logging_out(test_app, client: FlaskClient):
"""Tests logging out and then accessing restricted views"""
client.delete("/logout")
resp = client.get("/", follow_redirects=True)
assert resp.request.path == "/login"
def test_create_dir(test_app, client: FlaskClient):
"""Tests /folders/create endpoint"""
resp = client.post(
"/folders/create",
data={"parent_dir": "", "new_dir": "testing"},
follow_redirects=True,
)
assert resp.status_code == 200
assert resp.request.args.get("path") == "testing"
assert "testing" in get_dirs()
assert b"Folder successfully created" in resp.data
def test_creating_without_dirname_fails(test_app, client: FlaskClient):
resp = client.post(
"/folders/create", data={"parent_dir": ""}, follow_redirects=True
)
assert resp.status_code == 200
assert resp.request.path == "/"
assert b"Could not create folder." in resp.data
def test_visiting_nonexistent_dir_fails(test_app, client: FlaskClient):
resp = client.get("/?path=nonexistent_dir", follow_redirects=True)
assert b"Directory does not exist." in resp.data
def test_deleting_dir(test_app, client: FlaskClient):
create_dir("testing")
assert "testing" in get_dirs()
resp = client.post(
"/folders/delete", data={"dir_name": "testing"}, follow_redirects=True
)
assert not "testing" in get_dirs()
assert b"Folder successfully deleted." in resp.data
def test_deleting_nonexisting_folder_fails(test_app, client: FlaskClient):
resp = client.post("/folders/delete", data={"dir_name": "testing"})
assert resp.status_code == 404
def test_bookmarklet(test_app, client: FlaskClient):
resp = client.get("/bookmarklet")
assert resp.status_code == 200
def test_backlinks_are_saved(
test_app, client: FlaskClient, note_fixture, bookmark_fixture
):
test_app.config["SEARCH_CONF"]["enabled"] = 1
test_app.config["SEARCH_CONF"]["engine"] = "ripgrep"
resp = client.put(
f"/api/dataobjs/{note_fixture.id}",
json={"content": f"[[{bookmark_fixture.title}|{bookmark_fixture.id}]]"},
)
assert resp.status_code == 200
resp = client.get(f"/dataobj/{bookmark_fixture.id}")
assert b"Backlinks" in resp.data # backlink was detected
test_app.config["SEARCH_CONF"]["enabled"] = 0
def test_bookmark_with_long_title_gets_truncated(test_app, client, mocked_responses):
long_title = "a" * 300
# check that our mock title is indeed longer than the limit
# and would cause an error, without our truncating
assert os.pathconf("/", "PC_NAME_MAX") < len(long_title)
mocked_responses.add(GET, "https://example.com", f"<title>{long_title}</title>")
bookmark_data = {
"url": "https://example.com",
"submit": "true",
}
resp = client.post("/bookmarks/new", data=bookmark_data)
assert resp.status_code == 200
def test_move_data(test_app, note_fixture, client):
create_dir("random")
resp = client.post(
"/dataobj/move/1",
data={"path": "random", "submit": "true"},
follow_redirects=True,
)
assert resp.status_code == 200
assert b"Data successfully moved to random." in resp.data
assert get_item(1)["dir"] == "random"
def test_invalid_inputs_fail_move_data(test_app, note_fixture, client):
resp = client.post("/dataobj/move/1", follow_redirects=True)
assert b"No path specified." in resp.data
resp = client.post(
"/dataobj/move/2", data={"path": "aaa", "submit": "true"}, follow_redirects=True
)
assert b"Data not found" in resp.data
resp = client.post(
"/dataobj/move/1", data={"path": "", "submit": "true"}, follow_redirects=True
)
assert b"Data already in target directory" in resp.data
faulty_paths = ["../adarnad", "~/adasd", "ssss"]
for p in faulty_paths:
resp = client.post(
"/dataobj/move/1", data={"path": p, "submit": "true"}, follow_redirects=True
)
assert b"Data could not be moved to " + bytes(p, "utf-8") in resp.data
def test_rename_dir(test_app, client):
create_dir("random")
resp = client.post(
"/folders/rename",
data={"current_path": "random", "new_name": "renamed_random"},
follow_redirects=True,
)
assert resp.status_code == 200
assert b"Renamed successfully" in resp.data
def test_invalid_inputs_fail_renaming(test_app, client):
create_dir("random")
create_dir("random2")
resp = client.post(
"/folders/rename",
data={"current_path": "inexisting", "new_name": "random3"},
follow_redirects=True,
)
assert b"Directory not found" in resp.data
resp = client.post(
"/folders/rename",
data={"current_path": "random", "new_name": "random2"},
follow_redirects=True,
)
assert b"Target directory exists." in resp.data
faulty_paths = ["../adarnad", "~/adasd", "/illegal_dir", "."]
for p in faulty_paths:
print(p)
resp = client.post(
"/folders/rename",
data={"current_path": "random", "new_name": p},
follow_redirects=True,
)
assert b"Invalid input" in resp.data
def test_get_config_page(test_app, client):
resp = client.get("/config")
assert resp.status_code == 200
assert b"Edit Config" in resp.data
def test_post_updated_config(test_app, client):
# use dark theme as random conf value to change
dark_theme = test_app.config["THEME_CONF"]["use_theme_dark"]
resp = client.post(
"/config", data={"submit": True, "THEME_CONF-use_theme_dark": not dark_theme}
)
assert test_app.config["THEME_CONF"]["use_theme_dark"] == (not dark_theme)
def test_getting_all_tags(test_app, client, bookmark_fixture):
# bookmark fixture has embedded tags
resp = client.get("/tags")
bookmark_tags = ["embedded-tag", "tag2"]
assert resp.status_code == 200
print(bookmark_fixture.content)
for tag in bookmark_tags:
assert f"#{tag}" in str(resp.data)
def test_getting_matches_for_specific_tag(test_app, client, bookmark_fixture):
resp = client.get("/tags/tag2")
assert resp.status_code == 200
assert bookmark_fixture.title in str(resp.data)
assert str(bookmark_fixture.id) in str(resp.data)
def test_bookmarklet_upload(test_app, client):
title = "bookmarklet bookmark"
resp = client.post(
"/save_from_bookmarklet",
data={
"url": "https://example.com",
"html": f"<html><title>{title}</title><html>",
},
follow_redirects=True,
)
assert resp.status_code == 200
assert title in str(resp.data)
assert "/dataobj/" in resp.request.path