|
1 | | -# -*- coding: utf8 -*- |
| 1 | +# -*- coding: utf-8 -*- |
2 | 2 | from cStringIO import StringIO |
3 | 3 | from datetime import datetime, timedelta |
4 | 4 | from decimal import Decimal |
@@ -247,63 +247,81 @@ def test_check_paypal_id(urlopen_mock): |
247 | 247 | eq_(val, (True, None)) |
248 | 248 |
|
249 | 249 |
|
| 250 | +def test_nvp(): |
| 251 | + eq_(paypal._nvp_dump({'foo': 'bar'}), 'foo=bar') |
| 252 | + eq_(paypal._nvp_dump({'foo': 'ba r'}), 'foo=ba%20r') |
| 253 | + eq_(paypal._nvp_dump({'foo': 'bar', 'bar': 'foo'}), 'bar=foo&foo=bar') |
| 254 | + eq_(paypal._nvp_dump({'foo': ['bar', 'baa']}), 'foo(0)=bar&foo(1)=baa') |
| 255 | + |
| 256 | + |
250 | 257 | @mock.patch('paypal._call') |
251 | 258 | @mock.patch.object(settings, 'PAYPAL_PERMISSIONS_URL', 'something') |
252 | 259 | class TestRefundPermissions(amo.tests.TestCase): |
253 | 260 |
|
254 | 261 | def setUp(self): |
255 | 262 | self.addon = Addon(type=amo.ADDON_EXTENSION, slug='foo') |
256 | 263 |
|
257 | | - def test_refund_permissions_url(self, _call): |
| 264 | + def test_get_permissions_url(self, _call): |
258 | 265 | """ |
259 | | - `paypal_refund_permission_url` returns an URL for PayPal's |
| 266 | + `paypal_get_permission_url` returns an URL for PayPal's |
260 | 267 | permissions request service containing the token PayPal gives |
261 | 268 | us. |
262 | 269 | """ |
263 | 270 | _call.return_value = {'token': 'foo'} |
264 | | - assert 'foo' in paypal.refund_permission_url(self.addon) |
| 271 | + assert 'foo' in paypal.get_permission_url(self.addon, '', []) |
265 | 272 |
|
266 | | - def test_refund_permissions_url_settings(self, _call): |
| 273 | + def test_get_permissions_url_settings(self, _call): |
267 | 274 | settings.PAYPAL_PERMISSIONS_URL = '' |
268 | | - assert not paypal.refund_permission_url(self.addon) |
| 275 | + assert not paypal.get_permission_url(self.addon, '', []) |
269 | 276 |
|
270 | | - def test_refund_permissions_url_malformed(self, _call): |
| 277 | + def test_get_permissions_url_malformed(self, _call): |
271 | 278 | _call.side_effect = paypal.PaypalError(id='580028') |
272 | | - assert 'wont-work' in paypal.refund_permission_url(self.addon) |
| 279 | + assert 'wont-work' in paypal.get_permission_url(self.addon) |
273 | 280 |
|
274 | | - def test_refund_permissions_url_error(self, _call): |
| 281 | + def test_get_permissions_url_error(self, _call): |
275 | 282 | _call.side_effect = paypal.PaypalError |
276 | 283 | with self.assertRaises(paypal.PaypalError): |
277 | | - paypal.refund_permission_url(self.addon) |
| 284 | + paypal.get_permission_url(self.addon, '', []) |
| 285 | + |
| 286 | + def test_get_permissions_url_scope(self, _call): |
| 287 | + _call.return_value = {'token': 'foo'} |
| 288 | + paypal.get_permission_url(self.addon, '', ['REFUND', 'FOO']) |
| 289 | + eq_(_call.call_args[0][1]['scope'], ['REFUND', 'FOO']) |
278 | 290 |
|
279 | | - def test_check_refund_permission_fail(self, _call): |
| 291 | + def test_check_permission_fail(self, _call): |
280 | 292 | """ |
281 | 293 | `check_paypal_refund_permission` returns False if PayPal |
282 | 294 | doesn't put 'REFUND' in the permissions response. |
283 | 295 | """ |
284 | 296 | _call.return_value = {'scope(0)': 'HAM_SANDWICH'} |
285 | | - assert not paypal.check_refund_permission('foo') |
| 297 | + assert not paypal.check_permission('foo', ['REFUND']) |
286 | 298 |
|
287 | | - def test_check_refund_permission(self, _call): |
| 299 | + def test_check_permission(self, _call): |
288 | 300 | """ |
289 | 301 | `check_paypal_refund_permission` returns True if PayPal |
290 | 302 | puts 'REFUND' in the permissions response. |
291 | 303 | """ |
292 | 304 | _call.return_value = {'scope(0)': 'REFUND'} |
293 | | - eq_(paypal.check_refund_permission('foo'), True) |
| 305 | + eq_(paypal.check_permission('foo', ['REFUND']), True) |
294 | 306 |
|
295 | | - def test_check_refund_permission_error(self, _call): |
| 307 | + def test_check_permission_error(self, _call): |
296 | 308 | _call.side_effect = paypal.PaypalError |
297 | | - assert not paypal.check_refund_permission('oh-noes') |
| 309 | + assert not paypal.check_permission('oh-noes', ['REFUND']) |
298 | 310 |
|
299 | | - def test_check_refund_permission_settings(self, _call): |
| 311 | + def test_check_permission_settings(self, _call): |
300 | 312 | settings.PAYPAL_PERMISSIONS_URL = '' |
301 | | - assert not paypal.check_refund_permission('oh-noes') |
| 313 | + assert not paypal.check_permission('oh-noes', ['REFUND']) |
302 | 314 |
|
303 | 315 | def test_get_permissions_token(self, _call): |
304 | 316 | _call.return_value = {'token': 'FOO'} |
305 | 317 | eq_(paypal.get_permissions_token('foo', ''), 'FOO') |
306 | 318 |
|
| 319 | + def test_get_permissions_subset(self, _call): |
| 320 | + _call.return_value = {'scope(0)': 'REFUND', 'scope(1)': 'HAM'} |
| 321 | + eq_(paypal.check_permission('foo', ['REFUND', 'HAM']), True) |
| 322 | + eq_(paypal.check_permission('foo', ['REFUND', 'JAM']), False) |
| 323 | + eq_(paypal.check_permission('foo', ['REFUND']), True) |
| 324 | + |
307 | 325 |
|
308 | 326 | good_refund_string = ( |
309 | 327 | 'refundInfoList.refundInfo(0).receiver.amount=123.45' |
@@ -415,3 +433,55 @@ def test_preapproval_url(self, _call): |
415 | 433 | url = paypal.get_preapproval_url('foo') |
416 | 434 | assert (url.startswith(settings.PAYPAL_CGI_URL) and |
417 | 435 | url.endswith('foo')), 'Incorrect URL returned' |
| 436 | + |
| 437 | + |
| 438 | +# This data is truncated |
| 439 | +good_personal_basic = { |
| 440 | + 'response.personalData(0).personalDataKey': |
| 441 | + 'http://axschema.org/contact/country/home', |
| 442 | + 'response.personalData(0).personalDataValue': 'US', |
| 443 | + 'response.personalData(1).personalDataValue': 'batman@gmail.com', |
| 444 | + 'response.personalData(1).personalDataKey': |
| 445 | + 'http://axschema.org/contact/email', |
| 446 | + 'response.personalData(2).personalDataValue': 'man'} |
| 447 | + |
| 448 | +good_personal_advanced = { |
| 449 | + 'response.personalData(0).personalDataKey': |
| 450 | + 'http://schema.openid.net/contact/street1', |
| 451 | + 'response.personalData(0).personalDataValue': '1 Main St', |
| 452 | + 'response.personalData(1).personalDataKey': |
| 453 | + 'http://schema.openid.net/contact/street2', |
| 454 | + 'response.personalData(2).personalDataValue': 'San Jose', |
| 455 | + 'response.personalData(2).personalDataKey': |
| 456 | + 'http://axschema.org/contact/city/home'} |
| 457 | + |
| 458 | + |
| 459 | +@mock.patch('paypal._call') |
| 460 | +class TestPersonalLookup(amo.tests.TestCase): |
| 461 | + |
| 462 | + def setUp(self): |
| 463 | + self.data = {'GetBasicPersonalData': good_personal_basic, |
| 464 | + 'GetAdvancedPersonalData': good_personal_advanced} |
| 465 | + |
| 466 | + def _call(self, *args, **kw): |
| 467 | + return self.data[args[0]] |
| 468 | + |
| 469 | + def test_preapproval_works(self, _call): |
| 470 | + _call.side_effect = self._call |
| 471 | + eq_(paypal.get_personal_data('foo')['email'], 'batman@gmail.com') |
| 472 | + eq_(_call.call_count, 2) |
| 473 | + |
| 474 | + def test_preapproval_absent(self, _call): |
| 475 | + _call.side_effect = self._call |
| 476 | + eq_(paypal.get_personal_data('foo')['street2'], '') |
| 477 | + |
| 478 | + def test_preapproval_unicode(self, _call): |
| 479 | + key = 'response.personalData(2).personalDataValue' |
| 480 | + value = u'Österreich' |
| 481 | + self.data['GetAdvancedPersonalData'][key] = value |
| 482 | + _call.side_effect = self._call |
| 483 | + eq_(paypal.get_personal_data('foo')['city'], value) |
| 484 | + |
| 485 | + def test_preapproval_error(self, _call): |
| 486 | + _call.side_effect = paypal.PaypalError |
| 487 | + self.assertRaises(paypal.PaypalError, paypal.get_personal_data, 'foo') |
0 commit comments