|
1 | | -import pytest |
2 | 1 | from unittest.mock import patch |
3 | | -from fastapi.testclient import TestClient |
4 | | -from app.main import app |
5 | | -from app.models import IaCTemplateGeneration |
6 | | - |
7 | | -client = TestClient(app) |
8 | | - |
9 | | -@pytest.fixture |
10 | | -def sample_iac_template_input(): |
11 | | - return IaCTemplateGeneration( |
12 | | - service="terraform", |
13 | | - CI_integration=True, |
14 | | - base_config="ec2" |
15 | | - ) |
16 | | - |
17 | | -@patch("app.main.gpt_service") |
18 | | -@patch("app.main.edit_directory_generator") |
19 | | -@patch("app.main.execute_pythonfile") |
20 | | -def test_template(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service, sample_iac_template_input): |
21 | | - """ |
22 | | - Test the /IaC-template/ endpoint with valid input data to ensure it returns a 200 status code. |
23 | | - """ |
24 | | - mock_gpt_service.return_value = "Generated Python Code" |
25 | | - |
26 | | - response = client.post("/IaC-template/", json=sample_iac_template_input.model_dump()) |
27 | | - assert response.status_code == 200 |
28 | | - |
29 | | - |
30 | | -@patch("app.main.gpt_service") |
31 | | -@patch("app.main.edit_directory_generator") |
32 | | -@patch("app.main.execute_pythonfile") |
33 | | -def test_template_invalid(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service): |
34 | | - """ |
35 | | - Test the /IaC-template/ endpoint with an invalid 'base_config' value to ensure it returns a 422 status code. |
36 | | - """ |
37 | | - invalid_input = { |
38 | | - "CI_integration": True, |
39 | | - "base_config": "k8s", # Unsupported base_config |
40 | | - "service": "terraform" |
41 | | - } |
42 | | - response = client.post("/IaC-template/", json=invalid_input) |
43 | | - assert response.status_code == 422 |
| 2 | + |
| 3 | + |
| 4 | +class TestTerraformTemplates: |
| 5 | + def setup_method(self): |
| 6 | + self.mock_execute_pythonfile = patch('app.main.execute_pythonfile').start() |
| 7 | + self.mock_edit_directory_generator = patch('app.main.edit_directory_generator').start() |
| 8 | + self.mock_gpt_service = patch('app.main.gpt_service').start() |
| 9 | + self.mock_gpt_service.return_value = 'Generated Python Code' |
| 10 | + |
| 11 | + self.iac_template_docker_url = '/IaC-template/docker' |
| 12 | + self.iac_template_ec2_url = '/IaC-template/aws/ec2' |
| 13 | + self.iac_template_s3_url = '/IaC-template/aws/s3' |
| 14 | + self.iac_template_iam_url = '/IaC-template/aws/iam' |
| 15 | + self.iac_template_argocd_url = '/IaC-template/argocd' |
| 16 | + self.iac_template_elb_url = '/IaC-template/aws/elb' |
| 17 | + self.iac_template_efs_url = '/IaC-template/aws/efs' |
| 18 | + |
| 19 | + def teardown_method(self): |
| 20 | + patch.stopall() |
| 21 | + |
| 22 | + def test_iac_template_docker(self, client, iac_template_docker_sample_input): |
| 23 | + response = client.post(self.iac_template_docker_url, json=iac_template_docker_sample_input) |
| 24 | + assert response.status_code == 200 |
| 25 | + |
| 26 | + def test_iac_template_ec2(self, client, iac_template_ec2_sample_input): |
| 27 | + response = client.post(self.iac_template_ec2_url, json=iac_template_ec2_sample_input) |
| 28 | + assert response.status_code == 200 |
| 29 | + |
| 30 | + def test_iac_template_s3(self, client, iac_template_s3_sample_input): |
| 31 | + response = client.post(self.iac_template_s3_url, json=iac_template_s3_sample_input) |
| 32 | + assert response.status_code == 200 |
| 33 | + |
| 34 | + def test_iac_template_iam(self, client, iac_template_iam_sample_input): |
| 35 | + response = client.post(self.iac_template_iam_url, json=iac_template_iam_sample_input) |
| 36 | + assert response.status_code == 200 |
| 37 | + |
| 38 | + def test_iac_template_argocd(self, client, iac_template_argocd_sample_input): |
| 39 | + response = client.post(self.iac_template_argocd_url, json=iac_template_argocd_sample_input) |
| 40 | + assert response.status_code == 200 |
| 41 | + |
| 42 | + def test_iac_template_elb(self, client, iac_template_elb_sample_input): |
| 43 | + response = client.post(self.iac_template_elb_url, json=iac_template_elb_sample_input) |
| 44 | + assert response.status_code == 200 |
| 45 | + |
| 46 | + def test_iac_template_efs(self, client, iac_template_efs_sample_input): |
| 47 | + response = client.post(self.iac_template_efs_url, json=iac_template_efs_sample_input) |
| 48 | + assert response.status_code == 200 |
0 commit comments