Exemple #1
0
    def client(self, loop, aiohttp_server, aiohttp_client):
        async def handler0(request):
            raise RuntimeError('Error text')

        async def handler1(request):
            raise web.HTTPConflict(reason="foo", text='{"key": "conflict"}')

        async def handler2(request):
            raise web.HTTPNotFound(text='{"key": "not found"}')

        async def handler4(request):
            return web.json_response({"key": "Okay"})

        app = web.Application(loop=loop,
                              middlewares=[middleware.error_middleware])
        # fill the routes table
        routes.setup(app)
        app.router.add_route('GET', '/test', handler0)
        app.router.add_route('GET', '/test-web-ex1', handler1)
        app.router.add_route('GET', '/test-web-ex2', handler2)
        app.router.add_route('GET', '/test-okay', handler4)

        server = loop.run_until_complete(aiohttp_server(app))
        loop.run_until_complete(server.start_server(loop=loop))
        client = loop.run_until_complete(aiohttp_client(server))
        return client
Exemple #2
0
async def test_ping_https_allow_ping_true(test_server, ssl_ctx, test_client, loop, get_machine_detail):
    payload = '{"return": ["key", "description", "value"], "sort": {"column": "key", "direction": "asc"}}'
    result = {"rows": [
                {"value": 1, "key": "PURGED", "description": "blah6"},
                {"value": 2, "key": "READINGS", "description": "blah1"},
                {"value": 3, "key": "North Readings to PI", "description": "blah2"},
                {"value": 4, "key": "North Statistics to PI", "description": "blah3"},
                {"value": 10, "key": "North Statistics to OCS", "description": "blah5"},
                {"value": 100, "key": "Readings Sent", "description": "Readings Sent North"},
               ]}

    @asyncio.coroutine
    def mock_coro(*args, **kwargs):
        return result

    host_name, ip_addresses = get_machine_detail
    mock_storage_client_async = MagicMock(StorageClientAsync)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect, 'get_storage_async', return_value=mock_storage_client_async):
            with patch.object(mock_storage_client_async, 'query_tbl_with_payload', return_value=mock_coro()) as query_patch:
                    app = web.Application(loop=loop, middlewares=[middleware.optional_auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await test_server(app, ssl=ssl_ctx)
                    server.start_server(loop=loop)

                    with pytest.raises(aiohttp.ClientConnectorSSLError) as error_exec:
                        client = await test_client(server)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(error_exec)

                    with pytest.raises(aiohttp.ClientConnectorSSLError) as error_exec:
                        # self signed certificate,
                        # and we are not using SSL context here for client as verifier
                        connector = aiohttp.TCPConnector(verify_ssl=True, loop=loop)
                        client = await test_client(server, connector=connector)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(error_exec)

                    connector = aiohttp.TCPConnector(verify_ssl=False, loop=loop)
                    client = await test_client(server, connector=connector)
                    resp = await client.get('/foglamp/ping')
                    s = resp.request_info.url.human_repr()
                    assert "https" == s[:5]
                    assert 200 == resp.status
                    content = await resp.text()
                    content_dict = json.loads(content)
                    assert 0 <= content_dict["uptime"]
                    assert 2 == content_dict["dataRead"]
                    assert 100 == content_dict["dataSent"]
                    assert 1 == content_dict["dataPurged"]
                    assert content_dict["authenticationOptional"] is True
                    assert content_dict['serviceName'] == "FogLAMP"
                    assert content_dict['hostName'] == host_name
                    assert content_dict['ipAddresses'] == ip_addresses
                    assert content_dict['health'] == "green"
                    assert content_dict['safeMode'] is False
            query_patch.assert_called_once_with('statistics', payload)
        logger_info.assert_called_once_with('Received %s request for %s', 'GET', '/foglamp/ping')
Exemple #3
0
    def _make_app():
        """Creates the REST server

        :rtype: web.Application
        """
        app = web.Application(middlewares=[middleware.error_middleware])
        admin_routes.setup(app)
        return app
Exemple #4
0
 def client(self, loop, aiohttp_server, aiohttp_client):
     app = web.Application(loop=loop,  middlewares=[middleware.optional_auth_middleware])
     # fill the routes table
     routes.setup(app)
     server = loop.run_until_complete(aiohttp_server(app))
     loop.run_until_complete(server.start_server(loop=loop))
     client = loop.run_until_complete(aiohttp_client(server))
     return client
Exemple #5
0
async def test_ping_https_auth_required_allow_ping_false(test_server, ssl_ctx, test_client, loop, get_machine_detail):
    @asyncio.coroutine
    def mock_coro(*args, **kwargs):
        result = {"rows": [
            {"value": 1, "key": "PURGED", "description": "blah6"},
            {"value": 2, "key": "READINGS", "description": "blah1"},
            {"value": 3, "key": "North Readings to PI", "description": "blah2"},
            {"value": 4, "key": "North Statistics to PI", "description": "blah3"},
            {"value": 6, "key": "North Statistics to OCS", "description": "blah5"},
            {"value": 100, "key": "Readings Sent", "description": "Readings Sent North"},
        ]}
        return result

    async def mock_get_category_item():
        return {"value": "false"}

    mock_storage_client_async = MagicMock(StorageClientAsync)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect, 'get_storage_async', return_value=mock_storage_client_async):
            with patch.object(mock_storage_client_async, 'query_tbl_with_payload', return_value=mock_coro()) as query_patch:
                with patch.object(ConfigurationManager, "get_category_item", return_value=mock_get_category_item()) as mock_get_cat:
                    with patch.object(_logger, 'warning') as logger_warn:
                        app = web.Application(loop=loop, middlewares=[middleware.auth_middleware])
                        # fill route table
                        routes.setup(app)

                        server = await test_server(app, ssl=ssl_ctx)
                        server.start_server(loop=loop)

                        with pytest.raises(aiohttp.ClientConnectorSSLError) as error_exec:
                            client = await test_client(server)
                            await client.get('/foglamp/ping')
                        assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(error_exec)

                        with pytest.raises(aiohttp.ClientConnectorSSLError) as error_exec:
                            # self signed certificate,
                            # and we are not using SSL context here for client as verifier
                            connector = aiohttp.TCPConnector(verify_ssl=True, loop=loop)
                            client = await test_client(server, connector=connector)
                            await client.get('/foglamp/ping')
                        assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(error_exec)

                        connector = aiohttp.TCPConnector(verify_ssl=False, loop=loop)
                        client = await test_client(server, connector=connector)
                        resp = await client.get('/foglamp/ping')
                        s = resp.request_info.url.human_repr()
                        assert "https" == s[:5]
                        assert 403 == resp.status
                    logger_warn.assert_called_once_with('Permission denied for Ping when Auth is mandatory.')
                mock_get_cat.assert_called_once_with('rest_api', 'allowPing')
            assert 0 == query_patch.call_count
        logger_info.assert_called_once_with('Received %s request for %s', 'GET', '/foglamp/ping')
Exemple #6
0
async def test_ping_http_auth_required_allow_ping_true(test_server, test_client, loop, get_machine_detail):
    payload = '{"return": ["key", "description", "value"], "sort": {"column": "key", "direction": "asc"}}'
    result = {"rows": [
                {"value": 1, "key": "PURGED", "description": "blah6"},
                {"value": 2, "key": "READINGS", "description": "blah1"},
                {"value": 3, "key": "North Readings to PI", "description": "blah2"},
                {"value": 4, "key": "North Statistics to PI", "description": "blah3"},
                {"value": 10, "key": "North Statistics to OCS", "description": "blah5"},
                {"value": 100, "key": "Readings Sent", "description": "Readings Sent North"},
               ]}

    @asyncio.coroutine
    def mock_coro(*args, **kwargs):
        return result

    async def mock_get_category_item():
        return {"value": "true"}

    host_name, ip_addresses = get_machine_detail
    mock_storage_client_async = MagicMock(StorageClientAsync)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect, 'get_storage_async', return_value=mock_storage_client_async):
            with patch.object(mock_storage_client_async, 'query_tbl_with_payload', return_value=mock_coro()) as query_patch:
                with patch.object(ConfigurationManager, "get_category_item", return_value=mock_get_category_item()) as mock_get_cat:
                    app = web.Application(loop=loop, middlewares=[middleware.auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await test_server(app)
                    server.start_server(loop=loop)

                    client = await test_client(server)
                    # note: If the parameter is app aiohttp.web.Application
                    # the tool creates TestServer implicitly for serving the application.
                    resp = await client.get('/foglamp/ping')
                    assert 200 == resp.status
                    content = await resp.text()
                    content_dict = json.loads(content)
                    assert 0 <= content_dict["uptime"]
                    assert 2 == content_dict["dataRead"]
                    assert 100 == content_dict["dataSent"]
                    assert 1 == content_dict["dataPurged"]
                    assert content_dict["authenticationOptional"] is False
                    assert content_dict['serviceName'] == "FogLAMP"
                    assert content_dict['hostName'] == host_name
                    assert content_dict['ipAddresses'] == ip_addresses
                    assert content_dict['health'] == "green"
                    assert content_dict['safeMode'] is False
                mock_get_cat.assert_called_once_with('rest_api', 'allowPing')
            query_patch.assert_called_once_with('statistics', payload)
        log_params = 'Received %s request for %s', 'GET', '/foglamp/ping'
        logger_info.assert_called_once_with(*log_params)
Exemple #7
0
async def test_shutdown_http(test_server, test_client, loop):
    app = web.Application()
    # fill route table
    routes.setup(app)

    server = await test_server(app)
    server.start_server(loop=loop)

    client = await test_client(server)
    resp = await client.put('/foglamp/shutdown', data=None)
    assert 200 == resp.status
    content = await resp.text()
    content_dict = json.loads(content)
    assert "FogLAMP shutdown has been scheduled. Wait for few seconds for process cleanup." == content_dict["message"]
Exemple #8
0
    def _make_app(auth_required=True):
        """Creates the REST server

        :rtype: web.Application
        """
        app = web.Application(middlewares=[
            middleware.error_middleware, middleware.auth_middleware
        ])
        if not auth_required:
            app = web.Application(middlewares=[
                middleware.error_middleware,
                middleware.optional_auth_middleware
            ])
        admin_routes.setup(app)
        return app
Exemple #9
0
async def test_restart_http(test_server, test_client, loop):
    app = web.Application()
    # fill route table
    routes.setup(app)

    server = await test_server(app)
    server.start_server(loop=loop)

    with patch.object(_logger, 'info') as logger_info:
        client = await test_client(server)
        resp = await client.put('/foglamp/restart', data=None)
        assert 200 == resp.status
        content = await resp.text()
        content_dict = json.loads(content)
        assert "FogLAMP restart has been scheduled." == content_dict["message"]
    logger_info.assert_called_once_with('Executing controlled shutdown and start')
Exemple #10
0
async def test_ping_http_auth_required_allow_ping_false(test_server, test_client, loop, get_machine_detail):
    result = {"rows": [
        {"value": 1, "key": "PURGED", "description": "blah6"},
        {"value": 2, "key": "READINGS", "description": "blah1"},
        {"value": 3, "key": "North Readings to PI", "description": "blah2"},
        {"value": 4, "key": "North Statistics to PI", "description": "blah3"},
        {"value": 5, "key": "North Statistics to OCS", "description": "blah5"},
        {"value": 100, "key": "Readings Sent", "description": "Readings Sent North"},
    ]}

    @asyncio.coroutine
    def mock_coro(*args, **kwargs):
        return result

    async def mock_get_category_item():
        return {"value": "false"}

    mock_storage_client_async = MagicMock(StorageClientAsync)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect, 'get_storage_async', return_value=mock_storage_client_async):
            with patch.object(mock_storage_client_async, 'query_tbl_with_payload', return_value=mock_coro()) as query_patch:
                with patch.object(ConfigurationManager, "get_category_item", return_value=mock_get_category_item()) as mock_get_cat:
                    with patch.object(_logger, 'warning') as logger_warn:
                        app = web.Application(loop=loop, middlewares=[middleware.auth_middleware])
                        # fill route table
                        routes.setup(app)

                        server = await test_server(app)
                        server.start_server(loop=loop)

                        client = await test_client(server)
                        # note: If the parameter is app aiohttp.web.Application
                        # the tool creates TestServer implicitly for serving the application.
                        resp = await client.get('/foglamp/ping')
                        assert 403 == resp.status
                    logger_warn.assert_called_once_with('Permission denied for Ping when Auth is mandatory.')
                mock_get_cat.assert_called_once_with('rest_api', 'allowPing')
            assert 0 == query_patch.call_count
    log_params = 'Received %s request for %s', 'GET', '/foglamp/ping'
    logger_info.assert_called_once_with(*log_params)
Exemple #11
0
 def client(self, loop, test_client):
     app = web.Application(loop=loop)
     # fill the routes table
     routes.setup(app)
     return loop.run_until_complete(test_client(app))
Exemple #12
0
async def test_ping_https_auth_required_allow_ping_true(
        test_server, ssl_ctx, test_client, loop):
    payload = '{"return": ["key", "description", "value"], "sort": {"column": "key", "direction": "asc"}}'
    result = {
        "rows": [
            {
                "value": 1,
                "key": "PURGED",
                "description": "blah6"
            },
            {
                "value": 2,
                "key": "READINGS",
                "description": "blah1"
            },
            {
                "value": 3,
                "key": "SENT_1",
                "description": "blah2"
            },
            {
                "value": 4,
                "key": "SENT_2",
                "description": "blah3"
            },
            {
                "value": 10,
                "key": "SENT_4",
                "description": "blah5"
            },
        ]
    }

    @asyncio.coroutine
    def mock_coro(*args, **kwargs):
        return result

    async def mock_get_category_item():
        return {"value": "true"}

    mockedStorageClientAsync = MagicMock(StorageClientAsync)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect,
                          'get_storage_async',
                          return_value=mockedStorageClientAsync):
            with patch.object(mockedStorageClientAsync,
                              'query_tbl_with_payload',
                              return_value=mock_coro()) as query_patch:
                with patch.object(
                        ConfigurationManager,
                        "get_category_item",
                        return_value=mock_get_category_item()) as mock_get_cat:
                    app = web.Application(
                        loop=loop, middlewares=[middleware.auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await test_server(app, ssl=ssl_ctx)
                    server.start_server(loop=loop)

                    with pytest.raises(
                            aiohttp.ClientConnectorSSLError) as error_exec:
                        client = await test_client(server)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(
                        error_exec)

                    with pytest.raises(
                            aiohttp.ClientConnectorSSLError) as error_exec:
                        # self signed certificate,
                        # and we are not using SSL context here for client as verifier
                        connector = aiohttp.TCPConnector(verify_ssl=True,
                                                         loop=loop)
                        client = await test_client(server, connector=connector)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(
                        error_exec)

                    connector = aiohttp.TCPConnector(verify_ssl=False,
                                                     loop=loop)
                    client = await test_client(server, connector=connector)
                    resp = await client.get('/foglamp/ping')
                    s = resp.request_info.url.human_repr()
                    assert "https" == s[:5]
                    assert 200 == resp.status
                    content = await resp.text()
                    content_dict = json.loads(content)
                    assert 0.0 < content_dict["uptime"]
                    assert 2 == content_dict["dataRead"]
                    assert 17 == content_dict["dataSent"]
                    assert 1 == content_dict["dataPurged"]
                    assert content_dict["authenticationOptional"] is False
                    mock_get_cat.assert_called_once_with(
                        'rest_api', 'allowPing')
                query_patch.assert_called_once_with('statistics', payload)
            logger_info.assert_called_once_with('Received %s request for %s',
                                                'GET', '/foglamp/ping')
Exemple #13
0
async def test_ping_http_allow_ping_true(test_server, test_client, loop):
    payload = '{"return": ["key", "description", "value"], "sort": {"column": "key", "direction": "asc"}}'
    result = {
        "rows": [
            {
                "value": 1,
                "key": "PURGED",
                "description": "blah6"
            },
            {
                "value": 2,
                "key": "READINGS",
                "description": "blah1"
            },
            {
                "value": 3,
                "key": "SENT_1",
                "description": "blah2"
            },
            {
                "value": 4,
                "key": "SENT_2",
                "description": "blah3"
            },
            {
                "value": 10,
                "key": "SENT_4",
                "description": "blah5"
            },
        ]
    }

    @asyncio.coroutine
    def mock_coro(*args, **kwargs):
        return result

    async def mock_get_category_item():
        return {"value": "true"}

    attrs = {"query_tbl_with_payload.return_value": mock_coro()}
    mockedStorageClientAsync = MagicMock(spec=StorageClientAsync, **attrs)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect,
                          'get_storage_async',
                          return_value=mockedStorageClientAsync):
            with patch.object(mockedStorageClientAsync,
                              'query_tbl_with_payload',
                              return_value=mock_coro()) as query_patch:
                with patch.object(
                        ConfigurationManager,
                        "get_category_item",
                        return_value=mock_get_category_item()) as mock_get_cat:
                    app = web.Application(
                        loop=loop,
                        middlewares=[middleware.optional_auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await test_server(app)
                    server.start_server(loop=loop)

                    client = await test_client(server)
                    # note: If the parameter is app aiohttp.web.Application
                    # the tool creates TestServer implicitly for serving the application.
                    resp = await client.get('/foglamp/ping',
                                            headers={'authorization': "token"})
                    assert 200 == resp.status
                    content = await resp.text()
                    content_dict = json.loads(content)
                    assert 0.0 < content_dict["uptime"]
                    assert 2 == content_dict["dataRead"]
                    assert 17 == content_dict["dataSent"]
                    assert 1 == content_dict["dataPurged"]
                    assert content_dict["authenticationOptional"] is True
                mock_get_cat.assert_called_once_with('rest_api', 'allowPing')
            query_patch.assert_called_once_with('statistics', payload)
        log_params = 'Received %s request for %s', 'GET', '/foglamp/ping'
        logger_info.assert_called_once_with(*log_params)
Exemple #14
0
async def test_ping_https_auth_required_allow_ping_false(
        test_server, ssl_ctx, test_client, loop):
    async def mock_get_category_item():
        return {"value": "false"}

    payload = '{"return": ["key", "description", "value"], "sort": {"column": "key", "direction": "asc"}}'
    result = {
        "rows": [
            {
                "value": 1,
                "key": "PURGED",
                "description": "blah6"
            },
            {
                "value": 2,
                "key": "READINGS",
                "description": "blah1"
            },
            {
                "value": 3,
                "key": "SENT_1",
                "description": "blah2"
            },
            {
                "value": 4,
                "key": "SENT_2",
                "description": "blah3"
            },
            {
                "value": 5,
                "key": "SENT_3",
                "description": "blah4"
            },
            {
                "value": 6,
                "key": "SENT_4",
                "description": "blah5"
            },
        ]
    }

    mockedStorageClient = MagicMock(StorageClient)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect,
                          'get_storage',
                          return_value=mockedStorageClient):
            with patch.object(mockedStorageClient,
                              'query_tbl_with_payload',
                              return_value=result) as query_patch:
                with patch.object(
                        ConfigurationManager,
                        "get_category_item",
                        return_value=mock_get_category_item()) as mock_get_cat:
                    app = web.Application(
                        loop=loop, middlewares=[middleware.auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await test_server(app, ssl=ssl_ctx)
                    server.start_server(loop=loop)

                    with pytest.raises(
                            aiohttp.ClientConnectorSSLError) as error_exec:
                        client = await test_client(server)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(
                        error_exec)

                    with pytest.raises(
                            aiohttp.ClientConnectorSSLError) as error_exec:
                        # self signed certificate,
                        # and we are not using SSL context here for client as verifier
                        connector = aiohttp.TCPConnector(verify_ssl=True,
                                                         loop=loop)
                        client = await test_client(server, connector=connector)
                        resp = await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(
                        error_exec)

                    connector = aiohttp.TCPConnector(verify_ssl=False,
                                                     loop=loop)
                    client = await test_client(server, connector=connector)
                    resp = await client.get('/foglamp/ping')
                    s = resp.request_info.url.human_repr()
                    assert "https" == s[:5]
                    assert 403 == resp.status
                mock_get_cat.assert_called_once_with('rest_api', 'allowPing')
            assert 0 == query_patch.call_count
        logger_info.assert_called_once_with('Received %s request for %s',
                                            'GET', '/foglamp/ping')
Exemple #15
0
async def test_ping_http_auth_required_allow_ping_false(
        test_server, test_client, loop):
    async def mock_get_category_item():
        return {"value": "false"}

    payload = '{"return": ["key", "description", "value"], "sort": {"column": "key", "direction": "asc"}}'
    result = {
        "rows": [
            {
                "value": 1,
                "key": "PURGED",
                "description": "blah6"
            },
            {
                "value": 2,
                "key": "READINGS",
                "description": "blah1"
            },
            {
                "value": 3,
                "key": "SENT_1",
                "description": "blah2"
            },
            {
                "value": 4,
                "key": "SENT_2",
                "description": "blah3"
            },
            {
                "value": 5,
                "key": "SENT_3",
                "description": "blah4"
            },
            {
                "value": 6,
                "key": "SENT_4",
                "description": "blah5"
            },
        ]
    }

    mockedStorageClient = MagicMock(StorageClient)
    with patch.object(middleware._logger, 'info') as logger_info:
        with patch.object(connect,
                          'get_storage',
                          return_value=mockedStorageClient):
            with patch.object(mockedStorageClient,
                              'query_tbl_with_payload',
                              return_value=result) as query_patch:
                with patch.object(
                        ConfigurationManager,
                        "get_category_item",
                        return_value=mock_get_category_item()) as mock_get_cat:
                    app = web.Application(
                        loop=loop, middlewares=[middleware.auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await test_server(app)
                    server.start_server(loop=loop)

                    client = await test_client(server)
                    # note: If the parameter is app aiohttp.web.Application
                    # the tool creates TestServer implicitly for serving the application.
                    resp = await client.get('/foglamp/ping')
                    assert 403 == resp.status
                mock_get_cat.assert_called_once_with('rest_api', 'allowPing')
            assert 0 == query_patch.call_count
        log_params = 'Received %s request for %s', 'GET', '/foglamp/ping'
        logger_info.assert_called_once_with(*log_params)