Uh oh!
There was an error while loading. Please reload this page.
forked from facebookarchive/python-instagram
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
Latest commit
executable file
·253 lines (190 loc) · 7.99 KB
/
Copy pathtests.py
File metadata and controls
executable file
·253 lines (190 loc) · 7.99 KB
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
#!/usr/bin/env python
importtypes
importsix
try:
importsimplejsonasjson
exceptImportError:
importjson
importgetpass
importunittest
fromsix.moves.urllib.parseimporturlparse, parse_qs
frominstagramimportclient, oauth2, InstagramAPIError
TEST_AUTH=False
client_id="DEBUG"
client_secret="DEBUG"
access_token="DEBUG"
redirect_uri="http://example.com"
classMockHttp(object):
def__init__(self, *args, **kwargs):
pass
defrequest(self, url, method="GET", body=None, headers={}):
fail_state= {
'status':'400'
}, "{}"
parsed=urlparse(url)
options=parse_qs(parsed.query)
fn_name=str(active_call)
iffn_name=='get_authorize_login_url':
return {
'status': '200',
'content-location':'http://example.com/redirect/login'
}, None
ifnot'access_token'inoptionsandnot'client_id'inoptions:
fn_name+='_unauthorized'
if'self'inurlandnot'access_token'inoptions:
fn_name+='_no_auth_user'
fl=open('fixtures/%s.json'%fn_name)
content=fl.read()
fl.close()
json_content=json.loads(content)
status=json_content['meta']['code']
return {
'status': status
}, content
oauth2.Http=MockHttp
active_call=None
classTestInstagramAPI(client.InstagramAPI):
def__getattribute__(self, attr):
globalactive_call
actual_val=super(TestInstagramAPI, self).__getattribute__(attr)
ifisinstance(actual_val, types.MethodType):
active_call=attr
returnactual_val
classInstagramAuthTests(unittest.TestCase):
defsetUp(self):
self.unauthenticated_api=TestInstagramAPI(client_id=client_id, redirect_uri=redirect_uri, client_secret=client_secret)
deftest_authorize_login_url(self):
redirect_uri=self.unauthenticated_api.get_authorize_login_url()
assertredirect_uri
print("Please visit and authorize at:\n%s"%redirect_uri)
code=raw_input("Paste received code (blank to skip): ").strip()
ifnotcode:
return
access_token=self.unauthenticated_api.exchange_code_for_access_token(code)
assertaccess_token
deftest_xauth_exchange(self):
""" Your client ID must be authorized for xAuth access; email
xauth@instagram.com for access"""
username=raw_input("Enter username for XAuth (blank to skip): ").strip()
ifnotusername:
return
password=getpass.getpass("Enter password for XAuth (blank to skip): ").strip()
access_token=self.unauthenticated_api.exchange_xauth_login_for_access_token(username, password)
assertaccess_token
classInstagramAPITests(unittest.TestCase):
defsetUp(self):
super(InstagramAPITests, self).setUp()
self.client_only_api=TestInstagramAPI(client_id=client_id)
self.api=TestInstagramAPI(access_token=access_token)
deftest_media_popular(self):
self.api.media_popular(count=10)
deftest_media_search(self):
self.client_only_api.media_search(lat=37.7,lng=-122.22)
self.api.media_search(lat=37.7,lng=-122.22)
deftest_media_shortcode(self):
self.client_only_api.media_shortcode('os1NQjxtvF')
self.api.media_shortcode('os1NQjxtvF')
deftest_media_likes(self):
self.client_only_api.media_likes(media_id=4)
deftest_like_media(self):
self.api.like_media(media_id=4)
self.api.unlike_media(media_id=4)
"""
TEMP; disabled this test while we add
a proper response to create_media_comment
def test_comment_media(self):
comment = self.api.create_media_comment(media_id=4, text='test')
self.api.delete_comment(media_id=4, comment_id=comment.id)
"""
deftest_user_feed(self):
self.api.user_media_feed(count=50)
deftest_generator_user_feed(self):
generator=self.api.user_media_feed(as_generator=True, max_pages=3, count=2)
forpageingenerator:
str(generator)
deftest_generator_user_feed_all(self):
generator=self.api.user_media_feed(as_generator=True, max_pages=None)
foriinrange(10):
page=six.advance_iterator(generator)
str(generator)
generator=self.api.user_media_feed(as_generator=True, max_pages=0)
forpageingenerator:
assertFalse
deftest_user_liked_media(self):
self.api.user_liked_media(count=10)
deftest_user_recent_media(self):
media, url=self.api.user_recent_media(count=10)
self.assertTrue( all( [hasattr(obj, 'type') forobjinmedia] ) )
image=media[0]
self.assertEqual(
image.get_standard_resolution_url(),
"http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_7.jpg")
self.assertEqual(
image.get_low_resolution_url(),
"http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_6.jpg")
self.assertEqual(
image.get_thumbnail_url(),
"http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_5.jpg")
self.assertEqual( False, hasattr(image, 'videos') )
video=media[1]
self.assertEqual(
video.get_standard_resolution_url(),
video.videos['standard_resolution'].url)
self.assertEqual(
video.get_standard_resolution_url(),
"http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4")
self.assertEqual(
video.get_low_resolution_url(),
"http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4")
self.assertEqual(
video.get_thumbnail_url(),
"http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg")
deftest_user_search(self):
self.api.user_search('mikeyk', 10)
deftest_user_follows(self):
forpageinself.api.user_followed_by(as_generator=True):
str(page)
deftest_user_followed_by(self):
forpageinself.api.user_followed_by(as_generator=True):
str(page)
deftest_other_user_followed_by(self):
self.api.user_followed_by(user_id=3)
deftest_self_info(self):
self.api.user()
self.assertRaises(InstagramAPIError, self.client_only_api.user)
deftest_location_recent_media(self):
self.api.location_recent_media(location_id=1)
deftest_location_search(self):
self.api.location_search(lat=37.7,lng=-122.22, distance=2500)
deftest_location(self):
self.api.location(1)
deftest_tag_recent_media(self):
self.api.tag_recent_media(tag_name='1', count=5, max_tag_id='12345')
deftest_tag_recent_media_paginated(self):
forpageinself.api.tag_recent_media(tag_name='1', count=5, as_generator=True, max_pages=2):
str(page)
deftest_tag_search(self):
self.api.tag_search("coff")
deftest_tag(self):
self.api.tag("coffee")
deftest_user_follows(self):
self.api.user_follows()
deftest_user_followed_by(self):
self.api.user_followed_by()
deftest_user_followed_by(self):
self.api.user_followed_by()
deftest_user_requested_by(self):
self.api.user_followed_by()
deftest_user_incoming_requests(self):
self.api.user_incoming_requests()
deftest_change_relationship(self):
self.api.change_user_relationship(user_id=10, action="follow")
# test shortcuts as well
self.api.follow_user(user_id='10')
self.api.unfollow_user(user_id='10')
deftest_geography_recent_media(self):
self.api.geography_recent_media(geography_id=1)
if__name__=='__main__':
ifnotTEST_AUTH:
delInstagramAuthTests
unittest.main()