Exemplo n.º 1
0
class PostDetailTest(TestCase):
    def setUp(self):
        self.post = PostFactory(status='published')
        self.response = self.client.get(self.post.get_absolute_url())

    def test_post_detail_renders_detail_template(self):
        self.assertTemplateUsed(self.response, 'blog/post/detail.html')

    def test_response_contains_post_title(self):
        self.assertContains(self.response, self.post.title)

    def test_unknown_post_returns_404(self):
        response = self.client.get('/blog/2000/01/02/hello-world/')
        self.assertEqual(response.status_code, 404)

    def test_no_comments(self):
        self.assertEqual(self.response.context['post'], self.post)
        self.assertEqual(list(self.response.context['comments']), [])
        self.assertEqual(self.response.context['new_comment'], None)

    def test_incorrect_post(self):
        bad_response = self.client.post(self.post.get_absolute_url(), {})
        self.assertContains(bad_response, 'This field is required')
        self.assertEqual(self.response.context['new_comment'], None)

    def test_correct_post(self):
        good_response = self.client.post(
            self.post.get_absolute_url(), {
                'name': 'user',
                'email': '*****@*****.**',
                'body': 'Sample comment body'
            })
        self.assertContains(good_response, 'Sample comment body')
        self.assertContains(good_response, 'Your comment has been added.')
        self.assertEqual(good_response.context['new_comment'],
                         Comment.objects.get(post=self.post))

    def test_similar_posts(self):
        PostFactory(status='published')
        post2 = PostFactory(status='published')
        post3 = PostFactory(status='published')
        post4 = PostFactory(status='published')

        post4.tags.add('tag0', 'tag1', 'tag2')
        post3.tags.add('tag0', 'tag1')
        post2.tags.add('tag0')

        response = self.client.get(post4.get_absolute_url())

        self.assertEqual(list(response.context['similar_posts']),
                         [post3, post2])
Exemplo n.º 2
0
    def test_similar_posts(self):
        PostFactory(status='published')
        post2 = PostFactory(status='published')
        post3 = PostFactory(status='published')
        post4 = PostFactory(status='published')

        post4.tags.add('tag0', 'tag1', 'tag2')
        post3.tags.add('tag0', 'tag1')
        post2.tags.add('tag0')

        response = self.client.get(post4.get_absolute_url())

        self.assertEqual(list(response.context['similar_posts']),
                         [post3, post2])
Exemplo n.º 3
0
class PostShareTest(TestCase):
    def setUp(self):
        self.post = PostFactory(status='published')
        self.response = self.client.get('/blog/%s/share/' % self.post.id)

    def sample_post_share(self):
        return self.client.post('/blog/%s/share/' % self.post.id,
                                data={
                                    'name': 'user',
                                    'sender': '*****@*****.**',
                                    'recipient': '*****@*****.**',
                                    'comments': 'sample comments'
                                })

    def test_post_share_renders_share_template(self):
        self.assertTemplateUsed(self.response, 'blog/post/share.html')

    def test_response_contains_post_title(self):
        self.assertContains(self.response, self.post.title)

    def test_unknown_post_returns_404(self):
        response = self.client.get('/blog/999/share/')
        self.assertEqual(response.status_code, 404)

    def test_get_method_does_not_send_email(self):
        self.assertEqual(len(mail.outbox), 0)

    def test_get_method_returns_form(self):
        self.assertContains(self.response, 'form')

    def test_post_method_send_email(self):
        self.sample_post_share()
        subject = 'user ([email protected]) recommends you reading "%s"' % self.post.title
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, subject)

    def test_post_method_send_email_with_correct_url(self):
        self.sample_post_share()
        self.assertIn(self.post.get_absolute_url(), mail.outbox[0].body)

    def test_post_method_returns_success_message(self):
        post_response = self.sample_post_share()
        self.assertContains(post_response,
                            '"%s" was successfully sent.' % self.post.title)

    def test_post_with_invalid_form_returns_form(self):
        bad_post_response = self.client.post('/blog/%s/share/' % self.post.id)
        self.assertContains(bad_post_response, 'form')
Exemplo n.º 4
0
class TestPostShare(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Firefox()
        self.post = PostFactory(status='published')

    def tearDown(self):
        self.browser.quit()

    def test_post_share(self):
        # User can saw share link on a post detail page
        self.browser.get(self.live_server_url + self.post.get_absolute_url())
        share_url = self.browser.find_element_by_link_text('Share this post')

        # He click on it and redirects to share form
        share_url.click()
        self.assertEqual(
            self.browser.find_element_by_tag_name('h1').text,
            'Share "%s" by e-mail' % self.post.title)

        # He clicks send button and sees required errors
        self.browser.find_element_by_id('send').click()
        self.assertEqual(
            len(self.browser.find_elements_by_class_name('errorlist')), 3)

        # He fills out the form and clicks send, than he sees a success page
        self.browser.find_element_by_id('id_name').send_keys('user')
        self.browser.find_element_by_id('id_sender').send_keys(
            '*****@*****.**')
        self.browser.find_element_by_id('id_recipient').send_keys(
            '*****@*****.**')
        self.browser.find_element_by_id('id_comments').send_keys(
            'sample comment')
        self.browser.find_element_by_id('send').click()
        self.assertEqual(
            self.browser.find_element_by_tag_name('h1').text,
            'E-mail successfully sent')

        # Email has been sent
        self.assertEqual(len(mail.outbox), 1)
        subject = 'user ([email protected]) recommends you reading "%s"' % self.post.title
        self.assertEqual(mail.outbox[0].subject, subject)
Exemplo n.º 5
0
class TestPostDetail(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Firefox()
        self.post = PostFactory(status='published',
                                publish=timezone.datetime(2016,
                                                          10,
                                                          11,
                                                          2,
                                                          27,
                                                          tzinfo=UTC))
        self.post.tags.add('tag0', 'tag1')

        # He create and tag few posts
        self.post1 = PostFactory(status='published')
        self.post2 = PostFactory(status='published')
        self.post3 = PostFactory(status='published')

        self.post1.tags.add('tag0', 'tag1')
        self.post2.tags.add('tag0')

        # User goes to the post page
        self.browser.get(self.live_server_url + self.post.get_absolute_url())

    def tearDown(self):
        self.browser.quit()

    def add_comment(self, name, email, body):
        self.browser.find_element_by_id('id_name').send_keys(name)
        self.browser.find_element_by_id('id_email').send_keys(email)
        self.browser.find_element_by_id('id_body').send_keys(body)
        self.browser.find_element_by_css_selector('input[type=submit]').click()

    def test_post_detail(self):
        # He sees the post title, date, author and body
        self.assertEqual(
            self.browser.find_element_by_css_selector('h1').text,
            self.post.title)
        self.assertEqual(
            self.browser.find_element_by_css_selector(
                '#container p:nth-child(3)').text, self.post.body)
        date_author = 'Published Oct. 11, 2016, 5:27 a.m. by %s' % self.post.author.username
        self.assertEqual(
            self.browser.find_element_by_class_name('date').text, date_author)

    def test_comments(self):
        # He does not see any comments yet
        self.assertEqual(
            self.browser.find_element_by_id('comments-counter').text,
            '0 comments')
        self.assertEqual(
            self.browser.find_element_by_id('empty').text,
            'There are no comments yet.')

        # But he see a form to enter a new comment
        self.browser.find_element_by_id('new-comment')

        # He adds a comment
        self.add_comment('user1', '*****@*****.**', 'First comment body')

        # And sees success message
        self.assertEqual(
            self.browser.find_element_by_id('adding-success').text,
            'Your comment has been added.')

        # And his comment
        self.assertEqual(
            self.browser.find_element_by_css_selector(
                '.comment p:last-child').text, 'First comment body')

        # He refreshes the page and adds two more comments
        self.browser.get(self.live_server_url + self.post.get_absolute_url())
        self.add_comment('user2', '*****@*****.**', 'Second comment body')
        self.browser.get(self.live_server_url + self.post.get_absolute_url())
        self.add_comment('user3', '*****@*****.**', 'Third comment body')

        # He sees all comments in right order
        comments = [
            comment.text for comment in
            self.browser.find_elements_by_css_selector('.comment p:last-child')
        ]
        self.assertEqual(comments, [
            'First comment body', 'Second comment body', 'Third comment body'
        ])

    def test_similar_posts(self):
        # He can see similar posts
        similar_posts = [
            post.text for post in self.browser.find_elements_by_css_selector(
                '.similar-posts p a')
        ]
        self.assertEqual(similar_posts, [self.post1.title, self.post2.title])

        # There is no similar posts for post3
        self.browser.get(self.live_server_url + self.post3.get_absolute_url())
        self.assertEqual(
            self.browser.find_element_by_css_selector('.similar-posts p').text,
            'There are no similar posts yet.')
Exemplo n.º 6
0
 def test_absolute_url(self):
     post = PostFactory()
     self.assertEqual(
         post.get_absolute_url(), '/blog/%04d/%02d/%02d/%s/' %
         (int(post.publish.year), int(
             post.publish.month), int(post.publish.day), post.slug))
Exemplo n.º 7
0
class TestPostList(StaticLiveServerTestCase):
    def setUp(self):
        self.post0 = PostFactory(status='published')
        self.post1 = PostFactory(status='published')
        self.post2 = PostFactory()
        self.post3 = PostFactory(status='published')
        self.post4 = PostFactory(status='published')

        self.post3.tags.add('test_tag_0')
        self.post4.tags.add('test_tag_0')
        self.post4.tags.add('test_tag_1')

        self.browser = webdriver.Firefox()
        self.browser.get(self.live_server_url +
                         '/blog/')  # Go to the main page

    def tearDown(self):
        self.browser.quit()

    def can_see_actual_posts(self):
        posts = self.browser.find_elements_by_css_selector('#container a')
        post_titles = [post.text for post in posts]
        for post in [self.post4, self.post3, self.post1]:
            self.assertIn(post.title, post_titles)
        for post in [self.post2, self.post0]:
            self.assertNotIn(post.title, post_titles)

    def test_basic_post_list(self):
        # He sees the list of published posts
        self.can_see_actual_posts()

        # He can go to the second page and see old post
        self.browser.find_element_by_css_selector('.step-links a').click()
        self.assertEqual(
            self.browser.find_element_by_css_selector('#container a').text,
            self.post0.title)

        # And go back to the first page and see actual posts
        self.browser.find_element_by_css_selector('.step-links a').click()
        self.can_see_actual_posts()

        # He can go to specific post by click on it's title
        self.browser.find_element_by_css_selector('#container a').click()
        self.assertEqual(
            self.browser.find_element_by_tag_name('h1').text, self.post4.title)
        self.assertEqual(self.browser.current_url,
                         self.live_server_url + self.post4.get_absolute_url())

    def test_post_list_tags(self):
        # He sees post4 has two tags
        posts = self.browser.find_elements_by_class_name('post')
        tags = posts[0].find_elements_by_css_selector('.tags a')
        self.assertListEqual([tag.text for tag in tags],
                             ['test_tag_0', 'test_tag_1'])

        # And post3 has one tag
        tags = posts[1].find_elements_by_css_selector('.tags a')
        self.assertEqual([tag.text for tag in tags], ['test_tag_0'])

        # He clicks on the test_tag_0 and post list are filtered
        tags[0].click()
        self.assertEqual(
            self.browser.find_element_by_tag_name('h2').text,
            'Posts tagged with "test_tag_0"')
        titles = [
            title.text for title in self.browser.find_elements_by_css_selector(
                '.post h2 a')
        ]
        self.assertListEqual(titles, [self.post4.title, self.post3.title])

    def test_blog_tags__total_posts(self):
        """Custom total_posts tag works fine"""
        # User sees number of published posts in the sidebar welcome message
        sidebar = self.browser.find_element_by_id('sidebar')
        welcome_message = sidebar.find_element_by_tag_name('p').text
        self.assertIn('4', welcome_message)

    def test_blog_tags__show_latest_posts(self):
        """Custom show_latest_posts tag works fine"""
        self.post5 = PostFactory(status='published')
        self.post6 = PostFactory(status='published')
        self.browser.get(self.live_server_url + '/blog/')

        # User sees five latest posts
        expected_posts = [
            self.post6, self.post5, self.post4, self.post3, self.post1
        ]
        actual_post_elements = self.browser.find_elements_by_css_selector(
            '#latest-posts a')
        actual_post_titles = [post.text for post in actual_post_elements]
        self.assertEqual(actual_post_titles,
                         [post.title for post in expected_posts])

    def test_blog_tags__get_most_commented_posts(self):
        """Custom get_most_commented_posts tag works fine"""
        for _ in range(4):
            CommentFactory(post=self.post4)
        for _ in range(3):
            CommentFactory(post=self.post1)
        for _ in range(2):
            CommentFactory(post=self.post3)
        self.browser.get(self.live_server_url + '/blog/')

        # User sees most commented posts list
        expected_posts = [self.post4, self.post1, self.post3, self.post0]
        actual_post_elements = self.browser.find_elements_by_css_selector(
            '#most-commented-posts a')
        actual_post_titles = [post.text for post in actual_post_elements]
        self.assertEqual(actual_post_titles,
                         [post.title for post in expected_posts])