2023-05-03 21:02:11 +05:00
//go:build unit
2024-12-23 13:33:12 +02:00
// +build unit
2023-05-03 21:02:11 +05:00
2020-07-23 10:26:50 +02:00
package abaputils
import (
2020-08-21 14:49:48 +02:00
"bytes"
2020-08-13 09:48:40 +02:00
"fmt"
2023-08-16 12:57:04 +02:00
"io"
2020-08-21 14:49:48 +02:00
"net/http"
2020-07-23 10:26:50 +02:00
"testing"
2020-08-21 14:49:48 +02:00
"github.com/SAP/jenkins-library/pkg/log"
2020-07-23 10:26:50 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
2020-08-21 14:49:48 +02:00
"github.com/pkg/errors"
2023-07-18 09:05:53 +02:00
"github.com/sirupsen/logrus/hooks/test"
2020-07-23 10:26:50 +02:00
"github.com/stretchr/testify/assert"
)
func TestCloudFoundryGetAbapCommunicationInfo ( t * testing . T ) {
t . Run ( "CF GetAbapCommunicationArrangementInfo - Error - parameters missing" , func ( t * testing . T ) {
//given
options := AbapEnvironmentOptions {
//CfAPIEndpoint: "https://api.endpoint.com",
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
CfServiceInstance : "testInstance" ,
Username : "testUser" ,
Password : "testPassword" ,
CfServiceKeyName : "testServiceKeyName" ,
}
//when
var connectionDetails ConnectionDetailsHTTP
var err error
2020-07-31 14:43:23 +02:00
var autils = AbapUtils {
2021-01-26 20:23:59 +01:00
Exec : & mock . ExecMockRunner { } ,
2020-07-31 14:43:23 +02:00
}
connectionDetails , err = autils . GetAbapCommunicationArrangementInfo ( options , "" )
2020-07-23 10:26:50 +02:00
//then
assert . Equal ( t , "" , connectionDetails . URL )
assert . Equal ( t , "" , connectionDetails . User )
assert . Equal ( t , "" , connectionDetails . Password )
assert . Equal ( t , "" , connectionDetails . XCsrfToken )
2024-06-10 10:51:22 +02:00
assert . EqualError ( t , err , "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry API Endpoint, Organization, Space, Service Instance and Service Key" )
2020-07-23 10:26:50 +02:00
} )
t . Run ( "CF GetAbapCommunicationArrangementInfo - Error - reading service Key" , func ( t * testing . T ) {
//given
options := AbapEnvironmentOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
CfServiceInstance : "testInstance" ,
Username : "testUser" ,
Password : "testPassword" ,
CfServiceKeyName : "testServiceKeyName" ,
}
//when
var connectionDetails ConnectionDetailsHTTP
var err error
2020-07-31 14:43:23 +02:00
var autils = AbapUtils {
2021-01-26 20:23:59 +01:00
Exec : & mock . ExecMockRunner { } ,
2020-07-31 14:43:23 +02:00
}
connectionDetails , err = autils . GetAbapCommunicationArrangementInfo ( options , "" )
2020-07-23 10:26:50 +02:00
//then
assert . Equal ( t , "" , connectionDetails . URL )
assert . Equal ( t , "" , connectionDetails . User )
assert . Equal ( t , "" , connectionDetails . Password )
assert . Equal ( t , "" , connectionDetails . XCsrfToken )
2023-01-12 09:39:14 +01:00
assert . EqualError ( t , err , "Read service key failed: Parsing the service key failed for all supported formats. Service key is empty" )
2020-07-23 10:26:50 +02:00
} )
2023-01-12 09:39:14 +01:00
t . Run ( "CF GetAbapCommunicationArrangementInfo - Success V8" , func ( t * testing . T ) {
//given
const testURL = "https://testurl.com"
const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
const username = "test_user"
const password = "test_password"
const serviceKey = `
cf comment test \ n \ n
{ "credentials" : { "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 } } `
options := AbapEnvironmentOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
CfServiceInstance : "testInstance" ,
Username : "testUser" ,
Password : "testPassword" ,
CfServiceKeyName : "testServiceKeyName" ,
}
m := & mock . ExecMockRunner { }
m . StdoutReturn = map [ string ] string { "cf service-key testInstance testServiceKeyName" : serviceKey }
var autils = AbapUtils {
Exec : m ,
}
//when
var connectionDetails ConnectionDetailsHTTP
var err error
connectionDetails , err = autils . GetAbapCommunicationArrangementInfo ( options , oDataURL )
//then
assert . Equal ( t , testURL + oDataURL , connectionDetails . URL )
assert . Equal ( t , username , connectionDetails . User )
assert . Equal ( t , password , connectionDetails . Password )
assert . Equal ( t , "" , connectionDetails . XCsrfToken )
assert . NoError ( t , err )
} )
t . Run ( "CF GetAbapCommunicationArrangementInfo - Success V7" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
2020-07-23 10:26:50 +02:00
//given
const testURL = "https://testurl.com"
const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
const username = "test_user"
const password = "test_password"
const serviceKey = `
cf comment test \ n \ n
{ "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 } `
options := AbapEnvironmentOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
CfServiceInstance : "testInstance" ,
Username : "testUser" ,
Password : "testPassword" ,
CfServiceKeyName : "testServiceKeyName" ,
}
2020-07-31 14:43:23 +02:00
m := & mock . ExecMockRunner { }
2020-07-23 10:26:50 +02:00
m . StdoutReturn = map [ string ] string { "cf service-key testInstance testServiceKeyName" : serviceKey }
2020-07-31 14:43:23 +02:00
var autils = AbapUtils {
Exec : m ,
}
2020-07-23 10:26:50 +02:00
//when
var connectionDetails ConnectionDetailsHTTP
var err error
2020-07-31 14:43:23 +02:00
connectionDetails , err = autils . GetAbapCommunicationArrangementInfo ( options , oDataURL )
2020-07-23 10:26:50 +02:00
//then
assert . Equal ( t , testURL + oDataURL , connectionDetails . URL )
assert . Equal ( t , username , connectionDetails . User )
assert . Equal ( t , password , connectionDetails . Password )
assert . Equal ( t , "" , connectionDetails . XCsrfToken )
assert . NoError ( t , err )
} )
}
2020-10-05 14:38:35 +02:00
2020-07-23 10:26:50 +02:00
func TestHostGetAbapCommunicationInfo ( t * testing . T ) {
t . Run ( "HOST GetAbapCommunicationArrangementInfo - Success" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
2020-07-23 10:26:50 +02:00
//given
const testURL = "https://testurl.com"
const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
const username = "test_user"
const password = "test_password"
const serviceKey = `
cf comment test \ n \ n
2023-01-12 09:39:14 +01:00
{ "credentials" : { "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 } } `
2020-07-23 10:26:50 +02:00
options := AbapEnvironmentOptions {
Host : testURL ,
Username : username ,
Password : password ,
}
2020-07-31 14:43:23 +02:00
m := & mock . ExecMockRunner { }
2020-07-23 10:26:50 +02:00
m . StdoutReturn = map [ string ] string { "cf service-key testInstance testServiceKeyName" : serviceKey }
2020-07-31 14:43:23 +02:00
var autils = AbapUtils {
Exec : m ,
}
2020-07-23 10:26:50 +02:00
//when
var connectionDetails ConnectionDetailsHTTP
var err error
2020-07-31 14:43:23 +02:00
connectionDetails , err = autils . GetAbapCommunicationArrangementInfo ( options , oDataURL )
2020-07-23 10:26:50 +02:00
//then
assert . Equal ( t , testURL + oDataURL , connectionDetails . URL )
assert . Equal ( t , username , connectionDetails . User )
assert . Equal ( t , password , connectionDetails . Password )
assert . Equal ( t , "" , connectionDetails . XCsrfToken )
assert . NoError ( t , err )
} )
t . Run ( "HOST GetAbapCommunicationArrangementInfo - Success - w/o https" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
2020-07-23 10:26:50 +02:00
//given
const testURL = "testurl.com"
const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
const username = "test_user"
const password = "test_password"
const serviceKey = `
cf comment test \ n \ n
2023-01-12 09:39:14 +01:00
{ "credentials" : { "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 } } `
2020-07-23 10:26:50 +02:00
options := AbapEnvironmentOptions {
Host : testURL ,
Username : username ,
Password : password ,
}
2020-07-31 14:43:23 +02:00
m := & mock . ExecMockRunner { }
2020-07-23 10:26:50 +02:00
m . StdoutReturn = map [ string ] string { "cf service-key testInstance testServiceKeyName" : serviceKey }
2020-07-31 14:43:23 +02:00
var autils = AbapUtils {
Exec : m ,
}
2020-07-23 10:26:50 +02:00
//when
var connectionDetails ConnectionDetailsHTTP
var err error
2020-07-31 14:43:23 +02:00
connectionDetails , err = autils . GetAbapCommunicationArrangementInfo ( options , oDataURL )
2020-07-23 10:26:50 +02:00
//then
assert . Equal ( t , "https://" + testURL + oDataURL , connectionDetails . URL )
assert . Equal ( t , username , connectionDetails . User )
assert . Equal ( t , password , connectionDetails . Password )
assert . Equal ( t , "" , connectionDetails . XCsrfToken )
assert . NoError ( t , err )
} )
}
2020-10-05 14:38:35 +02:00
2020-07-23 10:26:50 +02:00
func TestReadServiceKeyAbapEnvironment ( t * testing . T ) {
t . Run ( "CF ReadServiceKeyAbapEnvironment - Failed to login to Cloud Foundry" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
//given .
2020-07-23 10:26:50 +02:00
options := AbapEnvironmentOptions {
Username : "testUser" ,
Password : "testPassword" ,
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testKey" ,
}
//when
var abapKey AbapServiceKey
var err error
2021-01-26 20:23:59 +01:00
abapKey , err = ReadServiceKeyAbapEnvironment ( options , & mock . ExecMockRunner { } )
2020-07-23 10:26:50 +02:00
//then
assert . Equal ( t , "" , abapKey . Abap . Password )
assert . Equal ( t , "" , abapKey . Abap . Username )
assert . Equal ( t , "" , abapKey . Abap . CommunicationArrangementID )
assert . Equal ( t , "" , abapKey . Abap . CommunicationScenarioID )
assert . Equal ( t , "" , abapKey . Abap . CommunicationSystemID )
assert . Equal ( t , "" , abapKey . Binding . Env )
assert . Equal ( t , "" , abapKey . Binding . Type )
assert . Equal ( t , "" , abapKey . Binding . ID )
assert . Equal ( t , "" , abapKey . Binding . Version )
assert . Equal ( t , "" , abapKey . SystemID )
assert . Equal ( t , "" , abapKey . URL )
2023-01-12 09:39:14 +01:00
assert . EqualError ( t , err , "Parsing the service key failed for all supported formats. Service key is empty" )
2020-07-23 10:26:50 +02:00
} )
}
2020-08-04 17:52:28 +02:00
2020-08-21 14:49:48 +02:00
func TestHandleHTTPError ( t * testing . T ) {
t . Run ( "Test" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
2020-08-26 16:45:09 +02:00
errorValue := "Received Error"
2020-08-21 14:49:48 +02:00
abapErrorCode := "abapErrorCode"
abapErrorMessage := "abapErrorMessage"
bodyString := ` { "error" : { "code" : " ` + abapErrorCode + ` ", "message" : { "lang" : "en", "value" : " ` + abapErrorMessage + ` " } } } `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2020-08-21 14:49:48 +02:00
}
receivedErr := errors . New ( errorValue )
message := "Custom Error Message"
2023-11-28 13:26:31 +01:00
_ , err := HandleHTTPError ( & resp , receivedErr , message , ConnectionDetailsHTTP { } )
2020-08-26 16:45:09 +02:00
assert . EqualError ( t , err , fmt . Sprintf ( "%s: %s - %s" , receivedErr . Error ( ) , abapErrorCode , abapErrorMessage ) )
log . Entry ( ) . Info ( err . Error ( ) )
} )
t . Run ( "Non JSON Error" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
2020-08-26 16:45:09 +02:00
errorValue := "Received Error"
bodyString := ` Error message `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2020-08-26 16:45:09 +02:00
}
receivedErr := errors . New ( errorValue )
message := "Custom Error Message"
2023-11-28 13:26:31 +01:00
_ , err := HandleHTTPError ( & resp , receivedErr , message , ConnectionDetailsHTTP { } )
2020-08-26 16:45:09 +02:00
assert . EqualError ( t , err , fmt . Sprintf ( "%s" , receivedErr . Error ( ) ) )
log . Entry ( ) . Info ( err . Error ( ) )
} )
t . Run ( "Different JSON Error" , func ( t * testing . T ) {
2021-02-04 15:19:42 +01:00
2020-08-26 16:45:09 +02:00
errorValue := "Received Error"
bodyString := ` { "abap" : { "key" : "value" } } `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2020-08-26 16:45:09 +02:00
}
receivedErr := errors . New ( errorValue )
message := "Custom Error Message"
2023-11-28 13:26:31 +01:00
_ , err := HandleHTTPError ( & resp , receivedErr , message , ConnectionDetailsHTTP { } )
2020-08-26 16:45:09 +02:00
assert . EqualError ( t , err , fmt . Sprintf ( "%s" , receivedErr . Error ( ) ) )
2020-08-21 14:49:48 +02:00
log . Entry ( ) . Info ( err . Error ( ) )
2020-08-13 09:48:40 +02:00
} )
2023-07-18 09:05:53 +02:00
t . Run ( "EOF Error" , func ( t * testing . T ) {
message := "Custom Error Message"
errorValue := "Received Error EOF"
receivedErr := errors . New ( errorValue )
_ , hook := test . NewNullLogger ( )
log . RegisterHook ( hook )
2023-11-28 13:26:31 +01:00
_ , err := HandleHTTPError ( nil , receivedErr , message , ConnectionDetailsHTTP { } )
2023-07-18 09:05:53 +02:00
assert . EqualError ( t , err , fmt . Sprintf ( "%s" , receivedErr . Error ( ) ) )
assert . Equal ( t , 5 , len ( hook . Entries ) , "Expected a different number of entries" )
assert . Equal ( t , ` A connection could not be established to the ABAP system. The typical root cause is the network configuration (firewall, IP allowlist, etc.) ` , hook . AllEntries ( ) [ 2 ] . Message , "Expected a different message" )
hook . Reset ( )
} )
2020-08-13 09:48:40 +02:00
}