-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanity_test.py
More file actions
executable file
·150 lines (119 loc) · 4.71 KB
/
sanity_test.py
File metadata and controls
executable file
·150 lines (119 loc) · 4.71 KB
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
#!/usr/bin/env python3
"""
An assortment of tests for each service to make sure the API isn't broken.
For each service, hit every endpoint and make sure it returns the
proper HTTP status code. Does not check content of response other
than status code.
"""
import json
import sys
import urllib.request
from unichat.translators import GroupMe
from unichat.util import print_err
def main():
has_config = len(sys.argv) > 1
if has_config:
config = open(sys.argv[1])
else:
print_err('WARNING: No config file provided. Tests requiring '
'authentication may fail.')
token = ''
summary = []
for test in [test_dummy,test_groupme, test_slack]:
if has_config:
token = config.readline().strip()
service, oks, fails, errors = test(token)
summary.append('{:<12} {} OK, {} FAIL, {} ERRORS'.format(
service + ':', oks, fails, errors))
for s in summary:
print_err(s)
def test_dummy(token):
endpoints = [
'http://localhost:8000/dummy/conversations',
'http://localhost:8000/dummy/conversations/42',
'http://localhost:8000/dummy/conversations/42/users',
'http://localhost:8000/dummy/conversations/42/messages'
]
oks, fails, errors = 0, 0, 0
for url in endpoints:
oks, fails, errors, _ = test_url(url, oks, fails, errors)
return ('dummy', oks, fails, errors)
def test_groupme(token):
primary_endpoint = 'http://localhost:8000/groupme/conversations?token={}'
endpoints = [
'http://localhost:8000/groupme/conversations/{}?token={}',
'http://localhost:8000/groupme/conversations/{}/users?token={}',
'http://localhost:8000/groupme/conversations/{}/messages?token={}'
]
oks, fails, errors = 0, 0, 0
oks, fails, errors, data = test_url(primary_endpoint.format(token),
oks, fails, errors)
if data is None or 'conversations' not in data:
# Whelp, can't do anything else.
return ('groupme', oks, fails, errors)
data = json.loads(data)
# Because the underlying groupme implementation behaves differently between
# direct messages (dms) and group chats, (groups), we test endpoints twice,
# for both types of conversations.
# It's unlikely the user has no DMs or no groups. This is just a test
# script, so I'm not going to bother checking that edge case..
conversations = data['conversations']
chat = conversations[0]['id']
# This is cheating, but necessary
first_dm_status = GroupMe._is_direct_message(chat)
for url in endpoints:
url = url.format(chat, token)
oks, fails, errors, _ = test_url(url, oks, fails, errors)
for chat in data['conversations']:
# If the first was a group, find a dm. If it was a dm, find a group.
chat = chat['id']
if GroupMe._is_direct_message(chat) == first_dm_status:
continue
# Rerun tests on other conversation type
for url in endpoints:
url = url.format(chat, token)
oks, fails, errors, _ = test_url(url, oks, fails, errors)
break
return ('groupme', oks, fails, errors)
def test_slack(token):
primary_endpoint = 'http://localhost:8000/slack/conversations?token={}'
endpoints = [
'http://localhost:8000/slack/conversations/{}?token={}',
'http://localhost:8000/slack/conversations/{}/users?token={}',
'http://localhost:8000/slack/conversations/{}/messages?token={}'
]
oks, fails, errors = 0, 0, 0
oks, fails, errors, data = test_url(primary_endpoint.format(token),
oks, fails, errors)
# Get some conversations to test
if data is None or 'conversations' not in data:
# Whelp, can't do anything else.
return ('slack', oks, fails, errors)
data = json.loads(data)
conversations = data['conversations']
chat = conversations[0]['id']
for url in endpoints:
url = url.format(chat, token)
oks, fails, errors, _ = test_url(url, oks, fails, errors)
return ('slack', oks, fails, errors)
def test_url(url, oks, fails, errs):
print(url)
try:
response = urllib.request.urlopen(url)
data = json.dumps(json.loads(response.read().decode('utf-8')),
indent=4)
print(data)
print()
if response.getcode() != 200:
fails += 1
else:
oks += 1
return oks, fails, errs, data
except urllib.request.HTTPError as e:
print_err('{} returned by {}'.format(e, url))
fails += 1
except Exception as e:
print_err('{} - {}'.format(e, url))
errs += 1
return oks, fails, errs, None
main()