def setUp(self): url = reverse('forums.threads', args=[u'test-forum']) self.context = {'request': RequestFactory().get(url)} self.group = GroupFactory() # Set up forum_1 f = self.forum_1 = ForumFactory() ct = ContentType.objects.get_for_model(self.forum_1) permission_names = [ 'forums_forum.thread_edit_forum', 'forums_forum.post_edit_forum', 'forums_forum.post_delete_forum', 'forums_forum.thread_delete_forum', 'forums_forum.thread_sticky_forum', 'forums_forum.thread_move_forum', ] for name in permission_names: PermissionFactory(codename=name, content_type=ct, object_id=f.id, group=self.group) # Set up forum_2 f = self.forum_2 = ForumFactory() PermissionFactory(codename='forums_forum.thread_move_forum', content_type=ct, object_id=f.id, group=self.group)
def test_delete_post_belongs_to_thread_and_forum(self): # Delete post action - post belongs to thread and thread # belongs to forum. f = ForumFactory() t = ThreadFactory(forum=f) # Post belongs to a different forum and thread. p = PostFactory() u = p.author # Give the user the permission to delete posts. g = GroupFactory() ct = ContentType.objects.get_for_model(f) PermissionFactory(codename='forums_forum.post_delete_forum', content_type=ct, object_id=p.thread.forum_id, group=g) PermissionFactory(codename='forums_forum.post_delete_forum', content_type=ct, object_id=f.id, group=g) g.user_set.add(u) self.client.login(username=u.username, password='******') # Post isn't in the passed forum: r = get(self.client, 'forums.delete_post', args=[f.slug, p.thread.id, p.id]) eq_(404, r.status_code) # Post isn't in the passed thread: r = get(self.client, 'forums.delete_post', args=[p.thread.forum.slug, t.id, p.id]) eq_(404, r.status_code)
def test_move_thread(self): """Move a thread.""" t = ThreadFactory() f = ForumFactory() u = UserFactory() g = GroupFactory() # Give the user permission to move threads between the two forums. ct = ContentType.objects.get_for_model(f) PermissionFactory(codename="forums_forum.thread_move_forum", content_type=ct, object_id=f.id, group=g) PermissionFactory( codename="forums_forum.thread_move_forum", content_type=ct, object_id=t.forum.id, group=g, ) g.user_set.add(u) self.client.login(username=u.username, password="******") response = post(self.client, "forums.move_thread", {"forum": f.id}, args=[t.forum.slug, t.id]) eq_(200, response.status_code) t = Thread.objects.get(pk=t.pk) eq_(f.id, t.forum.id)
def test_new_short_thread_errors(self): """Posting a short new thread shows errors.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password="******") response = post( self.client, "forums.new_thread", {"title": "wha?", "content": "wha?"}, args=[f.slug], ) doc = pq(response.content) errors = doc("ul.errorlist li a") eq_( errors[0].text, "Your title is too short (4 characters). " + "It must be at least 5 characters.", ) eq_( errors[1].text, "Your message is too short (4 characters). " + "It must be at least 5 characters.", )
def test_canonical_url(self): """Verify the canonical URL is set correctly.""" f = ForumFactory() response = get(self.client, 'forums.threads', args=[f.slug]) eq_('/forums/%s' % f.slug, pq(response.content)('link[rel="canonical"]')[0].attrib['href'])
def test_watch_forum_then_new_thread(self, get_current): """Watching a forum and creating a new thread should send email.""" get_current.return_value.domain = 'testserver' f = ForumFactory() poster = UserFactory(username='******', profile__name='Poster') watcher = UserFactory(username='******', profile__name='Watcher') self._toggle_watch_forum_as(f, watcher, turn_on=True) self.client.login(username=poster.username, password='******') post(self.client, 'forums.new_thread', { 'title': 'a title', 'content': 'a post' }, args=[f.slug]) t = Thread.objects.all().order_by('-id')[0] attrs_eq(mail.outbox[0], to=[watcher.email], subject=u'{f} - {t}'.format(f=f, t=t)) body = NEW_THREAD_EMAIL.format(username=poster.profile.name, forum_slug=f.slug, thread=t.title, thread_id=t.id) starts_with(mail.outbox[0].body, body)
def test_watch_forum_then_new_thread(self, get_current): """Watching a forum and creating a new thread should send email.""" get_current.return_value.domain = "testserver" f = ForumFactory() poster = UserFactory(username="******", profile__name="Poster") watcher = UserFactory(username="******", profile__name="Watcher") self._toggle_watch_forum_as(f, watcher, turn_on=True) self.client.login(username=poster.username, password="******") post( self.client, "forums.new_thread", { "title": "a title", "content": "a post" }, args=[f.slug], ) t = Thread.objects.all().order_by("-id")[0] attrs_eq(mail.outbox[0], to=[watcher.email], subject="{f} - {t}".format(f=f, t=t)) body = NEW_THREAD_EMAIL.format(username=poster.profile.name, forum_slug=f.slug, thread=t.title, thread_id=t.id) starts_with(mail.outbox[0].body, body)
def test_show_new_thread(self): """'Post new thread' shows if user has permission to post.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password="******") response = get(self.client, "forums.threads", args=[f.slug]) self.assertContains(response, "Post a new thread")
def test_threads_sort(self): """Ensure that threads are being sorted properly by date/time.""" # Threads are sorted descending by last post date. f = ForumFactory() ThreadFactory(forum=f, created=YESTERDAY) t2 = ThreadFactory(forum=f) eq_(t2.id, ThreadsFeed().items(f)[0].id)
def test_watch_GET_405(self): """Watch forum with HTTP GET results in 405.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password="******") response = get(self.client, "forums.watch_forum", args=[f.id]) eq_(405, response.status_code)
def test_edit_thread_belongs_to_forum(self): """Edit thread action - thread belongs to forum.""" f = ForumFactory() t = ThreadFactory() # Thread belongs to a different forum u = t.creator self.client.login(username=u.username, password="******") r = get(self.client, "forums.edit_thread", args=[f.slug, t.id]) eq_(404, r.status_code)
def test_public_access(self): # Assert Forums think they're publicly viewable and postable # at appropriate times. # By default, users have access to forums that aren't restricted. u = UserFactory() f = ForumFactory() assert f.allows_viewing_by(u) assert f.allows_posting_by(u)
def test_canonical_url(self): """Verify the canonical URL is set correctly.""" f = ForumFactory() response = get(self.client, "forums.threads", args=[f.slug]) eq_( "%s/en-US/forums/%s" % (settings.CANONICAL_URL, f.slug), pq(response.content)('link[rel="canonical"]')[0].attrib["href"], )
def test_discussion_filter_forum(self): """Filter by forum in discussion forums.""" forum1 = ForumFactory(name=u'Forum 1') thread1 = ThreadFactory(forum=forum1, title=u'audio 1') PostFactory(thread=thread1) forum2 = ForumFactory(name=u'Forum 2') thread2 = ThreadFactory(forum=forum2, title=u'audio 2') PostFactory(thread=thread2) self.refresh() qs = {'a': 1, 'w': 4, 'format': 'json'} for forum_id in (forum1.id, forum2.id): qs['forum'] = int(forum_id) response = self.client.get(reverse('search.advanced'), qs) eq_(json.loads(response.content)['total'], 1)
def test_discussion_filter_forum(self): """Filter by forum in discussion forums.""" forum1 = ForumFactory(name="Forum 1") thread1 = ThreadFactory(forum=forum1, title="audio 1") PostFactory(thread=thread1) forum2 = ForumFactory(name="Forum 2") thread2 = ThreadFactory(forum=forum2, title="audio 2") PostFactory(thread=thread2) self.refresh() qs = {"a": 1, "w": 4, "format": "json"} for forum_id in (forum1.id, forum2.id): qs["forum"] = int(forum_id) response = self.client.get(reverse("search.advanced"), qs) eq_(json.loads(response.content)["total"], 1)
def test_reply_thread_belongs_to_forum(self): """Reply action - thread belongs to forum.""" f = ForumFactory() t = ThreadFactory() # Thread belongs to a different forum u = UserFactory() self.client.login(username=u.username, password="******") r = post(self.client, "forums.reply", {}, args=[f.slug, t.id]) eq_(404, r.status_code)
def test_posts_thread_belongs_to_forum(self): """Posts view - redirect if thread does not belong to forum.""" f = ForumFactory() t = ThreadFactory() # Thread belongs to a different forum r = get(self.client, "forums.posts", args=[f.slug, t.id]) eq_(200, r.status_code) u = r.redirect_chain[0][0] assert u.endswith(t.get_absolute_url())
def test_display_order(self): """Verify the display_order is respected.""" forum1 = ForumFactory(display_order=1) forum2 = ForumFactory(display_order=2) # forum1 should be listed first r = get(self.client, "forums.forums") eq_(200, r.status_code) doc = pq(r.content) eq_(forum1.name, doc(".forums tr a").first().text()) forum1.display_order = 3 forum1.save() # forum2 should be listed first r = get(self.client, "forums.forums") eq_(200, r.status_code) doc = pq(r.content) eq_(forum2.name, doc(".forums tr a").first().text())
def test_move_thread_403(self): """Moving a thread without permissions returns 403.""" t = ThreadFactory() f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password='******') response = post(self.client, 'forums.move_thread', {'forum': f.id}, args=[t.forum.slug, t.id]) eq_(403, response.status_code)
def test_restricted_is_invisible(self): """Forums with restricted view_in permission shouldn't show up.""" restricted_forum = ForumFactory() # Make it restricted. ct = ContentType.objects.get_for_model(restricted_forum) PermissionFactory(codename='forums_forum.view_in_forum', content_type=ct, object_id=restricted_forum.id) response = get(self.client, 'forums.forums') self.assertNotContains(response, restricted_forum.slug)
def test_is_listed(self): """Verify is_listed is respected.""" forum1 = ForumFactory(is_listed=True) forum2 = ForumFactory(is_listed=True) # Both forums should be listed. r = get(self.client, "forums.forums") eq_(200, r.status_code) doc = pq(r.content) eq_(2, len(doc(".forums tr"))) forum1.is_listed = False forum1.save() # Only forum2 should be listed. r = get(self.client, "forums.forums") eq_(200, r.status_code) doc = pq(r.content) eq_(1, len(doc(".forums tr"))) eq_(forum2.name, doc(".forums tr a").text())
def test_perm_is_defined_on(self): """Test permission relationship Test whether we check for permission relationship, independent of whether the permission is actually assigned to anyone. """ from kitsune.forums.tests import ForumFactory, RestrictedForumFactory f1 = RestrictedForumFactory() f2 = ForumFactory() perm = 'forums_forum.view_in_forum' assert access.perm_is_defined_on(perm, f1) assert not access.perm_is_defined_on(perm, f2)
def test_new_thread_redirect(self): """Posting a new thread should redirect.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password="******") url = reverse("forums.new_thread", args=[f.slug]) data = {"title": "some title", "content": "some content"} r = self.client.post(url, data, follow=False) eq_(302, r.status_code) assert f.slug in r["location"] assert "last=" in r["location"]
def test_empty_thread_errors(self): """Posting an empty thread shows errors.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password='******') response = post(self.client, 'forums.new_thread', {'title': '', 'content': ''}, args=[f.slug]) doc = pq(response.content) errors = doc('ul.errorlist li a') eq_(errors[0].text, 'Please provide a title.') eq_(errors[1].text, 'Please provide a message.')
def test_watch_forum(self): """Watch and unwatch a forum.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password="******") response = post(self.client, "forums.watch_forum", {"watch": "yes"}, args=[f.slug]) self.assertContains(response, "Stop watching this forum") response = post(self.client, "forums.watch_forum", {"watch": "no"}, args=[f.slug]) self.assertNotContains(response, "Stop watching this forum")
def test_new_thread_redirect(self): """Posting a new thread should redirect.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password='******') url = reverse('forums.new_thread', args=[f.slug]) data = {'title': 'some title', 'content': 'some content'} r = self.client.post(url, data, follow=False) eq_(302, r.status_code) assert f.slug in r['location'] assert 'last=' in r['location']
def test_empty_thread_errors(self): """Posting an empty thread shows errors.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password="******") response = post( self.client, "forums.new_thread", {"title": "", "content": ""}, args=[f.slug], ) doc = pq(response.content) errors = doc("ul.errorlist li a") eq_(errors[0].text, "Please provide a title.") eq_(errors[1].text, "Please provide a message.")
def test_restricted_hide_new_thread(self): """'Post new thread' doesn't show if user has no permission to post. """ f = ForumFactory() ct = ContentType.objects.get_for_model(f) # If the forum has the permission and the user isn't assigned said # permission, then they can't post. PermissionFactory(codename='forums_forum.post_in_forum', content_type=ct, object_id=f.id) u = UserFactory() self.client.login(username=u.username, password='******') response = get(self.client, 'forums.threads', args=[f.slug]) self.assertNotContains(response, 'Post a new thread')
def test_access_restriction(self): """Assert Forums are inaccessible to the public when restricted.""" # If the a forum has 'forums_forum.view_in_forum' permission defined, # then it isn't public by default. If it has # 'forums_forum.post_in_forum', then it isn't postable to by default. f = ForumFactory() ct = ContentType.objects.get_for_model(f) PermissionFactory(codename='forums_forum.view_in_forum', content_type=ct, object_id=f.id) PermissionFactory(codename='forums_forum.post_in_forum', content_type=ct, object_id=f.id) unprivileged_user = UserFactory() assert not f.allows_viewing_by(unprivileged_user) assert not f.allows_posting_by(unprivileged_user)
def test_fire_on_new_thread(self, fire): """The event fires when there is a new thread.""" u = UserFactory() f = ForumFactory() self.client.login(username=u.username, password='******') post(self.client, 'forums.new_thread', { 'title': 'a title', 'content': 'a post' }, args=[f.slug]) # NewThreadEvent.fire() is called. assert fire.called