(t *testing.T)
| 1737 | } |
| 1738 | |
| 1739 | func 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) |
nothing calls this directly
no test coverage detected