replace badrequests

This commit is contained in:
duwenxin
2026-02-09 19:16:00 -05:00
parent f3ed08bec5
commit 95b15e4db7
11 changed files with 74 additions and 88 deletions

View File

@@ -402,7 +402,7 @@ func runAlloyDBListClustersTest(t *testing.T, vars map[string]string) {
{
name: "list clusters missing project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"location": "%s"}`, vars["location"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "list clusters non-existent location",
@@ -417,12 +417,12 @@ func runAlloyDBListClustersTest(t *testing.T, vars map[string]string) {
{
name: "list clusters empty project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "", "location": "%s"}`, vars["location"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "list clusters empty location",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": ""}`, vars["project"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
}
@@ -499,17 +499,17 @@ func runAlloyDBListUsersTest(t *testing.T, vars map[string]string) {
{
name: "list users missing project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"location": "%s", "cluster": "%s"}`, vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "list users missing location",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "cluster": "%s"}`, vars["project"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "list users missing cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s"}`, vars["project"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "list users non-existent project",
@@ -524,7 +524,7 @@ func runAlloyDBListUsersTest(t *testing.T, vars map[string]string) {
{
name: "list users non-existent cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "non-existent-cluster"}`, vars["project"], vars["location"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
}
@@ -636,7 +636,7 @@ func runAlloyDBListInstancesTest(t *testing.T, vars map[string]string) {
{
name: "list instances missing project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"location": "%s", "cluster": "%s"}`, vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "list instances non-existent project",
@@ -651,7 +651,7 @@ func runAlloyDBListInstancesTest(t *testing.T, vars map[string]string) {
{
name: "list instances non-existent cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "non-existent-cluster"}`, vars["project"], vars["location"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
}
@@ -725,22 +725,22 @@ func runAlloyDBGetClusterTest(t *testing.T, vars map[string]string) {
{
name: "get cluster missing project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"location": "%s", "cluster": "%s"}`, vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get cluster missing location",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "cluster": "%s"}`, vars["project"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get cluster missing cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s"}`, vars["project"], vars["location"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get cluster non-existent cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "non-existent-cluster"}`, vars["project"], vars["location"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
}
@@ -815,27 +815,27 @@ func runAlloyDBGetInstanceTest(t *testing.T, vars map[string]string) {
{
name: "get instance missing project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"location": "%s", "cluster": "%s", "instance": "%s"}`, vars["location"], vars["cluster"], vars["instance"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get instance missing location",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "cluster": "%s", "instance": "%s"}`, vars["project"], vars["cluster"], vars["instance"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get instance missing cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "instance": "%s"}`, vars["project"], vars["location"], vars["instance"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get instance missing instance",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "%s"}`, vars["project"], vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get instance non-existent instance",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "%s", "instance": "non-existent-instance"}`, vars["project"], vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
}
@@ -910,27 +910,27 @@ func runAlloyDBGetUserTest(t *testing.T, vars map[string]string) {
{
name: "get user missing project",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"location": "%s", "cluster": "%s", "user": "%s"}`, vars["location"], vars["cluster"], vars["user"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get user missing location",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "cluster": "%s", "user": "%s"}`, vars["project"], vars["cluster"], vars["user"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get user missing cluster",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "user": "%s"}`, vars["project"], vars["location"], vars["user"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get user missing user",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "%s"}`, vars["project"], vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "get non-existent user",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"project": "%s", "location": "%s", "cluster": "%s", "user": "non-existent-user"}`, vars["project"], vars["location"], vars["cluster"])),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
}

View File

@@ -169,11 +169,10 @@ func TestCloneInstanceToolEndpoints(t *testing.T) {
want: `{"name":"op2","status":"PENDING"}`,
},
{
name: "missing destination instance name",
toolName: "clone-instance",
body: `{"project": "p1", "sourceInstanceName": "source-instance"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing destination instance name",
toolName: "clone-instance",
body: `{"project": "p1", "sourceInstanceName": "source-instance"}`,
want: `{"error":"parameter \"destinationInstanceName\" is required"}`,
},
}

View File

@@ -158,11 +158,10 @@ func TestCreateBackupToolEndpoints(t *testing.T) {
want: `{"name":"op1","status":"PENDING"}`,
},
{
name: "missing instance name",
toolName: "create-backup",
body: `{"project": "p1", "escription": "invalid"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing instance name",
toolName: "create-backup",
body: `{"project": "p1", "escription": "invalid"}`,
want: `{"error":"parameter \"instance\" is required"}`,
},
}

View File

@@ -155,11 +155,10 @@ func TestCreateDatabaseToolEndpoints(t *testing.T) {
want: `{"name":"op1","status":"PENDING"}`,
},
{
name: "missing name",
toolName: "create-database",
body: `{"project": "p1", "instance": "i1"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing name",
toolName: "create-database",
body: `{"project": "p1", "instance": "i1"}`,
want: `{"error":"parameter \"name\" is required"}`,
},
}

View File

@@ -138,11 +138,10 @@ func TestListDatabasesToolEndpoints(t *testing.T) {
want: `[{"name":"db1","charset":"utf8","collation":"utf8_general_ci"},{"name":"db2","charset":"utf8mb4","collation":"utf8mb4_unicode_ci"}]`,
},
{
name: "missing instance",
toolName: "list-databases",
body: `{"project": "p1"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing instance",
toolName: "list-databases",
body: `{"project": "p1"}`,
want: `{"error":"parameter \"instance\" is required"}`,
},
}

View File

@@ -188,18 +188,16 @@ func TestRestoreBackupToolEndpoints(t *testing.T) {
want: `{"error":"error processing GCP request: source project and instance are required when restoring via backup ID"}`,
},
{
name: "missing backup identifier",
toolName: "restore-backup",
body: `{"target_project": "p1", "target_instance": "instance-project-level"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing backup identifier",
toolName: "restore-backup",
body: `{"target_project": "p1", "target_instance": "instance-project-level"}`,
want: `{"error":"source project and instance are required when restoring via backup ID"}`,
},
{
name: "missing target instance info",
toolName: "restore-backup",
body: `{"backup_id": "12345"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing target instance info",
toolName: "restore-backup",
body: `{"backup_id": "12345"}`,
want: `{"error":"parameter \"target_instance\" is required"}`,
},
}

View File

@@ -198,11 +198,10 @@ func TestCreateInstanceToolEndpoints(t *testing.T) {
want: `{"name":"op2","status":"RUNNING"}`,
},
{
name: "missing required parameter",
toolName: "create-instance-prod",
body: `{"name": "instance1"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing required parameter",
toolName: "create-instance-prod",
body: `{"name": "instance1"}`,
want: `{"error":"parameter \"project\" is required"}`,
},
}

View File

@@ -199,11 +199,10 @@ func TestCreateInstanceToolEndpoints(t *testing.T) {
want: `{"name":"op2","status":"RUNNING"}`,
},
{
name: "missing required parameter",
toolName: "create-instance-prod",
body: `{"name": "instance1"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
name: "missing required parameter",
toolName: "create-instance-prod",
body: `{"name": "instance1"}`,
want: `{"error":"parameter \"project\" is required"}`,
},
}

View File

@@ -273,28 +273,22 @@ func TestPreCheckToolEndpoints(t *testing.T) {
want: `{"preCheckResponse":[{"actionsRequired":["Fix this now."],"type":"","message":"This is a critical error.","messageType":"ERROR"}]}`,
},
{
name: "instance not found",
toolName: "precheck-tool",
body: `{"project": "p1", "instance": "instance-notfound", "targetDatabaseVersion": "POSTGRES_18"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
errorMsg: "Not authorized to access instance",
name: "instance not found",
toolName: "precheck-tool",
body: `{"project": "p1", "instance": "instance-notfound", "targetDatabaseVersion": "POSTGRES_18"}`,
want: `{"error":"Not authorized to access instance"}`,
},
{
name: "missing required parameter - project",
toolName: "precheck-tool",
body: `{"instance": "instance-ok", "targetDatabaseVersion": "POSTGRES_18"}`,
expectError: true,
errorStatus: http.StatusBadRequest,
errorMsg: "parameter \\\"project\\\" is required",
name: "missing required parameter - project",
toolName: "precheck-tool",
body: `{"instance": "instance-ok", "targetDatabaseVersion": "POSTGRES_18"}`,
want: `{"error":"parameter \"project\" is required"}`,
},
{
name: "missing required parameter - instance",
toolName: "precheck-tool",
body: `{"project": "p1", "targetDatabaseVersion": "POSTGRES_18"}`, // Missing instance
expectError: true,
errorStatus: http.StatusBadRequest,
errorMsg: "parameter \\\"instance\\\" is required",
name: "missing required parameter - instance",
toolName: "precheck-tool",
body: `{"project": "p1", "targetDatabaseVersion": "POSTGRES_18"}`, // Missing instance
want: `{"error":"parameter \"instance\" is required"}`,
},
{
name: "missing parameter - targetDatabaseVersion",

View File

@@ -529,8 +529,8 @@ func TestServerlessSparkToolEndpoints(t *testing.T) {
name: "missing op parameter",
toolName: "cancel-batch",
request: map[string]any{},
wantCode: http.StatusBadRequest,
wantMsg: "parameter \\\"operation\\\" is required",
wantCode: http.StatusOK,
wantMsg: `{"error":"parameter \"operation\" is required"}`,
},
{
name: "nonexistent op",

View File

@@ -1172,13 +1172,13 @@ func RunPostgresListTablesTest(t *testing.T, tableNameParam, tableNameAuth, user
name: "invoke list_tables with invalid output format",
api: "http://127.0.0.1:5000/api/tool/list_tables/invoke",
requestBody: bytes.NewBuffer([]byte(`{"table_names": "", "output_format": "abcd"}`)),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "invoke list_tables with malformed table_names parameter",
api: "http://127.0.0.1:5000/api/tool/list_tables/invoke",
requestBody: bytes.NewBuffer([]byte(`{"table_names": 12345, "output_format": "detailed"}`)),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "invoke list_tables with multiple table names",
@@ -3409,7 +3409,7 @@ func RunMySQLGetQueryPlanTest(t *testing.T, ctx context.Context, pool *sql.DB, d
{
name: "invoke get_query_plan with invalid query",
requestBody: bytes.NewBufferString(`{"sql_statement": "SELECT * FROM non_existent_table"}`),
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
checkResult: nil,
},
}
@@ -3543,13 +3543,13 @@ func RunMSSQLListTablesTest(t *testing.T, tableNameParam, tableNameAuth string)
name: "invoke list_tables with invalid output format",
api: "http://127.0.0.1:5000/api/tool/list_tables/invoke",
requestBody: `{"table_names": "", "output_format": "abcd"}`,
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "invoke list_tables with malformed table_names parameter",
api: "http://127.0.0.1:5000/api/tool/list_tables/invoke",
requestBody: `{"table_names": 12345, "output_format": "detailed"}`,
wantStatusCode: http.StatusBadRequest,
wantStatusCode: http.StatusOK,
},
{
name: "invoke list_tables with multiple table names",