-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
- Loading branch information
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
services/graph/pkg/service/v0/approleassignments_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package svc_test | ||
|
||
import ( | ||
"bytes" | ||
"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/golang/protobuf/ptypes/empty" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/stretchr/testify/mock" | ||
|
||
libregraph "github.com/owncloud/libre-graph-api-go" | ||
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" | ||
) | ||
|
||
type assignmentList struct { | ||
Value []*libregraph.AppRoleAssignment | ||
} | ||
|
||
var _ = Describe("AppRoleAssignments", 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("ListAppRoleAssignments", func() { | ||
It("lists the appRoleAssignments", func() { | ||
user := &libregraph.User{ | ||
Id: libregraph.PtrString("user1"), | ||
} | ||
assignments := []*settingsmsg.UserRoleAssignment{ | ||
{ | ||
Id: "some-appRoleAssignment-ID", | ||
AccountUuid: user.GetId(), | ||
RoleId: "some-appRole-ID", | ||
}, | ||
} | ||
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListRoleAssignmentsResponse{Assignments: assignments}, nil) | ||
|
||
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users/user1/appRoleAssignments", nil) | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add("userID", user.GetId()) | ||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) | ||
svc.ListAppRoleAssignments(rr, r) | ||
|
||
Expect(rr.Code).To(Equal(http.StatusOK)) | ||
|
||
data, err := io.ReadAll(rr.Body) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
responseList := assignmentList{} | ||
err = json.Unmarshal(data, &responseList) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(len(responseList.Value)).To(Equal(1)) | ||
Expect(responseList.Value[0].GetId()).ToNot(BeEmpty()) | ||
Expect(responseList.Value[0].GetAppRoleId()).To(Equal("some-appRole-ID")) | ||
Expect(responseList.Value[0].GetPrincipalId()).To(Equal(user.GetId())) | ||
Expect(responseList.Value[0].GetResourceId()).To(Equal(cfg.Service.ApplicationID)) | ||
|
||
}) | ||
|
||
}) | ||
|
||
Describe("CreateAppRoleAssignment", func() { | ||
It("creates an appRoleAssignment", func() { | ||
user := &libregraph.User{ | ||
Id: libregraph.PtrString("user1"), | ||
} | ||
userRoleAssignment := &settingsmsg.UserRoleAssignment{ | ||
Id: "some-appRoleAssignment-ID", | ||
AccountUuid: user.GetId(), | ||
RoleId: "some-appRole-ID", | ||
} | ||
roleService.On("AssignRoleToUser", mock.Anything, mock.Anything, mock.Anything).Return(&settings.AssignRoleToUserResponse{Assignment: userRoleAssignment}, nil) | ||
|
||
ara := libregraph.NewAppRoleAssignmentWithDefaults() | ||
ara.SetAppRoleId("some-appRole-ID") | ||
ara.SetPrincipalId(user.GetId()) | ||
ara.SetResourceId(cfg.Service.ApplicationID) | ||
|
||
araJson, err := json.Marshal(ara) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users/user1/appRoleAssignments", bytes.NewBuffer(araJson)) | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add("userID", user.GetId()) | ||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) | ||
svc.CreateAppRoleAssignment(rr, r) | ||
|
||
Expect(rr.Code).To(Equal(http.StatusCreated)) | ||
|
||
data, err := io.ReadAll(rr.Body) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
assignment := libregraph.AppRoleAssignment{} | ||
err = json.Unmarshal(data, &assignment) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(assignment.GetId()).ToNot(BeEmpty()) | ||
Expect(assignment.GetAppRoleId()).To(Equal("some-appRole-ID")) | ||
Expect(assignment.GetPrincipalId()).To(Equal("user1")) | ||
Expect(assignment.GetResourceId()).To(Equal(cfg.Service.ApplicationID)) | ||
}) | ||
|
||
}) | ||
|
||
Describe("DeleteAppRoleAssignment", func() { | ||
It("deletes an appRoleAssignment", func() { | ||
user := &libregraph.User{ | ||
Id: libregraph.PtrString("user1"), | ||
} | ||
|
||
assignments := []*settingsmsg.UserRoleAssignment{ | ||
{ | ||
Id: "some-appRoleAssignment-ID", | ||
AccountUuid: user.GetId(), | ||
RoleId: "some-appRole-ID", | ||
}, | ||
} | ||
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListRoleAssignmentsResponse{Assignments: assignments}, nil) | ||
|
||
roleService.On("RemoveRoleFromUser", mock.Anything, mock.Anything, mock.Anything).Return(&empty.Empty{}, nil) | ||
|
||
ara := libregraph.NewAppRoleAssignmentWithDefaults() | ||
ara.SetAppRoleId("some-appRole-ID") | ||
ara.SetPrincipalId(user.GetId()) | ||
ara.SetResourceId(cfg.Service.ApplicationID) | ||
|
||
araJson, err := json.Marshal(ara) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users/user1/appRoleAssignments/some-appRoleAssignment-ID", bytes.NewBuffer(araJson)) | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add("userID", user.GetId()) | ||
rctx.URLParams.Add("appRoleAssignmentID", "some-appRoleAssignment-ID") | ||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) | ||
svc.DeleteAppRoleAssignment(rr, r) | ||
|
||
Expect(rr.Code).To(Equal(http.StatusNoContent)) | ||
|
||
}) | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters