|
| 1 | +package fileupload_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/snyk/cli-extension-os-flows/internal/fileupload" |
| 14 | + "github.com/snyk/cli-extension-os-flows/internal/fileupload/uploadrevision" |
| 15 | +) |
| 16 | + |
| 17 | +func TestUploadFileIntegration(t *testing.T) { |
| 18 | + fileUploadClient := setupFileUploadClient(t) |
| 19 | + |
| 20 | + files := []uploadrevision.LoadedFile{ |
| 21 | + {Path: "src/main.go", Content: "package main"}, |
| 22 | + } |
| 23 | + |
| 24 | + dir, dirCleanup := createDirWithFiles(t, files) |
| 25 | + defer dirCleanup() |
| 26 | + |
| 27 | + fileuploadRevisionID, err := fileUploadClient.CreateRevisionFromFile(t.Context(), filepath.Join(dir.Name(), files[0].Path), fileupload.UploadOptions{}) |
| 28 | + if err != nil { |
| 29 | + t.Errorf("failed to create fileupload revision: %s", err.Error()) |
| 30 | + } |
| 31 | + assert.NotEqual(t, uuid.Nil, fileuploadRevisionID) |
| 32 | +} |
| 33 | + |
| 34 | +func TestUploadDirectoryIntegration(t *testing.T) { |
| 35 | + fileUploadClient := setupFileUploadClient(t) |
| 36 | + |
| 37 | + files := []uploadrevision.LoadedFile{ |
| 38 | + {Path: "src/main.go", Content: "package main"}, |
| 39 | + {Path: "src/utils.go", Content: "package utils"}, |
| 40 | + {Path: "README.md", Content: "# Project"}, |
| 41 | + } |
| 42 | + |
| 43 | + dir, dirCleanup := createDirWithFiles(t, files) |
| 44 | + defer dirCleanup() |
| 45 | + |
| 46 | + fileuploadRevisionID, err := fileUploadClient.CreateRevisionFromDir(t.Context(), dir.Name(), fileupload.UploadOptions{}) |
| 47 | + if err != nil { |
| 48 | + t.Errorf("failed to create fileupload revision: %s", err.Error()) |
| 49 | + } |
| 50 | + assert.NotEqual(t, uuid.Nil, fileuploadRevisionID) |
| 51 | +} |
| 52 | + |
| 53 | +func setupFileUploadClient(t *testing.T) fileupload.Client { |
| 54 | + t.Helper() |
| 55 | + |
| 56 | + if os.Getenv("INTEGRATION") == "" { |
| 57 | + t.Skip("skipping integration test; set INTEGRATION=1 to run") |
| 58 | + } |
| 59 | + |
| 60 | + envvars := extractEnvVariables(t) |
| 61 | + httpclient := &http.Client{ |
| 62 | + Transport: &CustomRoundTripper{ |
| 63 | + token: envvars.APIToken, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + return fileupload.NewClient( |
| 68 | + httpclient, |
| 69 | + fileupload.Config{ |
| 70 | + BaseURL: envvars.BaseURL, |
| 71 | + OrgID: envvars.OrgID, |
| 72 | + }, |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +type testConfig struct { |
| 77 | + BaseURL string |
| 78 | + OrgID uuid.UUID |
| 79 | + APIToken string |
| 80 | +} |
| 81 | + |
| 82 | +func readEnvVar(t *testing.T, name string) string { |
| 83 | + t.Helper() |
| 84 | + value, exists := os.LookupEnv(name) |
| 85 | + if !exists { |
| 86 | + t.Errorf("%v is not set", name) |
| 87 | + t.Fail() |
| 88 | + } |
| 89 | + return value |
| 90 | +} |
| 91 | + |
| 92 | +func extractEnvVariables(t *testing.T) testConfig { |
| 93 | + t.Helper() |
| 94 | + |
| 95 | + baseURL := readEnvVar(t, "SNYK_API_BASE_URL") |
| 96 | + snykAPIToken := readEnvVar(t, "SNYK_API_TOKEN") |
| 97 | + orgID := readEnvVar(t, "SNYK_ORG_ID") |
| 98 | + |
| 99 | + return testConfig{ |
| 100 | + BaseURL: baseURL, |
| 101 | + OrgID: uuid.MustParse(orgID), |
| 102 | + APIToken: snykAPIToken, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +type CustomRoundTripper struct { |
| 107 | + token string |
| 108 | +} |
| 109 | + |
| 110 | +func (crt *CustomRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { |
| 111 | + r.Header.Set("Authorization", fmt.Sprintf("token %s", crt.token)) |
| 112 | + return http.DefaultTransport.RoundTrip(r) |
| 113 | +} |
0 commit comments