NONRTRIC-946: Confirm ApiProvDomId for PUT equals path param
[nonrtric/plt/sme.git] / capifcore / internal / providermanagement / providermanagement_test.go
index de647fa..cbcf438 100644 (file)
@@ -49,6 +49,49 @@ var (
        funcIdAEF     = "AEF_id_rApp_as_AEF"
 )
 
+func TestFailedUpdateValidProviderWithNewFunction(t *testing.T) {
+       managerUnderTest, requestHandler := getEcho()
+
+       provider := getProvider()
+       provider.ApiProvDomId = &domainID
+       (*provider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
+       (*provider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
+       (*provider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
+       managerUnderTest.registeredProviders[domainID] = provider
+
+       // Modify the provider
+       updatedProvider := getProvider()
+
+       // For this test case, we do not set updatedProvider.ApiProvDomId, so that we can test for a 400 error below.
+       (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
+       (*updatedProvider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
+       (*updatedProvider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
+       newDomainInfo := "New domain info"
+       updatedProvider.ApiProvDomInfo = &newDomainInfo
+       newFunctionInfo := "New function info"
+       (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncInfo = &newFunctionInfo
+       newFuncInfoAEF := "new func as AEF"
+       testFuncs := *updatedProvider.ApiProvFuncs
+       testFuncs = append(testFuncs, provapi.APIProviderFunctionDetails{
+               ApiProvFuncInfo: &newFuncInfoAEF,
+               ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
+               RegInfo: provapi.RegistrationInformation{
+                       ApiProvPubKey: "key",
+               },
+       })
+       updatedProvider.ApiProvFuncs = &testFuncs
+
+       result := testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(updatedProvider).Go(t, requestHandler)
+       assert.Equal(t, http.StatusBadRequest, result.Code())
+
+       var resultError common29122.ProblemDetails
+       err := result.UnmarshalJsonToObject(&resultError)
+       assert.NoError(t, err, "error unmarshaling response")
+
+       assert.Contains(t, *resultError.Cause, "APIProviderEnrolmentDetails ApiProvDomId doesn't match path parameter")
+       assert.False(t, managerUnderTest.IsFunctionRegistered("AEF_id_new_func_as_AEF"))
+}
+
 func TestRegisterValidProvider(t *testing.T) {
        managerUnderTest, requestHandler := getEcho()
 
@@ -68,6 +111,15 @@ func TestRegisterValidProvider(t *testing.T) {
        assert.Empty(t, resultProvider.FailReason)
        assert.Equal(t, "http://example.com/registrations/"+*resultProvider.ApiProvDomId, result.Recorder.Header().Get(echo.HeaderLocation))
        assert.True(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
+
+       // Register same provider again should result in Forbidden
+       result = testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
+       var errorObj common29122.ProblemDetails
+       assert.Equal(t, http.StatusForbidden, result.Code())
+       err = result.UnmarshalBodyToObject(&errorObj)
+       assert.NoError(t, err, "error unmarshaling response")
+       assert.Equal(t, http.StatusForbidden, *errorObj.Status)
+       assert.Contains(t, *errorObj.Cause, "already registered")
 }
 
 func TestUpdateValidProviderWithNewFunction(t *testing.T) {
@@ -170,6 +222,7 @@ func TestUpdateMissingFunction(t *testing.T) {
        assert.Equal(t, http.StatusBadRequest, result.Code())
        err := result.UnmarshalBodyToObject(&errorObj)
        assert.NoError(t, err, "error unmarshaling response")
+       assert.Equal(t, http.StatusBadRequest, *errorObj.Status)
        assert.Contains(t, *errorObj.Cause, funcIdAPF)
        assert.Contains(t, *errorObj.Cause, "not registered")
 }
@@ -200,9 +253,8 @@ func TestProviderHandlingValidation(t *testing.T) {
        var problemDetails common29122.ProblemDetails
        err := result.UnmarshalBodyToObject(&problemDetails)
        assert.NoError(t, err, "error unmarshaling response")
-       badRequest := http.StatusBadRequest
-       assert.Equal(t, &badRequest, problemDetails.Status)
-       assert.Contains(t, *problemDetails.Cause, "Provider not valid")
+       assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
+       assert.Contains(t, *problemDetails.Cause, "missing")
        assert.Contains(t, *problemDetails.Cause, "regSec")
 }