From 0bc36f1cf569a6bcebaea3045020653227f13766 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Thu, 6 Jul 2023 17:24:53 +0200 Subject: [PATCH] graph: Turn PatchEducationSchool test into test table --- .../pkg/service/v0/educationschools_test.go | 68 ++++++------------- 1 file changed, 22 insertions(+), 46 deletions(-) diff --git a/services/graph/pkg/service/v0/educationschools_test.go b/services/graph/pkg/service/v0/educationschools_test.go index 0486b6626d0..8936ee64350 100644 --- a/services/graph/pkg/service/v0/educationschools_test.go +++ b/services/graph/pkg/service/v0/educationschools_test.go @@ -288,58 +288,34 @@ var _ = Describe("Schools", func() { Expect(rr.Code).To(Equal(http.StatusCreated)) }) }) - Describe("PatchEducationSchool", func() { - It("handles invalid body", func() { - r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/education/schools/", bytes.NewBufferString("{invalid")) - rctx := chi.NewRouteContext() - rctx.URLParams.Add("schoolID", *newSchool.Id) - r = r.WithContext(context.WithValue(ctxpkg.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) - svc.PatchEducationSchool(rr, r) - Expect(rr.Code).To(Equal(http.StatusBadRequest)) - }) - - It("handles missing or empty school id", func() { - r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/education/schools", nil) - svc.PatchEducationSchool(rr, r) - - Expect(rr.Code).To(Equal(http.StatusBadRequest)) - - r = httptest.NewRequest(http.MethodPatch, "/graph/v1.0/education/schools", nil) - rctx := chi.NewRouteContext() - rctx.URLParams.Add("schoolID", "") - r = r.WithContext(context.WithValue(ctxpkg.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) - svc.PatchEducationSchool(rr, r) - - Expect(rr.Code).To(Equal(http.StatusBadRequest)) - }) - It("handles malformed school id", func() { - r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/education/schools", nil) - rctx := chi.NewRouteContext() - rctx.URLParams.Add("schoolID", "school%id") - r = r.WithContext(context.WithValue(ctxpkg.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) - svc.PatchEducationSchool(rr, r) + Describe("updating a School", func() { + schoolUpdate := libregraph.NewEducationSchool() + schoolUpdate.SetDisplayName("New School Name") + schoolUpdateJson, _ := json.Marshal(schoolUpdate) - Expect(rr.Code).To(Equal(http.StatusBadRequest)) - }) - It("updates the school", func() { - newSchool = libregraph.NewEducationSchool() - newSchool.SetDisplayName("New School Name") - newSchoolJson, err := json.Marshal(newSchool) - Expect(err).ToNot(HaveOccurred()) - identityEducationBackend.On("UpdateEducationSchool", mock.Anything, mock.Anything, mock.Anything).Return(newSchool, nil) - r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/education/schools/schoolid", bytes.NewBuffer(newSchoolJson)) - rctx := chi.NewRouteContext() - rctx.URLParams.Add("schoolID", "school-id") - r = r.WithContext(context.WithValue(ctxpkg.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) - - svc.PatchEducationSchool(rr, r) - - Expect(rr.Code).To(Equal(http.StatusOK)) + BeforeEach(func() { + identityEducationBackend.On("UpdateEducationSchool", mock.Anything, mock.Anything, mock.Anything).Return(schoolUpdate, nil) }) + DescribeTable("PatchEducationSchool", + func(schoolId string, body io.Reader, statusCode int) { + r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/education/schools/", body) + rctx := chi.NewRouteContext() + if schoolId != "" { + rctx.URLParams.Add("schoolID", schoolId) + } + r = r.WithContext(context.WithValue(ctxpkg.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) + svc.PatchEducationSchool(rr, r) + Expect(rr.Code).To(Equal(statusCode)) + }, + Entry("handles invalid body", "school-id", bytes.NewBufferString("{invalid"), http.StatusBadRequest), + Entry("handles missing or empty school id", "", bytes.NewBufferString(""), http.StatusBadRequest), + Entry("handles malformed school id", "school%id", bytes.NewBuffer(schoolUpdateJson), http.StatusBadRequest), + Entry("updates the school", "school-id", bytes.NewBuffer(schoolUpdateJson), http.StatusOK), + ) }) Describe("DeleteEducationSchool", func() {