def test_topic_notification_ajax_order(self): """ order by is_read=False first then by date """ user = utils.create_user() for _ in range(10): topic = utils.create_topic(self.category, user=user) comment = utils.create_comment(topic=topic, user=user) TopicNotification.objects.create(user=self.user, topic=topic, comment=comment, is_active=True, action=COMMENT) TopicNotification.objects.filter(user=self.user).update(is_read=True) old_date = timezone.now() - datetime.timedelta(days=10) (TopicNotification.objects.filter( pk=self.topic_notification.pk).update(is_read=False, date=old_date)) utils.login(self) response = self.client.get( reverse('spirit:topic:notification:index-ajax'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') res = json.loads(response.content.decode('utf-8')) self.assertFalse(res['n'][0]['is_read']) self.assertTrue(res['n'][1]['is_read'])
def test_topic_notification_list_unread(self): """ topic notification list """ topic = utils.create_topic(self.category, user=self.user2) comment = utils.create_comment(topic=topic, user=self.user2) topic_notification = TopicNotification.objects.create(user=self.user, topic=topic, comment=comment, is_active=True, action=COMMENT) utils.login(self) response = self.client.get( reverse('spirit:topic:notification:index-unread')) self.assertEqual(list(response.context['page']), [topic_notification, self.topic_notification]) # fake next page response = self.client.get( reverse('spirit:topic:notification:index-unread'), { 'p': to_page_key(value=topic_notification.date, pk=topic_notification.pk) }) self.assertEqual(list(response.context['page']), [ self.topic_notification, ])
def test_topic_notification_ajax_escape(self): """ The receive username and topic title should be escaped """ user = utils.create_user(username="******") topic = utils.create_topic(self.category, title="<tag>Have you met Ted?</tag>") notification = TopicNotification.objects.create( user=self.user, topic=topic, comment=utils.create_comment(topic=topic, user=user), is_active=True, action=COMMENT) utils.login(self) response = self.client.get( reverse('spirit:topic:notification:index-ajax'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') res = json.loads(response.content.decode('utf-8')) expected = { 'user': "******", 'action': notification.action, 'title': "<tag>Have you met Ted?</tag>", 'url': notification.get_absolute_url(), 'is_read': notification.is_read } self.assertDictEqual(res['n'][0], expected)
def test_category_update(self): """ Category update """ utils.login(self) form_data = { "parent": "", "title": "foo", "description": "", "is_closed": False, "is_removed": False, "is_global": True, "color": "#ff0000" } response = self.client.post( reverse('spirit:admin:category:update', kwargs={ "category_id": self.category.pk, }), form_data) expected_url = reverse("spirit:admin:category:index") self.assertRedirects(response, expected_url, status_code=302) response = self.client.get( reverse('spirit:admin:category:update', kwargs={ "category_id": self.category.pk, })) self.assertEqual(response.status_code, 200)
def test_topic_publish(self): """ POST, create topic """ self.assertEqual(len(Topic.objects.all()), 0) utils.login(self) category = utils.create_category() form_data = { 'comment': 'foo', 'title': 'foobar', 'category': category.pk } response = self.client.post(reverse('spirit:topic:publish'), form_data) topic = Topic.objects.last() expected_url = topic.get_absolute_url() self.assertRedirects(response, expected_url, status_code=302) self.assertEqual(len(Topic.objects.all()), 1) # ratelimit response = self.client.post(reverse('spirit:topic:publish'), form_data) self.assertEqual(response.status_code, 200) self.assertEqual(len(Topic.objects.all()), 1) # get response = self.client.get(reverse('spirit:topic:publish')) self.assertEqual(response.status_code, 200)
def test_category_move_up_down(self): """Should order the category when moving up/down""" utils.login(self) self.another_category = utils.create_category() response = self.client.post( reverse('spirit:admin:category:move_dn', kwargs={ "category_id": self.category.pk, })) expected_url = reverse("spirit:admin:category:index") self.assertRedirects(response, expected_url, status_code=302) self.category.refresh_from_db() self.another_category.refresh_from_db() self.assertTrue(self.category.sort > self.another_category.sort) response = self.client.post( reverse('spirit:admin:category:move_up', kwargs={ "category_id": self.category.pk, })) expected_url = reverse("spirit:admin:category:index") self.assertRedirects(response, expected_url, status_code=302) self.category.refresh_from_db() self.another_category.refresh_from_db() self.assertTrue(self.category.sort < self.another_category.sort)
def test_topic_has_new_comments(self): """ Should return True when there are new replies """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category, user=self.user, comment_count=1) self.assertFalse( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().has_new_comments) CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1) self.assertFalse( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().has_new_comments) Topic.objects.filter(pk=topic.pk).update(comment_count=2) self.assertTrue( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().has_new_comments)
def test_register(self): """ register """ # get response = self.client.get(reverse('spirit:user:auth:register')) self.assertEqual(response.status_code, 200) # post form_data = { 'username': '******', 'email': '*****@*****.**', 'email2': '*****@*****.**', 'password': '******' } response = self.client.post(reverse('spirit:user:auth:register'), form_data) expected_url = reverse('spirit:user:auth:login') self.assertRedirects(response, expected_url, status_code=302) # redirect logged in user utils.login(self) response = self.client.get(reverse('spirit:user:auth:register')) self.assertRedirects(response, reverse('spirit:user:update'), status_code=302)
def test_topic_publish_in_category(self): """ POST, create topic in category """ utils.login(self) category = utils.create_category() form_data = { 'comment': 'foo', 'title': 'foobar', 'category': category.pk } response = self.client.post( reverse('spirit:topic:publish', kwargs={'category_id': category.pk}), form_data) topic = Topic.objects.last() expected_url = topic.get_absolute_url() self.assertRedirects(response, expected_url, status_code=302) # ratelimit response = self.client.post( reverse('spirit:topic:publish', kwargs={ 'category_id': category.pk, }), form_data) self.assertEqual(response.status_code, 200)
def test_logout(self): """ should log out on POST only """ utils.login(self) # get should display confirmation message response = self.client.get(reverse('spirit:user:auth:logout')) self.assertEqual(response.status_code, 200) self.assertTrue(self.client.session.items()) # post should log out the user (clear the session) response = self.client.post(reverse('spirit:user:auth:logout')) expected_url = "/" self.assertRedirects(response, expected_url, status_code=302) self.assertFalse(self.client.session.items()) # next utils.login(self) self.assertTrue(self.client.session.items()) response = self.client.post( reverse('spirit:user:auth:logout') + '?next=/fakepath/') self.assertRedirects(response, '/fakepath/', status_code=302, target_status_code=404) self.assertFalse(self.client.session.items())
def test_topic_detail_viewed(self): """ Calls utils.topic_viewed """ def mocked_topic_viewed(request, topic): self._user = request.user self._topic = topic org_viewed, utils_topic.topic_viewed = (utils_topic.topic_viewed, mocked_topic_viewed) try: utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category, user=self.user) response = self.client.get( reverse('spirit:topic:detail', kwargs={ 'pk': topic.pk, 'slug': topic.slug })) self.assertEqual(response.status_code, 200) self.assertEqual(self._topic, topic) self.assertEqual(self._user, self.user) finally: utils_topic.topic_viewed = org_viewed
def test_topic_notification_list_show_private_topic(self): """ topic private in notification list """ TopicNotification.objects.all().delete() topic_a = utils.create_private_topic(user=self.user) topic_notif = TopicNotification.objects.create(user=self.user, topic=topic_a.topic, comment=self.comment, is_active=True, action=COMMENT) utils.login(self) response = self.client.get(reverse('spirit:topic:notification:index')) self.assertEqual(list(response.context['notifications']), [ topic_notif, ]) # list unread should behave the same response = self.client.get( reverse('spirit:topic:notification:index-unread')) self.assertEqual(list(response.context['page']), [ topic_notif, ]) # ajax list should behave the same response = self.client.get( reverse('spirit:topic:notification:index-ajax'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') res = json.loads(response.content.decode('utf-8')) self.assertEqual(len(res['n']), 1)
def test_topic_get_bookmark_url(self): """ Should return the bookmark url """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category, user=self.user) # No bookmark topic_with_bookmark = (Topic.objects.filter( pk=topic.pk).with_bookmarks(self.user).first()) self.assertEqual(topic_with_bookmark.get_bookmark_url(), topic_with_bookmark.get_absolute_url()) # With bookmark CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1) topic_with_bookmark2 = (Topic.objects.filter( pk=topic.pk).with_bookmarks(self.user).first()) self.assertEqual(topic_with_bookmark2.get_bookmark_url(), topic_with_bookmark2.bookmark.get_absolute_url()) # With bookmark and new comment Topic.objects.filter(pk=topic.pk).update(comment_count=2) topic_with_bookmark3 = (Topic.objects.filter( pk=topic.pk).with_bookmarks(self.user).first()) self.assertEqual(topic_with_bookmark3.get_bookmark_url(), topic_with_bookmark3.bookmark.get_new_comment_url())
def test_topic_new_comments_count(self): """ Should return the new replies count """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category, user=self.user, comment_count=1) self.assertEqual( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().new_comments_count, 0) CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1) self.assertEqual( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().new_comments_count, 0) Topic.objects.filter(pk=topic.pk).update(comment_count=2) self.assertEqual( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().new_comments_count, 1) # topic.comment_count greater than bookmark.comment_number # should return 0 Topic.objects.filter(pk=topic.pk).update(comment_count=0) self.assertEqual( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().new_comments_count, 0)
def test_topic_publish_invalid_category(self): """ invalid topic category """ utils.login(self) response = self.client.get( reverse('spirit:topic:publish', kwargs={'category_id': str(99)})) self.assertEqual(response.status_code, 404)
def test_topic_notification_list(self): """ topic notification list """ utils.login(self) response = self.client.get(reverse('spirit:topic:notification:index')) self.assertEqual(list(response.context['notifications']), [self.topic_notification])
def test_register_next_logged_in(self): """ redirect next on register """ # redirect logged in user utils.login(self) response = self.client.get(reverse('spirit:user:auth:register') + "?next=/fakepath/") self.assertRedirects(response, '/fakepath/', status_code=302, target_status_code=404)
def test_resend_activation_email_redirect_logged(self): """ resend_activation_email redirect to profile if user is logged in """ utils.login(self) response = self.client.get(reverse('spirit:user:auth:resend-activation')) expected_url = reverse("spirit:user:update") self.assertRedirects(response, expected_url, status_code=302)
def test_category_list(self): """ Categories, excludes Topic Private and subcats """ utils.create_category(parent=self.category) categories = Category.objects.filter(is_private=False, parent=None) utils.login(self) response = self.client.get(reverse('spirit:admin:category:index')) self.assertEqual(list(response.context['categories']), list(categories))
def test_topic_update_invalid_user(self): """ POST, update topic """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category) form_data = {'title': 'foobar'} response = self.client.post( reverse('spirit:topic:update', kwargs={'pk': topic.pk}), form_data) self.assertEqual(response.status_code, 404)
def test_login_redirect(self): """ try to login with a logged in user """ utils.login(self) response = self.client.get(reverse('spirit:user:auth:login')) expected_url = self.user.st.get_absolute_url() self.assertRedirects(response, expected_url, status_code=302) # next response = self.client.get(reverse('spirit:user:auth:login') + '?next=/fakepath/') self.assertRedirects(response, '/fakepath/', status_code=302, target_status_code=404)
def test_topic_publish_long_title(self): """ POST, create topic with long title """ utils.login(self) category = utils.create_category() title = "a" * 255 form_data = {'comment': 'foo', 'title': title, 'category': category.pk} response = self.client.post(reverse('spirit:topic:publish'), form_data) self.assertEqual(response.status_code, 302) self.assertEqual(len(Topic.objects.all()), 1) self.assertEqual(Topic.objects.last().slug, title[:50])
def test_topic_update(self): """ POST, update topic """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category, user=self.user) form_data = {'title': 'foobar'} response = self.client.post( reverse('spirit:topic:update', kwargs={'pk': topic.pk}), form_data) self.assertRedirects(response, topic.get_absolute_url(), status_code=302)
def test_topic_notification_mark_all_as_read(self): TopicNotification.objects.all().delete() user2 = utils.create_user() topic = utils.create_topic(self.category) comment = utils.create_comment(topic=topic) TopicNotification.objects.create(user=user2, topic=topic, comment=comment, is_active=True, action=MENTION) topic0 = utils.create_topic(self.category) comment0 = utils.create_comment(topic=topic0) TopicNotification.objects.create(user=self.user, topic=topic0, comment=comment0, is_active=True, action=COMMENT) topic1 = utils.create_topic(self.category) comment1 = utils.create_comment(topic=topic1) TopicNotification.objects.create(user=self.user, topic=topic1, comment=comment1, is_active=True, action=COMMENT) topic2 = utils.create_topic(self.category) comment2 = utils.create_comment(topic=topic2) TopicNotification.objects.create(user=self.user, topic=topic2, comment=comment2, is_active=True, action=MENTION) self.assertEqual( TopicNotification.objects.filter(user=self.user, is_read=False).count(), 3) self.assertEqual( TopicNotification.objects.filter(user=user2, is_read=False).count(), 1) utils.login(self) response = self.client.post( reverse('spirit:topic:notification:mark-all-as-read')) self.assertRedirects(response, reverse('spirit:topic:notification:index'), status_code=302) self.assertEqual( TopicNotification.objects.filter(user=self.user, is_read=False).count(), 0) self.assertEqual( TopicNotification.objects.filter(user=user2, is_read=False).count(), 1)
def test_registration_login(self): """ Register and login """ form_data = {'username': '******', 'email': '*****@*****.**', 'email2': '*****@*****.**', 'password': '******'} form = RegistrationForm(data=form_data) self.assertEqual(form.is_valid(), True) user = form.save() self.assertFalse(user.is_active) user.is_active = True user.save() utils.login(self, user=user, password='******') # Asserts if can't login
def test_topic_detail_view_no_slug(self): """ no slug """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category) response = self.client.get( reverse('spirit:topic:detail', kwargs={ 'pk': topic.pk, 'slug': '' })) self.assertRedirects(response, topic.get_absolute_url(), status_code=301)
def test_topic_notification_create_no_access(self): """ raise Http404 if topic is private and user has no access """ private = utils.create_private_topic() utils.login(self) form_data = { 'is_active': True, } response = self.client.post( reverse('spirit:topic:notification:create', kwargs={ 'topic_id': private.topic.pk, }), form_data) self.assertEqual(response.status_code, 404)
def test_topic_is_visited(self): """ Should return True when the topic has been visited """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category, user=self.user) self.assertFalse( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().is_visited) CommentBookmark.objects.create(topic=topic, user=self.user) self.assertTrue( Topic.objects.filter(pk=topic.pk).with_bookmarks( self.user).first().is_visited)
def test_topic_notification_update_invalid_user(self): """ test user cant unsubscribe other user """ user = utils.create_user() notification = TopicNotification.objects.create(user=user, topic=self.topic, comment=self.comment) utils.login(self) form_data = {} response = self.client.post( reverse('spirit:topic:notification:update', kwargs={ 'pk': notification.pk, }), form_data) self.assertEqual(response.status_code, 404)
def test_topic_notification_update(self): """ update notification """ utils.login(self) form_data = { 'is_active': True, } response = self.client.post( reverse('spirit:topic:notification:update', kwargs={'pk': self.topic_notification.pk}), form_data) self.assertRedirects(response, self.topic.get_absolute_url(), status_code=302) notification = TopicNotification.objects.get( pk=self.topic_notification.pk) self.assertEqual(notification.action, COMMENT)