github.com/jfrog/jfrog-client-go@v1.40.2/tests/artifactorysystem_test.go (about) 1 package tests 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/jfrog/jfrog-client-go/artifactory/auth" 9 "github.com/jfrog/jfrog-client-go/artifactory/services" 10 "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestSystem(t *testing.T) { 15 initArtifactoryTest(t) 16 t.Run("getVersion", testGetVersion) 17 t.Run("getServiceId", testGetServiceId) 18 t.Run("getRunningNodes", testGetRunningNodes) 19 t.Run("getConfigDescriptor", testGetConfigDescriptor) 20 t.Run("activateKeyEncryption", testActivateKeyEncryption) 21 t.Run("deactivateKeyEncryption", testDeactivateKeyEncryption) 22 t.Run("deactivateKeyEncryptionNotEncrypted", testDeactivateKeyEncryptionNotEncrypted) 23 } 24 25 func testGetVersion(t *testing.T) { 26 version, err := testsSystemService.GetVersion() 27 assert.NoError(t, err) 28 assert.NotEmpty(t, version) 29 } 30 31 func testGetServiceId(t *testing.T) { 32 serviceId, err := testsSystemService.GetServiceId() 33 assert.NoError(t, err) 34 assert.NotEmpty(t, serviceId) 35 } 36 37 func testGetRunningNodes(t *testing.T) { 38 runningNodes, err := testsSystemService.GetRunningNodes() 39 assert.NoError(t, err) 40 assert.NotEmpty(t, runningNodes) 41 } 42 43 func testGetConfigDescriptor(t *testing.T) { 44 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 45 // Check method 46 assert.Equal(t, http.MethodGet, r.Method) 47 48 // Check URL 49 assert.Equal(t, "/api/system/configuration", r.URL.Path) 50 51 // Send response 200 OK 52 w.WriteHeader(http.StatusOK) 53 _, err := w.Write([]byte("<config></config>")) 54 assert.NoError(t, err) 55 }) 56 ts := httptest.NewServer(handler) 57 defer ts.Close() 58 59 service := createMockSystemService(t, ts.URL) 60 results, err := service.GetConfigDescriptor() 61 assert.NoError(t, err) 62 assert.Equal(t, "<config></config>", results) 63 } 64 65 func testActivateKeyEncryption(t *testing.T) { 66 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 67 // Check method 68 assert.Equal(t, http.MethodPost, r.Method) 69 70 // Check URL 71 assert.Equal(t, "/api/system/encrypt", r.URL.Path) 72 73 // Send response 200 OK 74 w.WriteHeader(http.StatusOK) 75 _, err := w.Write([]byte("Done")) 76 assert.NoError(t, err) 77 }) 78 ts := httptest.NewServer(handler) 79 defer ts.Close() 80 81 service := createMockSystemService(t, ts.URL) 82 assert.NoError(t, service.ActivateKeyEncryption()) 83 } 84 85 func testDeactivateKeyEncryption(t *testing.T) { 86 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 87 // Check method 88 assert.Equal(t, http.MethodPost, r.Method) 89 90 // Check URL 91 assert.Equal(t, "/api/system/decrypt", r.URL.Path) 92 93 // Send response 200 OK 94 w.WriteHeader(http.StatusOK) 95 _, err := w.Write([]byte("Done")) 96 assert.NoError(t, err) 97 }) 98 ts := httptest.NewServer(handler) 99 defer ts.Close() 100 101 service := createMockSystemService(t, ts.URL) 102 wasEncrypted, err := service.DeactivateKeyEncryption() 103 assert.NoError(t, err) 104 assert.True(t, wasEncrypted) 105 } 106 107 func testDeactivateKeyEncryptionNotEncrypted(t *testing.T) { 108 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 109 // Check method 110 assert.Equal(t, http.MethodPost, r.Method) 111 112 // Check URL 113 assert.Equal(t, "/api/system/decrypt", r.URL.Path) 114 115 // Send response 200 OK 116 w.WriteHeader(http.StatusConflict) 117 _, err := w.Write([]byte("Cannot decrypt without artifactory key file")) 118 assert.NoError(t, err) 119 }) 120 ts := httptest.NewServer(handler) 121 defer ts.Close() 122 123 service := createMockSystemService(t, ts.URL) 124 wasEncrypted, err := service.DeactivateKeyEncryption() 125 assert.NoError(t, err) 126 assert.False(t, wasEncrypted) 127 } 128 129 func createMockSystemService(t *testing.T, url string) *services.SystemService { 130 // Create artifactory details 131 rtDetails := auth.NewArtifactoryDetails() 132 rtDetails.SetUrl(url + "/") 133 134 // Create http client 135 client, err := jfroghttpclient.JfrogClientBuilder(). 136 SetInsecureTls(true). 137 SetClientCertPath(rtDetails.GetClientCertPath()). 138 SetClientCertKeyPath(rtDetails.GetClientCertKeyPath()). 139 AppendPreRequestInterceptor(rtDetails.RunPreRequestFunctions). 140 Build() 141 assert.NoError(t, err, "Failed to create Artifactory client: %v\n") 142 143 // Create system service 144 return services.NewSystemService(rtDetails, client) 145 }