diff --git a/s3_api/requirements.txt b/s3_api/requirements.txt index e69de29..42fcc44 100644 --- a/s3_api/requirements.txt +++ b/s3_api/requirements.txt @@ -0,0 +1,9 @@ +Flask +marshmallow +pytest +pytest-xdist +pytest-cov +python-dotenv +python-terraform +requests +selenium diff --git a/s3_api/tests/integration/test_integration.py b/s3_api/tests/integration/test_integration.py index e69de29..66944a3 100644 --- a/s3_api/tests/integration/test_integration.py +++ b/s3_api/tests/integration/test_integration.py @@ -0,0 +1,33 @@ +import os +import pytest +from app import BucketSchema + +def test_env_vars(monkeypatch): + monkeypatch.setenv("TFSTATE_BUCKET", "dummy-bucket") + monkeypatch.setenv("AWS_REGION", "eu-central-1") + assert os.getenv("TFSTATE_BUCKET") == "dummy-bucket" + assert os.getenv("AWS_REGION") == "eu-central-1" + +def test_bucket_schema_valid(): + data = { + "environment": "dev", + "bucket_name": "mybucket", + "versioning": "Enabled", + "encryption": "AES256", + "api_key": "123" + } + schema = BucketSchema() + result = schema.load(data) + assert result['bucket_name'] == "mybucket" + +def test_bucket_schema_missing_field(): + data = { + "environment": "dev", + "bucket_name": "mybucket", + "versioning": "Enabled", + # "encryption" missing + "api_key": "123" + } + schema = BucketSchema() + with pytest.raises(Exception): + schema.load(data) diff --git a/s3_api/tests/unit/test_app.py b/s3_api/tests/unit/test_app.py index e69de29..e9fc8d3 100644 --- a/s3_api/tests/unit/test_app.py +++ b/s3_api/tests/unit/test_app.py @@ -0,0 +1,54 @@ +import os +import pytest +from unittest.mock import patch, MagicMock +from app import app, create_bucket, BucketSchema + +# Set a dummy API_KEY for testing +os.environ['API_KEY'] = 'test-key' + +@pytest.fixture +def client(): + app.config['TESTING'] = True + with app.test_client() as client: + yield client + +def test_ping(client): + response = client.get('/') + assert response.status_code == 200 + assert response.json == ["Pong"] + +@patch('app.Terraform') +def test_create_bucket(mock_terraform): + # Mock Terraform.apply() returning (0, 'success') + mock_tf_instance = MagicMock() + mock_tf_instance.apply.return_value = (0, 'success') + mock_terraform.return_value = mock_tf_instance + + result = create_bucket("dev", "AES256", "mybucket", "Enabled") + assert result[0] == 0 + +def test_bucket_data_success(client): + with patch('app.create_bucket') as mock_create: + mock_create.return_value = (0, 'success') + payload = { + "environment": "dev", + "bucket_name": "mybucket", + "versioning": "Enabled", + "encryption": "AES256", + "api_key": "test-key" + } + response = client.post('/create_bucket', json=payload) + assert response.status_code == 200 + assert "Creating bucket mybucket in dev" in response.data.decode() + +def test_bucket_data_invalid_key(client): + payload = { + "environment": "dev", + "bucket_name": "mybucket", + "versioning": "Enabled", + "encryption": "AES256", + "api_key": "wrong-key" + } + response = client.post('/create_bucket', json=payload) + assert response.status_code == 403 + assert "Authentication error" in response.data.decode()