|
1 | 1 | # (C) 2025 GoodData Corporation |
2 | | -import os |
3 | | -from typing import Generator |
| 2 | + |
| 3 | +from unittest.mock import Mock |
4 | 4 |
|
5 | 5 | import boto3 |
6 | 6 | import pytest |
| 7 | +from moto import mock_aws |
7 | 8 |
|
8 | 9 | from gooddata_pipelines.api import GoodDataApi |
9 | 10 |
|
10 | 11 |
|
11 | | -@pytest.fixture(scope="session", autouse=True) |
12 | | -def aws_credentials() -> Generator[None, None, None]: |
13 | | - """ |
14 | | - Set dummy AWS credentials for the entire test session. |
15 | | - This is an autouse fixture, so it runs automatically. |
16 | | - """ |
17 | | - os.environ["AWS_ACCESS_KEY_ID"] = "testing" |
18 | | - os.environ["AWS_SECRET_ACCESS_KEY"] = "testing" |
19 | | - os.environ["AWS_SECURITY_TOKEN"] = "testing" |
20 | | - os.environ["AWS_SESSION_TOKEN"] = "testing" |
21 | | - os.environ["AWS_DEFAULT_REGION"] = "us-east-1" |
22 | | - yield |
| 12 | +@pytest.fixture(scope="function", autouse=True) |
| 13 | +def mock_aws_services(): |
| 14 | + """Mock AWS services for each test""" |
| 15 | + with mock_aws(): |
| 16 | + yield |
23 | 17 |
|
24 | 18 |
|
25 | | -@pytest.fixture |
26 | | -def mock_boto_session(mocker): |
27 | | - """ |
28 | | - Mocks boto3.Session to prevent it from using a real AWS profile. |
29 | | - It will return a default Session object, which then uses the |
30 | | - dummy credentials set by the conftest.py fixture. |
31 | | - """ |
32 | | - # We patch boto3.Session and make it return a new, default session object. |
33 | | - # This new object will not have the `profile_name` and will fall back |
34 | | - # to using the environment variables we set in conftest. |
35 | | - mocker.patch("boto3.Session", return_value=boto3.Session()) |
| 19 | +@pytest.fixture(scope="function") |
| 20 | +def mock_boto_session(): |
| 21 | + """Create a mock boto3 session for testing""" |
| 22 | + return boto3.Session( |
| 23 | + aws_access_key_id="testing", |
| 24 | + aws_secret_access_key="testing", |
| 25 | + aws_session_token="testing", |
| 26 | + region_name="us-east-1", |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="function", autouse=True) |
| 31 | +def mock_boto3_session(monkeypatch): |
| 32 | + """Mock boto3.Session to prevent real AWS authentication""" |
| 33 | + mock_client = Mock() |
| 34 | + mock_client.head_bucket.return_value = {} |
| 35 | + |
| 36 | + mock_resource = Mock() |
| 37 | + mock_resource.meta.client = mock_client |
| 38 | + mock_resource.Bucket.return_value = Mock() |
| 39 | + |
| 40 | + mock_session = Mock() |
| 41 | + mock_session.resource.return_value = mock_resource |
| 42 | + |
| 43 | + # Mock the boto3.Session constructor |
| 44 | + def mock_session_constructor(*args, **kwargs): |
| 45 | + return mock_session |
| 46 | + |
| 47 | + monkeypatch.setattr("boto3.Session", mock_session_constructor) |
| 48 | + return mock_session |
36 | 49 |
|
37 | 50 |
|
38 | 51 | @pytest.fixture |
|
0 commit comments