11#!/usr/bin/env python
2-
32import types
43import six
4+
55try :
66 import simplejson as json
77except ImportError :
1717access_token = "DEBUG"
1818redirect_uri = "http://example.com"
1919
20- class MockHttp (object ):
2120
21+ class MockHttp (object ):
2222 def __init__ (self , * args , ** kwargs ):
2323 pass
2424
2525 def request (self , url , method = "GET" , body = None , headers = {}):
26- fail_state = {
27- 'status' :'400'
28- }, "{}"
26+ fail_state = {'status' : '400' }, "{}"
2927
3028 parsed = urlparse (url )
3129 options = parse_qs (parsed .query )
3230
3331 fn_name = str (active_call )
3432 if fn_name == 'get_authorize_login_url' :
3533 return {
36- 'status' : '200' ,
37- 'content-location' :'http://example.com/redirect/login'
38- }, None
34+ 'status' : '200' ,
35+ 'content-location' : 'http://example.com/redirect/login'
36+ }, None
3937
40- if not 'access_token' in options and not 'client_id' in options :
38+ if 'access_token' not in options and 'client_id' not in options :
4139 fn_name += '_unauthorized'
42- if 'self' in url and not 'access_token' in options :
40+ if 'self' in url and 'access_token' not in options :
4341 fn_name += '_no_auth_user'
4442
4543 fl = open ('fixtures/%s.json' % fn_name )
4644 content = fl .read ()
4745 fl .close ()
46+
4847 json_content = json .loads (content )
4948 status = json_content ['meta' ]['code' ]
50- return {
51- 'status' : status
52- }, content
49+ return {'status' : status }, content
5350
54- oauth2 .Http = MockHttp
5551
52+ oauth2 .Http = MockHttp
5653active_call = None
54+
55+
5756class TestInstagramAPI (client .InstagramAPI ):
5857 def __getattribute__ (self , attr ):
5958 global active_call
@@ -62,13 +61,15 @@ def __getattribute__(self, attr):
6261 active_call = attr
6362 return actual_val
6463
64+
6565class InstagramAuthTests (unittest .TestCase ):
6666 def setUp (self ):
6767 self .unauthenticated_api = TestInstagramAPI (client_id = client_id , redirect_uri = redirect_uri , client_secret = client_secret )
6868
6969 def test_authorize_login_url (self ):
7070 redirect_uri = self .unauthenticated_api .get_authorize_login_url ()
7171 assert redirect_uri
72+
7273 print ("Please visit and authorize at:\n %s" % redirect_uri )
7374 code = raw_input ("Paste received code (blank to skip): " ).strip ()
7475 if not code :
@@ -83,12 +84,13 @@ def test_xauth_exchange(self):
8384 username = raw_input ("Enter username for XAuth (blank to skip): " ).strip ()
8485 if not username :
8586 return
86- password = getpass .getpass ("Enter password for XAuth (blank to skip): " ).strip ()
87+
88+ password = getpass .getpass ("Enter password for XAuth (blank to skip): " ).strip ()
8789 access_token = self .unauthenticated_api .exchange_xauth_login_for_access_token (username , password )
8890 assert access_token
8991
90- class InstagramAPITests (unittest .TestCase ):
9192
93+ class InstagramAPITests (unittest .TestCase ):
9294 def setUp (self ):
9395 super (InstagramAPITests , self ).setUp ()
9496 self .client_only_api = TestInstagramAPI (client_id = client_id )
@@ -98,8 +100,8 @@ def test_media_popular(self):
98100 self .api .media_popular (count = 10 )
99101
100102 def test_media_search (self ):
101- self .client_only_api .media_search (lat = 37.7 ,lng = - 122.22 )
102- self .api .media_search (lat = 37.7 ,lng = - 122.22 )
103+ self .client_only_api .media_search (lat = 37.7 , lng = - 122.22 )
104+ self .api .media_search (lat = 37.7 , lng = - 122.22 )
103105
104106 def test_media_shortcode (self ):
105107 self .client_only_api .media_shortcode ('os1NQjxtvF' )
@@ -144,43 +146,39 @@ def test_user_liked_media(self):
144146 def test_user_recent_media (self ):
145147 media , url = self .api .user_recent_media (count = 10 )
146148
147- self .assertTrue ( all ( [hasattr (obj , 'type' ) for obj in media ] ) )
149+ self .assertTrue (all ([hasattr (obj , 'type' ) for obj in media ]) )
148150
149151 image = media [0 ]
150152 self .assertEqual (
151- image .get_standard_resolution_url (),
152- "http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_7.jpg" )
153+ image .get_standard_resolution_url (),
154+ "http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_7.jpg" )
153155
154156 self .assertEqual (
155- image .get_low_resolution_url (),
156- "http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_6.jpg" )
157+ image .get_low_resolution_url (),
158+ "http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_6.jpg" )
157159
158160 self .assertEqual (
159- image .get_thumbnail_url (),
160- "http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_5.jpg" )
161+ image .get_thumbnail_url (),
162+ "http://distillery-dev.s3.amazonaws.com/media/2011/02/02/1ce5f3f490a640ca9068e6000c91adc5_5.jpg" )
161163
162- self .assertEqual ( False , hasattr (image , 'videos' ) )
164+ self .assertEqual (False , hasattr (image , 'videos' ))
163165
164166 video = media [1 ]
165167 self .assertEqual (
166- video .get_standard_resolution_url (),
167- video .videos ['standard_resolution' ].url )
168+ video .get_standard_resolution_url (),
169+ video .videos ['standard_resolution' ].url )
168170
169171 self .assertEqual (
170- video .get_standard_resolution_url (),
171- "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4" )
172+ video .get_standard_resolution_url (),
173+ "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4" )
172174
173175 self .assertEqual (
174- video .get_low_resolution_url (),
175- "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4" )
176+ video .get_low_resolution_url (),
177+ "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4" )
176178
177179 self .assertEqual (
178- video .get_thumbnail_url (),
179- "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg" )
180-
181-
182-
183-
180+ video .get_thumbnail_url (),
181+ "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg" )
184182
185183 def test_user_search (self ):
186184 self .api .user_search ('mikeyk' , 10 )
@@ -204,7 +202,7 @@ def test_location_recent_media(self):
204202 self .api .location_recent_media (location_id = 1 )
205203
206204 def test_location_search (self ):
207- self .api .location_search (lat = 37.7 ,lng = - 122.22 , distance = 2500 )
205+ self .api .location_search (lat = 37.7 , lng = - 122.22 , distance = 2500 )
208206
209207 def test_location (self ):
210208 self .api .location (1 )
@@ -225,12 +223,6 @@ def test_tag(self):
225223 def test_user_follows (self ):
226224 self .api .user_follows ()
227225
228- def test_user_followed_by (self ):
229- self .api .user_followed_by ()
230-
231- def test_user_followed_by (self ):
232- self .api .user_followed_by ()
233-
234226 def test_user_requested_by (self ):
235227 self .api .user_followed_by ()
236228
@@ -246,6 +238,7 @@ def test_change_relationship(self):
246238 def test_geography_recent_media (self ):
247239 self .api .geography_recent_media (geography_id = 1 )
248240
241+
249242if __name__ == '__main__' :
250243 if not TEST_AUTH :
251244 del InstagramAuthTests
0 commit comments