Skip to content

Commit e51829d

Browse files
authored
Merge pull request #55 from snyk/feat/add-fileupload-integration-test
feat: add integration test for file upload client
2 parents b8a7882 + dc52ded commit e51829d

File tree

2 files changed

+133
-2
lines changed

2 files changed

+133
-2
lines changed

.circleci/config.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
unit-test:
99
executor:
1010
name: go/default
11-
tag: '1.23.10'
11+
tag: "1.23.10"
1212
steps:
1313
- checkout
1414
- go/load-cache
@@ -18,6 +18,20 @@ jobs:
1818
covermode: atomic
1919
failfast: true
2020
race: true
21+
22+
integration-test:
23+
executor:
24+
name: go/default
25+
tag: "1.23.10"
26+
steps:
27+
- checkout
28+
- go/load-cache
29+
- go/mod-download
30+
- go/save-cache
31+
- run:
32+
name: Run Integration Tests
33+
command: |
34+
INTEGRATION=1 go test ./... -v -run="Integration"
2135
lint:
2236
docker:
2337
- image: golangci/golangci-lint:v1.64.6-alpine
@@ -28,7 +42,7 @@ jobs:
2842
security-scans:
2943
executor:
3044
name: go/default
31-
tag: '1.23.10'
45+
tag: "1.23.10"
3246
resource_class: small
3347
steps:
3448
- checkout
@@ -58,5 +72,9 @@ workflows:
5872
- analysis_unify
5973
- unit-test:
6074
name: Unit Test
75+
- integration-test:
76+
name: Integration Test
77+
context:
78+
- cli-extension-os-flows
6179
- lint:
6280
name: Linting
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)