forked from vtemian/buffpy
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_api.py
More file actions
98 lines (69 loc) · 2.71 KB
/
test_api.py
File metadata and controls
98 lines (69 loc) · 2.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
import json
from nose.tools import eq_, raises
from mock import MagicMock, patch
from buffpy.api import API
def test_api_get_request():
'''
Test simply api get request
'''
with patch('buffpy.api.OAuth2Session') as mocked_oauth2:
mocked_session = MagicMock()
mocked_response = MagicMock()
mocked_response.content = json.dumps({'status': 'ok'})
mocked_session.get.return_value = mocked_response
mocked_oauth2.return_value = mocked_session
api = API(client_id='1', client_secret='2', access_token='access_token')
api.get(url="hey")
mocked_session.get.assert_called_once_with(url='https://api.bufferapp.com/1/hey')
@raises(ValueError)
def test_api_get_request_no_access_token():
'''
Test simply api get request without access_token
'''
with patch('buffpy.api.OAuth2Session') as mocked_oauth2:
mocked_session = MagicMock()
mocked_session.access_token = None
mocked_oauth2.return_value = mocked_session
api = API(client_id='1', client_secret='2')
api.get(url="hey")
def test_api_post_request():
'''
Test simply api post request
'''
with patch('buffpy.api.OAuth2Session') as mocked_oauth2:
mocked_session = MagicMock()
mocked_response = MagicMock()
mocked_response.content = json.dumps({'status': 'ok'})
mocked_session.post.return_value = mocked_response
mocked_oauth2.return_value = mocked_session
api = API(client_id='1', client_secret='2', access_token='access_token')
api.post(url='hey', data='new=True')
headers = {'Content-Type':'application/x-www-form-urlencoded'}
mocked_session.post.assert_called_once_with(
url='https://api.bufferapp.com/1/hey', headers=headers, data='new=True')
@raises(ValueError)
def test_api_post_request_no_access_token():
'''
Test simply api post request without access_token
'''
with patch('buffpy.api.OAuth2Session') as mocked_oauth2:
mocked_session = MagicMock()
mocked_session.access_token = None
mocked_oauth2.return_value = mocked_session
api = API(client_id='1', client_secret='2', access_token='access_token')
api.post(url='hey', data='new=True')
def test_api_info():
'''
Test simple configuration retrieving
'''
with patch('buffpy.api.OAuth2Session') as mocked_oauth2:
mocked_session = MagicMock()
mocked_response = MagicMock()
mocked_response.content = json.dumps({'status': 'ok'})
mocked_session.get.return_value = mocked_response
mocked_oauth2.return_value = mocked_session
api = API(client_id='1', client_secret='2', access_token='access_token')
info = api.info
url = 'https://api.bufferapp.com/1/info/configuration.json'
mocked_session.get.assert_called_once_with(url=url)
eq_(info.status, 'ok')