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

Function TestUploadTaskPluginSourceSha256

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

Source from the content-addressed store, hash-verified

1737}
1738
1739func TestUploadTaskPluginSourceSha256(t *testing.T) {
1740 setupTaskPluginControllerTest(t)
1741 tests := []struct {
1742 name string
1743 key string
1744 withHash bool
1745 hash string
1746 wantSuccess bool
1747 wantError string
1748 }{
1749 {
1750 name: "matching hash succeeds",
1751 key: "sha256-match",
1752 withHash: true,
1753 wantSuccess: true,
1754 },
1755 {
1756 name: "mismatching hash rejected",
1757 key: "sha256-mismatch",
1758 withHash: true,
1759 hash: "deadbeef",
1760 wantError: "plugin source sha256 mismatch",
1761 },
1762 {
1763 name: "absent field unchanged",
1764 key: "sha256-absent",
1765 wantSuccess: true,
1766 },
1767 }
1768 for _, testCase := range tests {
1769 t.Run(testCase.name, func(t *testing.T) {
1770 cleanupTaskPluginControllerRuntime(t, testCase.key)
1771 source := taskPluginControllerTestSource(testCase.key, "1.0.0")
1772 payload := map[string]any{"source": source}
1773 if testCase.withHash {
1774 hash := testCase.hash
1775 if hash == "" {
1776 hash = " " + strings.ToUpper(fmt.Sprintf("%x", sha256.Sum256([]byte(source)))) + " "
1777 }
1778 payload["sourceSha256"] = hash
1779 }
1780 body, err := common.Marshal(payload)
1781 require.NoError(t, err)
1782 recorder := httptest.NewRecorder()
1783 context, _ := gin.CreateTestContext(recorder)
1784 context.Request = httptest.NewRequest(http.MethodPost, "/api/plugin/task", strings.NewReader(string(body)))
1785 context.Request.Header.Set("Content-Type", "application/json")
1786
1787 UploadTaskPlugin(context)
1788
1789 if testCase.wantSuccess {
1790 assert.Contains(t, recorder.Body.String(), `"success":true`)
1791 _, err = model.GetTaskPluginVersion(testCase.key, "")
1792 require.NoError(t, err)
1793 return
1794 }
1795 assert.Contains(t, recorder.Body.String(), `"success":false`)
1796 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