From 4e2b0618206193e13ba73831b6d6cb866e1d1dae Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Tue, 31 Jan 2023 11:11:38 +0100 Subject: [PATCH] Improve validation for invoker management Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: Ie0393d0139a0467be6a0eb242afba011c51b6052 --- capifcore/internal/invokermanagement/invokermanagement_test.go | 10 +++++----- capifcore/internal/invokermanagementapi/typevalidation.go | 6 ++++++ capifcore/internal/invokermanagementapi/typevalidation_test.go | 9 ++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/capifcore/internal/invokermanagement/invokermanagement_test.go b/capifcore/internal/invokermanagement/invokermanagement_test.go index d3c386c..e5fa008 100644 --- a/capifcore/internal/invokermanagement/invokermanagement_test.go +++ b/capifcore/internal/invokermanagement/invokermanagement_test.go @@ -106,7 +106,7 @@ func TestOnboardInvoker(t *testing.T) { // Onboard an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details invalidInvoker = invokermanagementapi.APIInvokerEnrolmentDetails{ - NotificationDestination: "url", + NotificationDestination: "http://golang.cafe/", } result = testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(invalidInvoker).Go(t, requestHandler) @@ -154,7 +154,7 @@ func TestUpdateInvoker(t *testing.T) { invokerId := "invokerId" invoker := invokermanagementapi.APIInvokerEnrolmentDetails{ ApiInvokerId: &invokerId, - NotificationDestination: "url", + NotificationDestination: "http://golang.cafe/", OnboardingInformation: invokermanagementapi.OnboardingInformation{ ApiInvokerPublicKey: "key", }, @@ -162,7 +162,7 @@ func TestUpdateInvoker(t *testing.T) { serviceUnderTest.onboardedInvokers[invokerId] = invoker // Update the invoker with valid invoker, should return 200 with updated invoker details - newNotifURL := "newUrl" + newNotifURL := "http://golang.org/" invoker.NotificationDestination = common29122.Uri(newNotifURL) newPublicKey := "newPublicKey" invoker.OnboardingInformation.ApiInvokerPublicKey = newPublicKey @@ -196,7 +196,7 @@ func TestUpdateInvoker(t *testing.T) { assert.Contains(t, *problemDetails.Cause, "NotificationDestination") // Update with an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details - invalidInvoker.NotificationDestination = "url" + invalidInvoker.NotificationDestination = "http://golang.org/" invalidInvoker.OnboardingInformation = invokermanagementapi.OnboardingInformation{} result = testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler) @@ -312,7 +312,7 @@ func getAefProfile(aefId string) publishserviceapi.AefProfile { func getInvoker(invokerInfo string) invokermanagementapi.APIInvokerEnrolmentDetails { newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ ApiInvokerInformation: &invokerInfo, - NotificationDestination: "url", + NotificationDestination: "http://golang.cafe/", OnboardingInformation: invokermanagementapi.OnboardingInformation{ ApiInvokerPublicKey: "key", }, diff --git a/capifcore/internal/invokermanagementapi/typevalidation.go b/capifcore/internal/invokermanagementapi/typevalidation.go index b12ddfb..be29481 100644 --- a/capifcore/internal/invokermanagementapi/typevalidation.go +++ b/capifcore/internal/invokermanagementapi/typevalidation.go @@ -22,6 +22,8 @@ package invokermanagementapi import ( "errors" + "fmt" + "net/url" ) func (ied *APIInvokerEnrolmentDetails) Validate() error { @@ -29,6 +31,10 @@ func (ied *APIInvokerEnrolmentDetails) Validate() error { return errors.New("APIInvokerEnrolmentDetails missing required NotificationDestination") } + if _, err := url.ParseRequestURI(string(ied.NotificationDestination)); err != nil { + return fmt.Errorf("APIInvokerEnrolmentDetails has invalid NotificationDestination, err=%s", err) + } + if ied.OnboardingInformation.ApiInvokerPublicKey == "" { return errors.New("APIInvokerEnrolmentDetails missing required OnboardingInformation.ApiInvokerPublicKey") } diff --git a/capifcore/internal/invokermanagementapi/typevalidation_test.go b/capifcore/internal/invokermanagementapi/typevalidation_test.go index 60e4ee5..58cd655 100644 --- a/capifcore/internal/invokermanagementapi/typevalidation_test.go +++ b/capifcore/internal/invokermanagementapi/typevalidation_test.go @@ -35,7 +35,14 @@ func TestValidateInvoker(t *testing.T) { assert.Contains(t, err.Error(), "NotificationDestination") } - invokerUnderTest.NotificationDestination = "destination" + invokerUnderTest.NotificationDestination = "invalid dest" + err = invokerUnderTest.Validate() + if assert.Error(t, err) { + assert.Contains(t, err.Error(), "invalid") + assert.Contains(t, err.Error(), "NotificationDestination") + } + + invokerUnderTest.NotificationDestination = "http://golang.cafe/" err = invokerUnderTest.Validate() if assert.Error(t, err) { assert.Contains(t, err.Error(), "missing") -- 2.16.6