|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests/xray" |
| 10 | + "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" |
| 11 | + "github.com/jfrog/jfrog-client-go/xray/services" |
| 12 | +) |
| 13 | + |
| 14 | +func TestArtifactStatus(t *testing.T) { |
| 15 | + initXrayTest(t) |
| 16 | + xrayServerPort := xray.StartXrayMockServer(t) |
| 17 | + xrayDetails := GetXrayDetails() |
| 18 | + client, err := jfroghttpclient.JfrogClientBuilder(). |
| 19 | + SetClientCertPath(xrayDetails.GetClientCertPath()). |
| 20 | + SetClientCertKeyPath(xrayDetails.GetClientCertKeyPath()). |
| 21 | + AppendPreRequestInterceptor(xrayDetails.RunPreRequestFunctions). |
| 22 | + Build() |
| 23 | + if err != nil { |
| 24 | + t.Error(err) |
| 25 | + } |
| 26 | + testsArtifactService := services.NewArtifactService(client) |
| 27 | + testsArtifactService.XrayDetails = xrayDetails |
| 28 | + testsArtifactService.XrayDetails.SetUrl("http://localhost:" + strconv.Itoa(xrayServerPort) + "/") |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + repo string |
| 33 | + path string |
| 34 | + expectedStatus services.ArtifactStatus |
| 35 | + expectedTime string |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "completed-scan", |
| 39 | + repo: "test-repo", |
| 40 | + path: "path/to/artifact", |
| 41 | + expectedStatus: services.ArtifactStatusDone, |
| 42 | + expectedTime: "2023-12-01T10:00:00Z", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "pending-scan", |
| 46 | + repo: "test-repo", |
| 47 | + path: "path/to/pending-artifact", |
| 48 | + expectedStatus: services.ArtifactStatusPending, |
| 49 | + expectedTime: "2023-12-01T09:30:00Z", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "unsupported-artifact", |
| 53 | + repo: "test-repo", |
| 54 | + path: "path/to/unsupported-artifact", |
| 55 | + expectedStatus: services.ArtifactStatusNotSupported, |
| 56 | + expectedTime: "2023-12-01T11:00:00Z", |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, test := range tests { |
| 61 | + t.Run(test.name, func(t *testing.T) { |
| 62 | + response, err := testsArtifactService.GetStatus(test.repo, test.path) |
| 63 | + assert.NoError(t, err) |
| 64 | + assert.NotNil(t, response) |
| 65 | + |
| 66 | + // Verify the overall status and timestamp |
| 67 | + assert.Equal(t, test.expectedStatus, response.Overall.Status) |
| 68 | + assert.Equal(t, test.expectedTime, response.Overall.Timestamp) |
| 69 | + |
| 70 | + // Verify that all details have timestamps |
| 71 | + assert.NotEmpty(t, response.Details.Sca.Timestamp) |
| 72 | + assert.NotEmpty(t, response.Details.ContextualAnalysis.Timestamp) |
| 73 | + assert.NotEmpty(t, response.Details.Exposures.Timestamp) |
| 74 | + assert.NotEmpty(t, response.Details.Violations.Timestamp) |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + // Test specific scenario details for the completed scan |
| 79 | + t.Run("completed-scan-details", func(t *testing.T) { |
| 80 | + response, err := testsArtifactService.GetStatus("test-repo", "path/to/artifact") |
| 81 | + assert.NoError(t, err) |
| 82 | + |
| 83 | + // Verify specific statuses for the completed scan |
| 84 | + assert.Equal(t, services.ArtifactStatusDone, response.Details.Sca.Status) |
| 85 | + assert.Equal(t, services.ArtifactStatusDone, response.Details.ContextualAnalysis.Status) |
| 86 | + assert.Equal(t, services.ArtifactStatusNotSupported, response.Details.Exposures.Status) |
| 87 | + assert.Equal(t, services.ArtifactStatusFailed, response.Details.Violations.Status) |
| 88 | + }) |
| 89 | + |
| 90 | + // Test specific scenario details for the pending scan |
| 91 | + t.Run("pending-scan-details", func(t *testing.T) { |
| 92 | + response, err := testsArtifactService.GetStatus("test-repo", "path/to/pending-artifact") |
| 93 | + assert.NoError(t, err) |
| 94 | + |
| 95 | + // Verify specific statuses for the pending scan |
| 96 | + assert.Equal(t, services.ArtifactStatusPending, response.Details.Sca.Status) |
| 97 | + assert.Equal(t, services.ArtifactStatusNotScanned, response.Details.ContextualAnalysis.Status) |
| 98 | + assert.Equal(t, services.ArtifactStatusNotSupported, response.Details.Exposures.Status) |
| 99 | + assert.Equal(t, services.ArtifactStatusNotScanned, response.Details.Violations.Status) |
| 100 | + }) |
| 101 | +} |
0 commit comments