-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bitly_api.py
64 lines (52 loc) · 1.67 KB
/
test_bitly_api.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
#!/usr/local/bin/python
"""
This is a py.test script
Example usage on Unix:
bitly-api-python $ BITLY_ACCESS_TOKEN=<accesstoken> nosetests
or 'export' the two environment variables prior to running nosetests
"""
import os
import sys
sys.path.append('../')
import bitly_api
BITLY_ACCESS_TOKEN = "BITLY_ACCESS_TOKEN"
def get_connection():
"""Create a Connection base on username and access token credentials"""
if BITLY_ACCESS_TOKEN not in os.environ:
raise ValueError("Environment variable '{}' required".format(BITLY_ACCESS_TOKEN))
access_token = os.getenv(BITLY_ACCESS_TOKEN)
bitly = bitly_api.Connection(access_token=access_token)
return bitly
def testApi():
bitly = get_connection()
data = bitly.shorten('http://google.com/')
assert data is not None
assert data['long_url'] == 'http://google.com/'
assert data['hash'] is not None
def testExpand():
bitly = get_connection()
data = bitly.expand(hash='test1_random_fjslfjieljfklsjflkas')
assert data is not None
assert len(data) == 1
assert data[0]['error'] == 'NOT_FOUND'
def testReferrer():
bitly = get_connection()
data = bitly.referrers(hash='a')
assert data is not None
assert len(data) > 1
def testProDomain():
bitly = get_connection()
test_data = {
'cnn.com': False,
'nyti.ms': True,
'g.co': False,
'j.mp': False,
'pep.si': True,
'http://pep.si': 'INVALID_BARE_DOMAIN',
}
for domain in test_data:
try:
result = bitly.pro_domain(domain)
assert result == test_data[domain], domain
except bitly_api.BitlyError, e:
assert str(e) == test_data[domain]