Esempio n. 1
0
def test_get_paginated_posts_successfully(client):
    posts = factory(Post, 7).create()
    factory(Post, 'is_draft', 2).create()

    response = client.get(
        '/all-posts',
        query_string='p=1',
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    data = response.get_json()
    expected_data = posts.serialize()

    assert response.status_code == HTTPStatus.OK
    assert data.items() >= {
        'total': 9,
        'per_page': 6,
        'current_page': 1,
        'last_page': 2,
        'previous_page': None,
        'next_page': 2,
    }.items()
    assert isinstance(data['data'], list) and len(data['data']) == 6
    assert all(item in expected_data for item in data['data'])
def test_blog_posts_update_full_post(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    new_data = factory(Post).make()
    params = {
        'title': new_data.title,
        'image': new_data.image,
        'abstract': new_data.abstract,
        'text': 'test_text',
    }

    response = client.put(
        f'/posts/{post.id}',
        json=params,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    params_copy = params.copy()
    params_copy.pop('text')

    assert response.status_code == HTTPStatus.OK
    assert response.get_json().items() >= params.items()
    assert Post.find(post.id).serialize().items() >= params_copy.items()
    assert PostContent.find(post_content.id).text == params.get('text')
def test_blog_posts_update_partial_post(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    params = {
        'title': 'Novo título',
    }

    response = client.put(
        f'/posts/{post.id}',
        json=params,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    updated_post = Post.find(post.id).serialize()
    updated_post.pop('updated_at')
    updated_title = updated_post.pop('title')

    old_post = post.serialize()
    old_post.pop('updated_at')
    old_title = old_post.pop('title')

    assert updated_title == params['title']
    assert old_post == updated_post
    assert response.get_json()['title'] == params['title']
def test_cli_token_list(runner):
    factory(Auth).create(token='token_1')
    factory(Auth).create(token='token_2')

    result = runner.invoke(cli, ['token', '-l'])

    assert result.exit_code == 0
    assert result.output == 'token_1\ntoken_2\n'
def client():
    from personalwebpageapi.api import app
    from personalwebpageapi.models.auth import Auth

    factory(Auth).create(token='valid_token')

    app.config['TESTING'] = True
    test_client = app.test_client()

    return test_client
def test_get_single_post_successfully(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    response = client.get(f'/posts/{post.id}', )

    assert response.status_code == HTTPStatus.OK
    assert response.get_json() == {
        **post.serialize(),
        'text': post_content.text,
    }
def test_blog_posts_create_successfully(client):
    data = factory(Post).make().serialize()

    data.pop('post_content_id')
    data.update({
        'text': 'test_text',
    })

    response = client.post(
        '/posts',
        json=data,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    data = response.get_json()
    saved_post = Post.find(data['id'])
    saved_content = PostContent.find(data['post_content_id'])

    assert response.status_code == HTTPStatus.OK
    assert bool(saved_post)
    assert bool(saved_content)
    assert data.items() >= saved_post.serialize().items()
    assert data['text'] == saved_content.text
def test_blog_posts_create_without_all_params(client, param_to_remove):
    data = factory(Post).make().serialize()

    data.pop('post_content_id')
    data.pop(param_to_remove, None)
    data.update({
        'text': 'test_text',
    })

    response = client.post(
        '/posts',
        json=data,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    assert response.status_code == HTTPStatus.BAD_REQUEST
    assert {
        'message': {
            f'{param_to_remove}': ('Missing required parameter in the JSON'
                                   ' body or the post body or the query'
                                   ' string'),
        },
    }
Esempio n. 9
0
def test_blog_posts_delete_successfully(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    response = client.delete(
        f'/posts/{post.id}',
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    data = response.get_json()

    assert 'message' in data
    assert data.get('message') == 'Successfully deleted'
    assert not Post.find(post.id)
    assert not PostContent.find(post_content.id)
Esempio n. 10
0
def test__blog_latest_posts_get(client, qty_posts):
    from operator import itemgetter

    posts = factory(Post, qty_posts).create()

    response = client.get('/latest-posts', )

    data = response.get_json()

    assert isinstance(data, list)
    assert len(data) <= 3

    sorted_data = sorted(data, key=itemgetter('created_at'), reverse=True)

    assert all(x.items() == y.items() for x, y in zip(data, sorted_data))
def test_blog_posts_create_Unknown_argument(client):
    data = factory(Post).make().serialize()

    data.pop('post_content_id')
    data.update({
        'text': 'test_text',
        'nonexistent': 'value',
    })

    response = client.post(
        '/posts',
        json=data,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    assert response.status_code == HTTPStatus.BAD_REQUEST
    assert response.get_json() == {'message': 'Unknown arguments: nonexistent'}
Esempio n. 12
0
 def setUp(self):
     super().setUp()
     self.collection = []
     for i in range(1, 4):
         self.collection.append(factory(CrudTest).create())
Esempio n. 13
0
 def test_000_factory(self):
     crud_test = factory(CrudTest).create()
     self.assertTrue(crud_test.id)
     self.assertTrue(crud_test.id == CrudTest.all().first().id)
Esempio n. 14
0
 def setUp( self ):
     super().setUp()
     self.collection=[]
     for i in range(1,4):
         self.collection.append(factory(MODEL_CLASS).create())
Esempio n. 15
0
 def test_000_factory(self):
     MODEL = factory(CLASS).create()
     self.assertTrue(MODEL.id)
     self.assertTrue(MODEL.id == CLASS.all().first().id)