forked from claymation/python-builtwith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
198 lines (146 loc) · 9.16 KB
/
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
import datetime
import unittest
from httpretty import HTTPretty, httprettified
import builtwith
API_VERSION_ONE = 1
API_VERSION_TWO = 2
API_VERSION_SEVEN = 7
UNSUPPORTED_API_VERSION = 3
TEST_EPOCH_TIME = 1346972400
TEST_DATETIME = datetime.datetime.utcfromtimestamp(TEST_EPOCH_TIME) # 2012-9-6
TEST_DATETIME_STRING = u'/Date(%s)/' % (TEST_EPOCH_TIME * 1000)
# Dates are named relative to the TEST_EPOCH_TIME
TEST_FULL_SCAN_DATE_BEFORE = datetime.date(2012, 5, 30)
TEST_FULL_SCAN_DATE_AFTER = datetime.date(2012, 10, 6)
TEST_TOPSITE_DATE = datetime.date(2012, 6, 19)
TEST_RESPONSE_JSON = {
u'Paths': [{u'Url': u'',
u'Domain': u'example.com',
u'SubDomain': u'',
u'Technologies': [{u'Name': u'HTML5 DocType',
u'FirstDetected': TEST_DATETIME_STRING,
u'Tag': u'docinfo',
u'Link': u'http://dev.w3.org/html5/spec/syntax.html#the-doctype',
u'LastDetected': TEST_DATETIME_STRING,
u'Description': u'The DOCTYPE is a required preamble for HTML5 websites.'},
{u'Name': u'Javascript',
u'FirstDetected': TEST_DATETIME_STRING,
u'Tag': u'docinfo',
u'Link': u'http://en.wikipedia.org/wiki/JavaScript',
u'LastDetected': TEST_DATETIME_STRING,
u'Description': u'JavaScript is a scripting language most often used for '
'client-side web development. Its proper name is ECMAScript, '
'though "JavaScript" is much more commonly used. The website '
'uses JavaScript.'}]},
{u'Url': u'',
u'Domain': u'example.com',
u'SubDomain': u'test',
u'Technologies': [{u'Name': u'HTML5 DocType',
u'FirstDetected': TEST_DATETIME_STRING,
u'Tag': u'docinfo',
u'Link': u'http://dev.w3.org/html5/spec/syntax.html#the-doctype',
u'LastDetected': TEST_DATETIME_STRING,
u'Description': u'The DOCTYPE is a required preamble for HTML5 websites.'},
{u'Name': u'Javascript',
u'FirstDetected': TEST_DATETIME_STRING,
u'Tag': u'docinfo',
u'Link': u'http://en.wikipedia.org/wiki/JavaScript',
u'LastDetected': TEST_DATETIME_STRING,
u'Description': u'JavaScript is a scripting language most often used for '
'client-side web development. Its proper name is ECMAScript, '
'though "JavaScript" is much more commonly used. The website '
'uses JavaScript.'}]}]}
class BuiltWithTests(unittest.TestCase):
@httprettified
def test_lookup_version_one(self):
bw = builtwith.BuiltWith('key')
HTTPretty.register_uri(HTTPretty.GET, builtwith.ENDPOINTS_BY_API_VERSION[API_VERSION_ONE], body='true')
result = bw.lookup('example.com')
qs = HTTPretty.last_request.querystring
self.assertEqual(qs.get('KEY'), ['key'])
self.assertEqual(qs.get('LOOKUP'), ['example.com'])
self.assertTrue(result)
@httprettified
def test_lookup_version_two(self):
bw = builtwith.BuiltWith('key', api_version=API_VERSION_TWO)
HTTPretty.register_uri(HTTPretty.GET, builtwith.ENDPOINTS_BY_API_VERSION[API_VERSION_TWO],
body='{"Paths": []}')
HTTPretty.register_uri(HTTPretty.GET, builtwith.ENDPOINTS_BY_API_VERSION[API_VERSION_TWO],
body='{"TOPSITE": "%s", "FULL": "%s"}' % (TEST_TOPSITE_DATE,
TEST_FULL_SCAN_DATE_BEFORE))
result = bw.lookup('example.com')
qs = HTTPretty.last_request.querystring
self.assertEqual(qs.get('KEY'), ['key'])
self.assertEqual(qs.get('LOOKUP'), ['example.com'])
self.assertTrue(result)
self.assertIsInstance(result, builtwith.BuiltWithDomainInfo)
self.assertDictEqual({'Paths': []}, result.api_response_json)
@httprettified
def test_lookup_version_seven(self):
bw = builtwith.BuiltWith('key', api_version=API_VERSION_SEVEN)
HTTPretty.register_uri(HTTPretty.GET, builtwith.ENDPOINTS_BY_API_VERSION[API_VERSION_SEVEN],
body='{"Results": [{"Result": {"Paths": []}}, {"Result": {"Paths": []}}]}')
HTTPretty.register_uri(HTTPretty.GET, builtwith.ENDPOINTS_BY_API_VERSION[API_VERSION_SEVEN],
body='{"TOPSITE": "%s", "FULL": "%s"}' % (TEST_TOPSITE_DATE,
TEST_FULL_SCAN_DATE_BEFORE))
result = bw.lookup(['example.com', 'example2.com'])
qs = HTTPretty.last_request.querystring
self.assertEqual(qs.get('KEY'), ['key'])
self.assertEqual(qs.get('LOOKUP'), ['example.com,example2.com'])
self.assertEqual(2, len(result))
self.assertIsInstance(result[0], builtwith.BuiltWithDomainInfo)
self.assertDictEqual({'Paths': []}, result[0].api_response_json)
self.assertIsInstance(result[1], builtwith.BuiltWithDomainInfo)
self.assertDictEqual({'Paths': []}, result[1].api_response_json)
@httprettified
def test_unsupported_version(self):
exception_raised = False
try:
builtwith.BuiltWith('key', api_version=UNSUPPORTED_API_VERSION)
except builtwith.UnsupportedApiVersion as e:
exception_raised = True
self.assertEqual(builtwith.VERSION_EXCEPTION_TEMPLATE % UNSUPPORTED_API_VERSION, str(e))
self.assertTrue(exception_raised)
def test_domain_info_object(self):
domain_info = builtwith.BuiltWithDomainInfo(TEST_RESPONSE_JSON)
self.assertListEqual(sorted([(u'example.com', u'', u''), (u'example.com', u'test', u'')]),
sorted(domain_info.available_urls()))
url_technologies = domain_info.get_technologies_by_url(domain='example.com', subdomain='', path='')
self.assertListEqual(sorted(['HTML5 DocType', 'Javascript']), sorted(url_technologies.list_technologies()))
js_info = url_technologies.get_technology_info('Javascript')
self.assertEqual(js_info['Name'], 'Javascript')
self.assertEqual(js_info['LastDetected'], TEST_DATETIME)
self.assertEqual(None, js_info.get('CurrentlyLive'))
html5_info = url_technologies.get_technology_info('HTML5 DocType')
self.assertEqual(html5_info['Name'], 'HTML5 DocType')
self.assertEqual(html5_info['LastDetected'], TEST_DATETIME)
self.assertEqual(None, html5_info.get('CurrentlyLive'))
def test_currently_live_fetching_with_scan_before(self):
domain_info = builtwith.BuiltWithDomainInfo(TEST_RESPONSE_JSON,
last_full_builtwith_scan_date=TEST_FULL_SCAN_DATE_BEFORE)
url_technologies = domain_info.get_technologies_by_url(domain='example.com', subdomain='', path='')
js_info = url_technologies.get_technology_info('Javascript')
self.assertEqual(js_info['Name'], 'Javascript')
self.assertEqual(js_info['LastDetected'], TEST_DATETIME)
self.assertTrue(js_info['CurrentlyLive'])
html5_info = url_technologies.get_technology_info('HTML5 DocType')
self.assertEqual(html5_info['Name'], 'HTML5 DocType')
self.assertEqual(html5_info['LastDetected'], TEST_DATETIME)
self.assertTrue(html5_info['CurrentlyLive'])
def test_currently_live_fetching_with_scan_after(self):
domain_info = builtwith.BuiltWithDomainInfo(TEST_RESPONSE_JSON,
last_full_builtwith_scan_date=TEST_FULL_SCAN_DATE_AFTER)
url_technologies = domain_info.get_technologies_by_url(domain='example.com', subdomain='', path='')
js_info = url_technologies.get_technology_info('Javascript')
self.assertEqual(js_info['Name'], 'Javascript')
self.assertEqual(js_info['LastDetected'], TEST_DATETIME)
self.assertFalse(js_info['CurrentlyLive'])
html5_info = url_technologies.get_technology_info('HTML5 DocType')
self.assertEqual(html5_info['Name'], 'HTML5 DocType')
self.assertEqual(html5_info['LastDetected'], TEST_DATETIME)
self.assertFalse(html5_info['CurrentlyLive'])
def test__convert_timestamp_to_utc_datetime(self):
converted_utc_datetime = builtwith._convert_timestamp_to_utc_datetime(TEST_DATETIME_STRING)
self.assertEqual(converted_utc_datetime, TEST_DATETIME)
converted_utc_datetime = builtwith._convert_timestamp_to_utc_datetime(TEST_EPOCH_TIME * 1000)
self.assertEqual(converted_utc_datetime, TEST_DATETIME)