def test_index_blog_shows_posts_and_can_be_consulted(self): # Nato goes to the blog and creates a posts and keeps the url first_post = PostFactory.build(title='First post title') create_post_page = CreatePostPage(self).create_post(first_post) first_post_url = self.browser.current_url # He can see the post in the index page of the posts index_blog_page = IndexBlogPage(self).go_to_index_blog_page() index_blog_page.wait_for_title_post_in_the_posts(first_post.title) # He clicks the Read More link and can see that he is in the page # for that post post = index_blog_page.get_post_from_this_page(first_post.title) post.find_element_by_link_text('READ MORE').click() self.wait_for( lambda: self.assertEqual(self.browser.current_url, first_post_url) ) # He creates another post and again saves the url second_post = PostFactory.build(title='Second post title') create_post_page.create_post(second_post) second_post_url = self.browser.current_url # And now he sees the new created post in the index index_blog_page = IndexBlogPage(self).go_to_index_blog_page() index_blog_page.wait_for_title_post_in_the_posts(second_post.title) # And he checks is the page that shows him the post post = index_blog_page.get_post_from_this_page(second_post.title) post.find_element_by_link_text('READ MORE').click() self.wait_for( lambda: self.assertEqual(self.browser.current_url, second_post_url) )
def test_passes_correct_post_to_template(self): correct_post = PostFactory() PostFactory(title='other post') response = self.client.get( reverse('posts:show', kwargs={'post_id': correct_post.id})) self.assertEqual(response.context['post'], correct_post)
def test_POST_incorrect_date_answers_not_saved_json(self): post = PostFactory() post.publication_date = '---' response = self.POST_post_object_to_ajax_publication_date_url(post) self.assertJSONEqual(str(response.content, encoding='utf8'), {'message': 'Enter a valid date.'})
def test_deletes_view_destroys_a_post(self): first_post = PostFactory(title='First post') PostFactory(title='Second post') self.client.post( reverse('posts:delete', kwargs={'post_id': first_post.id})) self.assertEqual(Post.objects.count(), 1)
def test_POST_incorrect_publication_date_does_not_change_saved_post(self): post = PostFactory() post.publication_date = '---' self.POST_post_object_to_ajax_publication_date_url(post) saved_post = Post.objects.first() self.assertNotEqual(post.publication_date, saved_post.publication_date)
def test_POST_correct_date_saves_new_post(self): post = PostFactory() post.publication_date = post.publication_date + datetime.timedelta(1) self.POST_post_object_to_ajax_publication_date_url(post) saved_post = Post.objects.first() self.assertEqual(post.publication_date, saved_post.publication_date)
def test_POST_correct_date_answers_correct_json(self): post = PostFactory() post.publication_date = datetime.date.today() response = self.POST_post_object_to_ajax_publication_date_url(post) self.assertJSONEqual( str(response.content, encoding='utf8'), {'message': 'The publication date has been saved.'})
def test_displays_correct_posts(self): PostFactory(title='first post title') PostFactory(title='second post title') PostFactory.build(title='This post has not been saved') response = self.client.get(reverse('home')) self.assertContains(response, 'first post title') self.assertContains(response, 'second post title') self.assertNotContains(response, 'This post has not been saved')
def test_POST_incorrect_post_answers_not_saved_json(self): post = PostFactory() post.title = '' post.content = '' response = self.POST_post_object_to_ajax_publication_date_url(post) self.assertJSONEqual( str(response.content, encoding='utf8'), {'message': 'The publication date has not been saved.'})
def test_displays_only_publicated_posts(self): PostFactory(title='Publicated post') expired_post = PostFactory.build(title='Not publicated post') expired_post.publication_date = datetime.date.today() + datetime.timedelta(1) expired_post.save() response = self.client.get(reverse('home')) self.assertContains(response, 'Publicated post') self.assertNotContains(response, 'Not publicated post')
def test_displays_correct_post(self): correct_post = PostFactory() other_post = PostFactory(title='other post') response = self.client.get( reverse('posts:edit', kwargs={'post_id': correct_post.id})) self.assertContains(response, correct_post.title) self.assertEqual(response.context['form'].instance.image, correct_post.image) self.assertContains(response, correct_post.content) self.assertContains(response, correct_post.expiring_date.strftime("%Y-%m-%d")) self.assertNotContains(response, other_post.title)
def test_displays_correct_post(self): correct_post = PostFactory() other_post = PostFactory(title='other post') response = self.client.get( reverse('posts:show', kwargs={'post_id': correct_post.id})) self.assertContains(response, correct_post.title) self.assertEqual(response.context['post'].image, correct_post.image) self.assertContains(response, correct_post.content) self.assertContains( response, correct_post.publication_date.strftime('%b. %d, %Y')) self.assertContains(response, correct_post.expiring_date.strftime('%b. %d, %Y')) self.assertNotContains(response, other_post.title)
def test_can_edit_a_post(self): # Nato goes to the blog and creates a post post_object = PostFactory.build() CreatePostPage(self).create_post(post_object) # Then he proceeds to edit it # He first goes to the page of the post index_post_page = IndexPostPage(self).go_to_index_post_page() post_html = index_post_page.get_post_from_this_page(post_object.title) post_html.find_element_by_link_text('EDIT POST').click() # Then he starts to edit the post field by field edit_post_page = EditPostPage(self) edit_post_page.write_in_title_input_box('New title') edit_post_page.write_in_content_input_box('New content') # He puts the image for the post ## TODO: today_plus_three_days = datetime.date.today() + datetime.timedelta(3) edit_post_page.write_expiring_date(today_plus_three_days.strftime('%Y-%m-%d')) # Then he presses the save button edit_post_page.click_save_post() edit_post_page.check_message_in_messages('The blog post has been updated') # And he can see the post has changed body_text = self.browser.find_element_by_tag_name('body').text self.assertIn('New title', body_text) self.assertIn('New content', body_text) self.assertIn(post_object.publication_date.strftime('%b. %d, %Y'), body_text) self.assertIn(today_plus_three_days.strftime('%b. %d, %Y'), body_text)
def test_displays_post_that_expire_today(self): today_expires_post = PostFactory.build(title='Today expires post') today_expires_post.publication_date = datetime.date.today() today_expires_post.expiring_date = datetime.date.today() today_expires_post.save() response = self.client.get(reverse('home')) self.assertContains(response, 'Today expires post')
def test_index_blog_does_not_show_non_published_post(self): # Nato goes to the blog and creates an expired post non_published_post = PostFactory.build(title='Expired post') non_published_post.publication_date = datetime.date.today( ) + datetime.timedelta(1) create_post_page = CreatePostPage(self).create_post(non_published_post) # And creates a non expired post published_post = PostFactory.build(title='Not expired post') published_post.publication_date = datetime.date.today() published_post.expiring_date = datetime.date.today() create_post_page.create_post(published_post) # He can see the post in the index page of the posts index_post_page = IndexBlogPage(self).go_to_index_blog_page() index_post_page.wait_for_title_post_in_the_posts(published_post.title) # And as expected he can't see the expired post body = self.browser.find_element_by_tag_name('body') self.assertNotIn(non_published_post.title, body.text)
def test_can_delete_a_post(self): # Nato goes to the blog and creates two post first_post = PostFactory.build(title='First post title') second_post = PostFactory.build(title='Second post title') create_post_page = CreatePostPage(self).create_post(first_post) create_post_page.create_post(second_post) # He can see the posts in the index index_post_page = IndexPostPage(self).go_to_index_post_page() index_post_page.wait_for_title_post_in_the_posts(first_post.title) index_post_page.wait_for_title_post_in_the_posts(second_post.title) # He clicks the Delete button of one post post = index_post_page.get_post_from_this_page(second_post.title) post.find_element_by_name('delete post').click() index_post_page.check_message_in_messages( 'The blog post has been succesfully deleted') # and he can see the post has been deleted body = self.browser.find_element_by_tag_name('body') self.assertNotIn(second_post.title, body.text) index_post_page.wait_for_title_post_in_the_posts(first_post.title)
def test_index_post_shows_truncated_content(self): # Nato goes to the blog and creates a post with a long content post = PostFactory.build(title='First post title') fake = Faker() post.content = fake.sentence(nb_words=100) CreatePostPage(self).create_post(post) # He can see the post in the index page of the posts index_post_page = IndexPostPage(self).go_to_index_post_page() post_html = index_post_page.get_post_from_this_page(post.title) # And he can see the content has been trimmed trimmed_content = post.content[:97] + '...' trimmed_content = trimmed_content.replace('\n', ' ') self.assertIn(trimmed_content, post_html.text)
def test_publication_date_updates_asynchronous(self): # Nato goes to the blog and creates a post post_object = PostFactory.build() CreatePostPage(self).create_post(post_object) first_browser = self.browser self.addCleanup(lambda: quit_if_possible(first_browser)) # And he can see it index_blog_page = IndexBlogPage(self).go_to_index_blog_page() index_blog_page.wait_for_title_post_in_the_posts(post_object.title) # Then he proceeds to edit it in another browser second_browser = webdriver.Firefox() self.addCleanup(lambda: quit_if_possible(second_browser)) self.browser = second_browser # He first goes to the page of the post index_post_page = IndexPostPage(self).go_to_index_post_page() post_html = index_post_page.get_post_from_this_page(post_object.title) post_html.find_element_by_link_text('EDIT POST').click() # Then he starts to edit the post publication date edit_post_page = EditPostPage(self) today_plus_three_days = datetime.date.today() + datetime.timedelta(3) edit_post_page.write_publication_date( today_plus_three_days.strftime('%Y-%m-%d')) ## Change the focus so the event is complete edit_post_page.write_in_title_input_box("") # And he sees a message telling him the publication date has changed whitout saving # the post edit_post_page.check_message_in_messages( "The publication date has been saved.") # And he can see the post has dissapeared from the index blog page self.browser = first_browser index_blog_page = IndexBlogPage(self).go_to_index_blog_page() body_text = self.browser.find_element_by_tag_name('body').text self.assertNotIn(post_object.title, body_text)
def test_uses_show_post_template(self): post = PostFactory() response = self.client.get( reverse('posts:show', kwargs={'post_id': post.id})) self.assertTemplateUsed(response, 'posts/show.html')
def test_deletes_view_redirects_to_index(self): post = PostFactory() response = self.client.post( reverse('posts:delete', kwargs={'post_id': post.id})) self.assertRedirects(response, reverse('posts:index'))
def post_invalid_post(self): post = PostFactory.build(title='') return self.POST_object_to_store_url(post)