-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter-tests.py
executable file
·207 lines (179 loc) · 7.86 KB
/
twitter-tests.py
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
#!/usr/bin/python2.7
import os
import json
import twitter
import unittest
import MySQLdb
class TwitterTestCase(unittest.TestCase):
def setUp(self):
self.MYSQL_DATABASE_HOST = "localhost"
self.MYSQL_DATABASE_USER = "twitter"
self.MYSQL_DATABASE_PASSWORD = "DF7U7q2yy6pUPSn3"
self.MYSQL_DATABASE_DB = "twitter"
self.db = MySQLdb.connect(
host=self.MYSQL_DATABASE_HOST,
user=self.MYSQL_DATABASE_USER,
passwd=self.MYSQL_DATABASE_PASSWORD,
db=self.MYSQL_DATABASE_DB)
self.c = self.db.cursor()
self.c.execute("DELETE FROM friends WHERE userId=6 OR followingId = 6")
twitter.app.config['TESTING'] = True
self.app = twitter.app.test_client()
self.urls = (
"/user_timeline.json?token=%s&username=%s",
"/friendslist.json?token=%s&username=%s",
"/followerslist.json?token=%s&username=%s",
"/createfriend.json?token=%s&username=%s",
"/destroyfriend.json?token=%s&username=%s")
self.user_timeline_url="/user_timeline.json?token=%s&username=%s"
self.friendslist_url="/friendslist.json?token=%s&username=%s"
self.followerslist_url="/followerslist.json?token=%s&username=%s"
self.addfriend_url="/createfriend.json?token=%s&username=%s"
self.destroyfriend_url="/destroyfriend.json?token=%s&username=%s"
self.token="1234567890abcdef"
#def tearDown(self):
#do nothing
def test_main_url(self):
"""Verifies that the main URL works"""
rv = self.app.get('/')
assert 'URLS' in rv.data
def test_timeline_json_response(self):
"""Verifies JSON list of tweets are returned"""
URL=self.user_timeline_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
assert "tweets" in rv.data
assert rv.status_code == 200
def test_friends_list_json_response(self):
"""Verifies JSON list of users is returned for all friends of a specified user"""
URL=self.friendslist_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
assert "users" in rv.data
assert rv.status_code == 200
def test_followers_list_json_response(self):
"""Verifies JSON list of users is returned for all users following a specified user"""
URL=self.followerslist_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
assert "users" in rv.data
assert rv.status_code == 200
def test_add_remove_friend_json_response(self):
"""Verifies a user can be added and removed as a friend"""
URL=self.addfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
assert json_out['user']['username'] == "brian"
assert rv.status_code == 200
URL=self.destroyfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
assert json_out['user']['username'] == "brian"
assert rv.status_code == 200
def test_null_tokens(self):
"""Verifies a null token argument will not allow access to any API calls"""
for url in self.urls:
URL = url[:url.find('?')]
rv = self.app.get(URL)
assert rv.status_code == 401
URL="/user_timeline.json"
rv2 = self.app.get(URL)
assert rv2.status_code == 401
def test_invalid_tokens(self):
"""Verifies an invalid token will not allow access to any API calls"""
for url in self.urls:
URL = url % (self.token + "1", "test")
rv = self.app.get(URL)
assert rv.status_code == 401
URL="/user_timeline.json"
rv2 = self.app.get(URL)
assert rv2.status_code == 401
def test_default_user_from_api(self):
"""Verifies user of the specified token is defaulted to when no user is specified
For the create/destroy calls, these end up both being Bad Requests"""
for url in self.urls:
URL = url % (self.token, "")
URL1 = url % (self.token, "test")
rv = self.app.get(URL)
rv1 = self.app.get(URL)
assert rv.data == rv1.data
assert rv.status_code == rv.status_code
def test_duplicate_add_fails(self):
"""Verifies adding a friend fails when they already exist"""
URL=self.addfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
URL=self.addfriend_url % (self.token, "brian")
rv = self.app.get(URL)
assert rv.status_code == 400
URL=self.destroyfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
def test_remove_nonexistant_user_fails(self):
"""Verifies removing a friend fails when they are not your friend"""
URL=self.destroyfriend_url % (self.token, "brian")
rv = self.app.get(URL)
assert rv.status_code == 400
def test_add_friend_view_tweets_only_of_friends(self):
"""Verifies only self-tweets and tweets of followers are visible on the given users timeline"""
URL=self.user_timeline_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
for tweet in json_out['tweets']:
assert tweet['username'] != "brian"
URL=self.addfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
URL=self.user_timeline_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
found_tweets_from_brian = 0
for tweet in json_out['tweets']:
if tweet['username'] == "brian": found_tweets_from_brian+=1
assert tweet['username'] in ("test", "brian")
assert found_tweets_from_brian != 0
URL=self.destroyfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
def test_new_friend_updates_friendslist_properly(self):
"""Verifies new friends show up in the friends list"""
URL=self.friendslist_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
for user in json_out['users']:
assert user['username'] != "brian"
URL=self.addfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
URL=self.friendslist_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
found_user_brian = 0
for user in json_out['users']:
if user['username'] == "brian": found_user_brian += 1
assert found_user_brian != 0
URL=self.destroyfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
def test_new_follower_updates_followers_properly(self):
"""Verifies new followers show up in the followers list"""
URL=self.followerslist_url % (self.token, "test")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
for user in json_out['users']:
assert user['username'] != "brian"
URL=self.addfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
URL=self.followerslist_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
found_user_test = 0
for user in json_out['users']:
if user['username'] == "test": found_user_test += 1
assert found_user_test != 0
URL=self.destroyfriend_url % (self.token, "brian")
rv = self.app.get(URL)
json_out = json.loads(rv.data)
if __name__ == '__main__':
unittest.main(verbosity=2)