-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_pinboard.py
176 lines (135 loc) · 5.67 KB
/
test_pinboard.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
#!/usr/bin/env python3
from pinboard import Pinboard
import configparser
import datetime
import os
import unittest
import functools
import time
import random
import string
class TestPinboardAPIPropagation(unittest.TestCase):
def setUp(self):
self.pinboard = Pinboard(os.environ['PINBOARD_API_TOKEN'])
response = self.pinboard.posts.recent(count=1, date=datetime.date.today())
bookmark = response['posts'][0]
self.url = bookmark.url
def bookmark(self):
response = self.pinboard.posts.get(url=self.url, meta="yes")
return response['posts'][0]
def test_add_tag_through_website(self):
bookmark = self.bookmark()
input("Click enter after adding a tag to this bookmark through the website ({}...)".format(bookmark.url[:20]))
updated_bookmark = self.bookmark()
assert(bookmark.meta != updated_bookmark.meta)
def test_remove_tag_through_website(self):
bookmark = self.bookmark()
input("Click enter after removing a tag from this bookmark through the website ({}...)".format(self.url[:20]))
updated_bookmark = self.bookmark()
assert(bookmark.meta != updated_bookmark.meta)
class TestPinboardAPI(unittest.TestCase):
def setUp(self):
api_token = os.environ.get('PINBOARD_API_TOKEN', None)
if api_token is None:
try:
config_file = os.path.expanduser("~/.pinboardrc")
config = configparser.RawConfigParser()
with open(config_file, "r") as f:
config.read_file(f)
except:
raise
else:
api_token = config.get("authentication", "api_token")
self.pinboard = Pinboard(api_token)
response = self.pinboard.posts.recent(count=1, date=datetime.date.today())
bookmark = response['posts'][0]
self.url = bookmark.url
def bookmark(self):
response = self.pinboard.posts.get(url=self.url, meta="yes")
return response['posts'][0]
def retry_until_true(self, fun, msg=None, max_seconds=16):
"""
Retries a test until it resolves to True, backing off and doubling
the time in between retries.
"""
seconds = 1
while seconds < max_seconds:
result = fun()
if result:
# print "\nSucceeded after {} seconds".format(seconds),
break
else:
seconds *= 2
time.sleep(seconds)
else:
if msg:
self.assertTrue(result, msg="{} ({} second backoff).".format(msg, max_seconds))
else:
self.assertTrue(result)
def ensure_last_update_time_changed(fun):
"""
Wraps a test to make sure that the update time changed after
bookmarks were edited
"""
@functools.wraps(fun)
def inner(self, *args, **kwargs):
update_time_1 = self.pinboard.posts.update()
fun(self, *args, **kwargs)
update_time_2 = self.pinboard.posts.update()
self.retry_until_true(lambda: update_time_2 > update_time_1,
msg="{} == {}".format(update_time_1, update_time_2),
max_seconds=16)
return inner
def check_meta_updated(self, bookmark_1, bookmark_2):
self.retry_until_true(lambda: str(bookmark_1) != str(bookmark_2),
msg="{} == {}".format(bookmark_1.meta, bookmark_2.meta))
@ensure_last_update_time_changed
def _test_add_and_remove_tag_through_api(self):
bookmark = self.bookmark()
bookmark.tags.append("testing")
bookmark.save()
updated_bookmark = self.bookmark()
self.check_meta_updated(bookmark, updated_bookmark)
bookmark = updated_bookmark
bookmark.tags.remove("testing")
bookmark.save()
updated_bookmark = self.bookmark()
self.check_meta_updated(bookmark, updated_bookmark)
@ensure_last_update_time_changed
def _test_change_privacy_through_api(self):
bookmark = self.bookmark()
bookmark.shared = not bookmark.shared
bookmark.save()
updated_bookmark = self.bookmark()
self.check_meta_updated(bookmark, updated_bookmark)
bookmark = updated_bookmark
bookmark.shared = not bookmark.shared
bookmark.save()
updated_bookmark = self.bookmark()
self.check_meta_updated(bookmark, updated_bookmark)
@ensure_last_update_time_changed
def _test_change_read_status_through_api(self):
bookmark = self.bookmark()
bookmark.toread = not bookmark.toread
bookmark.save()
updated_bookmark = self.bookmark()
self.check_meta_updated(bookmark, updated_bookmark)
bookmark = updated_bookmark
bookmark.toread = not bookmark.toread
bookmark.save()
updated_bookmark = self.bookmark()
self.check_meta_updated(bookmark, updated_bookmark)
@ensure_last_update_time_changed
def _test_add_bookmark_through_api(self, url):
self.pinboard.posts.add(url=url, description="Test Bookmark")
@ensure_last_update_time_changed
def _test_delete_bookmark_through_api(self, url):
self.pinboard.posts.delete(url=url)
def _test_add_and_remove_bookmark_through_api(self):
random_suffix = "".join(random.choice(string.ascii_letters) for i in range (6))
url = "http://example.com/{}".format(random_suffix)
self._test_add_bookmark_through_api(url)
self._test_delete_bookmark_through_api(url)
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(TestPinboardAPI)
unittest.TextTestRunner(verbosity=2).run(suite)