55# License: http://jkeyes.mit-license.org/
66#
77
8- import httpretty , os , re
8+ import httpretty
9+ import os
10+ import re
911
1012DIRPATH = os .path .dirname (__file__ )
1113FIXTURES = os .path .join (DIRPATH , 'fixtures' )
3234delete = httpretty .DELETE
3335r = re .compile
3436
37+
3538def fixture (fixture ):
36- fixture_path = os .path .join (FIXTURES , '%(fixture)s.json' % {'fixture' : fixture })
39+ fixture_path = os .path .join (
40+ FIXTURES , '%(fixture)s.json' % {'fixture' : fixture })
3741 return open (fixture_path ).read ()
3842
43+
3944@httpretty .activate
4045def test_users ():
4146 httpretty .register_uri (get , r (r"/v1/users" ), body = fixture ('v1-users' ))
4247 ok_ (len (User .all ()) > 0 )
4348
49+
4450@httpretty .activate
4551def test_user ():
46- httpretty .register_uri (get , r (r"/v1/users\?email=" ), body = fixture ('v1-user' ), match_querystring = True )
52+ httpretty .register_uri (
53+ get , r (r"/v1/users\?email=" ),
54+ body = fixture ('v1-user' ), match_querystring = True )
4755 user = User .find (email = 'somebody@example.com' )
4856 eq_ (user .name , 'Somebody' )
4957
50- httpretty .register_uri (get , r (r"/v1/users\?user_id=" ), body = fixture ('v1-user' ), match_querystring = True )
58+ httpretty .register_uri (
59+ get , r (r"/v1/users\?user_id=" ),
60+ body = fixture ('v1-user' ), match_querystring = True )
5161 user = User .find_by_user_id ('123' )
5262 eq_ (user .name , 'Somebody' )
5363
64+
5465@httpretty .activate
5566@raises (ResourceNotFound )
5667def test_not_found ():
57- httpretty .register_uri (get , r (r"/v1/users\?email=not-found" ), status = 404 , match_querystring = True )
68+ httpretty .register_uri (
69+ get , r (r"/v1/users\?email=not-found" ),
70+ status = 404 , match_querystring = True )
5871 User .find (email = 'not-found@example.com' )
5972
73+
6074@httpretty .activate
6175@raises (ResourceNotFound )
6276def test_not_found_qs ():
63- httpretty .register_uri (get , r (r"/v1/users\?email=not-found" ), body = fixture ('v1-user_not_found' ), status = 404 , match_querystring = True )
77+ httpretty .register_uri (
78+ get , r (r"/v1/users\?email=not-found" ),
79+ body = fixture ('v1-user_not_found' ), status = 404 , match_querystring = True )
6480 User .find (email = 'not-found@example.com' )
6581
82+
6683@httpretty .activate
6784@raises (ServerError )
6885def test_server_error ():
69- httpretty .register_uri (get , r (r"/v1/users\?email=server-error" ), status = 500 , match_querystring = True )
86+ httpretty .register_uri (
87+ get , r (r"/v1/users\?email=server-error" ),
88+ status = 500 , match_querystring = True )
7089 User .find (email = 'server-error@example.com' )
7190
91+
7292@httpretty .activate
7393@raises (ServerError )
7494def test_server_error_qs ():
75- httpretty .register_uri (get , r (r"/v1/users\?email=server-error" ), body = fixture ('v1-user_server_error' ), status = 500 , match_querystring = True )
95+ httpretty .register_uri (
96+ get , r (r"/v1/users\?email=server-error" ),
97+ body = fixture ('v1-user_server_error' ), status = 500 ,
98+ match_querystring = True )
7699 User .find (email = 'server-error@example.com' )
77100
101+
78102@httpretty .activate
79103@raises (AuthenticationError )
80104def test_bad_api_key ():
81- httpretty .register_uri (get , r (r"/v1/users\?email=authentication-error" ), status = 401 , match_querystring = True )
105+ httpretty .register_uri (
106+ get , r (r"/v1/users\?email=authentication-error" ),
107+ status = 401 , match_querystring = True )
82108 Intercom .app_id = 'bad-app-id'
83109 Intercom .api_key = 'bad-secret-key'
84110 User .find (email = 'authentication-error@example.com' )
85111
112+
86113@httpretty .activate
87114def test_message_threads ():
88- httpretty .register_uri (get , r (r"/v1/users/message_threads\?email=somebody" ), body = fixture ('v1-users-message_threads' ), match_querystring = True )
115+ httpretty .register_uri (
116+ get , r (r"/v1/users/message_threads\?email=somebody" ),
117+ body = fixture ('v1-users-message_threads' ), match_querystring = True )
89118 thread = MessageThread .find_all (email = 'somebody@example.com' )[0 ]
90119 for attr in ['thread_id' , 'read' , 'messages' , 'created_at' , 'updated_at' ]:
91120 ok_ (getattr (thread , attr ))
92121
122+
93123@nottest
94124@httpretty .activate
95125def test_message_thread ():
96- httpretty .register_uri (get , r (r"/v1/users/message_threads\?email=somebody" ), body = fixture ('v1-users-message_threads' ), match_querystring = True )
97- httpretty .register_uri (get , r (r"/v1/users/message_threads" ), body = fixture ('v1-users-message_thread' ))
126+ httpretty .register_uri (
127+ get , r (r"/v1/users/message_threads\?email=somebody" ),
128+ body = fixture ('v1-users-message_threads' ), match_querystring = True )
129+ httpretty .register_uri (
130+ get , r (r"/v1/users/message_threads" ),
131+ body = fixture ('v1-users-message_thread' ))
98132 thread = MessageThread .find_all (email = 'somebody@example.com' )[0 ]
99133 thread .mark_as_read ()
100134
135+
101136@httpretty .activate
102137def test_impression ():
103- httpretty .register_uri (post , r (r"/v1/users/impressions" ), body = fixture ('v1-users-impressions' ))
138+ httpretty .register_uri (
139+ post , r (r"/v1/users/impressions" ),
140+ body = fixture ('v1-users-impressions' ))
104141 impression = Impression .create (email = 'somebody@example.com' )
105142 ok_ (impression .unread_messages > 0 )
106143 # eq_(impression.email, 'somebody@example.com')
107144
145+
108146@httpretty .activate
109147def test_note ():
110- httpretty .register_uri (post , r (r"/v1/users/notes" ), body = fixture ('v1-users-note' ))
148+ httpretty .register_uri (
149+ post , r (r"/v1/users/notes" ),
150+ body = fixture ('v1-users-note' ))
111151 note = Note .create (body = "This is a note" , email = 'somebody@example.com' )
112152 eq_ (note .html , "<p>This is a note</p>" )
113153 eq_ (note .user .email , "somebody@example.com" )
114154
155+
115156@nottest
116157@httpretty .activate
117158def test_endpoints ():
118159 # FakeWeb.allow_net_connect = %r(127.0.0.7)
119- httpretty .register_uri (get , r (r"/v1/users\?email=" ), body = fixture ('v1-user' ), match_querystring = True )
160+ httpretty .register_uri (
161+ get , r (r"/v1/users\?email=" ),
162+ body = fixture ('v1-user' ), match_querystring = True )
120163 Intercom .endpoints = ("http://127.0.0.7" , "https://api.intercom.io" )
121164 user = User .find (email = 'somebody@example.com' )
122165 eq_ (user .name , 'Somebody' )
123166
167+
124168@nottest
125169@httpretty .activate
126170def test_unreachable ():
127171 class ServiceUnavailableError (Exception ):
128172 pass
129- httpretty .register_uri (get , r (r"example\.com" ), body = fixture ('v1-user' ), status = 503 )
173+ httpretty .register_uri (
174+ get , r (r"example\.com" ),
175+ body = fixture ('v1-user' ), status = 503 )
130176 Intercom .endpoints = ("http://example.com" )
131- User .find .when .called_with (email = 'somebody@example.com' ).should .throw (ServiceUnavailableError )
177+ User .find .when .called_with (email = 'somebody@example.com' )\
178+ .should .throw (ServiceUnavailableError )
132179 Intercom .endpoints = ("http://example.com" , "http://api.example.com" )
133- User .find .when .called_with (email = 'not-found@example.com' ).should .throw (ServiceUnavailableError )
180+ User .find .when .called_with (email = 'not-found@example.com' )\
181+ .should .throw (ServiceUnavailableError )
182+
134183
135184@nottest
136185@httpretty .activate
137186def test_bad_gateway ():
138187 class BadGatewayError (Exception ):
139188 pass
140- httpretty .register_uri (get , r (r"example\.com" ), body = fixture ('v1-user' ), status = 502 )
189+ httpretty .register_uri (
190+ get , r (r"example\.com" ),
191+ body = fixture ('v1-user' ), status = 502 )
141192 Intercom .endpoints = ("http://example.com" )
142- User .find .when .called_with (email = 'somebody@example.com' ).should .throw (BadGatewayError )
193+ User .find .when .called_with (email = 'somebody@example.com' )\
194+ .should .throw (BadGatewayError )
143195 Intercom .endpoints = ("http://example.com" , "http://api.example.com" )
144- User .find .when .called_with (email = 'not-found@example.com' ).should .throw (BadGatewayError )
196+ User .find .when .called_with (email = 'not-found@example.com' )\
197+ .should .throw (BadGatewayError )
198+
145199
146200@httpretty .activate
147201def test_doctest ():
@@ -151,49 +205,97 @@ def request_callback(method, uri, headers):
151205 parsed_body = httpretty .last_request ().parsed_body
152206 # handle the user updates
153207 if 'name' in parsed_body :
154- return (200 , headers , fixture ('v1-user-updated' ))
208+ return (200 , headers , fixture ('v1-user-updated' ))
155209 return (200 , headers , fixture ('v1-user' ))
156210
157- httpretty .register_uri (get , r (r'/v1/users$' ), body = fixture ('v1-users' ), match_querystring = True )
158- httpretty .register_uri (get , r (r'/v1/users\?page=1' ), body = fixture ('v1-users' ), match_querystring = True )
159- httpretty .register_uri (post , r (r'/v1/users$' ), body = fixture ('v1-user' ), match_querystring = True )
160- httpretty .register_uri (put , r (r'/v1/users$' ), body = request_callback , match_querystring = True )
161- httpretty .register_uri (delete , r (r'/v1/users$' ), body = fixture ('v1-user' ), match_querystring = True )
162- httpretty .register_uri (get , r (r"/v1/users\?email=somebody" ), body = fixture ('v1-user' ), match_querystring = True )
163- httpretty .register_uri (get , r (r"/v1/users\?user_id=123" ), body = fixture ('v1-user' ), match_querystring = True )
164- httpretty .register_uri (get , r (r"/v1/users\?email=not-found" ), status = 404 , match_querystring = True )
165- httpretty .register_uri (get , r (r"/v1/users\?email=server-error" ), status = 500 , match_querystring = True )
166- httpretty .register_uri (get , r (r"/v1/users\?email=authentication-error" ), status = 401 , match_querystring = True )
167-
168- httpretty .register_uri (get , r (r"/v1/tags\?name=Free" ), body = fixture ('v1-tag' ))
169- httpretty .register_uri (get , r (r"/v1/tags" ), body = fixture ('v1-tag' ))
170- httpretty .register_uri (post , r (r"/v1/tags" ), body = fixture ('v1-tag' ))
171- httpretty .register_uri (put , r (r"/v1/tags" ), body = fixture ('v1-tag' ))
172-
173- httpretty .register_uri (post , r (r"/v1/users/notes" ), body = fixture ('v1-users-note' ))
174-
175- httpretty .register_uri (get , r (r'/v1/users/message_threads$' ), body = fixture ('v1-users-message_threads' ), match_querystring = True )
176- httpretty .register_uri (post , r (r'/v1/users/message_threads$' ), body = fixture ('v1-users-message_thread' ), match_querystring = True )
177- httpretty .register_uri (put , r (r'/v1/users/message_threads$' ), body = fixture ('v1-users-message_thread' ), match_querystring = True )
178- httpretty .register_uri (get , r (r"/v1/users/message_threads\?thread_id=5591" ), body = fixture ('v1-users-message_thread' ), match_querystring = True )
179- httpretty .register_uri (get , r (r"/v1/users/message_threads\?email=somebody" ), body = fixture ('v1-users-message_threads' ), match_querystring = True )
180-
181- httpretty .register_uri (post , r (r"/v1/users/impressions" ), body = fixture ('v1-users-impressions' ))
182-
183- (failure_count , test_count ) = doctest .testfile ("../../intercom/user.py" )
211+ httpretty .register_uri (
212+ get , r (r'/v1/users$' ),
213+ body = fixture ('v1-users' ), match_querystring = True )
214+ httpretty .register_uri (
215+ get , r (r'/v1/users\?page=1' ),
216+ body = fixture ('v1-users' ), match_querystring = True )
217+ httpretty .register_uri (
218+ post , r (r'/v1/users$' ),
219+ body = fixture ('v1-user' ), match_querystring = True )
220+ httpretty .register_uri (
221+ put , r (r'/v1/users$' ),
222+ body = request_callback , match_querystring = True )
223+ httpretty .register_uri (
224+ delete , r (r'/v1/users$' ),
225+ body = fixture ('v1-user' ), match_querystring = True )
226+ httpretty .register_uri (
227+ get , r (r"/v1/users\?email=somebody" ),
228+ body = fixture ('v1-user' ), match_querystring = True )
229+ httpretty .register_uri (
230+ get , r (r"/v1/users\?user_id=123" ),
231+ body = fixture ('v1-user' ), match_querystring = True )
232+ httpretty .register_uri (
233+ get , r (r"/v1/users\?email=not-found" ),
234+ status = 404 , match_querystring = True )
235+ httpretty .register_uri (
236+ get , r (r"/v1/users\?email=server-error" ),
237+ status = 500 , match_querystring = True )
238+ httpretty .register_uri (
239+ get , r (r"/v1/users\?email=authentication-error" ),
240+ status = 401 , match_querystring = True )
241+
242+ httpretty .register_uri (
243+ get , r (r"/v1/tags\?name=Free" ),
244+ body = fixture ('v1-tag' ))
245+ httpretty .register_uri (
246+ get , r (r"/v1/tags" ),
247+ body = fixture ('v1-tag' ))
248+ httpretty .register_uri (
249+ post , r (r"/v1/tags" ),
250+ body = fixture ('v1-tag' ))
251+ httpretty .register_uri (
252+ put , r (r"/v1/tags" ),
253+ body = fixture ('v1-tag' ))
254+
255+ httpretty .register_uri (
256+ post , r (r"/v1/users/notes" ),
257+ body = fixture ('v1-users-note' ))
258+
259+ httpretty .register_uri (
260+ get , r (r'/v1/users/message_threads$' ),
261+ body = fixture ('v1-users-message_threads' ), match_querystring = True )
262+ httpretty .register_uri (
263+ post , r (r'/v1/users/message_threads$' ),
264+ body = fixture ('v1-users-message_thread' ), match_querystring = True )
265+ httpretty .register_uri (
266+ put , r (r'/v1/users/message_threads$' ),
267+ body = fixture ('v1-users-message_thread' ), match_querystring = True )
268+ httpretty .register_uri (
269+ get , r (r"/v1/users/message_threads\?thread_id=5591" ),
270+ body = fixture ('v1-users-message_thread' ), match_querystring = True )
271+ httpretty .register_uri (
272+ get , r (r"/v1/users/message_threads\?email=somebody" ),
273+ body = fixture ('v1-users-message_threads' ), match_querystring = True )
274+
275+ httpretty .register_uri (
276+ post , r (r"/v1/users/impressions" ),
277+ body = fixture ('v1-users-impressions' ))
278+
279+ (failure_count , test_count ) = doctest .testfile (
280+ "../../intercom/user.py" )
184281 eq_ (failure_count , 0 )
185282
186- (failure_count , test_count ) = doctest .testfile ("../../intercom/tag.py" )
283+ (failure_count , test_count ) = doctest .testfile (
284+ "../../intercom/tag.py" )
187285 eq_ (failure_count , 0 )
188286
189- (failure_count , test_count ) = doctest .testfile ("../../intercom/note.py" )
287+ (failure_count , test_count ) = doctest .testfile (
288+ "../../intercom/note.py" )
190289 eq_ (failure_count , 0 )
191290
192- (failure_count , test_count ) = doctest .testfile ("../../intercom/message_thread.py" )
291+ (failure_count , test_count ) = doctest .testfile (
292+ "../../intercom/message_thread.py" )
193293 eq_ (failure_count , 0 )
194294
195- (failure_count , test_count ) = doctest .testfile ("../../intercom/impression.py" )
295+ (failure_count , test_count ) = doctest .testfile (
296+ "../../intercom/impression.py" )
196297 eq_ (failure_count , 0 )
197298
198- (failure_count , test_count ) = doctest .testfile ("../../intercom/intercom.py" )
299+ (failure_count , test_count ) = doctest .testfile (
300+ "../../intercom/intercom.py" )
199301 eq_ (failure_count , 0 )
0 commit comments