Skip to content

Commit 3cb7f34

Browse files
committed
Removing Basic Auth support, removing stupid module globals in favor of proper configuration, bumping version accordingly.
1 parent f097e2d commit 3cb7f34

File tree

4 files changed

+177
-91
lines changed

4 files changed

+177
-91
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
argparse==1.2.1
2+
httplib2==0.7.4
23
nose==1.1.2
4+
oauth2==1.5.211
35
tornado==2.3
46
wsgiref==0.1.2

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='tweetstream',
20-
version='0.1.2',
20+
version='0.2.0',
2121
description='Simple Twitter Streaming client for Tornado ',
2222
author='Josh Marshall',
2323
author_email='catchjosh@gmail.com',

tests.py

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,69 @@
11
from tornado.testing import AsyncTestCase
22
import tweetstream
33
import logging
4+
import os
5+
import time
46

5-
try:
6-
import test_settings
7-
except ImportError:
8-
test_settings = None
7+
TEST_CONSUMER_KEY = os.environ.get("TWEETSTREAM_TEST_CONSUMER_KEY")
8+
TEST_CONSUMER_SECRET = os.environ.get("TWEETSTREAM_TEST_CONSUMER_SECRET")
9+
TEST_ACCESS_TOKEN = os.environ.get("TWEETSTREAM_TEST_ACCESS_TOKEN")
10+
TEST_ACCESS_SECRET = os.environ.get("TWEETSTREAM_TEST_ACCESS_TOKEN_SECRET")
911

12+
def test_real_config():
13+
configuration = {
14+
"twitter_consumer_secret": TEST_CONSUMER_SECRET,
15+
"twitter_consumer_key": TEST_CONSUMER_KEY,
16+
"twitter_access_token_secret": TEST_ACCESS_SECRET,
17+
"twitter_access_token": TEST_ACCESS_TOKEN
18+
}
19+
if None in configuration.values():
20+
logging.debug("Missing one or more test configuration values.")
21+
return None
22+
return configuration
1023

1124
class TestTweetStream(AsyncTestCase):
1225

13-
def setUp(self):
14-
super(TestTweetStream, self).setUp()
15-
self.original_app_password = tweetstream.TWITTER_APP_PASSWORD
16-
tweetstream.TWITTER_APP_PASSWORD = "foobar"
17-
18-
def tearDown(self):
19-
super(TestTweetStream, self).tearDown()
20-
tweetstream.TWITTER_APP_PASSWORD = self.original_app_password
21-
2226
def test_twitter_stream(self):
2327
""" Test that the twitter stream is started with module defaults """
2428
result = {}
2529
def error_callback(error):
2630
result["error"] = error
31+
print error
2732
self.stop()
28-
stream = tweetstream.TweetStream(ioloop=self.io_loop)
29-
stream.error_callback = error_callback
30-
stream.fetch("foobar?whats=up")
33+
34+
configuration = {
35+
"twitter_consumer_secret": "ABCDEF1234567890",
36+
"twitter_consumer_key": "0987654321ABCDEF",
37+
"twitter_access_token_secret": "1234567890ABCDEF",
38+
"twitter_access_token": "FEDCBA09123456789",
39+
}
40+
stream = tweetstream.TweetStream(configuration, ioloop=self.io_loop)
41+
stream.set_error_callback(error_callback)
42+
stream.fetch("/1/statuses/sample.json")
3143
self.wait()
3244
self.assertTrue("error" in result)
3345

46+
def test_twitter_stream_bad_configuration(self):
47+
"""Test the configuration missing values."""
48+
configuration = {
49+
"twitter_consumer_secret": "ABCDEF1234567890",
50+
"twitter_consumer_key": "0987654321ABCDEF",
51+
"twitter_access_token_secret": "1234567890ABCDEF",
52+
"twitter_access_token": "FEDCBA09123456789"
53+
}
54+
for key in configuration:
55+
bad_config = configuration.copy()
56+
del bad_config[key]
57+
self.assertRaises(tweetstream.MissingConfiguration,
58+
lambda: tweetstream.TweetStream(bad_config))
59+
3460
def test_twitter_stream_with_configuration(self):
3561
"""Test that the twitter stream supports instance configuration."""
3662
configuration = {
37-
"twitter_app_username": "newusername",
38-
"twitter_app_password": "newpassword",
63+
"twitter_consumer_secret": "ABCDEF1234567890",
64+
"twitter_consumer_key": "0987654321ABCDEF",
65+
"twitter_access_token_secret": "1234567890ABCDEF",
66+
"twitter_access_token": "FEDCBA09123456789",
3967
"twitter_stream_host": "whatever.com",
4068
"twitter_stream_port": 556,
4169
"twitter_stream_scheme": "http"
@@ -44,48 +72,42 @@ def test_twitter_stream_with_configuration(self):
4472
configuration=configuration)
4573
# this is evil, but until module stuff is removed and
4674
# proper configuration is refactored it will have to do.
47-
self.assertEqual("newusername", stream._twitter_app_user)
48-
self.assertEqual("newpassword", stream._twitter_app_password)
49-
self.assertEqual("whatever.com", stream._twitter_stream_host)
5075
self.assertEqual(556, stream._twitter_stream_port)
5176
self.assertEqual("http", stream._twitter_stream_scheme)
5277

5378

5479
class TestActualTwitterCalls(AsyncTestCase):
5580
""" Testing actual calls, assuming settings are loaded. """
5681

57-
def setUp(self):
58-
super(TestActualTwitterCalls, self).setUp()
59-
self.original_app_user = tweetstream.TWITTER_APP_USER
60-
self.original_app_password = tweetstream.TWITTER_APP_PASSWORD
61-
if test_settings:
62-
tweetstream.TWITTER_APP_USER = \
63-
test_settings.TWITTER_APP_USER
64-
tweetstream.TWITTER_APP_PASSWORD = \
65-
test_settings.TWITTER_APP_PASSWORD
66-
67-
def tearDown(self):
68-
super(TestActualTwitterCalls, self).tearDown()
69-
tweetstream.TWITTER_APP_USER = self.original_app_user
70-
tweetstream.TWITTER_APP_PASSWORD = self.original_app_password
71-
7282
def get_message(self, path, clean=False):
7383
""" Wraps the ioloop start much like self.fetch """
74-
stream = tweetstream.TweetStream(ioloop=self.io_loop, clean=clean)
84+
def error_callback(error):
85+
self.io_loop.stop()
86+
self.fail(str(error))
87+
88+
stream = tweetstream.TweetStream(
89+
configuration=test_real_config(),
90+
ioloop=self.io_loop, clean=clean)
91+
stream.set_error_callback(error_callback)
7592
result = {}
7693
def callback(message):
7794
""" Save result """
78-
result["message"] = message
79-
self.stop()
95+
if message.get("text"):
96+
result["message"] = message
97+
self.stop()
98+
# otherwise, it's not a tweet we care about...
99+
80100
stream.fetch(path, callback=callback)
81101
self.wait()
82102
# will block until a message comes in or timeout
103+
# now waiting to keep from hammering the stream connections
104+
time.sleep(5)
83105
return result["message"]
84106

85107
def test_message(self):
86108
""" Test that twitter connects. """
87109
#... if only everyone used 2.7 ...
88-
if not test_settings:
110+
if not test_real_config():
89111
logging.debug("Skipping test.")
90112
return
91113
result = self.get_message("/1/statuses/sample.json")
@@ -94,7 +116,7 @@ def test_message(self):
94116

95117
def test_stripped_message(self):
96118
""" Test that twitter connects and retrieves simple message. """
97-
if not test_settings:
119+
if not test_real_config():
98120
logging.debug("Skipping test")
99121
return
100122
result = self.get_message("/1/statuses/sample.json", clean=True)
@@ -103,3 +125,12 @@ def test_stripped_message(self):
103125
self.assertTrue("text" in result)
104126
self.assertTrue(result["type"] == "tweet")
105127

128+
def test_search_term(self):
129+
"""Test the statuses with a search term."""
130+
if not test_real_config():
131+
logging.debug("Skipping test")
132+
return
133+
result = self.get_message("/1/statuses/filter.json?track=twitter")
134+
self.assertTrue("user" in result)
135+
self.assertTrue("text" in result)
136+

0 commit comments

Comments
 (0)