Skip to content

Commit 3671bb8

Browse files
committed
FEAT: Access Token Login
1 parent 1c78d74 commit 3671bb8

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

mssql_python/connection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ def _construct_connection_string(self, connection_str: str, **kwargs) -> str:
8080

8181
# Add additional key-value pairs to the connection string
8282
for key, value in kwargs.items():
83-
if key.lower() == "host":
83+
if key.lower() == "host" or key.lower() == "server":
8484
key = "Server"
85-
elif key.lower() == "user":
85+
elif key.lower() == "user" or key.lower() == "uid":
8686
key = "Uid"
87-
elif key.lower() == "password":
87+
elif key.lower() == "password" or key.lower() == "pwd":
8888
key = "Pwd"
8989
elif key.lower() == "database":
9090
key = "Database"
@@ -95,6 +95,7 @@ def _construct_connection_string(self, connection_str: str, **kwargs) -> str:
9595
else:
9696
continue
9797
conn_str += f"{key}={value};"
98+
print(f"Connection string after adding driver: {conn_str}")
9899

99100
if ENABLE_LOGGING:
100101
logger.info("Final connection string: %s", conn_str)

mssql_python/db_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mssql_python.connection import Connection
77

88

9-
def connect(connection_str: str, autocommit: bool = True, attrs_before: dict = None, **kwargs) -> Connection:
9+
def connect(connection_str: str = "", autocommit: bool = True, attrs_before: dict = None, **kwargs) -> Connection:
1010
"""
1111
Constructor for creating a connection to the database.
1212

mssql_python/helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def add_driver_to_connection_str(connection_str):
4646
# Insert the driver attribute at the beginning of the connection string
4747
final_connection_attributes.insert(0, driver_name)
4848
connection_str = ";".join(final_connection_attributes)
49+
print(f"Connection string after adding driver: {connection_str}")
4950
except Exception as e:
5051
raise Exception(
5152
"Invalid connection string, Please follow the format: "

tests/test_003_connection.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,33 @@ def test_connection(db_connection):
3333

3434
def test_construct_connection_string(db_connection):
3535
# Check if the connection string is constructed correctly with kwargs
36-
conn_str = db_connection._construct_connection_string("",host="localhost", user="me", password="mypwd", database="mydb", encrypt="yes", trust_server_certificate="yes")
36+
conn_str = db_connection._construct_connection_string(host="localhost", user="me", password="mypwd", database="mydb", encrypt="yes", trust_server_certificate="yes")
37+
assert "Server=localhost;" in conn_str, "Connection string should contain 'Server=localhost;'"
38+
assert "Uid=me;" in conn_str, "Connection string should contain 'Uid=me;'"
39+
assert "Pwd=mypwd;" in conn_str, "Connection string should contain 'Pwd=mypwd;'"
40+
assert "Database=mydb;" in conn_str, "Connection string should contain 'Database=mydb;'"
41+
assert "Encrypt=yes;" in conn_str, "Connection string should contain 'Encrypt=yes;'"
42+
assert "TrustServerCertificate=yes;" in conn_str, "Connection string should contain 'TrustServerCertificate=yes;'"
43+
assert "APP=MSSQL-Python" in conn_str, "Connection string should contain 'APP=MSSQL-Python'"
44+
assert "Driver={ODBC Driver 18 for SQL Server}" in conn_str, "Connection string should contain 'Driver={ODBC Driver 18 for SQL Server}'"
45+
assert "Driver={ODBC Driver 18 for SQL Server};;APP=MSSQL-Python;Server=localhost;Uid=me;Pwd=mypwd;Database=mydb;Encrypt=yes;TrustServerCertificate=yes;" == conn_str, "Connection string is incorrect"
46+
47+
def test_connection_string_with_attrs_before(db_connection):
48+
# Check if the connection string is constructed correctly with attrs_before
49+
conn_str = db_connection._construct_connection_string(host="localhost", user="me", password="mypwd", database="mydb", encrypt="yes", trust_server_certificate="yes", attrs_before={1256: "token"})
50+
assert "Server=localhost;" in conn_str, "Connection string should contain 'Server=localhost;'"
51+
assert "Uid=me;" in conn_str, "Connection string should contain 'Uid=me;'"
52+
assert "Pwd=mypwd;" in conn_str, "Connection string should contain 'Pwd=mypwd;'"
53+
assert "Database=mydb;" in conn_str, "Connection string should contain 'Database=mydb;'"
54+
assert "Encrypt=yes;" in conn_str, "Connection string should contain 'Encrypt=yes;'"
55+
assert "TrustServerCertificate=yes;" in conn_str, "Connection string should contain 'TrustServerCertificate=yes;'"
56+
assert "APP=MSSQL-Python" in conn_str, "Connection string should contain 'APP=MSSQL-Python'"
57+
assert "Driver={ODBC Driver 18 for SQL Server}" in conn_str, "Connection string should contain 'Driver={ODBC Driver 18 for SQL Server}'"
58+
assert "{1256: token}" in conn_str, "Connection string should contain '{1256: token}'"
59+
60+
def test_connection_string_with_odbc_param(db_connection):
61+
# Check if the connection string is constructed correctly with ODBC parameters
62+
conn_str = db_connection._construct_connection_string(server="localhost", uid="me", pwd="mypwd", database="mydb", encrypt="yes", trust_server_certificate="yes")
3763
assert "Server=localhost;" in conn_str, "Connection string should contain 'Server=localhost;'"
3864
assert "Uid=me;" in conn_str, "Connection string should contain 'Uid=me;'"
3965
assert "Pwd=mypwd;" in conn_str, "Connection string should contain 'Pwd=mypwd;'"

0 commit comments

Comments
 (0)