PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/forums/tests/test_models.py

https://github.com/oremj/kitsune
Python | 258 lines | 238 code | 15 blank | 5 comment | 1 complexity | c1e482ccbbecef3271965b6837384ac0 MD5 | raw file
  1. import datetime
  2. from django.contrib.auth.models import User
  3. from nose.tools import eq_
  4. from forums.events import NewPostEvent, NewThreadEvent
  5. from forums.models import Forum, Thread, Post
  6. from forums.tests import ForumTestCase
  7. from sumo.urlresolvers import reverse
  8. from sumo.helpers import urlparams
  9. class ForumModelTestCase(ForumTestCase):
  10. def setUp(self):
  11. super(ForumModelTestCase, self).setUp()
  12. def test_forum_absolute_url(self):
  13. f = Forum.objects.get(pk=1)
  14. exp_ = reverse('forums.threads', kwargs={'forum_slug': f.slug})
  15. eq_(exp_, f.get_absolute_url())
  16. def test_thread_absolute_url(self):
  17. t = Thread.objects.get(pk=1)
  18. exp_ = reverse('forums.posts', kwargs={'forum_slug': t.forum.slug,
  19. 'thread_id': t.id})
  20. eq_(exp_, t.get_absolute_url())
  21. def test_post_absolute_url(self):
  22. p = Post.objects.get(pk=1)
  23. url_ = reverse('forums.posts',
  24. kwargs={'forum_slug': p.thread.forum.slug,
  25. 'thread_id': p.thread.id})
  26. exp_ = urlparams(url_, hash='post-%s' % p.id)
  27. eq_(exp_, p.get_absolute_url())
  28. p = Post.objects.get(pk=24)
  29. url_ = reverse('forums.posts',
  30. kwargs={'forum_slug': p.thread.forum.slug,
  31. 'thread_id': p.thread.id})
  32. exp_ = urlparams(url_, hash='post-%s' % p.id, page=2)
  33. eq_(exp_, p.get_absolute_url())
  34. def test_post_page(self):
  35. p = Post.objects.get(pk=1)
  36. eq_(1, p.page)
  37. p = Post.objects.get(pk=22)
  38. eq_(1, p.page)
  39. p = Post.objects.get(pk=24)
  40. eq_(2, p.page)
  41. def test_last_post_updated(self):
  42. """Adding/Deleting the last post in a thread and forum should
  43. update the last_post field
  44. """
  45. thread = Thread.objects.get(pk=4)
  46. user = User.objects.get(pk=118533)
  47. # add a new post, then check that last_post is updated
  48. new_post = Post(thread=thread, content="test", author=user)
  49. new_post.save()
  50. forum = Forum.objects.get(pk=1)
  51. thread = Thread.objects.get(pk=thread.id)
  52. eq_(forum.last_post.id, new_post.id)
  53. eq_(thread.last_post.id, new_post.id)
  54. # delete the new post, then check that last_post is updated
  55. new_post.delete()
  56. forum = Forum.objects.get(pk=1)
  57. thread = Thread.objects.get(pk=thread.id)
  58. eq_(forum.last_post.id, 25)
  59. eq_(thread.last_post.id, 25)
  60. def test_public_access(self):
  61. """Assert Forums think they're publicly viewable and postable at
  62. appropriate times."""
  63. forum = Forum.objects.get(pk=1)
  64. unprivileged_user = User.objects.get(pk=118533)
  65. assert forum.allows_viewing_by(unprivileged_user)
  66. assert forum.allows_posting_by(unprivileged_user)
  67. def test_access_restriction(self):
  68. """Assert Forums are inaccessible to the public when restricted."""
  69. forum = Forum.objects.get(pk=3)
  70. unprivileged_user = User.objects.get(pk=118533)
  71. assert not forum.allows_viewing_by(unprivileged_user)
  72. assert not forum.allows_posting_by(unprivileged_user)
  73. def test_move_updates_last_posts(self):
  74. """Moving the thread containing a forum's last post to a new forum
  75. should update the last_post of both forums. Consequently, deleting
  76. the last post shouldn't delete the old forum. [bug 588994]"""
  77. old_forum = Forum.objects.get(pk=1) # forum 1 has newest post
  78. new_forum = Forum.objects.get(pk=2)
  79. last_post = old_forum.last_post
  80. thread = last_post.thread
  81. thread.forum = new_forum
  82. thread.save()
  83. # Old forum's last_post updated?
  84. self.assertNotEqual(Forum.objects.get(pk=1).last_post, last_post)
  85. # New forum's last_post updated?
  86. eq_(Forum.objects.get(pk=2).last_post, last_post)
  87. # Delete the post, and both forums should still exist:
  88. last_post.delete()
  89. eq_(1, Forum.objects.filter(pk=1).count())
  90. eq_(1, Forum.objects.filter(pk=2).count())
  91. def test_delete_removes_watches(self):
  92. f = Forum.objects.get(pk=1)
  93. NewThreadEvent.notify('me@me.com', f)
  94. assert NewThreadEvent.is_notifying('me@me.com', f)
  95. f.delete()
  96. assert not NewThreadEvent.is_notifying('me@me.com', f)
  97. class ThreadModelTestCase(ForumTestCase):
  98. def setUp(self):
  99. super(ThreadModelTestCase, self).setUp()
  100. self.fixtures = self.fixtures + ['notifications.json']
  101. def test_delete_thread_with_last_forum_post(self):
  102. """Deleting the thread with a forum's last post should
  103. update the last_post field on the forum
  104. """
  105. forum = Forum.objects.get(pk=1)
  106. last_post = forum.last_post
  107. # add a new thread and post, verify last_post updated
  108. thread = Thread(title="test", forum=forum, creator_id=118533)
  109. thread.save()
  110. post = Post(thread=thread, content="test", author=thread.creator)
  111. post.save()
  112. forum = Forum.objects.get(pk=1)
  113. eq_(forum.last_post.id, post.id)
  114. # delete the post, verify last_post updated
  115. thread.delete()
  116. forum = Forum.objects.get(pk=1)
  117. eq_(forum.last_post.id, last_post.id)
  118. eq_(Thread.objects.filter(pk=thread.id).count(), 0)
  119. def test_delete_removes_watches(self):
  120. t = Thread.objects.get(pk=1)
  121. NewPostEvent.notify('me@me.com', t)
  122. assert NewPostEvent.is_notifying('me@me.com', t)
  123. t.delete()
  124. assert not NewPostEvent.is_notifying('me@me.com', t)
  125. def test_delete_last_and_only_post_in_thread(self):
  126. """Deleting the only post in a thread should delete the thread"""
  127. forum = Forum.objects.get(pk=1)
  128. thread = Thread(title="test", forum=forum, creator_id=118533)
  129. thread.save()
  130. post = Post(thread=thread, content="test", author=thread.creator)
  131. post.save()
  132. eq_(1, thread.post_set.count())
  133. post.delete()
  134. eq_(0, Thread.uncached.filter(pk=thread.id).count())
  135. class SaveDateTestCase(ForumTestCase):
  136. """
  137. Test that Thread and Post save methods correctly handle created
  138. and updated dates.
  139. """
  140. delta = datetime.timedelta(milliseconds=300)
  141. def setUp(self):
  142. super(SaveDateTestCase, self).setUp()
  143. self.user = User.objects.get(pk=118533)
  144. self.forum = Forum.objects.get(pk=1)
  145. self.thread = Thread.objects.get(pk=2)
  146. def assertDateTimeAlmostEqual(self, a, b, delta, msg=None):
  147. """
  148. Assert that two datetime objects are within `range` (a timedelta).
  149. """
  150. diff = abs(a - b)
  151. assert diff < abs(delta), msg or '%s ~= %s' % (a, b)
  152. def test_save_thread_no_created(self):
  153. """Saving a new thread should behave as if auto_add_now was set."""
  154. t = self.forum.thread_set.create(title='foo', creator=self.user)
  155. t.save()
  156. now = datetime.datetime.now()
  157. self.assertDateTimeAlmostEqual(now, t.created, self.delta)
  158. def test_save_thread_created(self):
  159. """
  160. Saving a new thread that already has a created date should respect
  161. that created date.
  162. """
  163. created = datetime.datetime(1992, 1, 12, 9, 48, 23)
  164. t = self.forum.thread_set.create(title='foo', creator=self.user,
  165. created=created)
  166. t.save()
  167. eq_(created, t.created)
  168. def test_save_old_thread_created(self):
  169. """Saving an old thread should not change its created date."""
  170. t = Thread.objects.get(pk=3)
  171. created = t.created
  172. t.save()
  173. eq_(created, t.created)
  174. def test_save_new_post_no_timestamps(self):
  175. """
  176. Saving a new post should behave as if auto_add_now was set on
  177. created and auto_now set on updated.
  178. """
  179. p = Post(thread=self.thread, content='bar', author=self.user)
  180. p.save()
  181. now = datetime.datetime.now()
  182. self.assertDateTimeAlmostEqual(now, p.created, self.delta)
  183. self.assertDateTimeAlmostEqual(now, p.updated, self.delta)
  184. def test_save_old_post_no_timestamps(self):
  185. """
  186. Saving an existing post should update the updated date.
  187. """
  188. p = Post.objects.get(pk=4)
  189. updated = datetime.datetime(2010, 5, 4, 14, 4, 31)
  190. eq_(updated, p.updated)
  191. p.content = 'baz'
  192. p.updated_by = self.user
  193. p.save()
  194. now = datetime.datetime.now()
  195. created = datetime.datetime(2010, 5, 4, 14, 4, 22)
  196. self.assertDateTimeAlmostEqual(now, p.updated, self.delta)
  197. eq_(created, p.created)
  198. def test_save_new_post_timestamps(self):
  199. """
  200. Saving a new post should allow you to override auto_add_now- and
  201. auto_now-like functionality.
  202. """
  203. created_ = datetime.datetime(1992, 1, 12, 10, 12, 32)
  204. p = Post(thread=self.thread, content='bar', author=self.user,
  205. created=created_, updated=created_)
  206. p.save()
  207. eq_(created_, p.created)
  208. eq_(created_, p.updated)
  209. def test_content_parsed_sanity(self):
  210. """The content_parsed field is populated."""
  211. p = Post.objects.get(pk=4)
  212. eq_('<p>yet another post\n</p>', p.content_parsed)