Exemplo n.º 1
0
def test_export_excel_endpoint(client: CustomFlaskClient, auth_header: any):
    response = client.post('/api/users/xlsx', json={}, headers=auth_header())
    json_response = response.get_json()

    assert 202 == response.status_code
    assert json_response.get('task')
    assert json_response.get('url')
Exemplo n.º 2
0
def test_create_user_endpoint(client: CustomFlaskClient, auth_header: any,
                              factory: any):
    role = RoleModel.get_by_id(1)

    ignore_fields = [
        'id', 'active', 'created_at', 'updated_at', 'deleted_at', 'created_by',
        'fs_uniquifier'
    ]
    data = factory('User').make(exclude=ignore_fields, to_dict=True)
    data['password'] = os.getenv('TEST_USER_PASSWORD')
    data['role_id'] = role.id
    db_wrapper.database.close()

    response = client.post('/api/users', json=data, headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 201 == response.status_code
    assert data.get('name') == json_data.get('name')
    assert data.get('last_name') == json_data.get('last_name')
    assert data.get('birth_date') == json_data.get('birth_date')
    assert data.get('genre') == json_data.get('genre')
    assert json_data.get('created_at')
    assert json_data.get('updated_at') == json_data.get('created_at')
    assert json_data.get('deleted_at') is None

    role_data = json_data.get('roles')[0]

    assert role.name == role_data.get('name')
    assert role.label == role_data.get('label')
Exemplo n.º 3
0
def test_update_document(client: CustomFlaskClient, auth_header: any):
    pdf_file = '%s/example.pdf' % current_app.config.get('MOCKUP_DIRECTORY')
    document = (DocumentModel.select().where(
        DocumentModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get())
    db_wrapper.database.close()
    document_id = document.id

    data = {
        'document': open(pdf_file, 'rb'),
    }

    headers = auth_header()
    headers['Content-Type'] = 'multipart/form-data'

    response = client.put(f'/api/documents/{document_id}',
                          data=data,
                          headers=headers)
    json_response = response.get_json()
    json_data = json_response.get('data')

    parse_url = urlparse(json_data.get('url'))

    assert 200 == response.status_code
    assert isinstance(json_data.get('created_by').get('id'), int)
    assert pdf_file == json_data.get('name')
    assert document.mime_type == json_data.get('mime_type')
    assert FileStorage.get_filesize(pdf_file) == json_data.get('size')
    assert parse_url.scheme and parse_url.netloc
    assert document.created_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'created_at')
    assert json_data.get('updated_at') > json_data.get('created_at')
    assert json_data.get('deleted_at') is None
Exemplo n.º 4
0
def test_get_user_endpoint(client: CustomFlaskClient, auth_header: any):
    user_id = (UserModel.select(UserModel.id).where(
        UserModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get().id)

    user = UserModel.get(UserModel.id == user_id)
    role = user.roles[0]
    db_wrapper.database.close()

    response = client.get('/api/users/%s' % user_id,
                          json={},
                          headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 200 == response.status_code
    assert user_id == json_data.get('id')
    assert user.name == json_data.get('name')
    assert user.last_name == json_data.get('last_name')
    assert user.birth_date.strftime('%Y-%m-%d') == json_data.get('birth_date')
    assert user.created_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'created_at')
    assert user.updated_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'updated_at')
    assert user.deleted_at == json_data.get('deleted_at')

    role_data = json_data.get('roles')[0]

    assert role.name == role_data.get('name')
    assert role.label == role_data.get('label')
Exemplo n.º 5
0
def test_get_document_data(client: CustomFlaskClient, auth_header: any):
    document = (DocumentModel.select().where(
        DocumentModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get())
    db_wrapper.database.close()
    document_id = document.id

    response = client.get(f'/api/documents/{document_id}',
                          json={},
                          headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    parse_url = urlparse(json_data.get('url'))

    assert 200 == response.status_code
    assert document.created_by.id == json_data.get('created_by').get('id')
    assert document.name == json_data.get('name')
    assert document.mime_type == json_data.get('mime_type')
    assert document.size == json_data.get('size')
    assert parse_url.scheme and parse_url.netloc
    assert document.created_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'created_at')
    assert document.updated_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'updated_at')
    assert document.deleted_at == json_data.get('deleted_at')
Exemplo n.º 6
0
def test_export_excel_and_word_endpoint(client: CustomFlaskClient,
                                        auth_header: any):
    response = client.post('/api/users/word_and_xlsx',
                           json={},
                           headers=auth_header())

    assert 202 == response.status_code
    assert isinstance(response.get_json(), dict)
Exemplo n.º 7
0
def test_request_reset_password(client: CustomFlaskClient):
    data = {
        'email': os.getenv('TEST_USER_EMAIL'),
    }

    response = client.post('/api/auth/reset_password', json=data)

    assert 202 == response.status_code
Exemplo n.º 8
0
def test_user_logout(client: CustomFlaskClient, auth_header: any):
    with client:
        response = client.post('/api/auth/logout',
                               json={},
                               headers=auth_header())

        assert 200 == response.status_code
        assert not current_user.is_authenticated
Exemplo n.º 9
0
def test_validate_reset_password(client: CustomFlaskClient, app: Flask):
    email = os.getenv('TEST_USER_EMAIL')

    user = UserModel.get(email=email)

    with app.app_context():
        token = user.get_reset_token()
        db_wrapper.database.close()

    response = client.get(f'/api/auth/reset_password/{token}', json={})

    assert 200 == response.status_code
Exemplo n.º 10
0
def test_get_document_file(client: CustomFlaskClient, auth_header: any):
    document = (DocumentModel.select().where(
        DocumentModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get())
    document_id = document.id
    db_wrapper.database.close()

    headers = auth_header()
    headers['Content-Type'] = 'application/octet-stream'

    response = client.get(f'/api/documents/{document_id}', headers=headers)

    assert 200 == response.status_code
    assert isinstance(response.get_data(), bytes)
Exemplo n.º 11
0
def test_create_invalid_user_endpoint(client: CustomFlaskClient,
                                      auth_header: any):
    data = {
        'name': 'string',
        'last_name': 'string',
        'email': 'string',
        'genre': 'string',
        'password': '******',
        'birth_date': 'string',
        'role_id': 1
    }

    response = client.post('/api/users', json=data, headers=auth_header())
    assert 422 == response.status_code
Exemplo n.º 12
0
def test_save_role_endpoint(client: CustomFlaskClient, auth_header: any,
                            factory: any):
    ignore_fields = ['id', 'created_at', 'updated_at', 'deleted_at', 'name']
    data = factory('Role').make(exclude=ignore_fields, to_dict=True)

    response = client.post('/api/roles', json=data, headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 201 == response.status_code
    assert data.get('label') == json_data.get('label')
    assert data.get('label').lower().replace(' ', '-') == json_data.get('name')
    assert json_data.get('created_at')
    assert json_data.get('updated_at') == json_data.get('created_at')
    assert json_data.get('deleted_at') is None
Exemplo n.º 13
0
def test_check_task_status(client: CustomFlaskClient, auth_header: any):
    _create_task_record()

    task_id = '59cc0424-6f97-44c1-a253-7b4d7566e3f7'
    response = client.get(f'/api/tasks/status/{task_id}',
                          json={},
                          headers=auth_header())
    json_data = response.get_json()

    assert 200 == response.status_code
    assert isinstance(json_data, dict)
    assert json_data.get('state')
    assert json_data.get('current')
    assert json_data.get('total')
    assert json_data.get('result')
Exemplo n.º 14
0
def test_delete_document(client: CustomFlaskClient, auth_header: any):
    document_id = (DocumentModel.select(DocumentModel.id).where(
        DocumentModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get().id)
    db_wrapper.database.close()

    response = client.delete('/api/documents/%s' % document_id,
                             json={},
                             headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 200 == response.status_code
    assert document_id == json_data.get('id')
    assert json_data.get('deleted_at') is not None
    assert json_data.get('deleted_at') >= json_data.get('updated_at')
Exemplo n.º 15
0
def test_reset_password(client: CustomFlaskClient):
    email = os.getenv('TEST_USER_EMAIL')

    user = UserModel.get(email=email)
    token = user.get_reset_token()
    db_wrapper.database.close()

    data = {
        'password': os.getenv('TEST_USER_PASSWORD'),
    }

    response = client.post(f'/api/auth/reset_password/{token}', json=data)
    json_response = response.get_json()

    assert 200 == response.status_code
    assert json_response.get('token')
Exemplo n.º 16
0
def test_get_role_endpoint(client: CustomFlaskClient, auth_header: any):
    role_id = (RoleModel.select(RoleModel.id).where(
        RoleModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get().id)

    role = RoleModel.get(RoleModel.id == role_id)
    db_wrapper.database.close()

    response = client.get('/api/roles/%s' % role_id,
                          json={},
                          headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 200 == response.status_code
    assert role_id == json_data.get('id')
    assert role.name == json_data.get('name')
    assert role.name.lower() == json_data.get('name').lower().replace('-', ' ')
    assert role.created_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'created_at')
    assert role.updated_at.strftime('%Y-%m-%d %H:%M:%S') == json_data.get(
        'updated_at')
    assert role.deleted_at == json_data.get('deleted_at')
Exemplo n.º 17
0
def test_update_user_endpoint(client: CustomFlaskClient, auth_header: any,
                              factory: any):
    user_id = (UserModel.select(UserModel.id).where(
        UserModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get().id)

    ignore_fields = [
        'id', 'active', 'created_at', 'updated_at', 'deleted_at', 'created_by'
    ]
    data = factory('User').make(to_dict=True, exclude=ignore_fields)

    data['password'] = os.getenv('TEST_USER_PASSWORD')
    role = (RoleModel.select().where(RoleModel.deleted_at.is_null()).order_by(
        fn.Random()).limit(1).get())
    data['role_id'] = role.id
    db_wrapper.database.close()

    response = client.put('/api/users/%s' % user_id,
                          json=data,
                          headers=auth_header())
    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 200 == response.status_code
    assert user_id == json_data.get('id')
    assert data.get('name') == json_data.get('name')
    assert data.get('last_name') == json_data.get('last_name')
    assert data.get('birth_date') == json_data.get('birth_date')
    assert data.get('genre') == json_data.get('genre')
    assert json_data.get('created_at')
    assert json_data.get('updated_at') >= json_data.get('created_at')
    assert json_data.get('deleted_at') is None

    role_data = json_data.get('roles')[0]

    assert role.name == role_data.get('name')
    assert role.label == role_data.get('label')
Exemplo n.º 18
0
def test_update_role_endpoint(client: CustomFlaskClient, auth_header: any,
                              factory: any):
    role_id = (RoleModel.select(RoleModel.id).where(
        RoleModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get().id)
    db_wrapper.database.close()

    ignore_fields = ['id', 'created_at', 'updated_at', 'deleted_at', 'name']
    data = factory('Role').make(exclude=ignore_fields, to_dict=True)

    response = client.put('/api/roles/%s' % role_id,
                          json=data,
                          headers=auth_header())

    json_response = response.get_json()
    json_data = json_response.get('data')

    assert 200 == response.status_code
    assert role_id == json_data.get('id')
    assert data.get('label') == json_data.get('label')
    assert data.get('label').lower().replace(' ', '-') == json_data.get('name')
    assert json_data.get('created_at')
    assert json_data.get('updated_at') >= json_data.get('created_at')
    assert json_data.get('deleted_at') is None
Exemplo n.º 19
0
def test_search_document(client: CustomFlaskClient, auth_header: any):
    document_name = (DocumentModel.select(DocumentModel.name).where(
        DocumentModel.deleted_at.is_null()).order_by(
            fn.Random()).limit(1).get().name)
    db_wrapper.database.close()

    json_body = {
        'search': [
            {
                'field_name': 'name',
                'field_operator': 'eq',
                'field_value': document_name,
            },
        ],
        'order': [
            {
                'field_name': 'name',
                'sorting': 'desc',
            },
        ],
    }

    response = client.post('/api/documents/search',
                           json=json_body,
                           headers=auth_header())
    json_response = response.get_json()

    document_data = json_response.get('data')
    records_total = json_response.get('records_total')
    records_filtered = json_response.get('records_filtered')

    assert 200 == response.status_code
    assert isinstance(document_data, list)
    assert records_total > 0
    assert 0 < records_filtered <= records_total
    assert document_data[0].get('name').find(document_name) != -1
Exemplo n.º 20
0
def test_save_document(client: CustomFlaskClient, auth_header: any):
    pdf_file = '%s/example.pdf' % current_app.config.get('MOCKUP_DIRECTORY')
    data = {
        'document': open(pdf_file, 'rb'),
    }

    headers = auth_header()
    headers['Content-Type'] = 'multipart/form-data'

    response = client.post('/api/documents', data=data, headers=headers)
    json_response = response.get_json()
    json_data = json_response.get('data')

    parse_url = urlparse(json_data.get('url'))

    assert 201 == response.status_code
    assert 1 == json_data.get('created_by').get('id')
    assert pdf_file == json_data.get('name')
    assert 'application/pdf' == json_data.get('mime_type')
    assert FileStorage.get_filesize(pdf_file) == json_data.get('size')
    assert parse_url.scheme and parse_url.netloc
    assert json_data.get('created_at')
    assert json_data.get('updated_at') == json_data.get('created_at')
    assert json_data.get('deleted_at') is None
Exemplo n.º 21
0
def test_welcome_api(client: CustomFlaskClient):
    response = client.get('/api/welcome', json={})

    assert 200 == response.status_code
    assert response.data == b'"Welcome to flask_api!"\n'
Exemplo n.º 22
0
def test_api_middleware(client: CustomFlaskClient, auth_header: any):
    response = client.post('/api/auth/logout', headers=auth_header())
    json_response = response.get_json()

    assert 400 == response.status_code
    assert 'Content type no valid' == json_response.get('message')
Exemplo n.º 23
0
def test_no_api_middleware(client: CustomFlaskClient):
    response = client.post('/welcome')
    assert 404 == response.status_code