-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
conftest.py
223 lines (182 loc) · 5.82 KB
/
conftest.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
import shutil
import tempfile
from pathlib import Path
import click
import pytest
import responses
from archivy import app, cli
from archivy.click_web import create_click_web_app, _flask_app
from archivy.helpers import get_db, load_hooks
from archivy.models import DataObj, User
_app = None
@pytest.fixture
def test_app():
"""Instantiate the app for each test with its own temporary data directory
Each test using this fixture will use its own db.json and its own data
directory, and then delete them.
"""
# create a temporary file to isolate the database for each test
global _app
if _app is None:
_app = create_click_web_app(cli, cli.cli, app)
app_dir = Path(tempfile.mkdtemp())
_app.config["INTERNAL_DIR"] = str(app_dir)
_app.config["USER_DIR"] = str(app_dir)
(app_dir / "data").mkdir()
(app_dir / "images").mkdir()
_app.config["TESTING"] = True
_app.config["WTF_CSRF_ENABLED"] = False
_app.config["SCRAPING_CONF"]["save_images"] = False
# This setups a TinyDB instance, using the `app_dir` temporary
# directory defined above
# Required so that `flask.current_app` can be called in data.py and
# models.py
# See https://flask.palletsprojects.com/en/1.1.x/appcontext/ for more
# information.
with _app.app_context():
_ = get_db()
user = {"username": "halcyon", "password": "password"}
User(**user).insert()
yield _app
# close and remove the temporary database
shutil.rmtree(app_dir)
@pytest.fixture
def client(test_app):
"""HTTP client for calling a test instance of the app"""
with test_app.test_client() as client:
client.post("/login", data={"username": "halcyon", "password": "password"})
yield client
@pytest.fixture
def mocked_responses():
"""
Setup mock responses using the `responses` python package.
Using https://pypi.org/project/responses/, this fixture will mock out
HTTP calls made by the requests library.
For example,
>>> mocked_responses.add(responses.GET, "http://example.org",
json={'key': 'val'}
)
>>> r = requests.get("http://example.org")
>>> print(r.json())
{'key': 'val'}
"""
with responses.RequestsMock() as rsps:
# this ensure that all requests calls are mocked out
rsps.assert_all_requests_are_fired = False
yield rsps
@pytest.fixture
def note_fixture(test_app):
note_dict = {
"type": "note",
"title": "Test Note",
"tags": ["testing", "archivy"],
"path": "",
}
with test_app.app_context():
note = DataObj(**note_dict)
note.insert()
return note
@pytest.fixture
def bookmark_fixture(test_app, mocked_responses):
mocked_responses.add(
responses.GET,
"https://example.com/",
body="""<html>
<head><title>Example</title></head><body><p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit
<script>console.log("this should be sanitized")</script>
<img src="/images/image1.png">
<a href="/testing-absolute-url">link</a>
<a href"/empty-link"></a>
#embedded-tag# #tag2#
</p></body></html>
""",
)
datapoints = {
"type": "bookmark",
"title": "Test Bookmark",
"tags": ["testing", "archivy"],
"path": "",
"url": "https://example.com/",
}
with test_app.app_context():
bookmark = DataObj(**datapoints)
bookmark.process_bookmark_url()
bookmark.insert()
return bookmark
@pytest.fixture
def different_bookmark_fixture(test_app, mocked_responses):
mocked_responses.add(
responses.GET,
"https://example2.com/",
body="""<html>
<head><title>Example</title></head><body><p>asdsad<div class="nested">aaa</div></body></html>
""",
)
datapoints = {
"type": "bookmark",
"title": "Test Bookmark2",
"url": "https://example2.com/",
}
with test_app.app_context():
bookmark = DataObj(**datapoints)
bookmark.process_bookmark_url()
bookmark.insert()
return bookmark
@pytest.fixture()
def user_fixture(test_app):
user = {"username": "__username__", "password": "__password__"}
user = User(**user)
user.insert()
return user
@pytest.fixture()
def pocket_fixture(test_app, mocked_responses):
"""Sets up pocket key and mocked responses for testing pocket sync
When using this fixture, all calls to https://getpocket.com/v3/get will
succeed and return a single article whose url is https://example.com.
"""
with test_app.app_context():
db = get_db()
mocked_responses.add(
responses.POST,
"https://getpocket.com/v3/oauth/authorize",
json={
"access_token": "5678defg-5678-defg-5678-defg56",
"username": "test_user",
},
)
# fake /get response from pocket API
mocked_responses.add(
responses.POST,
"https://getpocket.com/v3/get",
json={
"status": 1,
"complete": 1,
"list": {
"3088163616": {
"given_url": "https://example.com",
"status": "0",
"resolved_url": "https://example.com",
"excerpt": "Lorem ipsum",
"is_article": "1",
},
},
},
)
pocket_key = {
"type": "pocket_key",
"consumer_key": "1234-abcd1234abcd1234abcd1234",
"code": "dcba4321-dcba-4321-dcba-4321dc",
}
db.insert(pocket_key)
return pocket_key
@pytest.fixture()
def click_cli():
yield cli.cli
@pytest.fixture()
def ctx(click_cli):
with click.Context(click_cli, info_name=click_cli, parent=None) as ctx:
yield ctx
@pytest.fixture()
def cli_runner():
yield click.testing.CliRunner()