def test_get_post_page_data(self): self.login(self.user_email) blog_post = blog_services.get_blog_post_by_id(self.blog_post.id) json_response = self.get_json( '%s/%s' % (feconf.BLOG_HOMEPAGE_URL, blog_post.url_fragment), ) self.assertEqual(self.BLOG_ADMIN_USERNAME, json_response['blog_post_dict']['author_name']) self.assertEqual('<p>Hello Bloggers</p>', json_response['blog_post_dict']['content']) self.assertEqual(len(json_response['summary_dicts']), 1) self.assertIsNotNone(json_response['profile_picture_data_url']) blog_post_two_id = (blog_services.create_new_blog_post( self.blog_admin_id).id) change_dict_two = { 'title': 'Sample Title Two', 'thumbnail_filename': 'thumbnail.svg', 'content': '<p>Hello Blog</p>', 'tags': ['Newsletter', 'Learners'] } blog_services.update_blog_post(blog_post_two_id, change_dict_two) blog_services.publish_blog_post(blog_post_two_id) blog_post_two = blog_services.get_blog_post_by_id(blog_post_two_id) json_response = self.get_json( '%s/%s' % (feconf.BLOG_HOMEPAGE_URL, blog_post_two.url_fragment), ) self.assertEqual(self.BLOG_ADMIN_USERNAME, json_response['blog_post_dict']['author_name']) self.assertEqual('<p>Hello Blog</p>', json_response['blog_post_dict']['content']) self.assertEqual(len(json_response['summary_dicts']), 2) self.assertIsNotNone(json_response['profile_picture_data_url'])
def put(self): blog_post_id = self.normalized_payload.get('blog_post_id') author_username = self.normalized_payload.get('author_username') published_on = self.normalized_payload.get('published_on') author_id = user_services.get_user_id_from_username(author_username) if author_id is None: raise self.InvalidInputException('Invalid username: %s' % author_username) user_actions = user_services.get_user_actions_info(author_id).actions if role_services.ACTION_ACCESS_BLOG_DASHBOARD not in user_actions: raise self.InvalidInputException( 'User does not have enough rights to be blog post author.') blog_post = (blog_services.get_blog_post_by_id(blog_post_id, strict=False)) if blog_post is None: raise self.PageNotFoundException( Exception( 'The blog post with the given id or url doesn\'t exist.')) blog_services.update_blog_models_author_and_published_on_date( blog_post_id, author_id, published_on) self.render_json({})
def test_put_blog_post_data(self): # Checks blog editor can edit owned blog post. self.login(self.BLOG_EDITOR_EMAIL) csrf_token = self.get_new_csrf_token() payload = { 'change_dict': { 'title': 'Sample Title', 'content': '<p>Hello<p>', 'tags': ['New lessons', 'Learners'], 'thumbnail_filename': 'file.svg' }, 'new_publish_status': False } json_response = self.put_json( '%s/%s' % (feconf.BLOG_EDITOR_DATA_URL_PREFIX, self.blog_post.id), payload, csrf_token=csrf_token) self.assertEqual( json_response['blog_post']['title'], 'Sample Title') blog_post = ( blog_services.get_blog_post_by_id(self.blog_post.id)) self.assertEqual( blog_post.thumbnail_filename, 'file.svg') self.logout()
def get(self, blog_post_id: str) -> None: """Populates the data on the blog dashboard editor page.""" blog_domain.BlogPost.require_valid_blog_post_id(blog_post_id) blog_post = (blog_services.get_blog_post_by_id(blog_post_id, strict=False)) if blog_post is None: raise self.PageNotFoundException( 'The blog post with the given id or url doesn\'t exist.') user_settings = user_services.get_users_settings( [blog_post.author_id], strict=False, include_marked_deleted=True) username = user_settings[0].username max_no_of_tags = config_domain.Registry.get_config_property( 'max_number_of_tags_assigned_to_blog_post').value list_of_default_tags = config_domain.Registry.get_config_property( 'list_of_default_tags_for_blog_post').value blog_post_dict = blog_post.to_dict() del blog_post_dict['author_id'] blog_post_dict['author_username'] = username self.values.update({ 'blog_post_dict': blog_post_dict, 'username': username, 'profile_picture_data_url': (user_settings[0].profile_picture_data_url), 'max_no_of_tags': max_no_of_tags, 'list_of_default_tags': list_of_default_tags }) self.render_json(self.values)
def test_delete_blog_post(self): blog_services.delete_blog_post(self.blog_post_a_id) self.assertIsNone(blog_services.get_blog_post_rights( self.blog_post_a_id, strict=False)) self.assertIsNone(blog_services.get_blog_post_by_id( self.blog_post_a_id, strict=False)) self.assertIsNone(blog_services.get_blog_post_summary_by_id( self.blog_post_a_id, strict=False))
def test_get_blog_post_by_url_fragment(self): blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_one) expected_blog_post = ( blog_services.get_blog_post_by_id(self.blog_post_a_id)) lower_id = '-' + self.blog_post_a_id.lower() blog_post = blog_services.get_blog_post_by_url_fragment( 'sample-title' + lower_id) self.assertEqual(blog_post.to_dict(), expected_blog_post.to_dict())
def test_get_blog_post_by_url_fragment(self) -> None: blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_one) expected_blog_post = ( blog_services.get_blog_post_by_id(self.blog_post_a_id)) lower_id = '-' + self.blog_post_a_id.lower() blog_post = blog_services.get_blog_post_by_url_fragment( 'sample-title' + lower_id) # Ruling out the possibility of None for mypy type checking. assert blog_post is not None self.assertEqual(blog_post.to_dict(), expected_blog_post.to_dict())
def test_raise_exception_if_blog_post_does_not_exists(self): self.login(self.user_email) blog_post = blog_services.get_blog_post_by_id(self.blog_post.id) self.get_json( '%s/%s' % (feconf.BLOG_HOMEPAGE_URL, blog_post.url_fragment), ) blog_services.delete_blog_post(blog_post.id) self.get_json( '%s/%s' % (feconf.BLOG_HOMEPAGE_URL, blog_post.url_fragment), expected_status_int=404 )
def test_update_blog_post(self): self.assertEqual(self.blog_post_a.title, '') blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_one) updated_blog_post = ( blog_services.get_blog_post_by_id(self.blog_post_a_id)) self.assertEqual(updated_blog_post.thumbnail_filename, 'thummbnail.svg') self.assertEqual(updated_blog_post.content, '<p>Hello</p>') lower_id = '-' + self.blog_post_a_id.lower() self.assertEqual( updated_blog_post.url_fragment, 'sample-title' + lower_id) blog_services.update_blog_post(self.blog_post_a_id, self.change_dict) updated_blog_post = ( blog_services.get_blog_post_by_id(self.blog_post_a_id)) self.assertEqual(updated_blog_post.tags, ['one', 'two']) with self.assertRaisesRegex( Exception, ( 'Blog Post with given title already exists: %s' % 'Sample Title')): blog_services.update_blog_post( self.blog_post_b_id, self.change_dict_one)
def test_publish_blog_post(self): blog_post_rights = ( blog_services.get_blog_post_rights(self.blog_post_a_id)) self.assertFalse(blog_post_rights.blog_post_is_published) blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_two) blog_services.publish_blog_post(self.blog_post_a_id) blog_post_summary = ( blog_services.get_blog_post_summary_by_id(self.blog_post_a_id)) blog_post = blog_services.get_blog_post_by_id(self.blog_post_a_id) blog_post_rights = ( blog_services.get_blog_post_rights(self.blog_post_a_id)) self.assertTrue(blog_post_rights.blog_post_is_published) self.assertIsNotNone(blog_post.published_on) self.assertIsNotNone(blog_post_summary.published_on) self.assertEqual( blog_post.published_on, blog_post_summary.published_on)
def put(self, blog_post_id: str) -> None: """Updates properties of the given blog post.""" blog_domain.BlogPost.require_valid_blog_post_id(blog_post_id) blog_post_rights = (blog_services.get_blog_post_rights(blog_post_id, strict=False)) blog_post_currently_published = blog_post_rights.blog_post_is_published change_dict = self.normalized_payload.get('change_dict') blog_services.update_blog_post(blog_post_id, change_dict) new_publish_status = self.normalized_payload.get('new_publish_status') if new_publish_status: blog_services.publish_blog_post(blog_post_id) elif blog_post_currently_published: blog_services.unpublish_blog_post(blog_post_id) blog_post_dict = ( blog_services.get_blog_post_by_id(blog_post_id).to_dict()) self.values.update({'blog_post': blog_post_dict}) self.render_json(self.values)
def test_get_blog_post_by_id(self): expected_blog_post = self.blog_post_a.to_dict() blog_post = blog_services.get_blog_post_by_id(self.blog_post_a_id) self.assertEqual(blog_post.to_dict(), expected_blog_post)