MCPcopy Create free account
hub / github.com/QuantumNous/new-api / TestUploadTaskPluginSourceSha256

Function TestUploadTaskPluginSourceSha256

controller/task_plugin_test.go:980–1043  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

978}
979
980func TestUploadTaskPluginSourceSha256(t *testing.T) {
981 setupTaskPluginControllerTest(t)
982 tests := []struct {
983 name string
984 key string
985 withHash bool
986 hash string
987 wantSuccess bool
988 wantError string
989 }{
990 {
991 name: "matching hash succeeds",
992 key: "sha256-match",
993 withHash: true,
994 wantSuccess: true,
995 },
996 {
997 name: "mismatching hash rejected",
998 key: "sha256-mismatch",
999 withHash: true,
1000 hash: "deadbeef",
1001 wantError: "plugin source sha256 mismatch",
1002 },
1003 {
1004 name: "absent field unchanged",
1005 key: "sha256-absent",
1006 wantSuccess: true,
1007 },
1008 }
1009 for _, testCase := range tests {
1010 t.Run(testCase.name, func(t *testing.T) {
1011 cleanupTaskPluginControllerRuntime(t, testCase.key)
1012 source := taskPluginControllerTestSource(testCase.key, "1.0.0")
1013 payload := map[string]any{"source": source}
1014 if testCase.withHash {
1015 hash := testCase.hash
1016 if hash == "" {
1017 hash = " " + strings.ToUpper(fmt.Sprintf("%x", sha256.Sum256([]byte(source)))) + " "
1018 }
1019 payload["sourceSha256"] = hash
1020 }
1021 body, err := common.Marshal(payload)
1022 require.NoError(t, err)
1023 recorder := httptest.NewRecorder()
1024 context, _ := gin.CreateTestContext(recorder)
1025 context.Request = httptest.NewRequest(http.MethodPost, "/api/plugin/task", strings.NewReader(string(body)))
1026 context.Request.Header.Set("Content-Type", "application/json")
1027
1028 UploadTaskPlugin(context)
1029
1030 if testCase.wantSuccess {
1031 assert.Contains(t, recorder.Body.String(), `"success":true`)
1032 _, err = model.GetTaskPluginVersion(testCase.key, "")
1033 require.NoError(t, err)
1034 return
1035 }
1036 assert.Contains(t, recorder.Body.String(), `"success":false`)
1037 assert.Contains(t, recorder.Body.String(), testCase.wantError)

Callers

nothing calls this directly

Calls 12

MarshalFunction · 0.92
GetTaskPluginVersionFunction · 0.92
UploadTaskPluginFunction · 0.85
SetMethod · 0.80
ContainsMethod · 0.80
CountMethod · 0.80
RunMethod · 0.65
NewReaderMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected