diff --git a/controllers/rest/controller_standard_test.go b/controllers/rest/controller_standard_test.go index 9d862401..77c0125c 100644 --- a/controllers/rest/controller_standard_test.go +++ b/controllers/rest/controller_standard_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "io/ioutil" "mime/multipart" @@ -1610,4 +1611,111 @@ func TestStandardControllers_DeleteID(t *testing.T) { t.Errorf("expected response code to be %d, got %d", 200, response.StatusCode) } }) + t.Run("basic delete based on simple content type id parameter in path. (No weosID)", func(t *testing.T) { + mockEntity1 := &model.ContentEntity{} + mockEntity1.Property = mockBlog + + mockInterface1 := map[string]interface{}{"title": "Test Blog", "description": "testing description", "id": "12", "sequence_no": "1"} + + eventMock1 := &EventRepositoryMock{ + GetAggregateSequenceNumberFunc: func(ID string) (int64, error) { + return 2, nil + }, + } + + projection1 := &ProjectionMock{ + GetByKeyFunc: func(ctxt context.Context, entityFactory model.EntityFactory, identifiers map[string]interface{}) (map[string]interface{}, error) { + return mockInterface1, nil + }, + GetByEntityIDFunc: func(ctxt context.Context, entityFactory model.EntityFactory, id string) (map[string]interface{}, error) { + return mockInterface1, nil + }, + GetContentEntityFunc: func(ctx context.Context, entityFactory model.EntityFactory, weosID string) (*model.ContentEntity, error) { + return mockEntity1, nil + }, + } + + paramName := "id" + + accountID := "Delete Blog" + path := swagger.Paths.Find("/blogs/:" + paramName) + entityFactory := &EntityFactoryMock{ + NameFunc: func() string { + return "Blog" + }, + SchemaFunc: func() *openapi3.Schema { + return swagger.Components.Schemas["Blog"].Value + }, + } + resp := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodDelete, "/blogs/12", nil) + req.Header.Set(weoscontext.HeaderXAccountID, accountID) + mw := rest.Context(restAPI, projection, dispatcher, eventMock, entityFactory, path, path.Delete) + deleteMw := rest.DeleteMiddleware(restAPI, projection1, dispatcher, eventMock1, entityFactory, path, path.Delete) + controller := rest.DeleteController(restAPI, projection1, dispatcher, eventMock1, entityFactory) + e.DELETE("/blogs/:"+paramName, controller, mw, deleteMw) + e.ServeHTTP(resp, req) + + response := resp.Result() + defer response.Body.Close() + + if response.StatusCode != 404 { + t.Errorf("expected response code to be %d, got %d", 404, response.StatusCode) + } + }) + + t.Run("basic delete based on simple content type id parameter in path. (No weosID)", func(t *testing.T) { + mockEntity1 := &model.ContentEntity{} + mockEntity1.Property = mockBlog + + mockInterface1 := map[string]interface{}{"title": "Test Blog", "description": "testing description", "weos_id": "123456qwerty", "id": "12", "sequence_no": "1"} + + eventMock1 := &EventRepositoryMock{ + GetAggregateSequenceNumberFunc: func(ID string) (int64, error) { + return 2, nil + }, + } + + err1 := fmt.Errorf("this is an error") + + projection1 := &ProjectionMock{ + GetByKeyFunc: func(ctxt context.Context, entityFactory model.EntityFactory, identifiers map[string]interface{}) (map[string]interface{}, error) { + return nil, err1 + }, + GetByEntityIDFunc: func(ctxt context.Context, entityFactory model.EntityFactory, id string) (map[string]interface{}, error) { + return mockInterface1, nil + }, + GetContentEntityFunc: func(ctx context.Context, entityFactory model.EntityFactory, weosID string) (*model.ContentEntity, error) { + return mockEntity1, nil + }, + } + + paramName := "id" + + accountID := "Delete Blog" + path := swagger.Paths.Find("/blogs/:" + paramName) + entityFactory := &EntityFactoryMock{ + NameFunc: func() string { + return "Blog" + }, + SchemaFunc: func() *openapi3.Schema { + return swagger.Components.Schemas["Blog"].Value + }, + } + resp := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodDelete, "/blogs/12", nil) + req.Header.Set(weoscontext.HeaderXAccountID, accountID) + mw := rest.Context(restAPI, projection, dispatcher, eventMock, entityFactory, path, path.Delete) + deleteMw := rest.DeleteMiddleware(restAPI, projection1, dispatcher, eventMock1, entityFactory, path, path.Delete) + controller := rest.DeleteController(restAPI, projection1, dispatcher, eventMock1, entityFactory) + e.DELETE("/blogs/:"+paramName, controller, mw, deleteMw) + e.ServeHTTP(resp, req) + + response := resp.Result() + defer response.Body.Close() + + if response.StatusCode != 500 { + t.Errorf("expected response code to be %d, got %d", 404, response.StatusCode) + } + }) } diff --git a/controllers/rest/operation_initializers_test.go b/controllers/rest/operation_initializers_test.go index 7cffc4f0..23c47b58 100644 --- a/controllers/rest/operation_initializers_test.go +++ b/controllers/rest/operation_initializers_test.go @@ -163,6 +163,7 @@ func TestStandardInitializer(t *testing.T) { api.RegisterController("ListController", rest.ListController) api.RegisterController("UpdateController", rest.UpdateController) api.RegisterController("ViewController", rest.ViewController) + api.RegisterController("DeleteController", rest.DeleteController) t.Run("attach standard create", func(t *testing.T) { ctxt, err := rest.StandardInitializer(baseCtxt, api, "/blogs", http.MethodPost, api.Swagger, api.Swagger.Paths["/blogs"], api.Swagger.Paths["/blogs"].Post) if err != nil { @@ -215,4 +216,14 @@ func TestStandardInitializer(t *testing.T) { t.Fatalf("expected controller to be in the context") } }) + t.Run("attach standard delete", func(t *testing.T) { + ctxt, err := rest.StandardInitializer(baseCtxt, api, "/blogs/{id}", http.MethodDelete, api.Swagger, api.Swagger.Paths["/blogs/{id}"], api.Swagger.Paths["/blogs/{id}"].Delete) + if err != nil { + t.Fatalf("unexpected error loading api '%s'", err) + } + controller := rest.GetOperationController(ctxt) + if controller == nil { + t.Fatalf("expected controller to be in the context") + } + }) }