(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func TestAddonDownload(t *testing.T) { |
| 21 | t.Setenv("CLOUDQUERY_API_KEY", "testkey") |
| 22 | |
| 23 | tempDir := t.TempDir() |
| 24 | expectedFileAndPath := filepath.Join(tempDir, "cloudquery_visualization_test_v1.2.3.zip") |
| 25 | |
| 26 | wantCalls := map[string]int{ |
| 27 | "GET /teams": 1, |
| 28 | "GET /teams/test_team/addons/cloudquery/visualization/test/versions/v1.2.3/assets": 1, |
| 29 | "GET /assets/cloudquery/addon_visualization/test/v1.2.3/cloudquery_visualization_test_v1.2.3.zip": 1, |
| 30 | } |
| 31 | |
| 32 | payload := []byte("test payload") |
| 33 | |
| 34 | s := sha256.New() |
| 35 | s.Write(payload) |
| 36 | payloadChecksum := hex.EncodeToString(s.Sum(nil)) |
| 37 | |
| 38 | gotCalls := map[string]int{} |
| 39 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 40 | if r.Method != http.MethodGet { |
| 41 | t.Fatalf("unexpected method: %s %s", r.Method, r.URL.Path) |
| 42 | } |
| 43 | w.Header().Set("Content-Type", "application/json") |
| 44 | gotCalls[r.Method+" "+r.URL.Path]++ |
| 45 | switch r.URL.Path { |
| 46 | case "/teams/test_team/addons/cloudquery/visualization/test/versions/v1.2.3/assets": |
| 47 | checkAuthHeader(t, r) |
| 48 | w.WriteHeader(http.StatusOK) |
| 49 | b, _ := json.Marshal(map[string]string{ |
| 50 | "checksum": payloadChecksum, |
| 51 | "location": "http://" + r.Host + "/assets/cloudquery/addon_visualization/test/v1.2.3/cloudquery_visualization_test_v1.2.3.zip", |
| 52 | }) |
| 53 | _, err := w.Write(b) |
| 54 | require.NoError(t, err) |
| 55 | case "/assets/cloudquery/addon_visualization/test/v1.2.3/cloudquery_visualization_test_v1.2.3.zip": |
| 56 | w.Header().Set("Content-Type", "application/octet-stream") |
| 57 | w.WriteHeader(http.StatusOK) |
| 58 | _, err := w.Write(payload) |
| 59 | require.NoError(t, err) |
| 60 | case "/teams": |
| 61 | w.WriteHeader(http.StatusOK) |
| 62 | _, err := w.Write([]byte(`{"items":[{"name":"test_team","displayName":"Test Team"}]}`)) |
| 63 | require.NoError(t, err) |
| 64 | } |
| 65 | })) |
| 66 | defer ts.Close() |
| 67 | |
| 68 | cmd := NewCmdRoot() |
| 69 | t.Setenv(envAPIURL, ts.URL) |
| 70 | args := append([]string{"addon", "download", "cloudquery/visualization/test@v1.2.3", "-t", tempDir}, testCommandArgs(t)...) |
| 71 | cmd.SetArgs(args) |
| 72 | err := cmd.Execute() |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if diff := cmp.Diff(wantCalls, gotCalls); diff != "" { |
| 77 | t.Fatalf("mismatch (-want +got):\n%s", diff) |
nothing calls this directly
no test coverage detected