Skip to content

Commit 6e922a1

Browse files
committed
test(connection, auth-profile): auth via redshift authentication profile
1 parent fe8b721 commit 6e922a1

File tree

5 files changed

+375
-15
lines changed

5 files changed

+375
-15
lines changed

test/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
adfs_idp,
33
azure_browser_idp,
44
azure_idp,
5+
db_kwargs,
56
idp_arg,
67
jumpcloud_browser_idp,
78
jwt_azure_v2_idp,

test/integration/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
adfs_idp,
33
azure_browser_idp,
44
azure_idp,
5+
db_kwargs,
56
idp_arg,
67
jumpcloud_browser_idp,
78
jwt_azure_v2_idp,
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import typing
2+
from test import db_kwargs
3+
4+
import pytest
5+
6+
import redshift_connector
7+
8+
access_key_id: str = "replace_me"
9+
secret_access_key: str = "replace_me"
10+
session_token: str = "replace_me"
11+
12+
"""
13+
This is a manually executable test. It requires valid AWS credentials.
14+
15+
A Redshift authentication profile will be created prior to the test.
16+
Following the test, this profile will be deleted.
17+
18+
Please modify the fixture, handle_redshift_auth_profile, to include any additional arguments to boto3 client required
19+
for your testing configuration. This may include attributes such as endpoint_url and region. The contents of the auth
20+
profile can be modified as needed.
21+
"""
22+
23+
24+
creds = {"aws_access_key_id": "replace_me", "aws_session_token": "replace_me", "aws_secret_access_key": "replace_me"}
25+
26+
auth_profile_name: str = "PythonManualTest"
27+
28+
29+
@pytest.fixture(autouse=True)
30+
def handle_redshift_auth_profile(request, db_kwargs):
31+
32+
import json
33+
34+
import boto3
35+
from botocore.exceptions import ClientError
36+
37+
payload = json.dumps(
38+
{
39+
"host": db_kwargs["host"],
40+
"db_user": db_kwargs["user"],
41+
"max_prepared_statements": 5,
42+
"region": db_kwargs["region"],
43+
"cluster_identifier": db_kwargs["cluster_identifier"],
44+
"db_name": db_kwargs["database"],
45+
}
46+
)
47+
48+
try:
49+
client = boto3.client(
50+
"redshift",
51+
**{**creds, **{"region_name": db_kwargs["region"]}},
52+
verify=False,
53+
)
54+
client.create_authentication_profile(
55+
AuthenticationProfileName=auth_profile_name, AuthenticationProfileContent=payload
56+
)
57+
except ClientError:
58+
raise
59+
60+
def fin():
61+
import boto3
62+
from botocore.exceptions import ClientError
63+
64+
try:
65+
client = boto3.client(
66+
"redshift",
67+
**{**creds, **{"region_name": db_kwargs["region"]}},
68+
verify=False,
69+
)
70+
client.delete_authentication_profile(
71+
AuthenticationProfileName=auth_profile_name,
72+
)
73+
except ClientError:
74+
raise
75+
76+
request.addfinalizer(fin)
77+
78+
79+
@pytest.mark.skip(reason="manual")
80+
def test_redshift_auth_profile_can_connect(db_kwargs):
81+
82+
with redshift_connector.connect(
83+
region=db_kwargs["region"],
84+
access_key_id=creds["aws_access_key_id"],
85+
secret_access_key=creds["aws_secret_access_key"],
86+
session_token=creds["aws_session_token"],
87+
auth_profile=auth_profile_name,
88+
iam=True,
89+
) as conn:
90+
assert conn.user == "IAM:{}".format(db_kwargs["user"]).encode()
91+
assert conn.max_prepared_statements == 5

0 commit comments

Comments
 (0)