|
| 1 | +package commandsummary |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGenerateEvidenceUrlByType(t *testing.T) { |
| 11 | + // Set up test environment |
| 12 | + originalWorkflow := os.Getenv(workflowEnvKey) |
| 13 | + err := os.Setenv(workflowEnvKey, "JFrog CLI Core Tests") |
| 14 | + if err != nil { |
| 15 | + assert.FailNow(t, "Failed to set environment variable", err) |
| 16 | + } |
| 17 | + defer func() { |
| 18 | + if originalWorkflow != "" { |
| 19 | + err = os.Setenv(workflowEnvKey, originalWorkflow) |
| 20 | + if err != nil { |
| 21 | + assert.Fail(t, "Failed to restore workflow environment variable", err) |
| 22 | + return |
| 23 | + } |
| 24 | + } else { |
| 25 | + os.Unsetenv(workflowEnvKey) |
| 26 | + } |
| 27 | + }() |
| 28 | + |
| 29 | + // Configure static markdown config for tests |
| 30 | + StaticMarkdownConfig.setPlatformUrl("https://myplatform.com/") |
| 31 | + StaticMarkdownConfig.setPlatformMajorVersion(7) |
| 32 | + |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + data EvidenceSummaryData |
| 36 | + expectedURL string |
| 37 | + expectError bool |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "Package evidence URL", |
| 41 | + data: EvidenceSummaryData{ |
| 42 | + Subject: "repo/path/package.jar", |
| 43 | + SubjectType: SubjectTypePackage, |
| 44 | + }, |
| 45 | + expectedURL: "https://myplatform.com/ui/repos/tree/Evidence/repo/path/package.jar?clearFilter=true&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Artifact evidence URL", |
| 49 | + data: EvidenceSummaryData{ |
| 50 | + Subject: "repo/path/artifact.txt", |
| 51 | + SubjectType: SubjectTypeArtifact, |
| 52 | + }, |
| 53 | + expectedURL: "https://myplatform.com/ui/repos/tree/Evidence/repo/path/artifact.txt?clearFilter=true&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Release bundle evidence URL", |
| 57 | + data: EvidenceSummaryData{ |
| 58 | + Subject: "release-bundles-v2/my-bundle/1.0.0/release-bundle.json.evd", |
| 59 | + SubjectType: SubjectTypeReleaseBundle, |
| 60 | + ReleaseBundleName: "my-bundle", |
| 61 | + ReleaseBundleVersion: "1.0.0", |
| 62 | + RepoKey: "release-bundles-v2", |
| 63 | + }, |
| 64 | + expectedURL: "", // Will be checked with custom assertion |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Build evidence URL", |
| 68 | + data: EvidenceSummaryData{ |
| 69 | + Subject: "artifactory-build-info/my-build/123/1234567890.json", |
| 70 | + SubjectType: SubjectTypeBuild, |
| 71 | + BuildName: "my-build", |
| 72 | + BuildNumber: "123", |
| 73 | + BuildTimestamp: "1234567890", |
| 74 | + RepoKey: "artifactory-build-info", |
| 75 | + }, |
| 76 | + expectedURL: "https://myplatform.com/ui/builds/my-build/123/1234567890/Evidence/my-build?buildRepo=artifactory-build-info&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Build with special characters in name", |
| 80 | + data: EvidenceSummaryData{ |
| 81 | + Subject: "artifactory-build-info/my build with spaces/123/1234567890.json", |
| 82 | + SubjectType: SubjectTypeBuild, |
| 83 | + BuildName: "my build with spaces", |
| 84 | + BuildNumber: "123", |
| 85 | + BuildTimestamp: "1234567890", |
| 86 | + RepoKey: "artifactory-build-info", |
| 87 | + }, |
| 88 | + expectedURL: "https://myplatform.com/ui/builds/my+build+with+spaces/123/1234567890/Evidence/my+build+with+spaces?buildRepo=artifactory-build-info&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Invalid release bundle falls back to artifact URL", |
| 92 | + data: EvidenceSummaryData{ |
| 93 | + Subject: "invalid/path", |
| 94 | + SubjectType: SubjectTypeReleaseBundle, |
| 95 | + }, |
| 96 | + expectedURL: "https://myplatform.com/ui/repos/tree/Evidence/invalid/path?clearFilter=true&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Invalid build falls back to artifact URL", |
| 100 | + data: EvidenceSummaryData{ |
| 101 | + Subject: "invalid/build/path", |
| 102 | + SubjectType: SubjectTypeBuild, |
| 103 | + }, |
| 104 | + expectedURL: "https://myplatform.com/ui/repos/tree/Evidence/invalid/build/path?clearFilter=true&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "Default type uses artifact URL", |
| 108 | + data: EvidenceSummaryData{ |
| 109 | + Subject: "some/path/file.txt", |
| 110 | + SubjectType: "", // Empty type should default to artifact |
| 111 | + }, |
| 112 | + expectedURL: "https://myplatform.com/ui/repos/tree/Evidence/some/path/file.txt?clearFilter=true&gh_job_id=JFrog+CLI+Core+Tests&gh_section=evidence&m=3&s=1", |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tt := range tests { |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + url, err := GenerateEvidenceUrlByType(tt.data, evidenceSection) |
| 119 | + if tt.expectError { |
| 120 | + assert.Error(t, err) |
| 121 | + } else { |
| 122 | + assert.NoError(t, err) |
| 123 | + if tt.expectedURL != "" { |
| 124 | + assert.Equal(t, tt.expectedURL, url) |
| 125 | + } else if tt.name == "Release bundle evidence URL" { |
| 126 | + // Special handling for release bundle URL due to parameter ordering |
| 127 | + assert.Contains(t, url, "https://myplatform.com/ui/artifactory/lifecycle?") |
| 128 | + assert.Contains(t, url, "bundleName=my-bundle") |
| 129 | + assert.Contains(t, url, "repositoryKey=release-bundles-v2") |
| 130 | + assert.Contains(t, url, "releaseBundleVersion=1.0.0") |
| 131 | + assert.Contains(t, url, "activeVersionTab=Evidence+Graph") |
| 132 | + assert.Contains(t, url, "gh_job_id=JFrog+CLI+Core+Tests") |
| 133 | + assert.Contains(t, url, "gh_section=evidence") |
| 134 | + assert.Contains(t, url, "m=3") |
| 135 | + assert.Contains(t, url, "s=1") |
| 136 | + assert.Contains(t, url, "range=Any+Time") |
| 137 | + } |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments