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 test_get_homepage_data(self): self.login(self.user_email) json_response = self.get_json( '%s' % (feconf.BLOG_HOMEPAGE_DATA_URL), ) default_tags = config_domain.Registry.get_config_property( 'list_of_default_tags_for_blog_post').value self.assertEqual(default_tags, json_response['list_of_default_tags']) self.assertEqual( self.BLOG_ADMIN_USERNAME, json_response['blog_post_summary_dicts'][0]['author_name']) self.assertEqual( len(json_response['blog_post_summary_dicts']), 1) blog_post_two = blog_services.create_new_blog_post(self.blog_admin_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) json_response = self.get_json( '%s' % feconf.BLOG_HOMEPAGE_DATA_URL) self.assertEqual( len(json_response['blog_post_summary_dicts']), 2) self.assertTrue( json_response['blog_post_summary_dicts'][0]['published_on'] > json_response['blog_post_summary_dicts'][1]['published_on'] ) self.assertEqual( json_response['blog_post_summary_dicts'][0]['title'], 'Sample Title Two' )
def test_cannot_publish_invalid_blog_post(self): """Checks that an invalid blog post is not published.""" with self.assertRaisesRegex( Exception, ('Title should not be empty')): blog_services.publish_blog_post(self.blog_post_a_id) blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_one) with self.assertRaisesRegex( Exception, ('Atleast one tag should be selected')): blog_services.publish_blog_post(self.blog_post_a_id) change_dict_three = { 'title': 'Sample', 'thumbnail_filename': 'thummbnail.svg', 'content': '', 'tags': ['one', 'two'] } blog_services.update_blog_post(self.blog_post_a_id, change_dict_three) with self.assertRaisesRegex( Exception, ('Content can not be empty')): blog_services.publish_blog_post(self.blog_post_a_id) blog_services.delete_blog_post(self.blog_post_a_id) with self.assertRaisesRegex( Exception, ('The given blog post does not exist')): blog_services.publish_blog_post(self.blog_post_a_id)
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_does_blog_post_with_url_fragment_exist(self): blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_one) lower_id = '-' + self.blog_post_a_id.lower() self.assertTrue( blog_services.does_blog_post_with_url_fragment_exist( 'sample-title' + lower_id)) self.assertFalse( blog_services.does_blog_post_with_url_fragment_exist('title'))
def test_get_published_blog_post_summaries(self) -> None: self.assertEqual( len(blog_services.get_published_blog_post_summaries()), 0) blog_services.update_blog_post(self.blog_post_a_id, self.change_dict_two) blog_services.publish_blog_post(self.blog_post_a_id) number_of_published_blogs = ( blog_services.get_published_blog_post_summaries()) self.assertEqual(len(number_of_published_blogs), 1)
def test_filter_blog_post_ids(self): blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_two) blog_services.publish_blog_post(self.blog_post_a_id) filtered_model_ids = ( blog_services.filter_blog_post_ids(self.user_id_a, True)) self.assertEqual(filtered_model_ids, [self.blog_post_a_id]) filtered_model_ids = ( blog_services.filter_blog_post_ids(self.user_id_b, False)) self.assertEqual(filtered_model_ids, [self.blog_post_b_id])
def test_get_dashboard_page_data(self): # Checks blog editor can access blog dashboard. self.login(self.BLOG_EDITOR_EMAIL) json_response = self.get_json( '%s' % (feconf.BLOG_DASHBOARD_DATA_URL), ) self.assertEqual(self.BLOG_EDITOR_USERNAME, json_response['username']) self.assertIsNone(json_response['published_blog_post_summary_dicts']) self.assertIsNone(json_response['draft_blog_post_summary_dicts']) self.logout() # Checks blog admin can access blog dashboard. self.login(self.BLOG_ADMIN_EMAIL) json_response = self.get_json( '%s' % (feconf.BLOG_DASHBOARD_DATA_URL), ) self.assertEqual(self.BLOG_ADMIN_USERNAME, json_response['username']) self.assertIsNone(json_response['published_blog_post_summary_dicts']) self.assertIsNone(json_response['draft_blog_post_summary_dicts']) self.logout() # Checks non blog-admins and non-editors can not access blog dashboard. self.login(self.user_email) json_response = self.get_json( '%s' % (feconf.BLOG_DASHBOARD_DATA_URL), expected_status_int=401) self.logout() # Checks for correct published and draft blog post summary data. blog_post = blog_services.create_new_blog_post(self.blog_editor_id) change_dict = { 'title': 'Sample Title', 'thumbnail_filename': 'thumbnail.svg', 'content': '<p>Hello Bloggers<p>', 'tags': ['Newsletter', 'Learners'] } self.login(self.BLOG_EDITOR_EMAIL) json_response = self.get_json( '%s' % (feconf.BLOG_DASHBOARD_DATA_URL)) self.assertEqual(self.BLOG_EDITOR_USERNAME, json_response['username']) self.assertEqual( blog_post.id, json_response['draft_blog_post_summary_dicts'][0]['id']) blog_services.update_blog_post(blog_post.id, change_dict) blog_services.publish_blog_post(blog_post.id) json_response = self.get_json( '%s' % (feconf.BLOG_DASHBOARD_DATA_URL)) self.assertEqual(self.BLOG_EDITOR_USERNAME, json_response['username']) self.assertEqual( blog_post.id, json_response['published_blog_post_summary_dicts'][0]['id']) self.assertEqual( change_dict['title'], json_response['published_blog_post_summary_dicts'][0]['title']) self.assertIsNone(json_response['draft_blog_post_summary_dicts'])
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_get_published_blog_post_summaries(self): self.assertIsNone( blog_services.get_published_blog_post_summaries() ) blog_services.update_blog_post( self.blog_post_a_id, self.change_dict_two) blog_services.publish_blog_post(self.blog_post_a_id) self.assertEqual( len(blog_services.get_published_blog_post_summaries()), 1 )
def test_unpublish_blog_post(self): 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_rights = ( blog_services.get_blog_post_rights(self.blog_post_a_id)) self.assertTrue(blog_post_rights.blog_post_is_published) blog_services.unpublish_blog_post(self.blog_post_a_id) blog_post_rights = ( blog_services.get_blog_post_rights(self.blog_post_a_id)) self.assertFalse(blog_post_rights.blog_post_is_published)
def test_get_published_blog_post_summaries_by_user_id(self) -> None: self.assertEqual( len( blog_services.get_published_blog_post_summaries_by_user_id( self.user_id_a, 20)), 0) blog_services.update_blog_post(self.blog_post_a_id, self.change_dict_two) blog_services.publish_blog_post(self.blog_post_a_id) no_of_published_blog_post = ( blog_services.get_published_blog_post_summaries_by_user_id( self.user_id_a, 20)) self.assertEqual(len(no_of_published_blog_post), 1)
def test_update_blog_post_summary(self) -> None: blog_post_summary = (blog_services.get_blog_post_summary_by_id( self.blog_post_a_id)) self.assertEqual(blog_post_summary.title, '') blog_services.update_blog_post(self.blog_post_a_id, self.change_dict_one) updated_blog_post_summary = (blog_services.get_blog_post_summary_by_id( self.blog_post_a_id)) self.assertEqual(updated_blog_post_summary.thumbnail_filename, 'thummbnail.svg') self.assertEqual(updated_blog_post_summary.summary, 'Hello') lower_id = '-' + self.blog_post_a_id.lower() self.assertEqual(updated_blog_post_summary.url_fragment, 'sample-title' + lower_id)
def setUp(self): """Complete the setup process for testing.""" super(BlogPostHandlerTest, self).setUp() self.signup(self.BLOG_ADMIN_EMAIL, self.BLOG_ADMIN_USERNAME) self.blog_admin_id = (self.get_user_id_from_email( self.BLOG_ADMIN_EMAIL)) self.add_user_role(self.BLOG_ADMIN_USERNAME, feconf.ROLE_ID_BLOG_ADMIN) self.signup(self.user_email, self.username) self.blog_post = blog_services.create_new_blog_post(self.blog_admin_id) self.change_dict = { 'title': 'Sample Title', 'thumbnail_filename': 'thumbnail.svg', 'content': '<p>Hello Bloggers</p>', 'tags': ['Newsletter', 'Learners'] } blog_services.update_blog_post(self.blog_post.id, self.change_dict) blog_services.publish_blog_post(self.blog_post.id)
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_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)