55from app .models import IaCInstallationInput
66
77client = TestClient (app )
8- mocked_gpt_response = "Mocked shell script for installing terraform on Ubuntu."
98
109@pytest .fixture
1110def valid_installation_data ():
@@ -15,37 +14,29 @@ def valid_installation_data():
1514 min_tokens = 100 ,
1615 max_tokens = 500
1716 )
18- @patch ('app.main.gpt_service' , return_value = mocked_gpt_response )
17+
18+ @patch ('app.main.gpt_service' )
1919def test_install (mock_gpt_service , valid_installation_data ):
2020 """
21- Test the /IaC-install/ endpoint with valid input data to ensure correct output .
21+ Test the /IaC-install/ endpoint with valid input data to ensure it returns a 200 status code .
2222 """
23+ mock_gpt_service .return_value = "Mocked shell script for installing terraform on Ubuntu."
24+
2325 response = client .post ("/IaC-install/" , json = valid_installation_data .model_dump ())
2426 assert response .status_code == 200
25- assert response . json () == { "output" : mocked_gpt_response }
27+
2628
2729@patch ('app.main.gpt_service' )
2830def test_install_invalid (mock_gpt_service ):
2931 """
30- Test the /IaC-install/ endpoint with an invalid 'os' value to ensure proper validation .
32+ Test the /IaC-install/ endpoint with an invalid 'os' value to ensure it returns a 422 status code .
3133 """
3234 invalid_input = {
33- "os" : "Kali" ,
34- "service" : "terraform"
35+ "os" : "Kali" , # Unsupported OS
36+ "service" : "terraform" ,
37+ "min_tokens" : 100 ,
38+ "max_tokens" : 500
3539 }
3640
3741 response = client .post ("/IaC-install/" , json = invalid_input )
38-
39- assert response .status_code == 422 , f"Expected status code 422, got { response .status_code } "
40- assert "detail" in response .json (), "Response JSON does not contain 'detail'"
41- errors = response .json ()["detail" ]
42-
43- expected_error_loc = ["body" , "os" ]
44- expected_error_msg = "OS must be one of ['ubuntu', 'centos', 'debian']."
45-
46- assert any (
47- error ["loc" ] == expected_error_loc and error_msg in error ["msg" ]
48- for error in errors
49- for error_msg in [expected_error_msg ]
50- ), f"Expected error message '{ expected_error_msg } ' at location { expected_error_loc } , but not found."
51- mock_gpt_service .assert_not_called ()
42+ assert response .status_code == 422
0 commit comments