|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (c) 2016 dotzero <[email protected]> |
|
4
|
|
|
# |
|
5
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
# in the Software without restriction, including without limitation the rights |
|
8
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
# furnished to do so, subject to the following conditions: |
|
11
|
|
|
# |
|
12
|
|
|
# The above copyright notice and this permission notice shall be included |
|
13
|
|
|
# in all copies or substantial portions of the Software. |
|
14
|
|
|
# |
|
15
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
# SOFTWARE. |
|
22
|
|
|
|
|
23
|
|
|
import os |
|
24
|
|
|
import json |
|
25
|
|
|
|
|
26
|
|
|
try: |
|
27
|
|
|
# python3 |
|
28
|
|
|
from urllib.parse import urlencode |
|
29
|
|
|
except ImportError: # pragma: no cover |
|
30
|
|
|
# python2 |
|
31
|
|
|
from urllib import urlencode |
|
32
|
|
|
|
|
33
|
|
|
cwd = os.path.dirname(os.path.abspath(__file__)) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class MockRequest(object): |
|
37
|
|
|
def __init__(self): |
|
38
|
|
|
self.registry = {} |
|
39
|
|
|
self.fixture_ok = { |
|
40
|
|
|
'ok': 1, |
|
41
|
|
|
'server_time': '2016-04-15T13:12:45+03:00' |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
def __call__(self, *args, **kwargs): |
|
45
|
|
|
return self._request(*args, **kwargs) |
|
46
|
|
|
|
|
47
|
|
|
def register_uri(self, method, url, fixture=None): |
|
48
|
|
|
key = '%s:%s' % (method, url) |
|
49
|
|
|
if fixture is not None: |
|
50
|
|
|
with open(os.path.join(cwd, 'fixtures', fixture)) as f: |
|
51
|
|
|
self.registry[key] = json.load(f) |
|
52
|
|
|
else: |
|
53
|
|
|
self.registry[key] = self.fixture_ok |
|
54
|
|
|
|
|
55
|
|
|
def _request(self, url, method='GET', data=None): |
|
56
|
|
|
if data is not None: |
|
57
|
|
|
data = urlencode(data) |
|
58
|
|
|
if method in ['GET', 'DELETE']: |
|
59
|
|
|
url = url + '?' + data |
|
60
|
|
|
data = None |
|
61
|
|
|
|
|
62
|
|
|
key = '%s:%s' % (method, url) |
|
63
|
|
|
if key not in self.registry: |
|
64
|
|
|
raise Exception('Mock not found ' + key) |
|
65
|
|
|
|
|
66
|
|
|
return self.registry[key] |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
class BaseTest(object): |
|
70
|
|
|
"""This object represents a Base test and its sets of functions.""" |
|
71
|
|
|
|
|
72
|
|
|
def __init__(self, *args, **kwargs): |
|
73
|
|
|
super(BaseTest, self).__init__(*args, **kwargs) |
|
74
|
|
|
|
|
75
|
|
|
@staticmethod |
|
76
|
|
|
def is_json(string): |
|
77
|
|
|
try: |
|
78
|
|
|
json.loads(string) |
|
79
|
|
|
except ValueError: |
|
80
|
|
|
return False |
|
81
|
|
|
|
|
82
|
|
|
return True |
|
83
|
|
|
|
|
84
|
|
|
@staticmethod |
|
85
|
|
|
def is_dict(dictionary): |
|
86
|
|
|
if isinstance(dictionary, dict): |
|
87
|
|
|
return True |
|
88
|
|
|
|
|
89
|
|
|
return False |
|
90
|
|
|
|
|
91
|
|
|
@staticmethod |
|
92
|
|
|
def to_json(string): |
|
93
|
|
|
try: |
|
94
|
|
|
data = json.loads(string) |
|
95
|
|
|
except ValueError: |
|
96
|
|
|
return False |
|
97
|
|
|
|
|
98
|
|
|
return data |
|
99
|
|
|
|