def test_not_author_cant_delete_comment(self, api_client): new_comment = CommentFactory() random_user = UserFactory() api_client.force_authenticate(random_user) response = api_client.delete(new_comment.get_absolute_url()) assert response.status_code == 403
def test_parse_comment(self): response = '''{"response":[6, {"cid":2505,"uid":16271479,"date":1298365200,"text":"Добрый день , кароче такая идея когда опросы создаешь вместо статуса - можно выбрать аудитории опрашиваемых, например только женский или мужской пол могут участвовать (то бишь голосовать в опросе)."}, {"cid":2507,"uid":16271479,"date":1286105582,"text":"Это уже не практично, имхо.<br>Для этого делайте группу и там опрос, а в группу принимайте тех, кого нужно.","reply_to_uid":16271479,"reply_to_cid":2505}, {"cid":2547,"uid":2943,"date":1286218080,"text":"Он будет только для групп благотворительных организаций."}]} ''' user = UserFactory(remote_id=USER_ID) post = PostFactory(remote_id=POST_ID, wall_owner=user) #instance = Comment(post=post) instance = CommentFactory(post=post) author = UserFactory(remote_id=16271479) instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, '%s_2505' % USER_ID) self.assertEqual(instance.text, u'Добрый день , кароче такая идея когда опросы создаешь вместо статуса - можно выбрать аудитории опрашиваемых, например только женский или мужской пол могут участвовать (то бишь голосовать в опросе).') self.assertEqual(instance.author, author) self.assertTrue(isinstance(instance.date, datetime)) instance = Comment(post=post) instance.parse(json.loads(response)['response'][2]) instance.save() self.assertEqual(instance.remote_id, '%s_2507' % USER_ID) self.assertEqual(instance.reply_for.remote_id, 16271479)
def test_author_can_update_own_comment(self, api_client): new_comment = CommentFactory() api_client.force_authenticate(new_comment.author) data = {'title': 'updated title'} response = api_client.patch(new_comment.get_absolute_url(), data) assert response.status_code == 200
def test_comment_detail_page_render(self, api_client): new_comment = CommentFactory() api_client.force_authenticate(new_comment.author) response = api_client.get(new_comment.get_absolute_url()) assert response.status_code == 200
def test_comment_crud_methods(self): text = 'Test message' user = UserFactory.create(remote_id=TRAVIS_USER_ID) post = PostFactory.create(remote_id=TR_POST_ID, wall_owner=user) mock_comment = CommentFactory.create(text=text, post=post, wall_owner=user) # Create comment = Comment.objects.create( **CommentFactory.get_mock_params_dict( mock_comment, commit_remote=True) ) self.assertTrue(comment.remote_id > 0) self.assertEqual(comment.text, text) fetched_comment = post.fetch_comments(sort='asc').first() self.assertEqual(fetched_comment.text, text) # Update edited_message = 'Edited comment message' comment = Comment.objects.get(id=comment.id) comment.text = edited_message comment.save(commit_remote=True) self.assertEqual(comment.text, edited_message) fetched_comment = post.fetch_comments(sort='asc').first() self.assertEqual(fetched_comment.text, edited_message) # Delete comment.delete() comment1 = Comment.objects.get(id=comment.id) self.assertTrue(comment1.archived) fetched_ids = [comment.remote_id for comment in post.fetch_comments()] self.assertFalse(comment1.remote_id in fetched_ids) # Restore comment.restore() comment1 = Comment.objects.get(id=comment.id) self.assertFalse(comment1.archived) fetched_ids = [comment.remote_id for comment in post.fetch_comments()] self.assertTrue(comment1.remote_id in fetched_ids) # Create with save() comment = Comment() comment.__dict__.update( CommentFactory.get_mock_params_dict(mock_comment) ) comment.text = text + text comment.save(commit_remote=True) self.assertTrue(comment.remote_id > 0) self.assertEqual(comment.text, text + text) fetched_comment = post.fetch_comments(sort='asc').first() self.assertEqual(fetched_comment.text, text + text) comment.delete()
def test_not_author_cant_update_comment(self, api_client): new_comment = CommentFactory() random_user = UserFactory() api_client.force_authenticate(random_user) data = {'title': 'updated title'} response = api_client.patch(new_comment.get_absolute_url(), data) assert response.status_code == 403
def test_fetch_comment_likes(self): discussion = DiscussionFactory(id=GROUP_COMMENT_WITH_MANY_LIKES1_DISCUSSION_ID, object_type='GROUP_TOPIC') comment = CommentFactory(id=GROUP_COMMENT_WITH_MANY_LIKES1_ID, object_type='GROUP_TOPIC', discussion=discussion) users_initial = User.objects.count() users = comment.fetch_likes(all=True) self.assertTrue(comment.likes_count > 19) self.assertEqual(comment.likes_count, users.count()) self.assertEqual(comment.likes_count, User.objects.count() - users_initial) self.assertEqual(comment.likes_count, comment.like_users.count())
def test_fetch_comment_likes(self): post = PostFactory(graph_id=POST1_ID) comment = CommentFactory(graph_id=COMMENT1_ID, post=post) self.assertEqual(comment.like_users.count(), 0) self.assertEqual(User.objects.count(), 2) users = comment.fetch_likes(all=True) self.assertTrue(users.count() > 7) self.assertEqual(comment.likes_count, users.count()) self.assertEqual(comment.likes_count, User.objects.count() - 2) self.assertEqual(comment.likes_count, comment.like_users.count())
def test_fetch_group_post_comment_likes(self, *args, **kwargs): group = GroupFactory(remote_id=GROUP_ID) post = PostFactory(remote_id=GROUP_POST_ID, wall_owner=group) comment = CommentFactory(remote_id=GROUP_COMMENT_ID, post=post, wall_owner=group) self.assertEqual(comment.like_users.count(), 0) self.assertEqual(comment.likes, 0) users_initial = User.objects.count() users = comment.fetch_likes(all=True) self.assertTrue(comment.likes > 0) self.assertEqual(comment.likes, len(users)) self.assertEqual(comment.likes, User.objects.count() - users_initial) self.assertEqual(comment.likes, comment.like_users.count())
def test_comment_create_post_request_not_allowed(self, api_client): new_user = UserFactory() comment_data = CommentFactory.build() new_recipe = RecipeFactory() data = { 'title': {comment_data.title}, 'recipe': {new_recipe.id}, 'author': {new_user.id} } response = api_client.post(create_comment_url, data) assert response.status_code == 401
def test_comment_author_is_current_logged_in_user(self, api_client): ''' testing the method perform_create ''' new_user = UserFactory() api_client.force_authenticate(new_user) new_recipe = RecipeFactory() comment_data = CommentFactory.build() data = { 'title': {comment_data.title}, 'recipe': {new_recipe.id}, 'author': {new_user.id} } api_client.post(create_comment_url, data) new_comment = Comment.objects.get(title=comment_data.title) assert new_comment.author == new_user
def test_recipe_author_can_delete_other_users_recipe_comments_on_his_recipe( self, api_client): new_recipe = RecipeFactory() new_user = UserFactory() api_client.force_authenticate(new_user) comment_data = CommentFactory.build() data = { 'title': {comment_data.title}, 'recipe': {new_recipe.id}, 'author': {new_user.id} } response = api_client.post(create_comment_url, data) api_client.logout() api_client.force_authenticate(new_recipe.author) new_comment = Comment.objects.get(title=comment_data.title) response = api_client.delete(new_comment.get_delete_url()) assert response.status_code == 204
def fetch_post_comments_recursive_calls_ammount_side_effect( *args, **kwargs): comments_count = 100 if kwargs['offset'] == 0 else 6 comments = [CommentFactory() for i in range(comments_count)] return Comment.objects.filter( pk__in=[comment.pk for comment in comments])
def test_get_delete_url(): new_comment = CommentFactory() assert new_comment.get_delete_url() == reverse( 'comments:delete', kwargs={"pk": new_comment.id})
def test_guest_user_cant_update_comment(self, api_client): first_comment = CommentFactory() data = {'title': 'updated title'} response = api_client.patch(first_comment.get_absolute_url(), data) assert response.status_code == 401
def test_detail_reverse(self): new_comment = CommentFactory() url = reverse('comments:delete', kwargs={'pk': new_comment.id}) assert url == f'/api/comments/delete/{new_comment.id}/'
def test_detail_resolve(self): new_comment = CommentFactory() url = f'/api/comments/delete/{new_comment.id}/' assert resolve(url).view_name == 'comments:delete'
def test_get_absolute_url(): new_comment = CommentFactory() assert new_comment.get_absolute_url() == reverse( 'comments:detail', kwargs={"pk": new_comment.id})
def test_author_can_delete_own_comment(self, api_client): new_comment = CommentFactory() api_client.force_authenticate(new_comment.author) response = api_client.delete(new_comment.get_absolute_url()) assert response.status_code == 204
def test_parse_comment(self): response = '''{"response":[6, {"cid":2505,"uid":16271479,"date":1298365200,"text":"Добрый день , кароче такая идея когда опросы создаешь вместо статуса - можно выбрать аудитории опрашиваемых, например только женский или мужской пол могут участвовать (то бишь голосовать в опросе)."}, {"cid":2507,"uid":16271479,"date":1286105582,"text":"Это уже не практично, имхо.<br>Для этого делайте группу и там опрос, а в группу принимайте тех, кого нужно.","reply_to_uid":16271479,"reply_to_cid":2505}, {"cid":2547,"uid":2943,"date":1286218080,"text":"Он будет только для групп благотворительных организаций."}]} ''' user = UserFactory(remote_id=USER_ID) post = PostFactory(remote_id=POST_ID, wall_owner=user) #instance = Comment(post=post) instance = CommentFactory(post=post) author = UserFactory(remote_id=16271479) instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, '%s_2505' % USER_ID) self.assertEqual( instance.text, u'Добрый день , кароче такая идея когда опросы создаешь вместо статуса - можно выбрать аудитории опрашиваемых, например только женский или мужской пол могут участвовать (то бишь голосовать в опросе).' ) self.assertEqual(instance.author, author) self.assertTrue(isinstance(instance.date, datetime)) instance = Comment(post=post) instance.parse(json.loads(response)['response'][2]) instance.save() self.assertEqual(instance.remote_id, '%s_2507' % USER_ID) self.assertEqual(instance.reply_for.remote_id, 16271479)
def test__str__(): new_comment = CommentFactory() assert new_comment.__str__() == new_comment.title
def test_comment_delete(self, api_client): new_comment = CommentFactory() response = api_client.delete(new_comment.get_absolute_url()) assert response.status_code == 401
def fetch_post_comments_recursive_calls_ammount_side_effect(*args, **kwargs): comments_count = 100 if kwargs['offset'] == 0 else 6 comments = [CommentFactory.create() for i in range(comments_count)] return Comment.objects.filter(pk__in=[comment.pk for comment in comments])