Ejemplo n.º 1
0
 def test_search(self):
     PostFactory.create_batch(title="foo", content="dummy", size=10)
     PostFactory(title="potato")
     self.get("api_v1:post-list", data={"search": "potato"})
     self.response_200()
     data = self.last_response.json()
     self.assertEqual(1, len(data))
Ejemplo n.º 2
0
 def test_list_serializer(self):
     PostFactory.create_batch(size=10)
     self.get("api_v1:post-list")
     self.response_200()
     data = self.last_response.json()
     self.assertEqual(10, len(data))
     self.assertNotIn("content", data[0])
Ejemplo n.º 3
0
 def test_UserModel_followed_post_must_return_list_of_new_user_posts_with_own_user_posts(
         self):
     new_user = UserFactory()
     post1 = PostFactory(author=new_user)
     post2 = PostFactory(author=new_user)
     self.user.followed.append(new_user)
     expected = [post2, post1, *self.user.posts.all()]
     self.assertListEqual(expected, list(self.user.followed_posts()))
Ejemplo n.º 4
0
 def test_update_status(self):
     post = PostFactory(title="potato", status=DRAFT)
     self.patch("api_v1:post-detail",
                pk=post.pk,
                data={"status": PUBLISHED})
     self.response_200()
     post.refresh_from_db()
     self.assertEqual(PUBLISHED, post.status)
Ejemplo n.º 5
0
 def test_UserModel_followed_post_must_returned_list_must_contain_post1_and_post2(
         self):
     new_user = UserFactory()
     post1 = PostFactory(author=new_user)
     post2 = PostFactory(author=new_user)
     self.user.followed.append(new_user)
     user_posts = self.user.followed_posts().all()
     self.assertIn(post1, user_posts)
     self.assertIn(post2, user_posts)
Ejemplo n.º 6
0
 def test_check_no_handler_on_transition(self):
     post = PostFactory()
     self.assertEqual(DRAFT, post.status)
     exception = None
     try:
         post.status = DUMMY
         post.save()
     except ValidationError as validation_exception:
         exception = validation_exception
     self.assertIsNone(exception)
Ejemplo n.º 7
0
 def test_check_allowed_transitions(self):
     post = PostFactory()
     self.assertEqual(DRAFT, post.status)
     exception = None
     try:
         post.status = PUBLISHED
         post.save()
     except ValidationError as validation_exception:
         exception = validation_exception
     self.assertIsNone(exception)
Ejemplo n.º 8
0
 def test_check_forbidden_transitions(self):
     post = PostFactory(status=PUBLISHED)
     self.assertEqual(PUBLISHED, post.status)
     exception = None
     try:
         post.status = DRAFT
         post.save()
     except ValidationError as validation_exception:
         exception = validation_exception
     self.assertIsNotNone(exception)
     self.assertIsInstance(exception, ValidationError)
Ejemplo n.º 9
0
 def test_check_status_change_on_transition(self):
     post = PostFactory()
     self.assertEqual(DRAFT, post.status)
     exception = None
     try:
         post.status = AUTO
         post.save()
     except ValidationError as validation_exception:
         exception = validation_exception
     self.assertIsNone(exception)
     self.assertEqual(AUTO, post.status, "status doesn't change to published")
Ejemplo n.º 10
0
def test_list_view(client):
    posts = [PostFactory(), PostFactory(), PostFactory()]
    url = reverse('blog_list_posts')

    response = client.get(url)
    content = response.content.decode(response.charset)

    assert response.status_code == 200

    for post in posts:
        assert post.title in content
        assert post.lead in content
Ejemplo n.º 11
0
 def test_undo_logic_delete(self):
     post: Post = PostFactory()
     post.logic_delete()
     self.assertTrue(post.deleted)
     post.undo_logic_delete()
     self.assertFalse(post.deleted)
     self.assertIsNone(post.date_deleted)
Ejemplo n.º 12
0
    def test_index_shows_posts(self):
        posts = PostFactory.create_batch(random.randint(1, 10))
        response = self.client.get(url_for('main.index'))
        self.assertTemplateUsed('index.html')

        for post in posts:
            self.assertTrue(post.title in str(response.get_data()))
Ejemplo n.º 13
0
def test_get_post(db_connection, event_loop, test_client, cookies_fixture):
    category1, category2, category3 = event_loop.run_until_complete(
        CategoryFactory.create_batch(3))
    post = event_loop.run_until_complete(PostFactory.create())
    response = test_client.get(f'/admin/blog/{post.title_slug}',
                               cookies=cookies_fixture)

    assert response.status_code == 200
    assert '<form id="update-post" method="post">' \
        in response.text
    assert '<label for="title">Title:</label>' in response.text
    assert f'<input id="title" name="title" type="text" required autofocus ' \
           f'value="{post.title}">' in response.text
    assert '<label for="category_id">Category:</label>' in response.text
    assert '<input id="category_id" name="category_id" list="category_values' \
           f'" required value="{post.category_id}">' in response.text
    assert '<datalist id="category_values">' in response.text
    assert f'<option value="{category1.id}">{category1.name}</option>' \
        in response.text
    assert f'<option value="{category2.id}">{category2.name}</option>' \
        in response.text
    assert f'<option value="{category3.id}">{category3.name}</option>' \
        in response.text
    assert '</datalist>' in response.text
    assert '<label for="description">Description:</label>' in response.text
    assert '<input id="description" name="description" type="text" required ' \
           f'value="{post.description}">' in response.text
    assert '<label for="body">Body:</label>' in response.text
    assert '<textarea id="body" name="body" required>' \
           f'{post.body}</textarea>' in response.text
    assert '<input type="submit" value="Update">' in response.text
    assert '<p>post updated successfully</p>' not in response.text
Ejemplo n.º 14
0
def test_published_post_detail_with_pending_comment(db_connection, test_client,
                                                    event_loop):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED,
                           published_at=datetime.datetime.now()))
    post_comment = event_loop.run_until_complete(
        PostComment.create(name='test name',
                           email='*****@*****.**',
                           body='comment body',
                           post=post))
    response = test_client.get(f'/blog/{post.title_slug}')

    assert response.status_code == 200
    assert f'{post.title.capitalize()}' in response.text
    assert f'{markdown.markdown(post.body)}' in response.text
    assert f'{post.published_at.date()}' in response.text
    assert '<script defer data-api="/analytics/event" data-domain=' \
           '"raiseexception.dev"' in response.text
    # assert '<form id="comment" method="post">' \
    #     in response.text
    assert 'Name' in response.text
    # assert '<input id="name" name="name" type="text" placeholder=' \
    #     '"name or alias">' in response.text
    assert 'Email' in response.text
    # assert '<input id="email" name="email" type="email" placeholder=' \
    #     '"email will not be published"' in response.text
    assert 'Comment *' in response.text
    # assert '<textarea id="body" name="body" required></textarea>' \
    #     in response.text
    # assert '<input type="submit" value="Comment">' in response.text
    assert 'Comments:' not in response.text
    assert f'{post_comment.name}, {post_comment.created_at}' not in \
        response.text
    assert f'<p>{post_comment.body}</p>' not in response.text
    assert '<p>There are no comments.</p>'
Ejemplo n.º 15
0
def test_update_post_with_wrong_field(db_connection, event_loop, test_client,
                                      cookies_fixture):
    post = event_loop.run_until_complete(PostFactory.create())
    response = test_client.post(f'/admin/blog/{post.title_slug}',
                                cookies=cookies_fixture,
                                data={'wrong_field': 'value'})
    assert response.status_code == 400
    assert '<p>"wrong_field" is an invalid field</p>'
Ejemplo n.º 16
0
def test_without_body(db_connection, test_client, event_loop):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    response = test_client.post(f'/blog/{post.title_slug}', data={})
    comment = event_loop.run_until_complete(
        PostComment.get_or_none(post_id=post.id))

    assert response.status_code == 400
    assert comment is None
Ejemplo n.º 17
0
def test_lists_with_anonymous_user(db_connection, test_client, event_loop):
    author = event_loop.run_until_complete(UserFactory.create())
    draft_posts = event_loop.run_until_complete(
        PostFactory.create_batch(4, author=author))
    posts = event_loop.run_until_complete(
        PostFactory.create_batch(5, author=author, state=PostState.PUBLISHED))
    response = test_client.get('/blog')

    assert response.status_code == 200
    assert '<script defer data-api="/analytics/event" data-domain=' \
           '"raiseexception.dev"' in response.text
    for post in posts:
        assert post.title_slug in response.text
        assert post.title.capitalize() in response.text

    for post in draft_posts:
        assert post.title_slug not in response.text
        assert post.title.capitalize() not in response.text
Ejemplo n.º 18
0
def test_detail_view(client):
    post = PostFactory()
    url = reverse('blog_view_post', kwargs={"slug": post.slug})

    response = client.get(url)
    content = response.content.decode(response.charset)

    assert response.status_code == 200
    assert post.title in content
    assert post.lead in content
    assert post.body in content
Ejemplo n.º 19
0
def test_draft_post_detail_with_authenticated_user(db_connection, test_client,
                                                   event_loop,
                                                   cookies_fixture):
    post = event_loop.run_until_complete(PostFactory.create())
    response = test_client.get(f'/blog/{post.title_slug}',
                               cookies=cookies_fixture)

    assert response.status_code == 200
    assert post.title.capitalize() in response.text
    assert f'{markdown.markdown(post.body)}' in response.text
    assert '<script async defer data-domain="raiseexception.dev"' \
        not in response.text
Ejemplo n.º 20
0
def test_with_wrong_email(db_connection, test_client, event_loop):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    response = test_client.post(f'/blog/{post.title_slug}',
                                data={
                                    'body': 'test comment',
                                    'post_id': post.id,
                                    'email': 'wrong @email'
                                })

    assert response.status_code == 400
    assert 'invalid email' in response.text
Ejemplo n.º 21
0
def test_list_comments_with_no_pending_comments(db_connection, test_client,
                                                event_loop, cookies_fixture):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    event_loop.run_until_complete(
        PostCommentFactory.create_batch(5,
                                        post=post,
                                        state=PostCommentState.APPROVED))
    response = test_client.get('/admin/blog/comments', cookies=cookies_fixture)

    assert response.status_code == 200
    assert 'Pending comments:' not in response.text
    assert 'There are no pending comments.' in response.text
Ejemplo n.º 22
0
def test_publish_post(db_connection, event_loop, test_client, cookies_fixture,
                      mocker):
    post = event_loop.run_until_complete(PostFactory.create())
    event_loop.run_until_complete(SubscriptionFactory.create(verified=True))
    mail_client_spy = mocker.spy(MailClient, 'send')
    response = test_client.post('/admin/blog/publish',
                                cookies=cookies_fixture,
                                data={'post_id': post.id})
    published_post = event_loop.run_until_complete(Post.get(id=post.id))

    assert response.status_code == 302
    assert published_post.state == PostState.PUBLISHED
    assert published_post.published_at.date() == datetime.date.today()
    mail_client_spy.assert_called_once()
Ejemplo n.º 23
0
def test_without_name_or_email(db_connection, test_client, event_loop):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    response = test_client.post(f'/blog/{post.title_slug}',
                                data={
                                    'body': 'test comment',
                                    'post_id': post.id,
                                })
    comment = event_loop.run_until_complete(PostComment.get(post_id=post.id))

    assert response.status_code == 201
    assert comment.name == 'anonymous'
    assert comment.post_id == post.id
    assert comment.email is None
Ejemplo n.º 24
0
def test_list_comments(db_connection, test_client, event_loop,
                       cookies_fixture):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    comments = event_loop.run_until_complete(
        PostCommentFactory.create_batch(5, post=post))
    response = test_client.get('/admin/blog/comments', cookies=cookies_fixture)

    assert response.status_code == 200
    assert 'Pending comments:' in response.text
    for comment in comments:
        assert comment.name in response.text
        assert comment.post.title in response.text
        assert comment.body in response.text
        assert f'<input name="approve" type="checkbox" value="{post.id}">' \
            in response.text
Ejemplo n.º 25
0
def test_approve_comments(db_connection, test_client, event_loop,
                          cookies_fixture, mocker):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    comments = event_loop.run_until_complete(
        PostCommentFactory.create_batch(5, post=post))
    mail_client_spy = mocker.patch.object(MailClient,
                                          'send',
                                          new_callable=AsyncMock)
    response = test_client.post(
        '/admin/blog/comments',
        data={'approve': [comment.id for comment in comments]},
        cookies=cookies_fixture)
    comments = event_loop.run_until_complete(PostComment.all())

    assert response.status_code == 302
    for comment in comments:
        assert comment.state == PostCommentState.APPROVED
    assert mail_client_spy.await_count == 5
Ejemplo n.º 26
0
def test_success(db_connection, test_client, event_loop, mocker):
    post = event_loop.run_until_complete(
        PostFactory.create(state=PostState.PUBLISHED))
    mail_client_spy = mocker.spy(MailClient, 'send')
    response = test_client.post(f'/blog/{post.title_slug}',
                                data={
                                    'name': 'Julián',
                                    'body': 'test comment',
                                    'email': '*****@*****.**',
                                })
    comment = event_loop.run_until_complete(PostComment.get(post_id=post.id))

    assert response.status_code == 201
    assert comment.name == 'Julián'
    assert comment.post_id == post.id
    assert comment.email == '*****@*****.**'
    assert "<p>The comment has been created in pending state. It will be " \
           "displayed when it's approved. I'll let you know by email if the " \
           "email was sent.</p>".replace("'", '&#39;') in response.text

    event_loop.run_until_complete(asyncio.sleep(1))
    assert mail_client_spy.spy_return is True
    mail_client_spy.assert_called_once()
Ejemplo n.º 27
0
def test_update_post(db_connection, event_loop, test_client, cookies_fixture,
                     post_data):
    post = event_loop.run_until_complete(PostFactory.create())
    response = test_client.post(f'/admin/blog/{post.title_slug}',
                                cookies=cookies_fixture,
                                data=post_data)
    updated_post = event_loop.run_until_complete(Post.get(id=post.id))

    assert response.status_code == 200
    assert post.modified_at != updated_post.modified_at
    if 'state' in post_data:
        assert updated_post.state != post.state
        assert updated_post.state == PostState(post_data['state'])
    if 'title' in post_data:
        assert updated_post.title != post.title
        assert updated_post.title_slug != post.title_slug
        assert updated_post.title == post_data['title']
    if 'body' in post_data:
        assert updated_post.body != post.body
        assert updated_post.body == post_data['body']
    if 'description' in post_data:
        assert updated_post.description != post.description
        assert updated_post.description == post_data['description']
    assert '<p>post updated successfully</p>' in response.text
Ejemplo n.º 28
0
 def test_annotate_total_posts(self):
     blog = BlogFactory()
     PostFactory.create_batch(blog=blog, size=10)
     annotated_blog = Blog.objects.annotate_total_posts().first()
     self.assertEqual(10, annotated_blog.total_posts)
Ejemplo n.º 29
0
 def test_search_combined(self):
     PostFactory.create_batch(size=10)
     PostFactory(title="title and", content="content")
     posts = Post.objects.search(query="and content")
     self.assertEqual(1, posts.count())
Ejemplo n.º 30
0
 def test_search_mixin(self):
     PostFactory.create_batch(size=10)
     PostFactory(title="very exact title")
     posts = Post.objects.search(query="exact")
     self.assertEqual(1, posts.count())