Esempio n. 1
0
def test_http_article_put_article_with_assets(mocked_put_article,
                                              dummy_request, test_xml_file,
                                              test_article_files):
    #XXX aqui deveria ser um teste funcional simples, sem qualquer dublê de
    #testes (mocks e afins).
    xml_file = MockCGIFieldStorage("test_xml_file.xml",
                                   BytesIO(test_xml_file.encode('utf-8')))
    expected_assets_files = []
    assets_field = []
    for article_file in test_article_files[1:]:
        with open(article_file, 'rb') as fb:
            file_content = fb.read()
            expected_assets_files.append(
                _get_file_property(article_file.name, file_content,
                                   article_file.lstat().st_size), )
            assets_field.append(('asset_field',
                                 MockCGIFieldStorage(article_file.name,
                                                     BytesIO(file_content))))
    dummy_request.POST = MultiDict([('id', xml_file.filename),
                                    ('xml_file', xml_file)] + assets_field)

    article_api = ArticleAPI(dummy_request)
    response = article_api.put()
    mocked_put_article.assert_called_once()
    assert response.status_code == 200
    assert response.json is not None
    assert response.json.get('url').endswith(xml_file.filename)
Esempio n. 2
0
def test_http_article_put_article_succeeded(mocked_put_article, dummy_request,
                                            test_xml_file):
    xml_file = MockCGIFieldStorage("test_xml_file.xml",
                                   BytesIO(test_xml_file.encode('utf-8')))
    dummy_request.POST = MultiDict([('id', xml_file.filename),
                                    ('xml_file', xml_file)])

    article_api = ArticleAPI(dummy_request)
    response = article_api.put()
    assert response.status_code == 200
    assert response.json is not None
    assert response.json.get('url').endswith(xml_file.filename)
Esempio n. 3
0
def test_http_get_article_not_found(mocked_get_article_data, dummy_request):
    article_id = 'ID123456'
    error_msg = 'Article {} not found'.format(article_id)
    mocked_get_article_data.side_effect = \
        managers.article_manager.ArticleManagerException(
            message=error_msg
        )
    dummy_request.matchdict = {'id': article_id}

    article_api = ArticleAPI(dummy_request)
    with pytest.raises(HTTPNotFound) as excinfo:
        article_api.get()
    assert excinfo.value.message == error_msg
Esempio n. 4
0
def test_post_article_returns_article_version_url(mocked_post_article,
                                                  dummy_request,
                                                  test_xml_file):
    xml_file = MockCGIFieldStorage("test_xml_file.xml",
                                   BytesIO(test_xml_file.encode('utf-8')))
    dummy_request.POST = {
        'article_id': xml_file.filename,
        'xml_file': xml_file
    }
    article_api = ArticleAPI(dummy_request)
    response = article_api.collection_post()

    mocked_post_article.assert_called_once()
    assert response.status_code == 201
    assert response.json is not None
    assert response.json.get('url').endswith(xml_file.filename)
Esempio n. 5
0
def test_http_get_article_succeeded(mocked_get_article_data, dummy_request):
    article_id = 'ID123456'
    expected = {
        "document_id": article_id,
        "document_type": "ART",
        "content": {
            'xml': "test.xml",
            'assets': ["img1.png", "img2.png", "img3.png"]
        },
    }
    mocked_get_article_data.return_value = expected
    dummy_request.matchdict = {'id': article_id}

    article_api = ArticleAPI(dummy_request)
    response = article_api.get()
    assert response.status == '200 OK'
    assert response.json == expected
Esempio n. 6
0
def test_http_article_calls_put_article_service_error(mocked_put_article,
                                                      dummy_request,
                                                      test_xml_file):
    xml_file = MockCGIFieldStorage("test_xml_file.xml",
                                   BytesIO(test_xml_file.encode('utf-8')))
    error_msg = 'Missing XML file {}'.format("test_xml_file")
    mocked_put_article.side_effect = \
        managers.article_manager.ArticleManagerException(
            message=error_msg
        )
    dummy_request.POST = MultiDict([('id', xml_file.filename),
                                    ('xml_file', xml_file)])

    article_api = ArticleAPI(dummy_request)
    with pytest.raises(HTTPInternalServerError) as excinfo:
        article_api.put()
    assert excinfo.value.message == error_msg
Esempio n. 7
0
def test_post_article_internal_error(mocked_post_article, dummy_request,
                                     test_xml_file):
    xml_file = MockCGIFieldStorage("test_xml_file.xml",
                                   BytesIO(test_xml_file.encode('utf-8')))
    error_msg = 'Error Catalog Manager: {}'.format(123456)
    mocked_post_article.side_effect = \
        managers.article_manager.ArticleManagerException(
            message=error_msg
        )
    dummy_request.POST = {
        'article_id': xml_file.filename,
        'xml_file': xml_file
    }

    article_api = ArticleAPI(dummy_request)
    with pytest.raises(HTTPInternalServerError) as excinfo:
        article_api.collection_post()
    assert excinfo.value.message == error_msg
Esempio n. 8
0
def test_post_article_invalid_xml(mocked_post_article, mocked_create_file,
                                  dummy_request, test_xml_file):
    xml_file = MockCGIFieldStorage("test_xml_file.xml",
                                   BytesIO(test_xml_file.encode('utf-8')))
    error_msg = 'Invalid XML Content'
    mocked_create_file.side_effect = \
        managers.exceptions.ManagerFileError(
            message=error_msg
        )
    dummy_request.POST = {
        'article_id': xml_file.filename,
        'xml_file': xml_file
    }

    article_api = ArticleAPI(dummy_request)
    with pytest.raises(HTTPBadRequest) as excinfo:
        article_api.collection_post()
    assert excinfo.value.message == error_msg