|
| 1 | +package snykclient_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
| 12 | + "github.com/snyk/cli-extension-sbom/internal/mocks" |
| 13 | + "github.com/snyk/cli-extension-sbom/internal/snykclient" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_SBOMConvert(t *testing.T) { |
| 17 | + response := mocks.NewMockResponse( |
| 18 | + "application/json; charset=utf-8", |
| 19 | + []byte(`{"scanResults":[{"name":"Scan 1"},{"name":"Scan 2"}]}`), |
| 20 | + http.StatusOK, |
| 21 | + ) |
| 22 | + |
| 23 | + sbomContent := `{"foo":"bar"}` |
| 24 | + |
| 25 | + mockHTTPClient := mocks.NewMockSBOMService(response, func(r *http.Request) { |
| 26 | + assert.Equal(t, http.MethodPost, r.Method) |
| 27 | + assert.Equal(t, "/hidden/orgs/org1/sboms/convert?version=2025-03-06~beta", r.RequestURI) |
| 28 | + assert.Equal(t, "application/octet-stream", r.Header.Get("Content-Type")) |
| 29 | + }) |
| 30 | + |
| 31 | + client := snykclient.NewSnykClient(mockHTTPClient.Client(), mockHTTPClient.URL, "org1") |
| 32 | + depsResp, err := client.SBOMConvert( |
| 33 | + context.Background(), |
| 34 | + errFactory, |
| 35 | + bytes.NewBuffer([]byte(sbomContent)), |
| 36 | + ) |
| 37 | + |
| 38 | + assert.NoError(t, err) |
| 39 | + assert.Equal(t, 2, len(depsResp)) |
| 40 | + assert.Equal(t, "Scan 1", depsResp[0].Name) |
| 41 | + assert.Equal(t, "Scan 2", depsResp[1].Name) |
| 42 | +} |
| 43 | + |
| 44 | +func Test_SBOMConvert_InvalidJSONReturned(t *testing.T) { |
| 45 | + response := mocks.NewMockResponse( |
| 46 | + "application/json; charset=utf-8", |
| 47 | + []byte(`{"scanResults":[{"name":"Scan 1"`), |
| 48 | + http.StatusOK, |
| 49 | + ) |
| 50 | + |
| 51 | + sbomContent := `{"foo":"bar"}` |
| 52 | + |
| 53 | + mockHTTPClient := mocks.NewMockSBOMService(response) |
| 54 | + |
| 55 | + client := snykclient.NewSnykClient(mockHTTPClient.Client(), mockHTTPClient.URL, "org1") |
| 56 | + _, err := client.SBOMConvert( |
| 57 | + context.Background(), |
| 58 | + errFactory, |
| 59 | + bytes.NewBuffer([]byte(sbomContent)), |
| 60 | + ) |
| 61 | + |
| 62 | + assert.ErrorContains(t, err, "failed to unmarshal JSON response") |
| 63 | +} |
| 64 | + |
| 65 | +func Test_SBOMConvert_ServerErrors(t *testing.T) { |
| 66 | + testCases := []struct { |
| 67 | + name string |
| 68 | + statusCode int |
| 69 | + responseBody string |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "Forbidden (403) - Feature not available", |
| 73 | + statusCode: http.StatusForbidden, |
| 74 | + responseBody: `{"message":"This functionality is not available on your plan."}`, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "Bad Request (400) - Malformed Request", |
| 78 | + statusCode: http.StatusBadRequest, |
| 79 | + responseBody: "", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "Internal Server Error (500) - Server Issue", |
| 83 | + statusCode: http.StatusInternalServerError, |
| 84 | + responseBody: `{"message":"Internal server error."}`, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + sbomContent := `{"foo":"bar"}` |
| 89 | + |
| 90 | + for _, tc := range testCases { |
| 91 | + t.Run(tc.name, func(t *testing.T) { |
| 92 | + response := mocks.NewMockResponse( |
| 93 | + "application/json; charset=utf-8", |
| 94 | + []byte(tc.responseBody), |
| 95 | + tc.statusCode, |
| 96 | + ) |
| 97 | + |
| 98 | + mockHTTPClient := mocks.NewMockSBOMService(response) |
| 99 | + |
| 100 | + client := snykclient.NewSnykClient(mockHTTPClient.Client(), mockHTTPClient.URL, "org1") |
| 101 | + _, err := client.SBOMConvert(context.Background(), errFactory, bytes.NewBufferString(sbomContent)) |
| 102 | + |
| 103 | + assert.ErrorContainsf( |
| 104 | + t, |
| 105 | + err, |
| 106 | + fmt.Sprintf("%d", tc.statusCode), |
| 107 | + "Expected error to contain status code %d", |
| 108 | + tc.statusCode, |
| 109 | + ) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments