-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathxscconfigprofile_test.go
97 lines (78 loc) · 3.45 KB
/
xscconfigprofile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package tests
import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/xsc/services"
xscutils "github.com/jfrog/jfrog-client-go/xsc/services/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
const configProfileWithoutRepo = "default-test-profile"
func TestGetConfigurationProfileByName(t *testing.T) {
initXscTest(t, services.ConfigProfileMinXscVersion, xscutils.MinXrayVersionXscTransitionToXray)
xrayVersion, err := GetXrayDetails().GetVersion()
require.NoError(t, err)
mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion)
defer mockServer.Close()
configProfile, err := configProfileService.GetConfigurationProfileByName(configProfileWithoutRepo)
assert.NoError(t, err)
profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileExample.json")
assert.NoError(t, err)
var configProfileForComparison services.ConfigProfile
err = json.Unmarshal(profileFileContent, &configProfileForComparison)
assert.NoError(t, err)
assert.Equal(t, &configProfileForComparison, configProfile)
}
func TestGetConfigurationProfileByUrl(t *testing.T) {
initXscTest(t, "", services.ConfigProfileByUrlMinXrayVersion)
xrayVersion, err := GetXrayDetails().GetVersion()
require.NoError(t, err)
mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion)
defer mockServer.Close()
configProfile, err := configProfileService.GetConfigurationProfileByUrl(mockServer.URL)
assert.NoError(t, err)
profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileExample.json")
assert.NoError(t, err)
var configProfileForComparison services.ConfigProfile
err = json.Unmarshal(profileFileContent, &configProfileForComparison)
assert.NoError(t, err)
assert.Equal(t, &configProfileForComparison, configProfile)
}
// TODO backwards compatability can be removed once old Xsc service is removed from all servers
func createXscMockServerForConfigProfile(t *testing.T, xrayVersion string) (mockServer *httptest.Server, configProfileService *services.ConfigurationProfileService) {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiUrlPart := "api/v1/"
var isXrayAfterXscMigration bool
if isXrayAfterXscMigration = xscutils.IsXscXrayInnerService(xrayVersion); isXrayAfterXscMigration {
apiUrlPart = ""
}
switch {
case (strings.Contains(r.RequestURI, "/xsc/"+apiUrlPart+"profile/"+configProfileWithoutRepo) && r.Method == http.MethodGet) ||
strings.Contains(r.RequestURI, "xray/api/v1/xsc/profile_repos") && r.Method == http.MethodPost && isXrayAfterXscMigration:
w.WriteHeader(http.StatusOK)
content, err := os.ReadFile("testdata/configprofile/configProfileExample.json")
assert.NoError(t, err)
_, err = w.Write(content)
assert.NoError(t, err)
default:
assert.Fail(t, "received an unexpected request")
}
}))
xscDetails := GetXscDetails()
xscDetails.SetUrl(mockServer.URL + "/xsc")
xscDetails.SetAccessToken("")
xrayDetails := GetXrayDetails()
xrayDetails.SetUrl(mockServer.URL + "/xray")
xrayDetails.SetAccessToken("")
client, err := jfroghttpclient.JfrogClientBuilder().Build()
assert.NoError(t, err)
configProfileService = services.NewConfigurationProfileService(client)
configProfileService.XscDetails = xscDetails
configProfileService.XrayDetails = xrayDetails
return
}