Skip to content

Commit

Permalink
test getapplication
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Jan 6, 2023
1 parent 7d15d87 commit 0341a7b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 5 deletions.
7 changes: 2 additions & 5 deletions services/graph/pkg/service/v0/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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())
Expand All @@ -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)
}
110 changes: 110 additions & 0 deletions services/graph/pkg/service/v0/application_test.go
Original file line number Diff line number Diff line change
@@ -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"))

})

})

})
5 changes: 5 additions & 0 deletions services/graph/pkg/service/v0/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions services/graph/pkg/service/v0/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions services/graph/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions services/graph/pkg/service/v0/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0341a7b

Please sign in to comment.