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_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_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_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_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_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_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_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_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_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_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('ol.forums > li 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('ol.forums > li a').first().text())
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('ol.forums > li'))) 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('ol.forums > li'))) eq_(forum2.name, doc('ol.forums > li a').text())
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_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_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_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
def test_preview(self): """Preview the thread post.""" f = ForumFactory() u = UserFactory() self.client.login(username=u.username, password='******') content = 'Full of awesome.' response = post(self.client, 'forums.new_thread', {'title': 'Topic', 'content': content, 'preview': 'any string'}, args=[f.slug]) eq_(200, response.status_code) doc = pq(response.content) eq_(content, doc('#post-preview div.content').text()) eq_(0, f.thread_set.count()) # No thread was created.
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_delete_removes_watches(self): f = ForumFactory() NewThreadEvent.notify('*****@*****.**', f) assert NewThreadEvent.is_notifying('*****@*****.**', f) f.delete() assert not NewThreadEvent.is_notifying('*****@*****.**', f)
def test_forum_absolute_url(self): f = ForumFactory() eq_('/forums/%s' % f.slug, f.get_absolute_url())