386 lines
10 KiB
Go
386 lines
10 KiB
Go
package utils
|
|
|
|
import (
|
|
"beacon/pkg/pb"
|
|
"testing"
|
|
)
|
|
|
|
func TestParameterProcessor_ProcessTemplate(t *testing.T) {
|
|
// 创建测试用的全局变量
|
|
globalVariables := map[string]interface{}{
|
|
"base_url": "https://api.example.com",
|
|
"environment": "staging",
|
|
"api_version": "v1",
|
|
"timeout": "30",
|
|
"retry_count": "3",
|
|
}
|
|
|
|
// 创建参数处理器
|
|
processor := NewParameterProcessor(globalVariables)
|
|
|
|
// 模拟API测试结果
|
|
apiResult := &pb.ApiTestResult{
|
|
BaseResult: &pb.BaseTestResult{
|
|
Success: true,
|
|
Message: "API Test Passed",
|
|
},
|
|
ActualStatusCode: 200,
|
|
ResponseBody: `{"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "user_id": 456, "name": "John Doe"}`,
|
|
Headers: map[string]string{
|
|
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
|
|
// 添加activity结果
|
|
processor.AddActivityResult(123, &ActivityResult{
|
|
StepID: 123,
|
|
StepName: "RunApiTest",
|
|
Success: true,
|
|
Data: apiResult,
|
|
})
|
|
|
|
// 测试用例
|
|
tests := []struct {
|
|
name string
|
|
template string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "全局变量替换",
|
|
template: `{"endpoint": "${global.base_url}/${global.api_version}/users"}`,
|
|
expected: `{"endpoint": "https://api.example.com/v1/users"}`,
|
|
},
|
|
{
|
|
name: "API响应体替换",
|
|
template: `{"token": "${step.123.response_body}"}`,
|
|
expected: `{"token": "{\"token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...\", \"user_id\": 456, \"name\": \"John Doe\"}"}`,
|
|
},
|
|
{
|
|
name: "API状态码替换",
|
|
template: `{"status": "${step.123.actual_status_code}"}`,
|
|
expected: `{"status": "200"}`,
|
|
},
|
|
{
|
|
name: "API响应头替换",
|
|
template: `{"auth": "${step.123.headers.Authorization}"}`,
|
|
expected: `{"auth": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."}`,
|
|
},
|
|
{
|
|
name: "JSON字段提取",
|
|
template: `{"user_id": "${step.123.json.user_id}", "name": "${step.123.json.name}"}`,
|
|
expected: `{"user_id": "456", "name": "John Doe"}`,
|
|
},
|
|
{
|
|
name: "混合变量替换",
|
|
template: `{"endpoint": "${global.base_url}/${global.api_version}/users/${step.123.json.user_id}", "headers": {"Authorization": "${step.123.json.token}"}}`,
|
|
expected: `{"endpoint": "https://api.example.com/v1/users/456", "headers": {"Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."}}`,
|
|
},
|
|
{
|
|
name: "条件变量替换",
|
|
template: `{"env": "${if.step.123.success:production:staging}"}`,
|
|
expected: `{"env": "production"}`,
|
|
},
|
|
{
|
|
name: "函数调用",
|
|
template: `{"upper_name": "${upper:${step.123.json.name}}"}`,
|
|
expected: `{"upper_name": "JOHN DOE"}`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := processor.ProcessTemplate(tt.template)
|
|
if err != nil {
|
|
t.Errorf("ProcessTemplate() error = %v", err)
|
|
return
|
|
}
|
|
if result != tt.expected {
|
|
t.Errorf("ProcessTemplate() = %v, want %v", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParameterProcessor_ValidateTemplate(t *testing.T) {
|
|
// 创建测试用的全局变量
|
|
globalVariables := map[string]interface{}{
|
|
"base_url": "https://api.example.com",
|
|
"environment": "staging",
|
|
}
|
|
|
|
// 创建参数处理器
|
|
processor := NewParameterProcessor(globalVariables)
|
|
|
|
// 添加一个activity结果
|
|
apiResult := &pb.ApiTestResult{
|
|
BaseResult: &pb.BaseTestResult{
|
|
Success: true,
|
|
Message: "API Test Passed",
|
|
},
|
|
ActualStatusCode: 200,
|
|
ResponseBody: `{"token": "test-token"}`,
|
|
Headers: map[string]string{
|
|
"Authorization": "Bearer test-token",
|
|
},
|
|
}
|
|
|
|
processor.AddActivityResult(123, &ActivityResult{
|
|
StepID: 123,
|
|
StepName: "RunApiTest",
|
|
Success: true,
|
|
Data: apiResult,
|
|
})
|
|
|
|
// 测试用例
|
|
tests := []struct {
|
|
name string
|
|
template string
|
|
expectValid bool
|
|
expectErrors int
|
|
}{
|
|
{
|
|
name: "有效模板",
|
|
template: `{"endpoint": "${global.base_url}/users", "token": "${step.123.json.token}"}`,
|
|
expectValid: true,
|
|
expectErrors: 0,
|
|
},
|
|
{
|
|
name: "无效的全局变量",
|
|
template: `{"endpoint": "${global.invalid_key}/users"}`,
|
|
expectValid: false,
|
|
expectErrors: 1,
|
|
},
|
|
{
|
|
name: "无效的步骤ID",
|
|
template: `{"token": "${step.999.json.token}"}`,
|
|
expectValid: false,
|
|
expectErrors: 1,
|
|
},
|
|
{
|
|
name: "无效的字段",
|
|
template: `{"invalid": "${step.123.invalid_field}"}`,
|
|
expectValid: false,
|
|
expectErrors: 1,
|
|
},
|
|
{
|
|
name: "多个错误",
|
|
template: `{"endpoint": "${global.invalid_key}/users", "token": "${step.999.json.token}"}`,
|
|
expectValid: false,
|
|
expectErrors: 2,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
isValid, errors := processor.ValidateTemplate(tt.template)
|
|
if isValid != tt.expectValid {
|
|
t.Errorf("ValidateTemplate() isValid = %v, want %v", isValid, tt.expectValid)
|
|
}
|
|
if len(errors) != tt.expectErrors {
|
|
t.Errorf("ValidateTemplate() errors count = %d, want %d", len(errors), tt.expectErrors)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParameterProcessor_GetAvailableVariables(t *testing.T) {
|
|
// 创建测试用的全局变量
|
|
globalVariables := map[string]interface{}{
|
|
"base_url": "https://api.example.com",
|
|
"environment": "staging",
|
|
"api_version": "v1",
|
|
}
|
|
|
|
// 创建参数处理器
|
|
processor := NewParameterProcessor(globalVariables)
|
|
|
|
// 添加API测试结果
|
|
apiResult := &pb.ApiTestResult{
|
|
BaseResult: &pb.BaseTestResult{
|
|
Success: true,
|
|
Message: "API Test Passed",
|
|
},
|
|
ActualStatusCode: 200,
|
|
ResponseBody: `{"token": "test-token"}`,
|
|
Headers: map[string]string{
|
|
"Authorization": "Bearer test-token",
|
|
},
|
|
}
|
|
|
|
processor.AddActivityResult(123, &ActivityResult{
|
|
StepID: 123,
|
|
StepName: "RunApiTest",
|
|
Success: true,
|
|
Data: apiResult,
|
|
})
|
|
|
|
// 添加UI测试结果
|
|
uiResult := &pb.UiTestResult{
|
|
BaseResult: &pb.BaseTestResult{
|
|
Success: true,
|
|
Message: "UI Test Passed",
|
|
},
|
|
ScreenshotUrl: "https://s3.amazonaws.com/screenshots/test.png",
|
|
HtmlReportUrl: "https://s3.amazonaws.com/reports/test.html",
|
|
}
|
|
|
|
processor.AddActivityResult(456, &ActivityResult{
|
|
StepID: 456,
|
|
StepName: "RunUiTest",
|
|
Success: true,
|
|
Data: uiResult,
|
|
})
|
|
|
|
// 获取可用变量
|
|
variables := processor.GetAvailableVariables()
|
|
|
|
// 验证全局变量
|
|
if globalVars, exists := variables["global"]; !exists {
|
|
t.Error("Global variables not found")
|
|
} else {
|
|
expectedGlobalVars := []string{"base_url", "environment", "api_version"}
|
|
for _, expected := range expectedGlobalVars {
|
|
found := false
|
|
for _, actual := range globalVars {
|
|
if actual == expected {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Global variable '%s' not found", expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 验证步骤变量
|
|
if step123Vars, exists := variables["step.123"]; !exists {
|
|
t.Error("Step 123 variables not found")
|
|
} else {
|
|
expectedStepVars := []string{"response_body", "actual_status_code", "success", "message", "headers.*", "json.*"}
|
|
for _, expected := range expectedStepVars {
|
|
found := false
|
|
for _, actual := range step123Vars {
|
|
if actual == expected {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Step 123 variable '%s' not found", expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
if step456Vars, exists := variables["step.456"]; !exists {
|
|
t.Error("Step 456 variables not found")
|
|
} else {
|
|
expectedStepVars := []string{"screenshot_url", "html_report_url", "success", "message"}
|
|
for _, expected := range expectedStepVars {
|
|
found := false
|
|
for _, actual := range step456Vars {
|
|
if actual == expected {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Step 456 variable '%s' not found", expected)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParameterProcessor_ComplexScenarios(t *testing.T) {
|
|
// 创建测试用的全局变量
|
|
globalVariables := map[string]interface{}{
|
|
"base_url": "https://api.example.com",
|
|
"environment": "production",
|
|
"api_version": "v2",
|
|
"timeout": "60",
|
|
}
|
|
|
|
// 创建参数处理器
|
|
processor := NewParameterProcessor(globalVariables)
|
|
|
|
// 模拟用户注册结果
|
|
registerResult := &pb.ApiTestResult{
|
|
BaseResult: &pb.BaseTestResult{
|
|
Success: true,
|
|
Message: "User registered successfully",
|
|
},
|
|
ActualStatusCode: 201,
|
|
ResponseBody: `{"user_id": 789, "username": "newuser", "email": "newuser@example.com"}`,
|
|
Headers: map[string]string{
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
|
|
// 模拟用户登录结果
|
|
loginResult := &pb.ApiTestResult{
|
|
BaseResult: &pb.BaseTestResult{
|
|
Success: true,
|
|
Message: "User logged in successfully",
|
|
},
|
|
ActualStatusCode: 200,
|
|
ResponseBody: `{"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "refresh_token": "refresh123", "expires_in": 3600}`,
|
|
Headers: map[string]string{
|
|
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
|
|
// 添加activity结果
|
|
processor.AddActivityResult(201, &ActivityResult{
|
|
StepID: 201,
|
|
StepName: "RunApiTest",
|
|
Success: true,
|
|
Data: registerResult,
|
|
})
|
|
|
|
processor.AddActivityResult(202, &ActivityResult{
|
|
StepID: 202,
|
|
StepName: "RunApiTest",
|
|
Success: true,
|
|
Data: loginResult,
|
|
})
|
|
|
|
// 测试复杂的参数传递场景
|
|
complexTemplate := `{
|
|
"test_case_id": "get_user_profile",
|
|
"endpoint": "${global.base_url}/${global.api_version}/users/${step.201.json.user_id}/profile",
|
|
"http_method": "GET",
|
|
"headers": {
|
|
"Authorization": "Bearer ${step.202.json.token}",
|
|
"X-Environment": "${global.environment}",
|
|
"X-Timeout": "${global.timeout}",
|
|
"Content-Type": "application/json"
|
|
},
|
|
"expected_status_code": 200
|
|
}`
|
|
|
|
expected := `{
|
|
"test_case_id": "get_user_profile",
|
|
"endpoint": "https://api.example.com/v2/users/789/profile",
|
|
"http_method": "GET",
|
|
"headers": {
|
|
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
|
"X-Environment": "production",
|
|
"X-Timeout": "60",
|
|
"Content-Type": "application/json"
|
|
},
|
|
"expected_status_code": 200
|
|
}`
|
|
|
|
result, err := processor.ProcessTemplate(complexTemplate)
|
|
if err != nil {
|
|
t.Errorf("ProcessTemplate() error = %v", err)
|
|
return
|
|
}
|
|
|
|
if result != expected {
|
|
t.Errorf("ProcessTemplate() = %v, want %v", result, expected)
|
|
}
|
|
}
|