5
5
# License: http://jkeyes.mit-license.org/
6
6
#
7
7
8
- import httpretty , os , re
8
+ import httpretty
9
+ import os
10
+ import re
9
11
10
12
DIRPATH = os .path .dirname (__file__ )
11
13
FIXTURES = os .path .join (DIRPATH , 'fixtures' )
32
34
delete = httpretty .DELETE
33
35
r = re .compile
34
36
37
+
35
38
def 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 })
37
41
return open (fixture_path ).read ()
38
42
43
+
39
44
@httpretty .activate
40
45
def test_users ():
41
46
httpretty .register_uri (get , r (r"/v1/users" ), body = fixture ('v1-users' ))
42
47
ok_ (len (User .all ()) > 0 )
43
48
49
+
44
50
@httpretty .activate
45
51
def 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 )
47
55
user = User .find (email = 'somebody@example.com' )
48
56
eq_ (user .name , 'Somebody' )
49
57
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 )
51
61
user = User .find_by_user_id ('123' )
52
62
eq_ (user .name , 'Somebody' )
53
63
64
+
54
65
@httpretty .activate
55
66
@raises (ResourceNotFound )
56
67
def 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 )
58
71
User .find (email = 'not-found@example.com' )
59
72
73
+
60
74
@httpretty .activate
61
75
@raises (ResourceNotFound )
62
76
def 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 )
64
80
User .find (email = 'not-found@example.com' )
65
81
82
+
66
83
@httpretty .activate
67
84
@raises (ServerError )
68
85
def 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 )
70
89
User .find (email = 'server-error@example.com' )
71
90
91
+
72
92
@httpretty .activate
73
93
@raises (ServerError )
74
94
def 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 )
76
99
User .find (email = 'server-error@example.com' )
77
100
101
+
78
102
@httpretty .activate
79
103
@raises (AuthenticationError )
80
104
def 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 )
82
108
Intercom .app_id = 'bad-app-id'
83
109
Intercom .api_key = 'bad-secret-key'
84
110
User .find (email = 'authentication-error@example.com' )
85
111
112
+
86
113
@httpretty .activate
87
114
def 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 )
89
118
thread = MessageThread .find_all (email = 'somebody@example.com' )[0 ]
90
119
for attr in ['thread_id' , 'read' , 'messages' , 'created_at' , 'updated_at' ]:
91
120
ok_ (getattr (thread , attr ))
92
121
122
+
93
123
@nottest
94
124
@httpretty .activate
95
125
def 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' ))
98
132
thread = MessageThread .find_all (email = 'somebody@example.com' )[0 ]
99
133
thread .mark_as_read ()
100
134
135
+
101
136
@httpretty .activate
102
137
def 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' ))
104
141
impression = Impression .create (email = 'somebody@example.com' )
105
142
ok_ (impression .unread_messages > 0 )
106
143
# eq_(impression.email, 'somebody@example.com')
107
144
145
+
108
146
@httpretty .activate
109
147
def 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' ))
111
151
note = Note .create (body = "This is a note" , email = 'somebody@example.com' )
112
152
eq_ (note .html , "<p>This is a note</p>" )
113
153
eq_ (note .user .email , "somebody@example.com" )
114
154
155
+
115
156
@nottest
116
157
@httpretty .activate
117
158
def test_endpoints ():
118
159
# 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 )
120
163
Intercom .endpoints = ("http://127.0.0.7" , "https://api.intercom.io" )
121
164
user = User .find (email = 'somebody@example.com' )
122
165
eq_ (user .name , 'Somebody' )
123
166
167
+
124
168
@nottest
125
169
@httpretty .activate
126
170
def test_unreachable ():
127
171
class ServiceUnavailableError (Exception ):
128
172
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 )
130
176
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 )
132
179
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
+
134
183
135
184
@nottest
136
185
@httpretty .activate
137
186
def test_bad_gateway ():
138
187
class BadGatewayError (Exception ):
139
188
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 )
141
192
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 )
143
195
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
+
145
199
146
200
@httpretty .activate
147
201
def test_doctest ():
@@ -151,49 +205,97 @@ def request_callback(method, uri, headers):
151
205
parsed_body = httpretty .last_request ().parsed_body
152
206
# handle the user updates
153
207
if 'name' in parsed_body :
154
- return (200 , headers , fixture ('v1-user-updated' ))
208
+ return (200 , headers , fixture ('v1-user-updated' ))
155
209
return (200 , headers , fixture ('v1-user' ))
156
210
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" )
184
281
eq_ (failure_count , 0 )
185
282
186
- (failure_count , test_count ) = doctest .testfile ("../../intercom/tag.py" )
283
+ (failure_count , test_count ) = doctest .testfile (
284
+ "../../intercom/tag.py" )
187
285
eq_ (failure_count , 0 )
188
286
189
- (failure_count , test_count ) = doctest .testfile ("../../intercom/note.py" )
287
+ (failure_count , test_count ) = doctest .testfile (
288
+ "../../intercom/note.py" )
190
289
eq_ (failure_count , 0 )
191
290
192
- (failure_count , test_count ) = doctest .testfile ("../../intercom/message_thread.py" )
291
+ (failure_count , test_count ) = doctest .testfile (
292
+ "../../intercom/message_thread.py" )
193
293
eq_ (failure_count , 0 )
194
294
195
- (failure_count , test_count ) = doctest .testfile ("../../intercom/impression.py" )
295
+ (failure_count , test_count ) = doctest .testfile (
296
+ "../../intercom/impression.py" )
196
297
eq_ (failure_count , 0 )
197
298
198
- (failure_count , test_count ) = doctest .testfile ("../../intercom/intercom.py" )
299
+ (failure_count , test_count ) = doctest .testfile (
300
+ "../../intercom/intercom.py" )
199
301
eq_ (failure_count , 0 )
0 commit comments