This commit is contained in:
2026-02-18 09:52:08 +01:00
parent 490fad15c6
commit 38d1975731
3 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
Flask
marshmallow
pytest
pytest-xdist
pytest-cov
python-dotenv
python-terraform
requests
selenium

View File

@@ -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)

View File

@@ -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()