forked from PokemonGoF/PokemonGo-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_wrapper_test.py
123 lines (99 loc) · 4.74 KB
/
api_wrapper_test.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
import unittest
from mock import MagicMock, patch
from timeout_decorator import timeout, TimeoutError
from tests import FakeApi, get_fake_conf
from pgoapi import PGoApi
from pgoapi.exceptions import NotLoggedInException, ServerBusyOrOfflineException, NoPlayerPositionSetException, EmptySubrequestChainException
from pokemongo_bot.api_wrapper import ApiWrapper
class TestApiWrapper(unittest.TestCase):
def test_raises_not_logged_in_exception(self):
api = ApiWrapper(get_fake_conf())
api.set_position(*(42, 42, 0))
request = api.create_request()
request.get_inventory(test='awesome')
with self.assertRaises(NotLoggedInException):
request.call()
def test_api_call_with_no_requests_set(self):
request = ApiWrapper(get_fake_conf()).create_request()
with self.assertRaises(EmptySubrequestChainException):
request.call()
def test_api_wrong_request(self):
request = ApiWrapper(get_fake_conf()).create_request()
with self.assertRaises(AttributeError):
request.wrong_request()
def test_raises_no_player_position_set_exception(self):
request = ApiWrapper(get_fake_conf()).create_request()
request.get_inventory(test='awesome')
with self.assertRaises(NoPlayerPositionSetException):
request.call()
@patch('pokemongo_bot.api_wrapper.sleep')
def test_api_server_is_unreachable_raises_server_busy_or_offline_exception(self, sleep):
sleep.return_value = True # we don't need to really sleep
request = FakeApi().create_request('Wrong Value')
request.get_inventory()
# we expect an exception because the "server" isn't returning a valid response
with self.assertRaises(ServerBusyOrOfflineException):
request.call()
def test_mocked_call(self):
request = FakeApi().create_request(True)
request.is_response_valid = MagicMock(return_value=True)
request.get_inventory(test='awesome')
result = request.call()
self.assertTrue(result)
def test_return_value_is_not_valid(self):
api = FakeApi()
def returnRequest(ret_value):
request = api.create_request(ret_value)
request.get_inventory(test='awesome')
return request
wrong_return_values = [
None,
False,
{},
{'responses': {}},
{'status_code': 0},
{'responses': {'GET_INVENTORY_OR_NOT': {}}, 'status_code': 0}
]
for wrong in wrong_return_values:
request = returnRequest(wrong)
request_callers = request._pop_request_callers() # we can pop because we do no call
is_valid = request.is_response_valid(wrong, request_callers)
self.assertFalse(is_valid, 'return value {} is valid somehow ?'.format(wrong))
def test_return_value_is_valid(self):
request = FakeApi().create_request() # we set the return value below
request.get_inventory(test='awesome')
request_caller = request.request_callers[0] # only one request
self.assertEqual(request_caller.upper(), 'GET_INVENTORY')
good_return_value = {'responses': {request_caller.upper(): {}}, 'status_code': 0}
request._call.return_value = good_return_value
result = request.call()
self.assertEqual(result, good_return_value)
self.assertEqual(len(request.request_callers), 0, 'request_callers must be empty')
def test_multiple_requests(self):
request = FakeApi().create_request()
request.get_inventory(test='awesome')
request.fort_details()
good_return_value = {'responses': {'GET_INVENTORY': {}, 'FORT_DETAILS': {}}, 'status_code': 0}
request._call.return_value = good_return_value
result = request.call()
self.assertEqual(result, good_return_value)
@timeout(1)
def test_api_call_throttle_should_pass(self):
request = FakeApi().create_request()
request.is_response_valid = MagicMock(return_value=True)
request.requests_per_seconds = 5
for i in range(request.requests_per_seconds):
request.call()
@timeout(1) # expects a timeout
def test_api_call_throttle_should_fail(self):
request = FakeApi().create_request()
request.is_response_valid = MagicMock(return_value=True)
request.requests_per_seconds = 5
with self.assertRaises(TimeoutError):
for i in range(request.requests_per_seconds * 2):
request.call()
@patch('pokemongo_bot.api_wrapper.ApiRequest.is_response_valid')
def test_api_direct_call(self, mock_method):
mock_method.return_value = True
result = FakeApi().get_inventory()
self.assertEqual(result, 'mock return')