github.com/xgoffin/jenkins-library@v1.154.0/pkg/abaputils/abaputils_test.go (about) 1 package abaputils 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "testing" 9 10 "github.com/SAP/jenkins-library/pkg/log" 11 "github.com/SAP/jenkins-library/pkg/mock" 12 "github.com/pkg/errors" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCloudFoundryGetAbapCommunicationInfo(t *testing.T) { 17 t.Run("CF GetAbapCommunicationArrangementInfo - Error - parameters missing", func(t *testing.T) { 18 19 //given 20 options := AbapEnvironmentOptions{ 21 //CfAPIEndpoint: "https://api.endpoint.com", 22 CfSpace: "testSpace", 23 CfOrg: "testOrg", 24 CfServiceInstance: "testInstance", 25 Username: "testUser", 26 Password: "testPassword", 27 CfServiceKeyName: "testServiceKeyName", 28 } 29 30 //when 31 var connectionDetails ConnectionDetailsHTTP 32 var err error 33 var autils = AbapUtils{ 34 Exec: &mock.ExecMockRunner{}, 35 } 36 connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, "") 37 38 //then 39 assert.Equal(t, "", connectionDetails.URL) 40 assert.Equal(t, "", connectionDetails.User) 41 assert.Equal(t, "", connectionDetails.Password) 42 assert.Equal(t, "", connectionDetails.XCsrfToken) 43 44 assert.EqualError(t, err, "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510") 45 }) 46 t.Run("CF GetAbapCommunicationArrangementInfo - Error - reading service Key", func(t *testing.T) { 47 //given 48 options := AbapEnvironmentOptions{ 49 CfAPIEndpoint: "https://api.endpoint.com", 50 CfSpace: "testSpace", 51 CfOrg: "testOrg", 52 CfServiceInstance: "testInstance", 53 Username: "testUser", 54 Password: "testPassword", 55 CfServiceKeyName: "testServiceKeyName", 56 } 57 58 //when 59 var connectionDetails ConnectionDetailsHTTP 60 var err error 61 var autils = AbapUtils{ 62 Exec: &mock.ExecMockRunner{}, 63 } 64 connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, "") 65 66 //then 67 assert.Equal(t, "", connectionDetails.URL) 68 assert.Equal(t, "", connectionDetails.User) 69 assert.Equal(t, "", connectionDetails.Password) 70 assert.Equal(t, "", connectionDetails.XCsrfToken) 71 72 assert.EqualError(t, err, "Read service key failed: Parsing the service key failed. Service key is empty") 73 }) 74 t.Run("CF GetAbapCommunicationArrangementInfo - Success", func(t *testing.T) { 75 76 //given 77 78 const testURL = "https://testurl.com" 79 const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" 80 const username = "test_user" 81 const password = "test_password" 82 const serviceKey = ` 83 cf comment test \n\n 84 {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"` + username + `","password":"` + password + `","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}` 85 86 options := AbapEnvironmentOptions{ 87 CfAPIEndpoint: "https://api.endpoint.com", 88 CfSpace: "testSpace", 89 CfOrg: "testOrg", 90 CfServiceInstance: "testInstance", 91 Username: "testUser", 92 Password: "testPassword", 93 CfServiceKeyName: "testServiceKeyName", 94 } 95 96 m := &mock.ExecMockRunner{} 97 m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey} 98 var autils = AbapUtils{ 99 Exec: m, 100 } 101 //when 102 var connectionDetails ConnectionDetailsHTTP 103 var err error 104 connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL) 105 106 //then 107 assert.Equal(t, testURL+oDataURL, connectionDetails.URL) 108 assert.Equal(t, username, connectionDetails.User) 109 assert.Equal(t, password, connectionDetails.Password) 110 assert.Equal(t, "", connectionDetails.XCsrfToken) 111 112 assert.NoError(t, err) 113 }) 114 } 115 116 func TestHostGetAbapCommunicationInfo(t *testing.T) { 117 t.Run("HOST GetAbapCommunicationArrangementInfo - Success", func(t *testing.T) { 118 119 //given 120 121 const testURL = "https://testurl.com" 122 const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" 123 const username = "test_user" 124 const password = "test_password" 125 const serviceKey = ` 126 cf comment test \n\n 127 {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"XYZ","abap":{"username":"` + username + `","password":"` + password + `","communication_scenario_id": "SAP_COM_XYZ","communication_arrangement_id": "SK_testing","communication_system_id": "SK_testing","communication_inbound_user_id": "CC0000000000","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdtestKynaTqa32W"},"preserve_host_header": true}` 128 129 options := AbapEnvironmentOptions{ 130 Host: testURL, 131 Username: username, 132 Password: password, 133 } 134 135 m := &mock.ExecMockRunner{} 136 m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey} 137 var autils = AbapUtils{ 138 Exec: m, 139 } 140 141 //when 142 var connectionDetails ConnectionDetailsHTTP 143 var err error 144 connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL) 145 146 //then 147 assert.Equal(t, testURL+oDataURL, connectionDetails.URL) 148 assert.Equal(t, username, connectionDetails.User) 149 assert.Equal(t, password, connectionDetails.Password) 150 assert.Equal(t, "", connectionDetails.XCsrfToken) 151 152 assert.NoError(t, err) 153 }) 154 t.Run("HOST GetAbapCommunicationArrangementInfo - Success - w/o https", func(t *testing.T) { 155 156 //given 157 158 const testURL = "testurl.com" 159 const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" 160 const username = "test_user" 161 const password = "test_password" 162 const serviceKey = ` 163 cf comment test \n\n 164 {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"` + username + `","password":"` + password + `","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}` 165 166 options := AbapEnvironmentOptions{ 167 Host: testURL, 168 Username: username, 169 Password: password, 170 } 171 172 m := &mock.ExecMockRunner{} 173 m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey} 174 var autils = AbapUtils{ 175 Exec: m, 176 } 177 178 //when 179 var connectionDetails ConnectionDetailsHTTP 180 var err error 181 connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL) 182 183 //then 184 assert.Equal(t, "https://"+testURL+oDataURL, connectionDetails.URL) 185 assert.Equal(t, username, connectionDetails.User) 186 assert.Equal(t, password, connectionDetails.Password) 187 assert.Equal(t, "", connectionDetails.XCsrfToken) 188 189 assert.NoError(t, err) 190 }) 191 } 192 193 func TestReadServiceKeyAbapEnvironment(t *testing.T) { 194 t.Run("CF ReadServiceKeyAbapEnvironment - Failed to login to Cloud Foundry", func(t *testing.T) { 195 //given . 196 options := AbapEnvironmentOptions{ 197 Username: "testUser", 198 Password: "testPassword", 199 CfAPIEndpoint: "https://api.endpoint.com", 200 CfSpace: "testSpace", 201 CfOrg: "testOrg", 202 CfServiceInstance: "testInstance", 203 CfServiceKeyName: "testKey", 204 } 205 206 //when 207 var abapKey AbapServiceKey 208 var err error 209 abapKey, err = ReadServiceKeyAbapEnvironment(options, &mock.ExecMockRunner{}) 210 211 //then 212 assert.Equal(t, "", abapKey.Abap.Password) 213 assert.Equal(t, "", abapKey.Abap.Username) 214 assert.Equal(t, "", abapKey.Abap.CommunicationArrangementID) 215 assert.Equal(t, "", abapKey.Abap.CommunicationScenarioID) 216 assert.Equal(t, "", abapKey.Abap.CommunicationSystemID) 217 218 assert.Equal(t, "", abapKey.Binding.Env) 219 assert.Equal(t, "", abapKey.Binding.Type) 220 assert.Equal(t, "", abapKey.Binding.ID) 221 assert.Equal(t, "", abapKey.Binding.Version) 222 assert.Equal(t, "", abapKey.SystemID) 223 assert.Equal(t, "", abapKey.URL) 224 225 assert.EqualError(t, err, "Parsing the service key failed. Service key is empty") 226 }) 227 } 228 229 func TestTimeConverter(t *testing.T) { 230 t.Run("Test example time", func(t *testing.T) { 231 inputDate := "/Date(1585576809000+0000)/" 232 expectedDate := "2020-03-30 14:00:09 +0000 UTC" 233 result := ConvertTime(inputDate) 234 assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion") 235 }) 236 t.Run("Test Unix time", func(t *testing.T) { 237 inputDate := "/Date(0000000000000+0000)/" 238 expectedDate := "1970-01-01 00:00:00 +0000 UTC" 239 result := ConvertTime(inputDate) 240 assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion") 241 }) 242 t.Run("Test unexpected format", func(t *testing.T) { 243 inputDate := "/Date(0012300000001+0000)/" 244 expectedDate := "1970-01-01 00:00:00 +0000 UTC" 245 result := ConvertTime(inputDate) 246 assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion") 247 }) 248 } 249 250 func TestHandleHTTPError(t *testing.T) { 251 t.Run("Test", func(t *testing.T) { 252 253 errorValue := "Received Error" 254 abapErrorCode := "abapErrorCode" 255 abapErrorMessage := "abapErrorMessage" 256 bodyString := `{"error" : { "code" : "` + abapErrorCode + `", "message" : { "lang" : "en", "value" : "` + abapErrorMessage + `" } } }` 257 body := []byte(bodyString) 258 259 resp := http.Response{ 260 Status: "400 Bad Request", 261 StatusCode: 400, 262 Body: ioutil.NopCloser(bytes.NewReader(body)), 263 } 264 receivedErr := errors.New(errorValue) 265 message := "Custom Error Message" 266 267 err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{}) 268 assert.EqualError(t, err, fmt.Sprintf("%s: %s - %s", receivedErr.Error(), abapErrorCode, abapErrorMessage)) 269 log.Entry().Info(err.Error()) 270 }) 271 272 t.Run("Non JSON Error", func(t *testing.T) { 273 274 errorValue := "Received Error" 275 bodyString := `Error message` 276 body := []byte(bodyString) 277 278 resp := http.Response{ 279 Status: "400 Bad Request", 280 StatusCode: 400, 281 Body: ioutil.NopCloser(bytes.NewReader(body)), 282 } 283 receivedErr := errors.New(errorValue) 284 message := "Custom Error Message" 285 286 err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{}) 287 assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error())) 288 log.Entry().Info(err.Error()) 289 }) 290 291 t.Run("Different JSON Error", func(t *testing.T) { 292 293 errorValue := "Received Error" 294 bodyString := `{"abap" : { "key" : "value" } }` 295 body := []byte(bodyString) 296 297 resp := http.Response{ 298 Status: "400 Bad Request", 299 StatusCode: 400, 300 Body: ioutil.NopCloser(bytes.NewReader(body)), 301 } 302 receivedErr := errors.New(errorValue) 303 message := "Custom Error Message" 304 305 err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{}) 306 assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error())) 307 log.Entry().Info(err.Error()) 308 }) 309 }