PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/profiles/tests/__init__.py

https://bitbucket.org/yourcelf/old-intertwinkles
Python | 163 lines | 161 code | 0 blank | 2 comment | 0 complexity | 8d048f4636a4211f2997cf30069266a2 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. """
  2. This file demonstrates writing tests using the unittest module. These will pass
  3. when you run "manage.py test".
  4. Replace this with more appropriate tests for your application.
  5. """
  6. import os, time
  7. from urllib import urlencode
  8. from django.test import TestCase, LiveServerTestCase
  9. from django.conf import settings
  10. from django.core.urlresolvers import reverse
  11. from django.contrib.auth.models import User
  12. from django.test.utils import override_settings
  13. from PIL import Image, ImageChops
  14. from django_browserid.tests import mock_browserid
  15. from django_browserid import get_audience
  16. from bs4 import BeautifulSoup
  17. from profiles.models import Icon, Profile, EmailChangeRequest
  18. from profiles.tasks import render_icon
  19. @override_settings(CELERY_ALWAYS_EAGER=True)
  20. class TestIcons(TestCase):
  21. def test_icon_rendering(self):
  22. """
  23. Tests that 1 + 1 always equals 2.
  24. """
  25. icon = Icon.objects.get(source_id='icon-2244')
  26. paths = render_icon.delay(icon.pk, "00FF00", [16]).get()
  27. # HACK: can't seem to get imagemagick to block.
  28. time.sleep(0.1)
  29. reference = Image.open(os.path.join(
  30. os.path.dirname(__file__),
  31. "icon-2244-Oven-00FF00-16.png"))
  32. test_img = Image.open(os.path.join(settings.MEDIA_ROOT, paths['16']))
  33. self.assertTrue(
  34. ImageChops.difference(reference, test_img).getbbox() is None
  35. )
  36. @override_settings(CELERY_ALWAYS_EAGER=True)
  37. class TestBrowserID(TestCase):
  38. def login(self, email):
  39. with mock_browserid(email):
  40. self.assertTrue(
  41. self.client.login(assertion='one', audience="two")
  42. )
  43. def create_user_and_profile(self, username, email):
  44. with self.settings(CELERY_ALWAYS_EAGER=True):
  45. u = User.objects.create(username=username, email=email)
  46. profile = Profile.objects.create(name=username, user=u)
  47. profile.full_clean()
  48. profile.save()
  49. self.assertNotEqual(u.profile.source_icon, None)
  50. return u
  51. def test_change_email(self):
  52. user = self.create_user_and_profile("test", "test@example.com")
  53. self.login("test@example.com")
  54. res = self.client.post(reverse("profiles_edit_profile"), {
  55. 'name': 'test',
  56. 'email': 'new@example.com',
  57. 'source_icon': user.profile.source_icon.pk,
  58. 'icon_color': user.profile.icon_color,
  59. }, follow=True)
  60. self.assertRedirects(res, reverse("profiles_signin"))
  61. self.assertTrue("new@example.com" in res.content)
  62. # An email change request now exists ...
  63. self.assertEquals(
  64. len(EmailChangeRequest.objects.filter(
  65. email="new@example.com", user=user
  66. )), 1)
  67. # ... but the email hasn't yet been changed.
  68. self.assertEquals(User.objects.get(pk=user.pk).email, "test@example.com")
  69. # We're not logged in anymore; we've been prompted to login with the new email.
  70. url = reverse("profiles_edit_profile")
  71. self.assertRedirects(
  72. self.client.get(url, follow=True),
  73. reverse("profiles_signin") + "?{0}".format(urlencode({'next': url}))
  74. )
  75. # If we log in again with the old address, on profile edit, we see
  76. # a message that shows the old address is unconfirmed.
  77. self.login("test@example.com")
  78. res = self.client.get(reverse("profiles_edit_profile"))
  79. self.assertTrue("This address is unconfirmed" in res.content)
  80. self.assertTrue("test@example.com" in res.content)
  81. soup = BeautifulSoup(res.content)
  82. self.assertEquals(soup.find(id="id_email").get("value"),
  83. "new@example.com")
  84. # But if we log in as the new address, and hit the 'post_signin'
  85. # page, the address is changed.
  86. self.client.logout()
  87. self.login("new@example.com")
  88. res = self.client.get(reverse("profiles_post_signin"), follow=True)
  89. res = self.client.get(reverse("profiles_edit_profile"), follow=True)
  90. self.assertFalse("This address is unconfirmed" in res.content)
  91. soup = BeautifulSoup(res.content)
  92. self.assertEquals(soup.find(id="id_email").get("value"),
  93. "new@example.com")
  94. self.assertEquals(
  95. len(EmailChangeRequest.objects.filter(email="new@example.com")),
  96. 0
  97. )
  98. self.assertEquals(
  99. len(EmailChangeRequest.objects.filter(user=user)),
  100. 0
  101. )
  102. self.assertEquals(
  103. len(User.objects.filter(email="test@example.com")), 0)
  104. self.assertEquals(
  105. len(User.objects.filter(email="new@example.com")), 1)
  106. def _change_profile(self, user, attrs):
  107. self.login(user.email)
  108. post_data = {
  109. 'name': user.profile.name,
  110. 'email': user.email,
  111. 'source_icon': user.profile.source_icon.pk,
  112. 'icon_color': user.profile.icon_color,
  113. }
  114. post_data.update(attrs)
  115. return self.client.post(
  116. reverse("profiles_edit_profile"),
  117. post_data, follow=True)
  118. def test_existing_addresses(self):
  119. u1 = self.create_user_and_profile("u1", "one@example.com")
  120. u2 = self.create_user_and_profile("u2", "two@example.com")
  121. # If you try to change your address to one another user has, you get
  122. # rejected.
  123. res = self._change_profile(u2, {'email': u1.email})
  124. self.assertEquals(res.status_code, 200)
  125. self.assertTrue(
  126. "That email address is already in use" in res.content
  127. )
  128. self.assertEquals(EmailChangeRequest.objects.count(), 0)
  129. # Try a new address. Address should be accepted; but will still be
  130. # uncoonfirmed.
  131. res = self._change_profile(u2, {'email': "new@example.com"})
  132. self.assertRedirects(res, reverse("profiles_signin"))
  133. self.assertEquals(User.objects.get(pk=u2.pk).email, "two@example.com")
  134. self.assertEquals(
  135. EmailChangeRequest.objects.get(email='new@example.com').user,
  136. u2)
  137. # Meanwhile, the second user tries to change to the same unconfirmed
  138. # address. The address shows as not valid.
  139. res = self._change_profile(u1, {'email': "new@example.com"})
  140. self.assertEquals(res.status_code, 200)
  141. self.assertTrue(
  142. "That email address is already in use" in res.content
  143. )
  144. self.assertEquals(EmailChangeRequest.objects.count(), 1)