(t *testing.T)
| 962 | } |
| 963 | |
| 964 | func TestUploadTaskPluginSourceSha256(t *testing.T) { |
| 965 | setupTaskPluginControllerTest(t) |
| 966 | tests := []struct { |
| 967 | name string |
| 968 | key string |
| 969 | withHash bool |
| 970 | hash string |
| 971 | wantSuccess bool |
| 972 | wantError string |
| 973 | }{ |
| 974 | { |
| 975 | name: "matching hash succeeds", |
| 976 | key: "sha256-match", |
| 977 | withHash: true, |
| 978 | wantSuccess: true, |
| 979 | }, |
| 980 | { |
| 981 | name: "mismatching hash rejected", |
| 982 | key: "sha256-mismatch", |
| 983 | withHash: true, |
| 984 | hash: "deadbeef", |
| 985 | wantError: "plugin source sha256 mismatch", |
| 986 | }, |
| 987 | { |
| 988 | name: "absent field unchanged", |
| 989 | key: "sha256-absent", |
| 990 | wantSuccess: true, |
| 991 | }, |
| 992 | } |
| 993 | for _, testCase := range tests { |
| 994 | t.Run(testCase.name, func(t *testing.T) { |
| 995 | cleanupTaskPluginControllerRuntime(t, testCase.key) |
| 996 | source := taskPluginControllerTestSource(testCase.key, "1.0.0") |
| 997 | payload := map[string]any{"source": source} |
| 998 | if testCase.withHash { |
| 999 | hash := testCase.hash |
| 1000 | if hash == "" { |
| 1001 | hash = " " + strings.ToUpper(fmt.Sprintf("%x", sha256.Sum256([]byte(source)))) + " " |
| 1002 | } |
| 1003 | payload["sourceSha256"] = hash |
| 1004 | } |
| 1005 | body, err := common.Marshal(payload) |
| 1006 | require.NoError(t, err) |
| 1007 | recorder := httptest.NewRecorder() |
| 1008 | context, _ := gin.CreateTestContext(recorder) |
| 1009 | context.Request = httptest.NewRequest(http.MethodPost, "/api/plugin/task", strings.NewReader(string(body))) |
| 1010 | context.Request.Header.Set("Content-Type", "application/json") |
| 1011 | |
| 1012 | UploadTaskPlugin(context) |
| 1013 | |
| 1014 | if testCase.wantSuccess { |
| 1015 | assert.Contains(t, recorder.Body.String(), `"success":true`) |
| 1016 | _, err = model.GetTaskPluginVersion(testCase.key, "") |
| 1017 | require.NoError(t, err) |
| 1018 | return |
| 1019 | } |
| 1020 | assert.Contains(t, recorder.Body.String(), `"success":false`) |
| 1021 | assert.Contains(t, recorder.Body.String(), testCase.wantError) |
nothing calls this directly
no test coverage detected