github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/abaputils/abaputils_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package abaputils
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"testing"
    12  
    13  	"github.com/SAP/jenkins-library/pkg/log"
    14  	"github.com/SAP/jenkins-library/pkg/mock"
    15  	"github.com/pkg/errors"
    16  	"github.com/sirupsen/logrus/hooks/test"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestCloudFoundryGetAbapCommunicationInfo(t *testing.T) {
    21  	t.Run("CF GetAbapCommunicationArrangementInfo - Error - parameters missing", func(t *testing.T) {
    22  
    23  		//given
    24  		options := AbapEnvironmentOptions{
    25  			//CfAPIEndpoint:     "https://api.endpoint.com",
    26  			CfSpace:           "testSpace",
    27  			CfOrg:             "testOrg",
    28  			CfServiceInstance: "testInstance",
    29  			Username:          "testUser",
    30  			Password:          "testPassword",
    31  			CfServiceKeyName:  "testServiceKeyName",
    32  		}
    33  
    34  		//when
    35  		var connectionDetails ConnectionDetailsHTTP
    36  		var err error
    37  		var autils = AbapUtils{
    38  			Exec: &mock.ExecMockRunner{},
    39  		}
    40  		connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, "")
    41  
    42  		//then
    43  		assert.Equal(t, "", connectionDetails.URL)
    44  		assert.Equal(t, "", connectionDetails.User)
    45  		assert.Equal(t, "", connectionDetails.Password)
    46  		assert.Equal(t, "", connectionDetails.XCsrfToken)
    47  
    48  		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")
    49  	})
    50  	t.Run("CF GetAbapCommunicationArrangementInfo - Error - reading service Key", func(t *testing.T) {
    51  		//given
    52  		options := AbapEnvironmentOptions{
    53  			CfAPIEndpoint:     "https://api.endpoint.com",
    54  			CfSpace:           "testSpace",
    55  			CfOrg:             "testOrg",
    56  			CfServiceInstance: "testInstance",
    57  			Username:          "testUser",
    58  			Password:          "testPassword",
    59  			CfServiceKeyName:  "testServiceKeyName",
    60  		}
    61  
    62  		//when
    63  		var connectionDetails ConnectionDetailsHTTP
    64  		var err error
    65  		var autils = AbapUtils{
    66  			Exec: &mock.ExecMockRunner{},
    67  		}
    68  		connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, "")
    69  
    70  		//then
    71  		assert.Equal(t, "", connectionDetails.URL)
    72  		assert.Equal(t, "", connectionDetails.User)
    73  		assert.Equal(t, "", connectionDetails.Password)
    74  		assert.Equal(t, "", connectionDetails.XCsrfToken)
    75  
    76  		assert.EqualError(t, err, "Read service key failed: Parsing the service key failed for all supported formats. Service key is empty")
    77  	})
    78  	t.Run("CF GetAbapCommunicationArrangementInfo - Success V8", func(t *testing.T) {
    79  
    80  		//given
    81  
    82  		const testURL = "https://testurl.com"
    83  		const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
    84  		const username = "test_user"
    85  		const password = "test_password"
    86  		const serviceKey = `
    87  		cf comment test \n\n
    88  		{ "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} }`
    89  
    90  		options := AbapEnvironmentOptions{
    91  			CfAPIEndpoint:     "https://api.endpoint.com",
    92  			CfSpace:           "testSpace",
    93  			CfOrg:             "testOrg",
    94  			CfServiceInstance: "testInstance",
    95  			Username:          "testUser",
    96  			Password:          "testPassword",
    97  			CfServiceKeyName:  "testServiceKeyName",
    98  		}
    99  
   100  		m := &mock.ExecMockRunner{}
   101  		m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey}
   102  		var autils = AbapUtils{
   103  			Exec: m,
   104  		}
   105  		//when
   106  		var connectionDetails ConnectionDetailsHTTP
   107  		var err error
   108  		connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL)
   109  
   110  		//then
   111  		assert.Equal(t, testURL+oDataURL, connectionDetails.URL)
   112  		assert.Equal(t, username, connectionDetails.User)
   113  		assert.Equal(t, password, connectionDetails.Password)
   114  		assert.Equal(t, "", connectionDetails.XCsrfToken)
   115  
   116  		assert.NoError(t, err)
   117  	})
   118  
   119  	t.Run("CF GetAbapCommunicationArrangementInfo - Success V7", func(t *testing.T) {
   120  
   121  		//given
   122  
   123  		const testURL = "https://testurl.com"
   124  		const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
   125  		const username = "test_user"
   126  		const password = "test_password"
   127  		const serviceKey = `
   128  		cf comment test \n\n
   129  		{"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}`
   130  
   131  		options := AbapEnvironmentOptions{
   132  			CfAPIEndpoint:     "https://api.endpoint.com",
   133  			CfSpace:           "testSpace",
   134  			CfOrg:             "testOrg",
   135  			CfServiceInstance: "testInstance",
   136  			Username:          "testUser",
   137  			Password:          "testPassword",
   138  			CfServiceKeyName:  "testServiceKeyName",
   139  		}
   140  
   141  		m := &mock.ExecMockRunner{}
   142  		m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey}
   143  		var autils = AbapUtils{
   144  			Exec: m,
   145  		}
   146  		//when
   147  		var connectionDetails ConnectionDetailsHTTP
   148  		var err error
   149  		connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL)
   150  
   151  		//then
   152  		assert.Equal(t, testURL+oDataURL, connectionDetails.URL)
   153  		assert.Equal(t, username, connectionDetails.User)
   154  		assert.Equal(t, password, connectionDetails.Password)
   155  		assert.Equal(t, "", connectionDetails.XCsrfToken)
   156  
   157  		assert.NoError(t, err)
   158  	})
   159  }
   160  
   161  func TestHostGetAbapCommunicationInfo(t *testing.T) {
   162  	t.Run("HOST GetAbapCommunicationArrangementInfo - Success", func(t *testing.T) {
   163  
   164  		//given
   165  
   166  		const testURL = "https://testurl.com"
   167  		const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
   168  		const username = "test_user"
   169  		const password = "test_password"
   170  		const serviceKey = `
   171  		cf comment test \n\n
   172  		{ "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} }`
   173  
   174  		options := AbapEnvironmentOptions{
   175  			Host:     testURL,
   176  			Username: username,
   177  			Password: password,
   178  		}
   179  
   180  		m := &mock.ExecMockRunner{}
   181  		m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey}
   182  		var autils = AbapUtils{
   183  			Exec: m,
   184  		}
   185  
   186  		//when
   187  		var connectionDetails ConnectionDetailsHTTP
   188  		var err error
   189  		connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL)
   190  
   191  		//then
   192  		assert.Equal(t, testURL+oDataURL, connectionDetails.URL)
   193  		assert.Equal(t, username, connectionDetails.User)
   194  		assert.Equal(t, password, connectionDetails.Password)
   195  		assert.Equal(t, "", connectionDetails.XCsrfToken)
   196  
   197  		assert.NoError(t, err)
   198  	})
   199  	t.Run("HOST GetAbapCommunicationArrangementInfo - Success - w/o https", func(t *testing.T) {
   200  
   201  		//given
   202  
   203  		const testURL = "testurl.com"
   204  		const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
   205  		const username = "test_user"
   206  		const password = "test_password"
   207  		const serviceKey = `
   208  		cf comment test \n\n
   209  		{ "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} }`
   210  
   211  		options := AbapEnvironmentOptions{
   212  			Host:     testURL,
   213  			Username: username,
   214  			Password: password,
   215  		}
   216  
   217  		m := &mock.ExecMockRunner{}
   218  		m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey}
   219  		var autils = AbapUtils{
   220  			Exec: m,
   221  		}
   222  
   223  		//when
   224  		var connectionDetails ConnectionDetailsHTTP
   225  		var err error
   226  		connectionDetails, err = autils.GetAbapCommunicationArrangementInfo(options, oDataURL)
   227  
   228  		//then
   229  		assert.Equal(t, "https://"+testURL+oDataURL, connectionDetails.URL)
   230  		assert.Equal(t, username, connectionDetails.User)
   231  		assert.Equal(t, password, connectionDetails.Password)
   232  		assert.Equal(t, "", connectionDetails.XCsrfToken)
   233  
   234  		assert.NoError(t, err)
   235  	})
   236  }
   237  
   238  func TestReadServiceKeyAbapEnvironment(t *testing.T) {
   239  	t.Run("CF ReadServiceKeyAbapEnvironment - Failed to login to Cloud Foundry", func(t *testing.T) {
   240  		//given .
   241  		options := AbapEnvironmentOptions{
   242  			Username:          "testUser",
   243  			Password:          "testPassword",
   244  			CfAPIEndpoint:     "https://api.endpoint.com",
   245  			CfSpace:           "testSpace",
   246  			CfOrg:             "testOrg",
   247  			CfServiceInstance: "testInstance",
   248  			CfServiceKeyName:  "testKey",
   249  		}
   250  
   251  		//when
   252  		var abapKey AbapServiceKey
   253  		var err error
   254  		abapKey, err = ReadServiceKeyAbapEnvironment(options, &mock.ExecMockRunner{})
   255  
   256  		//then
   257  		assert.Equal(t, "", abapKey.Abap.Password)
   258  		assert.Equal(t, "", abapKey.Abap.Username)
   259  		assert.Equal(t, "", abapKey.Abap.CommunicationArrangementID)
   260  		assert.Equal(t, "", abapKey.Abap.CommunicationScenarioID)
   261  		assert.Equal(t, "", abapKey.Abap.CommunicationSystemID)
   262  
   263  		assert.Equal(t, "", abapKey.Binding.Env)
   264  		assert.Equal(t, "", abapKey.Binding.Type)
   265  		assert.Equal(t, "", abapKey.Binding.ID)
   266  		assert.Equal(t, "", abapKey.Binding.Version)
   267  		assert.Equal(t, "", abapKey.SystemID)
   268  		assert.Equal(t, "", abapKey.URL)
   269  
   270  		assert.EqualError(t, err, "Parsing the service key failed for all supported formats. Service key is empty")
   271  	})
   272  }
   273  
   274  func TestTimeConverter(t *testing.T) {
   275  	t.Run("Test example time", func(t *testing.T) {
   276  		inputDate := "/Date(1585576809000+0000)/"
   277  		expectedDate := "2020-03-30 14:00:09 +0000 UTC"
   278  		result := ConvertTime(inputDate)
   279  		assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
   280  	})
   281  	t.Run("Test Unix time", func(t *testing.T) {
   282  		inputDate := "/Date(0000000000000+0000)/"
   283  		expectedDate := "1970-01-01 00:00:00 +0000 UTC"
   284  		result := ConvertTime(inputDate)
   285  		assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
   286  	})
   287  	t.Run("Test unexpected format", func(t *testing.T) {
   288  		inputDate := "/Date(0012300000001+0000)/"
   289  		expectedDate := "1970-01-01 00:00:00 +0000 UTC"
   290  		result := ConvertTime(inputDate)
   291  		assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
   292  	})
   293  }
   294  
   295  func TestHandleHTTPError(t *testing.T) {
   296  	t.Run("Test", func(t *testing.T) {
   297  
   298  		errorValue := "Received Error"
   299  		abapErrorCode := "abapErrorCode"
   300  		abapErrorMessage := "abapErrorMessage"
   301  		bodyString := `{"error" : { "code" : "` + abapErrorCode + `", "message" : { "lang" : "en", "value" : "` + abapErrorMessage + `" } } }`
   302  		body := []byte(bodyString)
   303  
   304  		resp := http.Response{
   305  			Status:     "400 Bad Request",
   306  			StatusCode: 400,
   307  			Body:       io.NopCloser(bytes.NewReader(body)),
   308  		}
   309  		receivedErr := errors.New(errorValue)
   310  		message := "Custom Error Message"
   311  
   312  		err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{})
   313  		assert.EqualError(t, err, fmt.Sprintf("%s: %s - %s", receivedErr.Error(), abapErrorCode, abapErrorMessage))
   314  		log.Entry().Info(err.Error())
   315  	})
   316  
   317  	t.Run("Non JSON Error", func(t *testing.T) {
   318  
   319  		errorValue := "Received Error"
   320  		bodyString := `Error message`
   321  		body := []byte(bodyString)
   322  
   323  		resp := http.Response{
   324  			Status:     "400 Bad Request",
   325  			StatusCode: 400,
   326  			Body:       io.NopCloser(bytes.NewReader(body)),
   327  		}
   328  		receivedErr := errors.New(errorValue)
   329  		message := "Custom Error Message"
   330  
   331  		err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{})
   332  		assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
   333  		log.Entry().Info(err.Error())
   334  	})
   335  
   336  	t.Run("Different JSON Error", func(t *testing.T) {
   337  
   338  		errorValue := "Received Error"
   339  		bodyString := `{"abap" : { "key" : "value" } }`
   340  		body := []byte(bodyString)
   341  
   342  		resp := http.Response{
   343  			Status:     "400 Bad Request",
   344  			StatusCode: 400,
   345  			Body:       io.NopCloser(bytes.NewReader(body)),
   346  		}
   347  		receivedErr := errors.New(errorValue)
   348  		message := "Custom Error Message"
   349  
   350  		err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{})
   351  		assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
   352  		log.Entry().Info(err.Error())
   353  	})
   354  
   355  	t.Run("EOF Error", func(t *testing.T) {
   356  
   357  		message := "Custom Error Message"
   358  		errorValue := "Received Error EOF"
   359  		receivedErr := errors.New(errorValue)
   360  
   361  		_, hook := test.NewNullLogger()
   362  		log.RegisterHook(hook)
   363  
   364  		err := HandleHTTPError(nil, receivedErr, message, ConnectionDetailsHTTP{})
   365  
   366  		assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
   367  		assert.Equal(t, 5, len(hook.Entries), "Expected a different number of entries")
   368  		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")
   369  		hook.Reset()
   370  	})
   371  }