Skip to content

Commit

Permalink
graph: Turn PatchEducationSchool test into test table
Browse files Browse the repository at this point in the history
  • Loading branch information
rhafer committed Jul 12, 2023
1 parent 5304184 commit 0bc36f1
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions services/graph/pkg/service/v0/educationschools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 0bc36f1

Please sign in to comment.