Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

testcases.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import httplib
  2. from django.test import TestCase
  3. from django.test.client import Client
  4. from django.core.urlresolvers import reverse
  5. from django.contrib.auth.models import Permission
  6. from django_webtest import WebTest
  7. from purl import URL
  8. from oscar.core.compat import get_user_model
  9. User = get_user_model()
  10. def add_permissions(user, permissions):
  11. """
  12. :param permissions: e.g. ['partner.dashboard_access']
  13. """
  14. for permission in permissions:
  15. app_label, _, codename = permission.partition('.')
  16. perm = Permission.objects.get(content_type__app_label=app_label,
  17. codename=codename)
  18. user.user_permissions.add(perm)
  19. class ClientTestCase(TestCase):
  20. """
  21. Helper TestCase for using Django's test client. The class provides
  22. auto-creation of a user to avoid boilerplate code.
  23. """
  24. username = 'dummyuser'
  25. email = 'dummyuser@example.com'
  26. password = 'staffpassword'
  27. is_anonymous = False
  28. is_staff = False
  29. is_superuser = False
  30. permissions = []
  31. def setUp(self):
  32. self.client = Client()
  33. if not self.is_anonymous:
  34. self.login()
  35. def login(self):
  36. self.user = self.create_user()
  37. self.client.login(username=self.username,
  38. password=self.password)
  39. def create_user(self, username=None, password=None, email=None,
  40. is_staff=None, is_superuser=None, permissions=None):
  41. user = User.objects.create_user(username or self.username,
  42. email or self.email,
  43. password or self.password)
  44. user.is_staff = is_staff or self.is_staff
  45. user.is_superuser = is_superuser or self.is_superuser
  46. user.save()
  47. perms = permissions if permissions is not None else self.permissions
  48. add_permissions(user, perms)
  49. return user
  50. def assertIsRedirect(self, response, expected_url=None):
  51. self.assertTrue(response.status_code in (httplib.FOUND,
  52. httplib.MOVED_PERMANENTLY))
  53. if expected_url:
  54. location = URL.from_string(response['Location'])
  55. self.assertEqual(expected_url, location.path())
  56. def assertRedirectUrlName(self, response, name, kwargs=None):
  57. self.assertIsRedirect(response)
  58. location = response['Location'].replace('http://testserver', '')
  59. self.assertEqual(location, reverse(name, kwargs=kwargs))
  60. def assertIsOk(self, response):
  61. self.assertEqual(httplib.OK, response.status_code)
  62. def assertNoAccess(self, response):
  63. self.assertTrue(response.status_code in (httplib.NOT_FOUND,
  64. httplib.FORBIDDEN))
  65. def assertInContext(self, response, key):
  66. self.assertTrue(key in response.context,
  67. "Context should contain a variable '%s'" % key)
  68. class WebTestCase(WebTest):
  69. is_staff = False
  70. is_anonymous = True
  71. username = 'testuser'
  72. email = 'testuser@buymore.com'
  73. password = 'somefancypassword'
  74. def setUp(self):
  75. self.user = None
  76. if not self.is_anonymous or self.is_staff:
  77. self.user = User.objects.create_user(self.username, self.email,
  78. self.password)
  79. self.user.is_staff = self.is_staff
  80. self.user.save()
  81. def get(self, url, **kwargs):
  82. kwargs.setdefault('user', self.user)
  83. return self.app.get(url, **kwargs)
  84. def post(self, url, **kwargs):
  85. kwargs.setdefault('user', self.user)
  86. return self.app.post(url, **kwargs)
  87. # Custom assertions
  88. def assertIsRedirect(self, response, expected_url=None):
  89. self.assertTrue(response.status_code in (httplib.FOUND,
  90. httplib.MOVED_PERMANENTLY))
  91. if expected_url:
  92. location = URL.from_string(response['Location'])
  93. self.assertEqual(expected_url, location.path())
  94. def assertRedirectsTo(self, response, url_name):
  95. self.assertTrue(str(response.status_code).startswith('3'))
  96. location = response.headers['Location']
  97. redirect_path = location.replace('http://localhost:80', '')
  98. self.assertEqual(reverse(url_name), redirect_path)