| 
1 |  | -from datajoint.axon import Session  | 
 | 1 | +import os  | 
 | 2 | +from datajoint.axon import Session, get_s3_client  | 
 | 3 | +import json  | 
2 | 4 | import pytest  | 
3 | 5 | import boto3  | 
4 | 6 | from moto import mock_aws  | 
 | 7 | +import dotenv  | 
 | 8 | +dotenv.load_dotenv(dotenv.find_dotenv())  | 
5 | 9 | 
 
  | 
6 | 10 | 
 
  | 
7 | 11 | @pytest.fixture  | 
8 | 12 | def moto_account_id():  | 
9 | 13 |     """Default account ID for moto"""  | 
10 | 14 |     return "123456789012"  | 
11 | 15 | 
 
  | 
 | 16 | + | 
 | 17 | +@pytest.fixture  | 
 | 18 | +def keycloak_client_secret():  | 
 | 19 | +    secret = os.getenv("OAUTH_CLIENT_SECRET")  | 
 | 20 | +    if not secret:  | 
 | 21 | +        pytest.skip("No client secret found")  | 
 | 22 | +    else:  | 
 | 23 | +        return secret  | 
 | 24 | + | 
 | 25 | + | 
 | 26 | +@pytest.fixture  | 
 | 27 | +def keycloak_client_id():  | 
 | 28 | +    return os.getenv("OAUTH_CLIENT_ID", "works")  | 
 | 29 | + | 
 | 30 | + | 
 | 31 | +@pytest.fixture(scope="function")  | 
 | 32 | +def aws_credentials():  | 
 | 33 | +    """Mocked AWS Credentials for moto."""  | 
 | 34 | +    os.environ["AWS_ACCESS_KEY_ID"] = "testing"  | 
 | 35 | +    os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"  | 
 | 36 | +    os.environ["AWS_SECURITY_TOKEN"] = "testing"  | 
 | 37 | +    os.environ["AWS_SESSION_TOKEN"] = "testing"  | 
 | 38 | +    os.environ["AWS_DEFAULT_REGION"] = "us-east-1"  | 
 | 39 | + | 
 | 40 | + | 
 | 41 | +@pytest.fixture(scope="function")  | 
 | 42 | +def s3_client(aws_credentials):  | 
 | 43 | +    """  | 
 | 44 | +    Return a mocked S3 client  | 
 | 45 | +    """  | 
 | 46 | +    with mock_aws():  | 
 | 47 | +        yield boto3.client("s3", region_name="us-east-1")  | 
 | 48 | + | 
 | 49 | + | 
 | 50 | +@pytest.fixture(scope="function")  | 
 | 51 | +def iam_client(aws_credentials):  | 
 | 52 | +    """  | 
 | 53 | +    Return a mocked S3 client  | 
 | 54 | +    """  | 
 | 55 | +    with mock_aws():  | 
 | 56 | +        yield boto3.client("iam", region_name="us-east-1")  | 
 | 57 | + | 
 | 58 | + | 
 | 59 | +@pytest.fixture  | 
 | 60 | +def s3_policy(iam_client):  | 
 | 61 | +    """Create a policy with S3 read access using boto3."""  | 
 | 62 | +    policy_doc = {  | 
 | 63 | +        "Version": "2012-10-17",  | 
 | 64 | +        "Statement": [  | 
 | 65 | +            {  | 
 | 66 | +                "Effect": "Allow",  | 
 | 67 | +                "Action": "s3:GetObject",  | 
 | 68 | +                "Resource": "arn:aws:s3:::mybucket/*",  | 
 | 69 | +            }  | 
 | 70 | +        ],  | 
 | 71 | +    }  | 
 | 72 | +    return iam_client.create_policy(  | 
 | 73 | +        PolicyName="test-policy",  | 
 | 74 | +        Path="/",  | 
 | 75 | +        PolicyDocument=json.dumps(policy_doc),  | 
 | 76 | +        Description="Test policy",  | 
 | 77 | +    )  | 
 | 78 | + | 
 | 79 | +@pytest.fixture  | 
 | 80 | +def s3_role(moto_account_id, s3_policy):  | 
 | 81 | +    """Create a mock role and policy document for testing"""  | 
 | 82 | +    return "123456789012"  | 
 | 83 | + | 
 | 84 | + | 
12 | 85 | @mock_aws  | 
 | 86 | +@pytest.mark.skip  | 
13 | 87 | class TestSession:  | 
14 |  | -    def test_can_init(self):  | 
 | 88 | +    def test_can_init(self, s3_role, keycloak_client_id, keycloak_client_secret, moto_account_id):  | 
15 | 89 |         session = Session(  | 
16 | 90 |             aws_account_id=moto_account_id,  | 
17 |  | -            s3_role="test-role",  | 
18 |  | -            auth_client_id="test-client-id",  | 
19 |  | -            auth_client_secret="test-client-secret",  | 
 | 91 | +            s3_role=s3_role,  | 
 | 92 | +            auth_client_id=keycloak_client_id,  | 
 | 93 | +            auth_client_secret=keycloak_client_secret,  | 
20 | 94 |         )  | 
21 | 95 |         assert session.bearer_token, "Bearer token not set"  | 
 | 96 | + | 
 | 97 | +def test_get_s3_client(s3_role, keycloak_client_id, keycloak_client_secret, moto_account_id):  | 
 | 98 | +    client = get_s3_client(  | 
 | 99 | +        auth_client_id=keycloak_client_id,  | 
 | 100 | +        auth_client_secret=keycloak_client_secret,  | 
 | 101 | +        aws_account_id=moto_account_id,  | 
 | 102 | +        s3_role=s3_role,  | 
 | 103 | +        bearer_token=None,  | 
 | 104 | +    )  | 
 | 105 | +    assert client  | 
 | 106 | + | 
0 commit comments