1"""Base support for API unit tests."""
2
3from __future__ import unicode_literals
4
5from rbtools.api.transport import Transport
6from rbtools.testing import TestCase
7
8
9class MockResponse(object):
10    """A mock up for a response from urllib2."""
11
12    def __init__(self, code, headers, body):
13        """Create a new MockResponse."""
14        self.code = code
15        self.headers = headers
16        self.body = body
17
18        if self.body:
19            self.headers['Content-Length'] = len(body)
20
21            if 'Content-Type' not in self.headers:
22                self.headers['Content-Type'] = 'text/plain'
23
24    def info(self):
25        """Get the response headers."""
26        return self.headers
27
28    def read(self):
29        """Get the response body."""
30        return self.body
31
32    def getcode(self):
33        """Get the response code."""
34        return self.code
35
36
37class MockTransport(Transport):
38    """Mock transport which returns HttpRequests without executing them"""
39
40    def __init__(self):
41        pass
42
43
44class TestWithPayloads(TestCase):
45    transport = MockTransport()
46
47    item_payload = {
48        'resource_token': {
49            'field1': 1,
50            'field2': 2,
51            'nested_field': {
52                'nested1': 1,
53                'nested2': 2,
54            },
55            'nested_list': [
56                {
57                    'href': 'http://localhost:8080/api/',
58                    'method': 'GET',
59                },
60                {
61                    'href': 'http://localhost:8080/api/',
62                    'method': 'GET',
63                },
64            ],
65            'link_field': {
66                'href': 'http://localhost:8080/api/',
67                'method': 'GET',
68                'title': 'Link Field',
69            },
70        },
71        'links': {
72            'self': {
73                'href': 'http://localhost:8080/api/',
74                'method': 'GET',
75            },
76            'update': {
77                'href': 'http://localhost:8080/api/',
78                'method': 'PUT',
79            },
80            'delete': {
81                'href': 'http://localhost:8080/api/',
82                'method': 'DELETE',
83            },
84            'other_link': {
85                'href': 'http://localhost:8080/api/',
86                'method': 'GET',
87            },
88        },
89        'stat': 'ok',
90    }
91
92    list_payload = {
93        'resource_token': [
94            {
95                'field1': 1,
96                'field2': 2,
97                'links': {
98                    'self': {
99                        'href': 'http://localhost:8080/api/',
100                        'method': 'GET',
101                    },
102                },
103                'name': 'testname1',
104                'path': 'testpath1',
105                'tool': 'Git',
106            },
107            {
108                'field1': 1,
109                'field2': 2,
110                'links': {
111                    'self': {
112                        'href': 'http://localhost:8080/api/',
113                        'method': 'GET',
114                    },
115                },
116                'name': 'testname2',
117                'path': 'testpath2',
118                'tool': 'Git',
119            },
120        ],
121        'links': {
122            'self': {
123                'href': 'http://localhost:8080/api/',
124                'method': 'GET',
125            },
126            'create': {
127                'href': 'http://localhost:8080/api/',
128                'method': 'POST',
129            },
130            'other_link': {
131                'href': 'http://localhost:8080/api/',
132                'method': 'GET',
133            },
134        },
135        'total_results': 10,
136        'stat': 'ok',
137    }
138
139    list_payload_no_repos = {
140        'resource_token': [],
141        'links': {
142            'self': {
143                'href': 'http://localhost:8080/api/',
144                'method': 'GET',
145            },
146            'create': {
147                'href': 'http://localhost:8080/api/',
148                'method': 'POST',
149            },
150            'other_link': {
151                'href': 'http://localhost:8080/api/',
152                'method': 'GET',
153            },
154        },
155        'total_results': 10,
156        'stat': 'ok',
157    }
158
159    count_payload = {
160        'count': 10,
161        'stat': 'ok',
162    }
163
164    root_payload = {
165        'uri_templates': {
166            'reviews': ('http://localhost:8080/api/review-requests/'
167                        '{review_request_id}/reviews/'),
168        },
169        'links': {
170            'self': {
171                'href': 'http://localhost:8080/api/',
172                'method': 'GET',
173            },
174            'groups': {
175                'href': 'http://localhost:8080/api/groups',
176                'method': 'GET',
177            },
178            'repositories': {
179                'href': 'http://localhost:8080/api/repositories/',
180                'method': 'GET',
181            }
182        },
183        'stat': 'ok',
184    }
185