From ef63ad583a4b37287d5f9b0d1aa683d324ebebc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Franke?= Date: Tue, 10 Jan 2023 10:29:57 +0100 Subject: [PATCH] Add test for new configurable patch limit. Also make test for old default match on the actual error message. --- services/graph/pkg/service/v0/groups_test.go | 41 +++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/services/graph/pkg/service/v0/groups_test.go b/services/graph/pkg/service/v0/groups_test.go index 02cbe9d71fc..8c620ef2b24 100644 --- a/services/graph/pkg/service/v0/groups_test.go +++ b/services/graph/pkg/service/v0/groups_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "io" + "io/ioutil" "net/http" "net/http/httptest" @@ -284,8 +285,10 @@ var _ = Describe("Groups", func() { It("fails when the number of users is exceeded - spec says 20 max", func() { updatedGroup := libregraph.NewGroup() updatedGroup.SetDisplayName("group1 updated") - updatedGroup.SetMembersodataBind([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", - "19", "20", "21"}) + updatedGroup.SetMembersodataBind([]string{ + "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", + "19", "20", "21", + }) updatedGroupJson, err := json.Marshal(updatedGroup) Expect(err).ToNot(HaveOccurred()) @@ -295,6 +298,40 @@ var _ = Describe("Groups", func() { r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) svc.PatchGroup(rr, r) + resp, err := ioutil.ReadAll(rr.Body) + Expect(err).ToNot(HaveOccurred()) + + Expect(string(resp)).To(ContainSubstring("Request is limited to 20")) + Expect(rr.Code).To(Equal(http.StatusBadRequest)) + }) + + It("fails when the number of users is exceeded when configured to 5", func() { + updatedGroup := libregraph.NewGroup() + updatedGroup.SetDisplayName("group1 updated") + updatedGroup.SetMembersodataBind([]string{ + "1", "2", "3", "4", "5", "6", + }) + updatedGroupJson, err := json.Marshal(updatedGroup) + Expect(err).ToNot(HaveOccurred()) + + cfg.API.GroupMembersPatchLimit = 5 + svc = service.NewService( + service.Config(cfg), + service.WithGatewayClient(gatewayClient), + service.EventsPublisher(&eventsPublisher), + service.WithIdentityBackend(identityBackend), + ) + + r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me/groups", bytes.NewBuffer(updatedGroupJson)) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("groupID", *newGroup.Id) + r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) + svc.PatchGroup(rr, r) + + resp, err := ioutil.ReadAll(rr.Body) + Expect(err).ToNot(HaveOccurred()) + + Expect(string(resp)).To(ContainSubstring("Request is limited to 5")) Expect(rr.Code).To(Equal(http.StatusBadRequest)) })