X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Finvokermanagement%2Finvokermanagement_test.go;h=b46e925a2bea3479726ccfc90c7609c48277eb89;hb=4974b9d1c7256e90cb206b327b0c81f7364beeab;hp=4365eb320283fe3a11e94021f46dd15a8efc41e4;hpb=f62685e5a2187fb58c7f27cdd1f14dd2c152880d;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/invokermanagement/invokermanagement_test.go b/capifcore/internal/invokermanagement/invokermanagement_test.go index 4365eb3..b46e925 100644 --- a/capifcore/internal/invokermanagement/invokermanagement_test.go +++ b/capifcore/internal/invokermanagement/invokermanagement_test.go @@ -29,12 +29,14 @@ import ( "oransc.org/nonrtric/capifcore/internal/eventsapi" "oransc.org/nonrtric/capifcore/internal/invokermanagementapi" + "oransc.org/nonrtric/capifcore/internal/keycloak" "github.com/labstack/echo/v4" "oransc.org/nonrtric/capifcore/internal/common29122" "oransc.org/nonrtric/capifcore/internal/publishserviceapi" + keycloackmocks "oransc.org/nonrtric/capifcore/internal/keycloak/mocks" "oransc.org/nonrtric/capifcore/internal/publishservice" publishmocks "oransc.org/nonrtric/capifcore/internal/publishservice/mocks" @@ -42,6 +44,7 @@ import ( "github.com/deepmap/oapi-codegen/pkg/testutil" echomiddleware "github.com/labstack/echo/v4/middleware" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) func TestOnboardInvoker(t *testing.T) { @@ -55,11 +58,20 @@ func TestOnboardInvoker(t *testing.T) { AefProfiles: &aefProfiles, }, } + + invokerInfo := "invoker a" + wantedInvokerSecret := "onboarding_secret_" + strings.Replace(invokerInfo, " ", "_", 1) + var client keycloak.Client + client.Secret = &wantedInvokerSecret publishRegisterMock := publishmocks.PublishRegister{} publishRegisterMock.On("GetAllPublishedServices").Return(publishedServices) - invokerUnderTest, eventChannel, requestHandler := getEcho(&publishRegisterMock) - invokerInfo := "invoker a" + accessMgmMock := keycloackmocks.AccessManagement{} + accessMgmMock.On("AddClient", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil) + accessMgmMock.On("GetClientRepresentation", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(&client, nil) + + invokerUnderTest, eventChannel, requestHandler := getEcho(&publishRegisterMock, &accessMgmMock) + newInvoker := getInvoker(invokerInfo) // Onboard a valid invoker @@ -73,7 +85,7 @@ func TestOnboardInvoker(t *testing.T) { assert.Equal(t, wantedInvokerId, *resultInvoker.ApiInvokerId) assert.Equal(t, newInvoker.NotificationDestination, resultInvoker.NotificationDestination) assert.Equal(t, newInvoker.OnboardingInformation.ApiInvokerPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey) - wantedInvokerSecret := "onboarding_secret_" + strings.Replace(invokerInfo, " ", "_", 1) + assert.Equal(t, wantedInvokerSecret, *resultInvoker.OnboardingInformation.OnboardingSecret) assert.Equal(t, "http://example.com/onboardedInvokers/"+*resultInvoker.ApiInvokerId, result.Recorder.Header().Get(echo.HeaderLocation)) assert.True(t, invokerUnderTest.IsInvokerRegistered(wantedInvokerId)) @@ -128,7 +140,7 @@ func TestOnboardInvoker(t *testing.T) { } func TestDeleteInvoker(t *testing.T) { - invokerUnderTest, eventChannel, requestHandler := getEcho(nil) + invokerUnderTest, eventChannel, requestHandler := getEcho(nil, nil) invokerId := "invokerId" newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ @@ -154,10 +166,33 @@ func TestDeleteInvoker(t *testing.T) { } } +func TestFailedUpdateInvoker(t *testing.T) { + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{}) + serviceUnderTest, _, requestHandler := getEcho(&publishRegisterMock, nil) + + invokerInfo := "invoker a" + invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1) + + // Attempt to update with an invoker without the ApiInvokerId provided in the parameter body. We should get 400 with problem details. + invalidInvoker := getInvoker(invokerInfo) + serviceUnderTest.onboardedInvokers[invokerId] = invalidInvoker + + result := testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler) + assert.Equal(t, http.StatusBadRequest, result.Code()) + + var problemDetails common29122.ProblemDetails + err := result.UnmarshalBodyToObject(&problemDetails) + assert.NoError(t, err, "error unmarshaling response") + assert.Equal(t, http.StatusBadRequest, *problemDetails.Status) + + assert.Contains(t, *problemDetails.Cause, "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter") +} + func TestUpdateInvoker(t *testing.T) { publishRegisterMock := publishmocks.PublishRegister{} publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{}) - serviceUnderTest, _, requestHandler := getEcho(&publishRegisterMock) + serviceUnderTest, _, requestHandler := getEcho(&publishRegisterMock, nil) invokerId := "invokerId" invoker := invokermanagementapi.APIInvokerEnrolmentDetails{ @@ -224,8 +259,7 @@ func TestUpdateInvoker(t *testing.T) { err = result.UnmarshalBodyToObject(&problemDetails) assert.NoError(t, err, "error unmarshaling response") assert.Equal(t, http.StatusBadRequest, *problemDetails.Status) - assert.Contains(t, *problemDetails.Cause, "not matching") - assert.Contains(t, *problemDetails.Cause, "ApiInvokerId") + assert.Contains(t, *problemDetails.Cause, "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter") // Update an invoker that has not been onboarded, should get 404 with problem details missingId := "1" @@ -261,7 +295,7 @@ func TestGetInvokerApiList(t *testing.T) { }) publishRegisterMock := publishmocks.PublishRegister{} publishRegisterMock.On("GetAllPublishedServices").Return(apiList) - invokerUnderTest, _, _ := getEcho(&publishRegisterMock) + invokerUnderTest, _, _ := getEcho(&publishRegisterMock, nil) invokerInfo := "invoker a" newInvoker := getInvoker(invokerInfo) @@ -280,7 +314,7 @@ func TestGetInvokerApiList(t *testing.T) { assert.Equal(t, apiId, *(*wantedApiList)[0].ApiId) } -func getEcho(publishRegister publishservice.PublishRegister) (*InvokerManager, chan eventsapi.EventNotification, *echo.Echo) { +func getEcho(publishRegister publishservice.PublishRegister, keycloakMgm keycloak.AccessManagement) (*InvokerManager, chan eventsapi.EventNotification, *echo.Echo) { swagger, err := invokermanagementapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err) @@ -290,7 +324,7 @@ func getEcho(publishRegister publishservice.PublishRegister) (*InvokerManager, c swagger.Servers = nil eventChannel := make(chan eventsapi.EventNotification) - im := NewInvokerManager(publishRegister, eventChannel) + im := NewInvokerManager(publishRegister, keycloakMgm, eventChannel) e := echo.New() e.Use(echomiddleware.Logger())