def test_create_with_parent(self): article = ArticleFactory(section__slug='politic') comment = CommentFactory(article=article) data = { 'article': article.id, 'parent': comment.id, 'username': '******', 'content': 'Бабушка и волки', 'captcha_0': '*', 'captcha_1': 'passed' } resp = self.app.post('/comment/create/', data, follow=True) url, status_code = resp.redirect_chain[0] self.assertEqual(status_code, 302) self.assertEqual(url, '/material/politic/{}/'.format(article.id)) comments = Comment.objects.order_by('-id') self.assertEqual(len(comments), 2) self.assertEqual(comments[0].article, article) self.assertEqual(comments[0].parent, comment) # comment exists on page self.assertContains(resp, 'Шапочка') self.assertContains(resp, '(без названия)')
def test_delete(self): comment = CommentFactory() # anonymous resp = self.app.get('/comment/{}/delete/'.format(comment.id), HTTP_REFERER='/material/news/1/') self.assertEqual(resp.status_code, 403) self.assertTrue(Comment.objects.active().exists()) # auth user user = UserFactory(username='******') user.set_password('secret') user.save() self.app.login(username='******', password='******') resp = self.app.get('/comment/{}/delete/'.format(comment.id), HTTP_REFERER='/material/news/1/') self.assertEqual(resp.status_code, 403) self.assertTrue(Comment.objects.active().exists()) # admin user.is_staff = True user.save() resp = self.app.get('/comment/{}/delete/'.format(comment.id), HTTP_REFERER='/material/news/1/') self.assertEqual(resp.status_code, 302) self.assertEqual(resp.url, '/material/news/1/') self.assertFalse(Comment.objects.active().exists())
def test_vote_atricle_fail(self): self.app.get('/') # create session_key # fake comment id resp = self.app.post('/votes/comment/', { 'object_id': 999999999, 'score': 1 }, HTTP_REFERER='/material/news/1/') self.assertEqual(resp.status_code, 302) self.assertEqual(resp.url, '/material/news/1/') self.assertFalse(Vote.objects.exists()) article = ArticleFactory() comment = CommentFactory(article=article) # bad score resp = self.app.post('/votes/comment/', { 'object_id': comment.id, 'score': 2 }, HTTP_REFERER='/material/news/{}/'.format( article.id), follow=True) url, status_code = resp.redirect_chain[0] self.assertEqual(status_code, 302) self.assertEqual(url, '/material/news/{}/'.format(article.id)) self.assertFalse(Vote.objects.exists()) self.assertContains( resp, 'Возможность оценить комментарий временно недоступна')
def test_thread_page(self): article = ArticleFactory(title='Печенька') CommentFactory(title='Круг', content='Яркий', article=article) CommentFactory(title='Квадрат', content='Бледный', article=article) CommentFactory(title='Ромб', content='Серый') resp = self.app.get('/forum/{}/'.format(article.id)) self.assertEqual(resp.status_code, 200) comments = resp.context['object_list'] self.assertEqual(len(comments), 2) self.assertEqual(comments[0].title, 'Круг') self.assertEqual(comments[1].title, 'Квадрат') self.assertContains(resp, ':: Печенька') self.assertContains(resp, 'Яркий') self.assertContains(resp, 'Бледный') self.assertNotContains(resp, 'Серый') self.assertContains(resp, 'Ответить')
def test_comment_likes(self): comment = CommentFactory() for score in (1, -1, 1, 1): VoteFactory(content_object=comment, score=score) comment.refresh_from_db() self.assertEqual(comment.karma, 2) Vote.objects.all().delete() comment.refresh_from_db() self.assertEqual(comment.karma, 0)
def test_create_vote_comment_anonymous(self): comment = CommentFactory() self.app.get('/') # create session_key token = Session.objects.first().session_key resp = self.app.post('/votes/comment/', { 'object_id': comment.id, 'score': -1 }, HTTP_REFERER='/material/news/1/') self.assertEqual(resp.status_code, 302) self.assertEqual(resp.url, '/material/news/1/') votes = Vote.objects.all() self.assertEqual(len(votes), 1) self.assertEqual(votes[0].token, token) self.assertEqual(votes[0].object_id, comment.id) self.assertEqual(votes[0].score, -1)
def test_create_vote_comment_user(self): comment = CommentFactory() user = UserFactory(username='******') user.set_password('secret') user.save() self.app.login(username='******', password='******') resp = self.app.post('/votes/comment/', { 'object_id': comment.id, 'score': 1 }, HTTP_REFERER='/material/news/1/') self.assertEqual(resp.status_code, 302) self.assertEqual(resp.url, '/material/news/1/') votes = Vote.objects.all() self.assertEqual(len(votes), 1) self.assertEqual(votes[0].user, user) self.assertEqual(votes[0].object_id, comment.id) self.assertEqual(votes[0].score, 1)
def handle(self, *args, **kwargs): STR = LOW if kwargs.get('middle'): STR = MIDDLE elif kwargs.get('height'): STR = HEIGHT self.stdout.write('A <{}> strategy is chosen'.format(STR['label'])) article_ct = ContentType.objects.get_for_model(Article) comment_ct = ContentType.objects.get_for_model(Comment) choice_ct = ContentType.objects.get_for_model(Choice) self.stdout.write('Remove all data from DB') Comment.objects.all().delete() Multimedia.objects.all().delete() if kwargs.get('dev'): Article.objects.all().delete() Section.objects.all().delete() Notice.objects.all().delete() if kwargs.get('dev'): Author.objects.all().delete() if kwargs.get('dev'): Entry.objects.all().delete() Blogger.objects.all().delete() Choice.objects.all().delete() Poll.objects.all().delete() if kwargs.get('dev'): Tag.objects.all().delete() Vote.objects.all().delete() Resource.objects.all().delete() if kwargs.get('dev'): Partition.objects.all().delete() get_user_model().objects.exclude(is_staff=True).delete() # --------------------- tokens = [ str(uuid.uuid4()).replace('-', '') for i in range(STR['token_count']) ] self.stdout.write('Generate users...') users = UserFactory.create_batch(STR['user_count']) if kwargs.get('dev'): self.stdout.write('Generate authors...') authors = AuthorFactory.create_batch(STR['author_count']) else: authors = list(Author.objects.all()) if kwargs.get('dev'): self.stdout.write('Generate tags...') tags = TagFactory.create_batch(STR['tag_count']) else: tags = list(Tag.objects.all()) if kwargs.get('dev'): self.stdout.write('Generate sections...') sections = [ SectionFactory(name='Политический расклад', slug='politic'), SectionFactory(name='Экономическая реальность', slug='economic'), SectionFactory(name='Жизнь регионов', slug='region'), SectionFactory(name='Общество и его культура', slug='society'), SectionFactory(name='Силовые структуры', slug='power'), SectionFactory(name='Особенности внешней политики', slug='fpolitic'), SectionFactory(name='Компрометирующие материалы', slug='kompromat'), SectionFactory(name='Московский листок', slug='moscow'), # video VideoSectionFactory(name='Новости политики', slug='video_politic'), VideoSectionFactory(name='Экономический расклад', slug='video_economic'), VideoSectionFactory(name='Проиcшествия', slug='video_accidents'), VideoSectionFactory(name='Внешняя политика', slug='video_fpolitic'), VideoSectionFactory(name='Общество и его культура', slug='video_society'), VideoSectionFactory(name='Народное видео', slug='video_national'), # partner video PartnerVideoSectionFactory(name='Луганск 24', slug='video_partner_lugansk24'), PartnerVideoSectionFactory(name='Программа Сергея Доренко', slug='video_partner_dorenko'), PartnerVideoSectionFactory(name='Красное.ТВ', slug='video_partner_krasnoetv'), PartnerVideoSectionFactory(name='Nevex.TV', slug='video_partner_nevextv') ] else: sections = list(Section.objects.all()) a_count_list = [2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] if kwargs.get('dev'): self.stdout.write('Generate articles...') for i in range(len(sections) * STR['avg_art_count'] * 2): section = random.choice(sections) if section.is_video and not random.randint(0, 1): continue params = dict( section=section, is_news=not bool(random.randint(0, 8)), is_ticker=not bool(random.randint(0, 8)), is_main_news=not bool(random.randint(0, 8)), is_day_material=not bool(random.randint(0, 8)), ) # authors a_count = random.choice(a_count_list) if not a_count: # no authors if random.randint(0, 1): params['author_names'] = AuthorFactory.build( ).cover_name else: params['authors'] = random.sample(authors, a_count) # tags a_tags = random.sample(tags, random.randint(0, 6)) if a_tags: params['tags'] = a_tags # source if not random.randint(0, 5): params['with_source'] = True if random.randint(0, 1): params['with_source_link'] = True if not random.randint(0, 20): params['show_comments'] = False # hide comments if not random.randint(0, 3): params[ 'discussion_status'] = Article.DISCUSSION_STATUS.close # close discussion if section.is_video: params.update(image=None, with_video=True) ArticleFactory(**params) self.stdout.write('Generate articles comments and media...') for article in Article.objects.all(): # comments comment_count = random.randint(0, STR['max_com_count']) comment_tokens = random.sample(tokens, comment_count) comment_users = random.sample(users, comment_count) for j in range(comment_count): c_params = dict(article=article) if random.randint(0, 1): c_params['token'] = comment_tokens[j] else: c_params['user'] = comment_users[j] CommentFactory(**c_params) # multimedia if not random.randint(0, 8): if random.randint(0, 5): MultimediaFactory(article=article) else: MultimediaFactory.create_batch(2, article=article) if kwargs.get('dev'): self.stdout.write('Generate resources...') resource_types = [ PartitionFactory(name='Персональные сайты'), PartitionFactory(name='Партии и общественные движения'), PartitionFactory(name='Оппозиционные СМИ'), PartitionFactory(name='Аналитика'), PartitionFactory(name='Креативные проекты'), PartitionFactory(name='Блоги и форумы'), PartitionFactory(name='Музыка'), PartitionFactory(name='Литература и искусство'), PartitionFactory(name='Региональные организации'), PartitionFactory(name='Библиотеки'), PartitionFactory(name='История') ] else: resource_types = list(Partition.objects.all()) for i in range(len(resource_types) * 6): ResourceFactory(partition=random.choice(resource_types), rating=random.randint(0, 10)) self.stdout.write('Generate ratings(articles votes)...') all_article_ids = list(Article.objects.values_list('id', flat=True)) article_ids = random.sample(all_article_ids, int(len(all_article_ids) * 0.9)) # 90% articles with votes for article_id in article_ids: self.create_votes_for(article_id, article_ct, STR['max_art_vote_count'], tokens, users, lambda: random.randint(1, 5)) self.stdout.write('Generate likes(comments votes)...') all_comment_ids = list(Comment.objects.values_list('id', flat=True)) comment_ids = random.sample(all_comment_ids, int(len(all_comment_ids) * 0.9)) # 90% comments with likes for comment_id in comment_ids: self.create_votes_for(comment_id, comment_ct, STR['max_like_count'], tokens, users, lambda: random.choice([-1, 1])) if kwargs.get('dev'): self.stdout.write('Generate notices...') for i in range(STR['notice_count']): random_status = random.randint(0, 2) if random_status == 1: NoticeFactory(status=Notice.STATUS.new) elif random_status == 2: NoticeFactory(status=Notice.STATUS.rejected) else: NoticeFactory() self.stdout.write('Generate polls...') polls = PollFactory.create_batch(STR['poll_count']) for poll in polls: choices = ChoiceFactory.create_batch(random.randint(3, 8), poll=poll) for choice in choices: self.create_votes_for(choice.id, choice_ct, STR['max_choice_vote_count'], tokens, users, lambda: 1) if kwargs.get('dev'): self.stdout.write('Generate bloggers with entries...') bloggers = BloggerFactory.create_batch(10) for blogger in bloggers: EntryFactory.create_batch(random.randint( STR['min_entry_count'], STR['max_entry_count']), blogger=blogger)