This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
testClient.py
217 lines (185 loc) · 7.71 KB
/
testClient.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
tests.testClient
----------------
"""
import json
import logging
import os
import unittest
from base64 import b64encode
import httplib2
import requests
from mock import Mock
from nose import SkipTest
from nose.tools import eq_
import marketplace
log = logging.getLogger('test.%s' % __name__)
# Preparing to mock the requests
OLD_POST = requests.post
OLD_PUT = requests.put
OLD_GET = requests.get
OLD_DELETE = requests.delete
MARKETPLACE_PORT = 443
MARKETPLACE_DOMAIN = 'marketplace-dev.allizom.org'
MARKETPLACE_PROTOCOL = 'https'
class Response:
def __init__(self, status_code, content=None):
self.status_code = status_code
self.content = content
class TestClient(unittest.TestCase):
def setUp(self):
self.marketplace = marketplace.Client(
domain=MARKETPLACE_DOMAIN,
port=MARKETPLACE_PORT,
protocol=MARKETPLACE_PROTOCOL,
consumer_key='consumer_key',
consumer_secret='consumer_secret')
def tearDown(self):
requests.post = OLD_POST
requests.put = OLD_PUT
requests.get = OLD_GET
requests.delete = OLD_DELETE
def test_init(self):
eq_(self.marketplace.domain, MARKETPLACE_DOMAIN)
eq_(self.marketplace.port, MARKETPLACE_PORT)
eq_(self.marketplace.protocol, MARKETPLACE_PROTOCOL)
def test_order_validation(self):
manifest_url = 'http://example.com/'
resp = {'id': 'abcd',
'manifest': manifest_url,
'processed': False,
'resource_uri': '/en-US/api/apps/validation/123/',
'valid': False,
'validation': ''}
requests.post = Mock(return_value=Response(201, resp))
response = self.marketplace.validate_manifest(manifest_url)
log.debug(response.content)
log.debug(response.status_code)
eq_(response.content['id'], 'abcd')
def test_get_validation_result(self):
resp = {'id': 'abcd',
'processed': False,
'resource_uri': '/en-US/api/apps/validation/123/',
'valid': False,
'validation': ''}
requests.get = Mock(return_value=Response(201, json.dumps(resp)))
# providing id by string
response = self.marketplace.get_manifest_validation_result('abcd')
content = json.loads(response.content)
eq_(content['id'], 'abcd')
def test_is_manifest_valid(self):
# not processed
resp = {'id': '123',
'processed': False,
'resource_uri': '/en-US/api/apps/validation/123/',
'valid': False,
'validation': ''}
requests.get = Mock(return_value=Response(200, json.dumps(resp)))
eq_(self.marketplace.is_manifest_valid(123), None)
# valid
resp = {'id': '123',
'processed': True,
'resource_uri': '/en-US/api/apps/validation/123/',
'valid': True,
'validation': ''}
requests.get = Mock(return_value=Response(200, json.dumps(resp)))
eq_(self.marketplace.is_manifest_valid(123), True)
# invalid
resp = {'id': 'abcd',
'processed': True,
'resource_uri': '/en-US/api/apps/validation/123/',
'valid': False,
'validation': {'validation': 'object'}}
requests.get = Mock(return_value=Response(200, json.dumps(resp)))
eq_(self.marketplace.is_manifest_valid(123), resp['validation'])
def test_create(self):
resp = {'categories': [],
'description': None,
'device_types': [],
'homepage': None,
'id': 1,
'manifest': '0a650e5e4c434b5cb60c5495c0d88a89',
'name': 'MozillaBall',
'premium_type': 'free',
'privacy_policy': None,
'resource_uri': '/en-US/api/apps/app/1/',
'slug': 'mozillaball',
'status': 0,
'summary': 'Exciting Open Web development action!',
'support_email': None,
'support_url': None}
requests.post = Mock(return_value=Response(201, json.dumps(resp)))
response = self.marketplace.create('abcd')
content = json.loads(response.content)
eq_(response.status_code, 201)
eq_(content['manifest'], '0a650e5e4c434b5cb60c5495c0d88a89')
def test_update(self):
requests.put = Mock(return_value=Response(202, ''))
response = self.marketplace.update(123, {
'name': 'MozillaBall',
'summary': 'Changed Summary',
'categories': ['Business', 'Game'],
'support_email': 'john@doe.com',
'device_types': ['phone',],
'privacy_policy': 'any',
'payment_type': 'free'})
eq_(response.status_code, 202)
assert not response.content
def test_status(self):
resp = {'categories': ['Business', 'Game'],
'description': None,
'device_types': ['phone',],
'homepage': None,
'id': 1,
'manifest': '0a650e5e4c434b5cb60c5495c0d88a89',
'name': 'MozillaBall',
'premium_type': 'free',
'privacy_policy': None,
'resource_uri': '/en-US/api/apps/app/1/',
'slug': 'mozillaball',
'status': 0,
'summary': 'Exciting Open Web development action!',
'support_email': 'john@doe.com',
'support_url': None}
requests.get = Mock(return_value=Response(200, json.dumps(resp)))
response = self.marketplace.status(1)
content = json.loads(response.content)
eq_(response.status_code, 200)
eq_(content['manifest'], '0a650e5e4c434b5cb60c5495c0d88a89')
def test_delete(self):
raise SkipTest()
requests.delete = Mock(return_value=Response(204, ''))
response = self.marketplace.delete(1)
eq_(response.status_code, 204)
assert not response.content
def test_delete_not_implemented(self):
self.assertRaises(NotImplementedError, self.marketplace.delete, 1)
def test_add_screenshot(self):
path = lambda *a: os.path.join(
os.path.dirname(os.path.abspath(__file__)), *a)
resp = {'filetype': 'image/png',
'thumbnail_url': 'https://marketplace-dev-cdn.allizom.org/img/'
'uploads/previews/thumbs/71/71761.png?'
'modified=1340899716',
'image_url': 'https://marketplace-dev-cdn.allizom.org/img/'
'uploads/previews/full/71/71761.png?'
'modified=1340899716',
'position': 1,
'id': 71761,
'resource_uri': '/en-US/api/apps/preview/71761'}
requests.post = Mock(return_value=Response(201, resp))
response = self.marketplace.create_screenshot(123, path('mozilla.jpg'))
eq_(response.status_code, 201)
eq_(response.content['position'], 1)
with open(path('mozilla.jpg')) as moz_file:
b64_file = b64encode(moz_file.read())
data = json.loads(requests.post.call_args[1]['data'])
eq_(data['position'], 1)
eq_(data['file']['data'], b64_file)
eq_(data['file']['type'], 'image/jpeg')
# create a screenshot with a jpeg image and not default position
self.marketplace.create_screenshot(123,
path('mozilla.jpg'), mimetype='image/jpeg', position=2)
data = json.loads(requests.post.call_args[1]['data'])
eq_(data['position'], 2)
eq_(data['file']['type'], 'image/jpeg')