Merge lp:~elopio/u1-test-utils/refactor_api into lp:u1-test-utils

Proposed by Leo Arias
Status: Merged
Approved by: Ricardo Kirkner
Approved revision: 66
Merged at revision: 65
Proposed branch: lp:~elopio/u1-test-utils/refactor_api
Merge into: lp:u1-test-utils
Diff against target: 312 lines (+74/-65)
7 files modified
u1testutils/pay/__init__.py (+1/-1)
u1testutils/pay/api.py (+2/-3)
u1testutils/pay/selftests/unit/test_pay_api_client.py (+7/-7)
u1testutils/sso/api.py (+33/-28)
u1testutils/sso/data.py (+5/-4)
u1testutils/sso/selftests/unit/test_data.py (+5/-5)
u1testutils/sso/selftests/unit/test_sso_api_client.py (+21/-17)
To merge this branch: bzr merge lp:~elopio/u1-test-utils/refactor_api
Reviewer Review Type Date Requested Status
Ricardo Kirkner (community) Approve
Review via email: mp+163246@code.launchpad.net

Commit message

Renamed the api helpers and refactored the SSO one to receive a server url.

Description of the change

When running SSO tests against staging or production, the django open_id server variable is set to local.
This refactor makes the SSO API helper look more like the pay one, and allows to pass an arbitrary server on its constructors.

Here I also renamed the helpers from client to api, as suggested by Ricardo.

To post a comment you must log in.
Revision history for this message
Leo Arias (elopio) wrote :

----------------------------------------------------------------------
Ran 61 tests in 150.203s

OK
Totals: ran(61), skipped(0), errors(0), failures(0)

Revision history for this message
Ricardo Kirkner (ricardokirkner) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'u1testutils/pay/__init__.py'
2--- u1testutils/pay/__init__.py 2013-03-27 18:36:28 +0000
3+++ u1testutils/pay/__init__.py 2013-05-10 01:35:30 +0000
4@@ -20,4 +20,4 @@
5
6 import data
7 import environment
8-from client import APIClient
9+from api import APIClient
10
11=== renamed file 'u1testutils/pay/client.py' => 'u1testutils/pay/api.py'
12--- u1testutils/pay/client.py 2013-05-09 18:26:20 +0000
13+++ u1testutils/pay/api.py 2013-05-10 01:35:30 +0000
14@@ -22,9 +22,8 @@
15 class APIClient(object):
16 """API client helper for payment tests.
17
18- It extends the PaymentServiceAPI to take the credentials from the Django
19- configuration file and to make it easier for the tests to call the API
20- methods.
21+ It takes the credentials from the Django configuration file to make it
22+ easier for the tests to call the API methods.
23
24 """
25
26
27=== modified file 'u1testutils/pay/selftests/unit/test_pay_api_client.py'
28--- u1testutils/pay/selftests/unit/test_pay_api_client.py 2013-05-09 18:26:20 +0000
29+++ u1testutils/pay/selftests/unit/test_pay_api_client.py 2013-05-10 01:35:30 +0000
30@@ -18,7 +18,7 @@
31 import payclient
32
33 import u1testutils.django
34-from u1testutils.pay import client
35+from u1testutils.pay import api
36 from u1testutils.pay import data as pay_data
37 from u1testutils.sso import data as sso_data
38
39@@ -26,33 +26,33 @@
40 class APIClientTestCase(unittest.TestCase):
41
42 def test_get_api_client(self):
43- api_client = client.APIClient()
44+ api_client = api.APIClient()
45 self.assertTrue(
46 isinstance(api_client.client, payclient.PaymentServiceAPI))
47
48 def test_get_api_client_with_default_url(self):
49- api_client = client.APIClient()
50+ api_client = api.APIClient()
51 self.assertEquals(
52 api_client.client._service_root, 'http://localhost:8000/api/2.0/')
53
54 def test_get_api_client_with_settings_url(self):
55 with u1testutils.django.patch_settings(
56 PAY_SERVER_URL='http://settings.pay.local:8000'):
57- api_client = client.APIClient()
58+ api_client = api.APIClient()
59 self.assertEquals(
60 api_client.client._service_root,
61 'http://settings.pay.local:8000/api/2.0/')
62
63 def test_get_api_client_with_argument_url(self):
64 pay_server_url = 'http://argument.pay.local:8000'
65- api_client = client.APIClient(pay_server_url)
66+ api_client = api.APIClient(pay_server_url)
67 self.assertEquals(
68 api_client.client._service_root,
69 'http://argument.pay.local:8000/api/2.0/')
70
71 def test_get_api_client_with_argument_url_and_trailing_slash(self):
72 pay_server_url = 'http://argument.pay.local:8000/'
73- api_client = client.APIClient(pay_server_url)
74+ api_client = api.APIClient(pay_server_url)
75 self.assertEquals(
76 api_client.client._service_root,
77 'http://argument.pay.local:8000/api/2.0/')
78@@ -69,7 +69,7 @@
79 def test_new_credit_card_added(self):
80 shop_id = 'TEST'
81 make_unattended_default = True
82- api_client = client.APIClient()
83+ api_client = api.APIClient()
84 with mock.patch('payclient.PaymentServiceAPI.new_credit_card') \
85 as mock_api_call:
86 api_client.add_new_credit_card(
87
88=== renamed file 'u1testutils/sso/client.py' => 'u1testutils/sso/api.py'
89--- u1testutils/sso/client.py 2013-04-22 20:01:40 +0000
90+++ u1testutils/sso/api.py 2013-05-10 01:35:30 +0000
91@@ -17,31 +17,36 @@
92 from u1testutils.sso import environment
93
94
95-def get_api_client():
96- base_url = environment.get_sso_base_url()
97- client = V2ApiClient(base_url + '/api/v2')
98- return client
99-
100-
101-def get_account_openid(email, password, token_name):
102- client = get_api_client()
103- response = client.login(email=email, password=password,
104- token_name=token_name)
105- data = response.json()
106- openid = data.get('consumer_key')
107- return openid
108-
109-
110-def create_new_account(user, captcha_id=None, captcha_solution=None):
111- client = get_api_client()
112- data = dict(
113- email=user.email,
114- password=user.password,
115- displayname=user.full_name
116- )
117- if captcha_id is not None:
118- data['captcha_id'] = captcha_id
119- if captcha_solution is not None:
120- data['captcha_solution'] = captcha_solution
121- response = client.register(data)
122- return response.status_code == 201
123+class APIClient(object):
124+ """API client helper for identity tests."""
125+
126+ def __init__(self, sso_server_url=None):
127+ sso_server_url = self._get_sso_server_url(sso_server_url)
128+ self.client = V2ApiClient(sso_server_url + '/api/v2')
129+
130+ def _get_sso_server_url(self, sso_server_url=None):
131+ if sso_server_url is None:
132+ sso_server_url = environment.get_sso_base_url()
133+ else:
134+ sso_server_url = sso_server_url.rstrip('/')
135+ return sso_server_url
136+
137+ def get_account_openid(self, email, password, token_name):
138+ response = self.client.login(
139+ email=email, password=password, token_name=token_name)
140+ data = response.json()
141+ openid = data.get('consumer_key')
142+ return openid
143+
144+ def create_new_account(self, user, captcha_id=None, captcha_solution=None):
145+ data = dict(
146+ email=user.email,
147+ password=user.password,
148+ displayname=user.full_name
149+ )
150+ if captcha_id is not None:
151+ data['captcha_id'] = captcha_id
152+ if captcha_solution is not None:
153+ data['captcha_solution'] = captcha_solution
154+ response = self.client.register(data)
155+ return response.status_code == 201
156
157=== modified file 'u1testutils/sso/data.py'
158--- u1testutils/sso/data.py 2013-05-03 15:55:59 +0000
159+++ u1testutils/sso/data.py 2013-05-10 01:35:30 +0000
160@@ -16,7 +16,7 @@
161
162 from django.conf import settings
163
164-from u1testutils.sso import client
165+from u1testutils.sso import api
166
167
168 class User(object):
169@@ -34,9 +34,10 @@
170 def openid(self):
171 if self._openid is None:
172 token_name = "token-%s" % self.email
173- self._openid = client.get_account_openid(email=self.email,
174- password=self.password,
175- token_name=token_name)
176+ api_client = api.APIClient()
177+ self._openid = api_client.get_account_openid(
178+ email=self.email, password=self.password,
179+ token_name=token_name)
180 return self._openid
181
182 @classmethod
183
184=== modified file 'u1testutils/sso/selftests/unit/test_data.py'
185--- u1testutils/sso/selftests/unit/test_data.py 2013-05-03 16:17:38 +0000
186+++ u1testutils/sso/selftests/unit/test_data.py 2013-05-10 01:35:30 +0000
187@@ -19,7 +19,7 @@
188 from mock import patch
189
190 import u1testutils.django
191-from u1testutils.sso import data
192+from u1testutils.sso import api, data
193
194
195 class DataTestCase(unittest.TestCase):
196@@ -63,15 +63,15 @@
197 self.assertIsNone(self.user._openid)
198
199 def test_openid_for_valid_account(self):
200- with patch('u1testutils.sso.data.client') as mock_client:
201- mock_client.get_account_openid.return_value = 'foo1234'
202+ with patch.object(api.APIClient, 'get_account_openid') as mock_open_id:
203+ mock_open_id.return_value = 'foo1234'
204
205 self.assertEqual(self.user.openid, 'foo1234')
206 self.assertEqual(self.user._openid, 'foo1234')
207
208 def test_openid_for_invalid_account(self):
209- with patch('u1testutils.sso.data.client') as mock_client:
210- mock_client.get_account_openid.return_value = None
211+ with patch.object(api.APIClient, 'get_account_openid') as mock_open_id:
212+ mock_open_id.return_value = None
213
214 self.assertEqual(self.user.openid, None)
215 self.assertIsNone(self.user._openid, None)
216
217=== renamed file 'u1testutils/sso/selftests/unit/test_client.py' => 'u1testutils/sso/selftests/unit/test_sso_api_client.py'
218--- u1testutils/sso/selftests/unit/test_client.py 2013-04-22 20:01:02 +0000
219+++ u1testutils/sso/selftests/unit/test_sso_api_client.py 2013-05-10 01:35:30 +0000
220@@ -16,23 +16,21 @@
221
222 from mock import Mock, patch
223
224-from u1testutils.sso import client, data
225+from u1testutils.sso import api, data
226 from ssoclient.v2 import V2ApiClient
227
228
229 class ClientTestCase(unittest.TestCase):
230
231- base_namespace = 'u1testutils.sso.client'
232+ base_namespace = 'u1testutils.sso.api'
233
234 def test_get_api_client(self):
235- mock_get_base_url = Mock(return_value='http://localhost:8000')
236- with patch(self.base_namespace + '.environment.get_sso_base_url',
237- mock_get_base_url):
238- api_client = client.get_api_client()
239+ api_client = api.APIClient('http://localhost:8000')
240
241- self.assertTrue(isinstance(api_client, V2ApiClient))
242- self.assertEqual(api_client.session.endpoint,
243- 'http://localhost:8000/api/v2/')
244+ self.assertTrue(isinstance(api_client.client, V2ApiClient))
245+ self.assertEqual(
246+ api_client.client.session.endpoint,
247+ 'http://localhost:8000/api/v2/')
248
249 def test_get_account_openid(self):
250 mock_login = Mock()
251@@ -45,8 +43,9 @@
252 }
253
254 with patch(self.base_namespace + '.V2ApiClient.login', mock_login):
255- openid = client.get_account_openid('email', 'password',
256- 'token_name')
257+ api_client = api.APIClient()
258+ openid = api_client.get_account_openid(
259+ 'email', 'password', 'token_name')
260
261 self.assertEqual(openid, 'oid1234')
262
263@@ -59,8 +58,9 @@
264 }
265
266 with patch(self.base_namespace + '.V2ApiClient.login', mock_login):
267- openid = client.get_account_openid('email', 'password',
268- 'token_name')
269+ api_client = api.APIClient()
270+ openid = api_client.get_account_openid(
271+ 'email', 'password', 'token_name')
272
273 self.assertIsNone(openid)
274
275@@ -70,7 +70,8 @@
276 mock_register.return_value.status_code = 201
277 client_register = self.base_namespace + '.V2ApiClient.register'
278 with patch(client_register, mock_register):
279- created = client.create_new_account(user)
280+ api_client = api.APIClient()
281+ created = api_client.create_new_account(user)
282 self.assertTrue(created)
283
284 def test_create_new_account_failed(self):
285@@ -79,7 +80,8 @@
286 mock_register.return_value.status_code = 400
287 client_register = self.base_namespace + '.V2ApiClient.register'
288 with patch(client_register, mock_register):
289- created = client.create_new_account(user)
290+ api_client = api.APIClient()
291+ created = api_client.create_new_account(user)
292 self.assertFalse(created)
293
294 def test_create_new_account_without_captcha(self):
295@@ -91,7 +93,8 @@
296 )
297 client_register = self.base_namespace + '.V2ApiClient.register'
298 with patch(client_register) as mock_register:
299- client.create_new_account(user)
300+ api_client = api.APIClient()
301+ api_client.create_new_account(user)
302 mock_register.assert_called_once_with(expected_data)
303
304 def test_create_new_account_with_captcha(self):
305@@ -105,5 +108,6 @@
306 )
307 client_register = self.base_namespace + '.V2ApiClient.register'
308 with patch(client_register) as mock_register:
309- client.create_new_account(user, 'Test id', 'Test solution')
310+ api_client = api.APIClient()
311+ api_client.create_new_account(user, 'Test id', 'Test solution')
312 mock_register.assert_called_once_with(expected_data)

Subscribers

People subscribed via source and target branches

to all changes: