From 1df02b49393cd58105d62fc4271cb32319c77da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 6 Jan 2023 15:36:31 +0000 Subject: [PATCH] test getapplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- services/graph/pkg/service/v0/application.go | 7 +- .../graph/pkg/service/v0/application_test.go | 110 ++++++++++++++++++ services/graph/pkg/service/v0/instrument.go | 5 + services/graph/pkg/service/v0/logging.go | 5 + services/graph/pkg/service/v0/service.go | 3 + services/graph/pkg/service/v0/tracing.go | 5 + 6 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 services/graph/pkg/service/v0/application_test.go diff --git a/services/graph/pkg/service/v0/application.go b/services/graph/pkg/service/v0/application.go index 570550eb5cc..326649bcdd0 100644 --- a/services/graph/pkg/service/v0/application.go +++ b/services/graph/pkg/service/v0/application.go @@ -7,7 +7,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/render" libregraph "github.com/owncloud/libre-graph-api-go" - "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode" ) @@ -24,9 +23,7 @@ func (g Graph) GetApplication(w http.ResponseWriter, r *http.Request) { return } - s := settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient()) - - lbr, err := s.ListRoles(r.Context(), &settingssvc.ListBundlesRequest{}) + lbr, err := g.roleService.ListRoles(r.Context(), &settingssvc.ListBundlesRequest{}) if err != nil { logger.Error().Err(err).Msg("could not list roles: transport error") errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) @@ -45,5 +42,5 @@ func (g Graph) GetApplication(w http.ResponseWriter, r *http.Request) { application.SetAppRoles(roles) render.Status(r, http.StatusOK) - render.JSON(w, r, &ListResponse{Value: application}) + render.JSON(w, r, application) } diff --git a/services/graph/pkg/service/v0/application_test.go b/services/graph/pkg/service/v0/application_test.go new file mode 100644 index 00000000000..9c2c4b66fc2 --- /dev/null +++ b/services/graph/pkg/service/v0/application_test.go @@ -0,0 +1,110 @@ +package svc_test + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + + userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + revactx "github.com/cs3org/reva/v2/pkg/ctx" + "github.com/go-chi/chi/v5" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + libregraph "github.com/owncloud/libre-graph-api-go" + "github.com/stretchr/testify/mock" + + ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" + "github.com/owncloud/ocis/v2/ocis-pkg/shared" + settingsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/settings/v0" + settings "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" + "github.com/owncloud/ocis/v2/services/graph/mocks" + "github.com/owncloud/ocis/v2/services/graph/pkg/config" + "github.com/owncloud/ocis/v2/services/graph/pkg/config/defaults" + identitymocks "github.com/owncloud/ocis/v2/services/graph/pkg/identity/mocks" + service "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0" +) + +var _ = Describe("Applications", func() { + var ( + svc service.Service + ctx context.Context + cfg *config.Config + gatewayClient *mocks.GatewayClient + eventsPublisher mocks.Publisher + roleService *mocks.RoleService + identityBackend *identitymocks.Backend + + rr *httptest.ResponseRecorder + + currentUser = &userv1beta1.User{ + Id: &userv1beta1.UserId{ + OpaqueId: "user", + }, + } + ) + + BeforeEach(func() { + eventsPublisher.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + identityBackend = &identitymocks.Backend{} + roleService = &mocks.RoleService{} + gatewayClient = &mocks.GatewayClient{} + + rr = httptest.NewRecorder() + ctx = context.Background() + + cfg = defaults.FullDefaultConfig() + cfg.Identity.LDAP.CACert = "" // skip the startup checks, we don't use LDAP at all in this tests + cfg.TokenManager.JWTSecret = "loremipsum" + cfg.Commons = &shared.Commons{} + cfg.GRPCClientTLS = &shared.GRPCClientTLS{} + cfg.Service.ApplicationID = "some-application-ID" + + _ = ogrpc.Configure(ogrpc.GetClientOptions(cfg.GRPCClientTLS)...) + svc = service.NewService( + service.Config(cfg), + service.WithGatewayClient(gatewayClient), + service.EventsPublisher(&eventsPublisher), + service.WithIdentityBackend(identityBackend), + service.WithRoleService(roleService), + ) + }) + + Describe("GetApplication", func() { + It("gets the application with appRoles", func() { + roleService.On("ListRoles", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListBundlesResponse{ + Bundles: []*settingsmsg.Bundle{ + { + Id: "some-appRole-ID", + Type: settingsmsg.Bundle_TYPE_ROLE, + DisplayName: "A human readable name for a role", + }, + }, + }, nil) + + r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/applications/some-application-ID", nil) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("applicationID", cfg.Service.ApplicationID) + r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) + svc.GetApplication(rr, r) + + Expect(rr.Code).To(Equal(http.StatusOK)) + + data, err := io.ReadAll(rr.Body) + Expect(err).ToNot(HaveOccurred()) + + application := libregraph.Application{} + err = json.Unmarshal(data, &application) + Expect(err).ToNot(HaveOccurred()) + Expect(application.Id).To(Equal(cfg.Service.ApplicationID)) + Expect(len(application.GetAppRoles())).To(Equal(1)) + Expect(application.GetAppRoles()[0].GetId()).To(Equal("some-appRole-ID")) + Expect(application.GetAppRoles()[0].GetDisplayName()).To(Equal("A human readable name for a role")) + + }) + + }) + +}) diff --git a/services/graph/pkg/service/v0/instrument.go b/services/graph/pkg/service/v0/instrument.go index cecf3c03fb2..e1e9cdf581d 100644 --- a/services/graph/pkg/service/v0/instrument.go +++ b/services/graph/pkg/service/v0/instrument.go @@ -24,6 +24,11 @@ func (i instrument) ServeHTTP(w http.ResponseWriter, r *http.Request) { i.next.ServeHTTP(w, r) } +// GetApplication implements the Service interface. +func (i instrument) GetApplication(w http.ResponseWriter, r *http.Request) { + i.next.GetMe(w, r) +} + // GetMe implements the Service interface. func (i instrument) GetMe(w http.ResponseWriter, r *http.Request) { i.next.GetMe(w, r) diff --git a/services/graph/pkg/service/v0/logging.go b/services/graph/pkg/service/v0/logging.go index acd077adb4a..4bd2bd97be9 100644 --- a/services/graph/pkg/service/v0/logging.go +++ b/services/graph/pkg/service/v0/logging.go @@ -24,6 +24,11 @@ func (l logging) ServeHTTP(w http.ResponseWriter, r *http.Request) { l.next.ServeHTTP(w, r) } +// GetApplication implements the Service interface. +func (l logging) GetApplication(w http.ResponseWriter, r *http.Request) { + l.next.GetMe(w, r) +} + // GetMe implements the Service interface. func (l logging) GetMe(w http.ResponseWriter, r *http.Request) { l.next.GetMe(w, r) diff --git a/services/graph/pkg/service/v0/service.go b/services/graph/pkg/service/v0/service.go index bf8582a39f6..a42428a0f94 100644 --- a/services/graph/pkg/service/v0/service.go +++ b/services/graph/pkg/service/v0/service.go @@ -30,6 +30,9 @@ const ( // Service defines the service handlers. type Service interface { ServeHTTP(http.ResponseWriter, *http.Request) + + GetApplication(http.ResponseWriter, *http.Request) + GetMe(http.ResponseWriter, *http.Request) GetUsers(http.ResponseWriter, *http.Request) GetUser(http.ResponseWriter, *http.Request) diff --git a/services/graph/pkg/service/v0/tracing.go b/services/graph/pkg/service/v0/tracing.go index bd6f197e46d..4790052b47f 100644 --- a/services/graph/pkg/service/v0/tracing.go +++ b/services/graph/pkg/service/v0/tracing.go @@ -20,6 +20,11 @@ func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) { t.next.ServeHTTP(w, r) } +// GetApplication implements the Service interface. +func (t tracing) GetApplication(w http.ResponseWriter, r *http.Request) { + t.next.GetMe(w, r) +} + // GetMe implements the Service interface. func (t tracing) GetMe(w http.ResponseWriter, r *http.Request) { t.next.GetMe(w, r)