async def test_request_success( self, event_loop: asyncio.AbstractEventLoop, httpserver: HTTPServer, influxdb_writer: InfluxDBWriter, log_records: LogRecordsType, mocker: MockerFixture, ) -> None: httpserver.expect_oneshot_request( "/influx/api/v2/write", method="POST", data="measurement,tag=tagval field=1.0 ", query_string={"bucket": "test_db", "precision": "s"}, ).respond_with_json({}, status=204) task = event_loop.create_task(influxdb_writer.task()) influxdb_writer.put(mocker.Mock()) await asyncio.sleep(0.1) httpserver.check_assertions() assert not any(r.levelno == logging.ERROR for r in log_records()) task.cancel() with contextlib.suppress(asyncio.CancelledError): await task
def test_constructor_with_params(httpserver: test_server.HTTPServer): json_response = {"ok": True, "name": "root", "roles": ["_admin"]} httpserver.expect_oneshot_request("/_session", method="POST") \ .respond_with_json(json_response, headers={'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; ' 'Version=1; Path=/; HttpOnly'}) couch = couchapy.CouchDB(custom_headers={"header_key": "header_value"}, session_timeout=60, keep_alive=True, host="https://localhost", port=6984, name="someadmin", password="******", admin_party=True, auto_connect=False) assert 'header_key' in couch.custom_headers and couch.custom_headers[ 'header_key'] == 'header_value' assert couch.session_timeout == 60 assert couch.keep_alive is True assert couch.host == "https://localhost" assert couch.port == 6984 assert couch.name == "someadmin" assert couch.password == "somepassword" assert couch._admin_party is True assert couch.session.auth_token is None couch = couchapy.CouchDB(custom_headers={"header_key": "header_value"}, session_timeout=60, port=8000, keep_alive=True, name="someadmin", password="******", auto_connect=True) assert couch._auto_renew_worker is not None
def test_get_replicator_docs(httpserver: test_server.HTTPServer): expected_json = { "docs": [ { "database": "other/_replicator", "doc_id": "cdyno-0000001-0000002", "error_count": 0, "id": "e327d79214831ca4c11550b4a453c9ba+continuous", "info": None, "last_updated": "2017-04-29T05:01:37Z", "node": "[email protected]", "proxy": None, "source": "http://myserver.com/foo", "start_time": "2017-04-29T05:01:37Z", "state": "running", "target": "http://*****:*****@localhost:15984/cdyno-0000002/" } ], "offset": 0, "total_rows": 1 } httpserver.expect_oneshot_request("/_scheduler/docs/other/_replicator", method="GET").respond_with_json(expected_json) response = couch.server.replicator_docs(uri_segments={'db': 'other'}) assert response == expected_json for code in [401]: httpserver.expect_oneshot_request("/_scheduler/docs/other/_replicator", method="GET").respond_with_json({}, status=code) response = couch.server.replicator_docs(uri_segments={'db': 'other'}) assert isinstance(response, couchapy.CouchError) is True httpserver.expect_request("/_scheduler/docs/other/_replicator", method="GET").respond_with_json({}) for k in AllowedKeys.SERVER__SCHEDULER_DOCS__PARAMS: response = couch.server.replicator_docs(uri_segments={'db': 'other'}, params={k: ['test']}) assert isinstance(response, couchapy.CouchError) is False with pytest.raises(couchapy.InvalidKeysException): couch.server.replicator_docs(uri_segments={'db': 'other'}, params={'nonexisting_key': ''})
def test_set_config(httpserver: HTTPServer): expected_json = {'data': "5984"} httpserver.expect_oneshot_request( "/_node/_local/_config/httpd/port", method="PUT").respond_with_json(expected_json) response = couch.server.set_config(uri_segments={ 'node_name': '_local', 'key': 'httpd/port' }, data="5984") assert response == expected_json for code in [400, 401, 500]: httpserver.expect_oneshot_request("/_node/_local/_config/httpd/port", method="PUT").respond_with_json( expected_json, status=code) response = couch.server.set_config(uri_segments={ 'node_name': '_local', 'key': 'httpd/port' }, data="5984") assert isinstance(response, CouchError) is True
def test_replicate(httpserver: test_server.HTTPServer): expected_json = { "history": [ { "doc_write_failures": 0, "docs_read": 10, "docs_written": 10, "end_last_seq": 28, "end_time": "Sun, 11 Aug 2013 20:38:50 GMT", "missing_checked": 10, "missing_found": 10, "recorded_seq": 28, "session_id": "142a35854a08e205c47174d91b1f9628", "start_last_seq": 1, "start_time": "Sun, 11 Aug 2013 20:38:50 GMT" }, { "doc_write_failures": 0, "docs_read": 1, "docs_written": 1, "end_last_seq": 1, "end_time": "Sat, 10 Aug 2013 15:41:54 GMT", "missing_checked": 1, "missing_found": 1, "recorded_seq": 1, "session_id": "6314f35c51de3ac408af79d6ee0c1a09", "start_last_seq": 0, "start_time": "Sat, 10 Aug 2013 15:41:54 GMT" } ], "ok": True, "replication_id_version": 3, "session_id": "142a35854a08e205c47174d91b1f9628", "source_last_seq": 28 } httpserver.expect_oneshot_request("/_replicate", method="POST").respond_with_json(expected_json) response = couch.server.replicate() assert response == expected_json for code in [202]: httpserver.expect_oneshot_request("/_replicate", method="POST").respond_with_json({}, status=code) response = couch.server.replicate() assert isinstance(response, couchapy.CouchError) is False for code in [400, 401, 404, 500]: httpserver.expect_oneshot_request("/_replicate", method="POST").respond_with_json({}, status=code) response = couch.server.replicate() assert isinstance(response, couchapy.CouchError) is True httpserver.expect_request("/_replicate", method="POST").respond_with_json({}) for k in AllowedKeys.SERVER__REPLICATE__DATA: response = couch.server.replicate(data={k: ['test']}) assert isinstance(response, couchapy.CouchError) is False with pytest.raises(couchapy.InvalidKeysException): couch.server.replicate(data={'nonexisting_key': ''})
def test_get_replicator_doc(httpserver: test_server.HTTPServer): expected_json = { "database": "other/_replicator", "doc_id": "cdyno-0000001-0000002", "error_count": 0, "id": "e327d79214831ca4c11550b4a453c9ba+continuous", "info": None, "last_updated": "2017-04-29T05:01:37Z", "node": "[email protected]", "proxy": None, "source": "http://myserver.com/foo", "start_time": "2017-04-29T05:01:37Z", "state": "running", "target": "http://*****:*****@localhost:15984/cdyno-0000002/" } httpserver.expect_oneshot_request("/_scheduler/docs/other/_replicator/replication-doc-id", method="GET").respond_with_json(expected_json) response = couch.server.replicator_doc(uri_segments={'db': 'other', 'docid': 'replication-doc-id'}) assert response == expected_json for code in [401]: httpserver.expect_oneshot_request("/_scheduler/docs/other/_replicator/replication-doc-id", method="GET").respond_with_json({}, status=code) response = couch.server.replicator_doc(uri_segments={'db': 'other', 'docid': 'replication-doc-id'}) assert isinstance(response, couchapy.CouchError) is True
def test_get_session_info(httpserver: HTTPServer): expected_json = { "info": { "authenticated": "cookie", "authentication_db": "_users", "authentication_handlers": ["cookie", "default"] }, "ok": True, "userCtx": { "name": "root", "roles": ["_admin"] } } httpserver.expect_request("/_session", method="POST").respond_with_json( expected_json, headers={ 'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly' }) httpserver.expect_oneshot_request( "/_session", method="GET" ).respond_with_json( expected_json, headers={ 'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly' }) couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000, auto_connect=True) response = couch.session.get_session_info() assert response == expected_json httpserver.expect_oneshot_request( "/_session", method="GET" ).respond_with_json( expected_json, headers={ 'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly' }) response = couch.session.renew_session() assert response == expected_json for code in [401]: httpserver.expect_oneshot_request( "/_session", method="GET").respond_with_json({}, status=code) response = couch.session.get_session_info() assert isinstance(response, couchapy.error.CouchError) is True
def test_generate_uiids(httpserver: HTTPServer): expected_json = { "uuids": [ "75480ca477454894678e22eec6002413", "75480ca477454894678e22eec600250b", "75480ca477454894678e22eec6002c41", "75480ca477454894678e22eec6003b90", "75480ca477454894678e22eec6003fca", "75480ca477454894678e22eec6004bef", "75480ca477454894678e22eec600528f", "75480ca477454894678e22eec6005e0b", "75480ca477454894678e22eec6006158", "75480ca477454894678e22eec6006161" ] } httpserver.expect_oneshot_request( "/_uuids", method="GET").respond_with_json(expected_json) response = couch.server.generate_uuids() assert response == expected_json['uuids'] expected_json = {"uuids": ["75480ca477454894678e22eec6002413"]} httpserver.expect_oneshot_request( "/_uuids", method="GET").respond_with_json(expected_json) response = couch.server.generate_uuids() assert response == expected_json['uuids'][0] for code in [400]: httpserver.expect_oneshot_request( "/_uuids", method="GET").respond_with_json(expected_json, status=code) response = couch.server.generate_uuids() assert isinstance(response, CouchError) is True httpserver.expect_request("/_uuids", method="GET").respond_with_json(expected_json) for k in AllowedKeys.SERVER__UUIDS__PARAMS: response = couch.server.generate_uuids(params={k: ['test']}) assert isinstance(response, CouchError) is False with pytest.raises(InvalidKeysException): couch.server.generate_uuids(params={'nonexisting_key': ''})
def test_device_twin( self, azure_user: User, httpserver: HTTPServer, httpserver_ssl_context: ssl.SSLContext, ): """Test device state synchronization with IoT Hub Device Twin""" dev = self._prepare_device(azure_user, httpserver, httpserver_ssl_context) self._check_if_device_status_is_set_to_value(azure_user, httpserver, dev.id, "enabled") # get the all device states (device twins) if self.azure_iot_hub_mock: httpserver.expect_oneshot_request( re.compile("^/devices"), method="GET", query_string="api-version=2021-04-12", ).respond_with_json(self._prepare_iot_hub_upsert_device_response()) rsp = self.api_azure.with_auth(azure_user.token).call( "GET", iot.URL_DEVICE_STATE(dev.id)) assert rsp.status_code == 200 states = rsp.json() assert len(states.keys()) == 1 integration_id = list(states.keys())[0] assert "desired" in states[integration_id] assert "reported" in states[integration_id] # set the device state (device twin) if self.azure_iot_hub_mock: httpserver.expect_oneshot_request( re.compile("^/twins"), method="GET", query_string="api-version=2021-04-12", ).respond_with_json(self._prepare_iot_hub_upsert_device_response()) httpserver.expect_oneshot_request( re.compile("^/twins"), method="PUT", query_string="api-version=2021-04-12", ).respond_with_data(status=200) twin = { "desired": { "key": "value" }, } rsp = (self.api_azure.with_auth(azure_user.token).with_header( "Content-Type", "application/json").call( "PUT", iot.URL_DEVICE_STATE(dev.id) + "/" + integration_id, twin)) assert rsp.status_code == 200 state = rsp.json() assert "desired" in state assert "reported" in states[integration_id] assert state["desired"]["key"] == "value" # get the device state (device twin) if self.azure_iot_hub_mock: httpserver.expect_oneshot_request( re.compile("^/twins"), method="GET", query_string="api-version=2021-04-12", ).respond_with_json(self._prepare_iot_hub_upsert_device_response()) rsp = self.api_azure.with_auth(azure_user.token).call( "GET", iot.URL_DEVICE_STATE(dev.id) + "/" + integration_id) assert rsp.status_code == 200 state = rsp.json() assert "desired" in state assert "reported" in states[integration_id] assert state["desired"]["key"] == "value"
def test_iteratively_retry(httpserver: HTTPServer): logger = CustomLogger() p = IterativelyPlugin( 'My-Key', httpserver.url_for('/track'), IterativelyOptions( flush_queue_size=3, flush_interval=timedelta(seconds=1), retry_options=IterativelyRetryOptions( max_retries=3, delay_initial=timedelta(seconds=0.1), delay_maximum=timedelta(seconds=0.5)), )) expected_request = {'objects': [post_track_1_expected_request]} try: p.load( PluginLoadOptions(environment=Environment.DEVELOPMENT, logger=logger)) httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data(status=429) httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data(status=501) httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data() p.post_track(*post_track_1_args) p.flush() time.sleep(2) requests = _get_cleaned_requests(httpserver) assert requests == [ expected_request, expected_request, expected_request ] assert logger.log_lines == [] httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data(status=429) httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data(status=501) httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data(status=504) httpserver.expect_oneshot_request( re.compile('/track')).respond_with_data() p.post_track(*post_track_1_args) p.flush() time.sleep(2) requests = _get_cleaned_requests(httpserver) assert requests == [ expected_request, expected_request, expected_request, expected_request, expected_request, expected_request, ] assert logger.log_lines == [ 'Error. Failed to upload events. Maximum attempts exceeded.' ] finally: p.shutdown() httpserver.stop()
def _setup_oneshot(server: HTTPServer): server.expect_request("/permanent").respond_with_data("OK permanent") server.expect_oneshot_request("/oneshot1").respond_with_data("OK oneshot1") server.expect_oneshot_request("/oneshot2").respond_with_data("OK oneshot2")
def test_get_database_names_without_params(httpserver: test_server.HTTPServer): expected_json = ["_users", "contacts", "docs", "invoices", "locations"] httpserver.expect_oneshot_request("/_all_dbs", method="GET").respond_with_json(expected_json) response = couch.server.database_names() assert response == expected_json
def test_get_databases_without_params(httpserver: test_server.HTTPServer): expected_json = [] httpserver.expect_oneshot_request("/_dbs_info", method="POST").respond_with_json(expected_json) response = couch.server.databases() assert response == expected_json
def test_get_server_config(httpserver: HTTPServer): expected_json = { "attachments": { "compressible_types": "text/*, application/javascript, application/json, application/xml", "compression_level": "8" }, "couchdb": { "users_db_suffix": "_users", "database_dir": "/var/lib/couchdb", "delayed_commits": "true", "max_attachment_chunk_size": "4294967296", "max_dbs_open": "100", "os_process_timeout": "5000", "uri_file": "/var/lib/couchdb/couch.uri", "util_driver_dir": "/usr/lib64/couchdb/erlang/lib/couch-1.5.0/priv/lib", "view_index_dir": "/var/lib/couchdb" }, "chttpd": { "backlog": "512", "bind_address": "0.0.0.0", "docroot": "./share/www", "port": "5984", "require_valid_user": "******", "socket_options": "[{sndbuf, 262144}, {nodelay, true}]", "server_options": "[{recbuf, undefined}]" }, "httpd": { "allow_jsonp": "false", "authentication_handlers": "{couch_httpd_auth, cookie_authentication_handler}, {couch_httpd_auth, default_authentication_handler}", "bind_address": "192.168.0.2", "max_connections": "2048", "port": "5984", "secure_rewrites": "true" }, "log": { "writer": "file", "file": "/var/log/couchdb/couch.log", "include_sasl": "true", "level": "info" }, "query_server_config": { "reduce_limit": "true" }, "replicator": { "max_http_pipeline_size": "10", "max_http_sessions": "10" }, "stats": { "rate": "1000", "samples": "[0, 60, 300, 900]" }, "uuids": { "algorithm": "utc_random" } } httpserver.expect_oneshot_request( "/_node/_local/_config", method="GET").respond_with_json(expected_json) response = couch.server.get_server_config( uri_segments={'node_name': '_local'}) assert response == expected_json for code in [401]: httpserver.expect_oneshot_request("/_node/_local/_config", method="GET").respond_with_json( {}, status=code) response = couch.server.get_server_config( uri_segments={'node_name': '_local'}) assert isinstance(response, CouchError) is True