Skip to content

Commit 7722acc

Browse files
PAT - small fixes, add tests (#2403)
1 parent cc867aa commit 7722acc

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/snowflake/connector/auth/by_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AuthType(Enum):
5050
ID_TOKEN = "ID_TOKEN"
5151
USR_PWD_MFA = "USERNAME_PASSWORD_MFA"
5252
OKTA = "OKTA"
53-
PAT = "PROGRAMMATIC_ACCESS_TOKEN'"
53+
PAT = "PROGRAMMATIC_ACCESS_TOKEN"
5454
NO_AUTH = "NO_AUTH"
5555
WORKLOAD_IDENTITY = "WORKLOAD_IDENTITY"
5656
PAT_WITH_EXTERNAL_SESSION = "PAT_WITH_EXTERNAL_SESSION"

src/snowflake/connector/auth/pat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self, pat_token: str, **kwargs) -> None:
1313
super().__init__(**kwargs)
1414
self._pat_token: str | None = pat_token
1515

16+
@property
1617
def type_(self) -> AuthType:
1718
return AuthType.PAT
1819

test/unit/test_auth_pat.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
4+
#
5+
6+
from __future__ import annotations
7+
8+
from snowflake.connector.auth import AuthByPAT
9+
from snowflake.connector.auth.by_plugin import AuthType
10+
from snowflake.connector.network import PROGRAMMATIC_ACCESS_TOKEN
11+
12+
13+
def test_auth_pat():
14+
"""Simple PAT test."""
15+
token = "patToken"
16+
auth = AuthByPAT(token)
17+
assert auth.type_ == AuthType.PAT
18+
assert auth.assertion_content == token
19+
body = {"data": {}}
20+
auth.update_body(body)
21+
assert body["data"]["TOKEN"] == token, body
22+
assert body["data"]["AUTHENTICATOR"] == PROGRAMMATIC_ACCESS_TOKEN, body
23+
24+
auth.reset_secrets()
25+
assert auth.assertion_content is None
26+
27+
28+
def test_auth_pat_reauthenticate():
29+
"""Test PAT reauthenticate."""
30+
token = "patToken"
31+
auth = AuthByPAT(token)
32+
result = auth.reauthenticate()
33+
assert result == {"success": False}
34+
35+
36+
def test_pat_authenticator_creates_auth_by_pat(monkeypatch):
37+
"""Test that using PROGRAMMATIC_ACCESS_TOKEN authenticator creates AuthByPAT instance."""
38+
import snowflake.connector
39+
40+
def mock_post_request(request, url, headers, json_body, **kwargs):
41+
return {
42+
"success": True,
43+
"message": None,
44+
"data": {
45+
"token": "TOKEN",
46+
"masterToken": "MASTER_TOKEN",
47+
"idToken": None,
48+
"parameters": [{"name": "SERVICE_NAME", "value": "FAKE_SERVICE_NAME"}],
49+
},
50+
}
51+
52+
monkeypatch.setattr(
53+
snowflake.connector.network.SnowflakeRestful, "_post_request", mock_post_request
54+
)
55+
56+
# Create connection with PAT authenticator
57+
conn = snowflake.connector.connect(
58+
user="user",
59+
account="account",
60+
database="TESTDB",
61+
warehouse="TESTWH",
62+
authenticator=PROGRAMMATIC_ACCESS_TOKEN,
63+
token="test_pat_token",
64+
)
65+
66+
# Verify that the auth_class is an instance of AuthByPAT
67+
assert isinstance(conn.auth_class, AuthByPAT)
68+
69+
conn.close()

0 commit comments

Comments
 (0)