diff --git a/app/tests/conftest.py b/app/tests/conftest.py new file mode 100644 index 00000000..b0a51877 --- /dev/null +++ b/app/tests/conftest.py @@ -0,0 +1,52 @@ +import pytest + +from fastapi.testclient import TestClient + +from app.main import app +from app.models import ( + IaCTemplateGenerationDocker, IaCTemplateGenerationEC2, IaCTemplateGenerationS3, IaCTemplateGenerationIAM, + IaCTemplateGenerationArgoCD, IaCTemplateGenerationELB, IaCTemplateGenerationEFS, SyncPolicy, ArgoApplication +) + + +@pytest.fixture +def client(): + with TestClient(app) as client: + yield client + + +@pytest.fixture +def iac_template_docker_sample_input(): + return IaCTemplateGenerationDocker().model_dump() + + +@pytest.fixture +def iac_template_ec2_sample_input(): + return IaCTemplateGenerationEC2().model_dump() + + +@pytest.fixture +def iac_template_s3_sample_input(): + return IaCTemplateGenerationS3().model_dump() + + +@pytest.fixture +def iac_template_iam_sample_input(): + return IaCTemplateGenerationIAM().model_dump() + + +@pytest.fixture +def iac_template_argocd_sample_input(): + sync_policy = SyncPolicy() + argocd_application = ArgoApplication(sync_policy=sync_policy) + return IaCTemplateGenerationArgoCD(argocd_application=argocd_application).model_dump() + + +@pytest.fixture +def iac_template_elb_sample_input(): + return IaCTemplateGenerationELB().model_dump() + + +@pytest.fixture +def iac_template_efs_sample_input(): + return IaCTemplateGenerationEFS().model_dump() diff --git a/app/tests/test_iac_template.py b/app/tests/test_iac_template.py index 0b1d1c54..7c79eaf8 100644 --- a/app/tests/test_iac_template.py +++ b/app/tests/test_iac_template.py @@ -1,43 +1,48 @@ -import pytest from unittest.mock import patch -from fastapi.testclient import TestClient -from app.main import app -from app.models import IaCTemplateGeneration - -client = TestClient(app) - -@pytest.fixture -def sample_iac_template_input(): - return IaCTemplateGeneration( - service="terraform", - CI_integration=True, - base_config="ec2" - ) - -@patch("app.main.gpt_service") -@patch("app.main.edit_directory_generator") -@patch("app.main.execute_pythonfile") -def test_template(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service, sample_iac_template_input): - """ - Test the /IaC-template/ endpoint with valid input data to ensure it returns a 200 status code. - """ - mock_gpt_service.return_value = "Generated Python Code" - - response = client.post("/IaC-template/", json=sample_iac_template_input.model_dump()) - assert response.status_code == 200 - - -@patch("app.main.gpt_service") -@patch("app.main.edit_directory_generator") -@patch("app.main.execute_pythonfile") -def test_template_invalid(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service): - """ - Test the /IaC-template/ endpoint with an invalid 'base_config' value to ensure it returns a 422 status code. - """ - invalid_input = { - "CI_integration": True, - "base_config": "k8s", # Unsupported base_config - "service": "terraform" - } - response = client.post("/IaC-template/", json=invalid_input) - assert response.status_code == 422 + + +class TestTerraformTemplates: + def setup_method(self): + self.mock_execute_pythonfile = patch('app.main.execute_pythonfile').start() + self.mock_edit_directory_generator = patch('app.main.edit_directory_generator').start() + self.mock_gpt_service = patch('app.main.gpt_service').start() + self.mock_gpt_service.return_value = 'Generated Python Code' + + self.iac_template_docker_url = '/IaC-template/docker' + self.iac_template_ec2_url = '/IaC-template/aws/ec2' + self.iac_template_s3_url = '/IaC-template/aws/s3' + self.iac_template_iam_url = '/IaC-template/aws/iam' + self.iac_template_argocd_url = '/IaC-template/argocd' + self.iac_template_elb_url = '/IaC-template/aws/elb' + self.iac_template_efs_url = '/IaC-template/aws/efs' + + def teardown_method(self): + patch.stopall() + + def test_iac_template_docker(self, client, iac_template_docker_sample_input): + response = client.post(self.iac_template_docker_url, json=iac_template_docker_sample_input) + assert response.status_code == 200 + + def test_iac_template_ec2(self, client, iac_template_ec2_sample_input): + response = client.post(self.iac_template_ec2_url, json=iac_template_ec2_sample_input) + assert response.status_code == 200 + + def test_iac_template_s3(self, client, iac_template_s3_sample_input): + response = client.post(self.iac_template_s3_url, json=iac_template_s3_sample_input) + assert response.status_code == 200 + + def test_iac_template_iam(self, client, iac_template_iam_sample_input): + response = client.post(self.iac_template_iam_url, json=iac_template_iam_sample_input) + assert response.status_code == 200 + + def test_iac_template_argocd(self, client, iac_template_argocd_sample_input): + response = client.post(self.iac_template_argocd_url, json=iac_template_argocd_sample_input) + assert response.status_code == 200 + + def test_iac_template_elb(self, client, iac_template_elb_sample_input): + response = client.post(self.iac_template_elb_url, json=iac_template_elb_sample_input) + assert response.status_code == 200 + + def test_iac_template_efs(self, client, iac_template_efs_sample_input): + response = client.post(self.iac_template_efs_url, json=iac_template_efs_sample_input) + assert response.status_code == 200