def test_create_basic_auth_header(httpserver: HTTPServer): couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000) basic_auth_header = couch.session._create_basic_auth_header() assert basic_auth_header == {'Authorization': 'Basic dGVzdDp0ZXN0'}
def test_context_manager(): with couchapy.CouchDB() as couch: assert couch._context_manager is True, \ "Expected context manager flag to be True inside of a with block" assert couch._context_manager is False, \ "Expected context manager flag to be False outisde of a with block"
def setup(): """ setup any state specific to the execution of the given module.""" global couch couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000) yield
def test_close(httpserver: HTTPServer): httpserver.expect_request("/_session", method="DELETE").respond_with_json({}) couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000) response = couch.session.close() assert response is None
def test_auto_connect_on_initialization(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(name="test", password="******", host="http://127.0.0.1", port=8000, auto_connect=True) assert couch.session.auth_token == 'cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw', \ "Expected session auth token to match, but it didn't" couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000, auto_connect=False) assert couch.session.auth_token is None, \ "Expected auth token to be None when auto_connect is False" couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000) assert couch.session.auth_token is None, \ "Expected session auth token to be None when auto_connect is not provided" for code in [401]: httpserver.expect_oneshot_request( "/_session", method="POST").respond_with_json({}, status=code) couch = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000) response = couch.session.authenticate() assert isinstance(response, couchapy.error.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_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_empty_constructor(): couch = couchapy.CouchDB() assert couch._context_manager is False assert couch._auto_renew_worker is None assert couch.host == 'http://127.0.0.1' assert couch.port == 5984 assert couch.name is None assert couch.password is None assert couch.keep_alive == 0 assert couch._admin_party is False assert isinstance(couch.session, couchapy.Session) assert isinstance(couch.session, couchapy.Session) and couch.session.auth_token is None
def test_authenticate(httpserver: 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(name="test", password="******", host="http://127.0.0.1", port=8000, auto_connect=False) couch.session.authenticate(data={ 'name': couch.name, 'password': couch.password }) assert couch.session.auth_token == "cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw"
def test_instanced_class_with_args_has_correct_properties(httpserver: test_server.HTTPServer): server = couchapy.CouchDB(name="test", password="******", host="http://127.0.0.1", port=8000, server_kwargs={'predefined_segments': {'node_name': 'test'}}).server assert 'node_name' in server._predefined_segments and server._predefined_segments['node_name'] == 'test'