Skip to content

Commit

Permalink
MG-2457 - Update SDK tests (#2539)
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Gateru <[email protected]>
Signed-off-by: Arvindh <[email protected]>
Co-authored-by: Arvindh <[email protected]>
  • Loading branch information
2 people authored and dborovcanin committed Dec 2, 2024
1 parent 1d9f1c7 commit 4daa02b
Show file tree
Hide file tree
Showing 61 changed files with 858 additions and 3,841 deletions.
2 changes: 2 additions & 0 deletions auth/api/grpc/auth/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func startGRPCServer(svc auth.Service, port int) *grpc.Server {

func TestIdentify(t *testing.T) {
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
defer conn.Close()
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
grpcClient := grpcapi.NewAuthClient(conn, time.Second)

Expand Down Expand Up @@ -107,6 +108,7 @@ func TestIdentify(t *testing.T) {

func TestAuthorize(t *testing.T) {
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
defer conn.Close()
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
grpcClient := grpcapi.NewAuthClient(conn, time.Second)

Expand Down
4 changes: 3 additions & 1 deletion auth/api/grpc/token/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

const (
port = 8081
port = 8082
secret = "secret"
email = "[email protected]"
id = "testID"
Expand Down Expand Up @@ -63,6 +63,7 @@ func startGRPCServer(svc auth.Service, port int) *grpc.Server {

func TestIssue(t *testing.T) {
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
defer conn.Close()
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
grpcClient := grpcapi.NewTokenClient(conn, time.Second)

Expand Down Expand Up @@ -126,6 +127,7 @@ func TestIssue(t *testing.T) {

func TestRefresh(t *testing.T) {
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
defer conn.Close()
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
grpcClient := grpcapi.NewTokenClient(conn, time.Second)

Expand Down
25 changes: 6 additions & 19 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ var (
// ErrExpiry indicates that the token is expired.
ErrExpiry = errors.New("token is expired")

errIssueUser = errors.New("failed to issue new login key")
errIssueTmp = errors.New("failed to issue new temporary key")
errRevoke = errors.New("failed to remove key")
errRetrieve = errors.New("failed to retrieve key data")
errIdentify = errors.New("failed to validate token")
errPlatform = errors.New("invalid platform id")
errCreateDomainPolicy = errors.New("failed to create domain policy")
errAddPolicies = errors.New("failed to add policies")
errRemovePolicies = errors.New("failed to remove the policies")
errRollbackPolicy = errors.New("failed to rollback policy")
errRemoveLocalPolicy = errors.New("failed to remove from local policy copy")
errRemovePolicyEngine = errors.New("failed to remove from policy engine")
errIssueUser = errors.New("failed to issue new login key")
errIssueTmp = errors.New("failed to issue new temporary key")
errRevoke = errors.New("failed to remove key")
errRetrieve = errors.New("failed to retrieve key data")
errIdentify = errors.New("failed to validate token")
errPlatform = errors.New("invalid platform id")
)

// Authz represents a authorization service. It exposes
Expand Down Expand Up @@ -229,13 +223,6 @@ func (svc service) checkDomain(ctx context.Context, subjectType, subject, domain
return svcerr.ErrDomainAuthorization
}

// ToDo: Add domain status in spiceDB like with new relation called status

// d, err := svc.domains.RetrieveByID(ctx, domainID)
// if err != nil {
// return errors.Wrap(svcerr.ErrViewEntity, err)
// }

return nil
}

Expand Down
17 changes: 5 additions & 12 deletions auth/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ const (
)

var (
errIssueUser = errors.New("failed to issue new login key")
errCreateDomainPolicy = errors.New("failed to create domain policy")
errRetrieve = errors.New("failed to retrieve key data")
ErrExpiry = errors.New("token is expired")
errRollbackPolicy = errors.New("failed to rollback policy")
errAddPolicies = errors.New("failed to add policies")
errPlatform = errors.New("invalid platform id")
inValidToken = "invalid"
inValid = "invalid"
valid = "valid"
userID = testsutil.GenerateUUID(&testing.T{})
domainID = testsutil.GenerateUUID(&testing.T{})
errIssueUser = errors.New("failed to issue new login key")
ErrExpiry = errors.New("token is expired")
inValidToken = "invalid"
userID = testsutil.GenerateUUID(&testing.T{})
domainID = testsutil.GenerateUUID(&testing.T{})
)

var (
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func addEndpoint(svc bootstrap.Service) endpoint.Endpoint {
config := bootstrap.Config{
ClientID: req.ClientID,
ExternalID: req.ExternalID,
ExternalKey: req.ExternalSecret,
ExternalKey: req.ExternalKey,
Channels: channels,
Name: req.Name,
ClientCert: req.ClientCert,
Expand Down
28 changes: 14 additions & 14 deletions bootstrap/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ var (
addClientSecret = testsutil.GenerateUUID(&testing.T{})
addReq = struct {
ClientID string `json:"client_id"`
ClinetSecret string `json:"client_secret"`
ClientSecret string `json:"client_secret"`
ExternalID string `json:"external_id"`
ExternalKey string `json:"external_key"`
Channels []string `json:"channels"`
Name string `json:"name"`
Content string `json:"content"`
}{
ClientID: addClientID,
ClinetSecret: addClientSecret,
ClientSecret: addClientSecret,
ExternalID: addExternalID,
ExternalKey: addExternalKey,
Channels: []string{"1"},
Expand All @@ -74,19 +74,19 @@ var (
}

updateReq = struct {
Channels []string `json:"channels,omitempty"`
Content string `json:"content,omitempty"`
State bootstrap.State `json:"state,omitempty"`
ClientCert string `json:"client_cert,omitempty"`
ClientKey string `json:"client_secret,omitempty"`
CACert string `json:"ca_cert,omitempty"`
Channels []string `json:"channels,omitempty"`
Content string `json:"content,omitempty"`
State bootstrap.State `json:"state,omitempty"`
ClientCert string `json:"client_cert,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
CACert string `json:"ca_cert,omitempty"`
}{
Channels: []string{"1"},
Content: "config update",
State: 1,
ClientCert: "newcert",
ClientKey: "newkey",
CACert: "newca",
Channels: []string{"1"},
Content: "config update",
State: 1,
ClientCert: "newcert",
ClientSecret: "newkey",
CACert: "newca",
}

missingIDRes = toJSON(apiutil.ErrorRes{Err: apiutil.ErrMissingID.Error(), Msg: apiutil.ErrValidation.Error()})
Expand Down
24 changes: 12 additions & 12 deletions bootstrap/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
const maxLimitSize = 100

type addReq struct {
token string
ClientID string `json:"client_id"`
ExternalID string `json:"external_id"`
ExternalSecret string `json:"external_secret"`
Channels []string `json:"channels"`
Name string `json:"name"`
Content string `json:"content"`
ClientCert string `json:"client_cert"`
ClientKey string `json:"client_key"`
CACert string `json:"ca_cert"`
token string
ClientID string `json:"client_id"`
ExternalID string `json:"external_id"`
ExternalKey string `json:"external_key"`
Channels []string `json:"channels"`
Name string `json:"name"`
Content string `json:"content"`
ClientCert string `json:"client_cert"`
ClientKey string `json:"client_key"`
CACert string `json:"ca_cert"`
}

func (req addReq) validate() error {
Expand All @@ -32,7 +32,7 @@ func (req addReq) validate() error {
return apiutil.ErrMissingID
}

if req.ExternalSecret == "" {
if req.ExternalKey == "" {
return apiutil.ErrBearerKey
}

Expand Down Expand Up @@ -76,7 +76,7 @@ func (req updateReq) validate() error {
}

type updateCertReq struct {
clientID string
clientID string
ClientCert string `json:"client_cert"`
ClientKey string `json:"client_key"`
CACert string `json:"ca_cert"`
Expand Down
16 changes: 8 additions & 8 deletions bootstrap/api/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func TestAddReqValidation(t *testing.T) {

for _, tc := range cases {
req := addReq{
token: tc.token,
ExternalID: tc.externalID,
ExternalSecret: tc.externalKey,
Channels: tc.channels,
token: tc.token,
ExternalID: tc.externalID,
ExternalKey: tc.externalKey,
Channels: tc.channels,
}

err := req.validate()
Expand Down Expand Up @@ -151,14 +151,14 @@ func TestUpdateReqValidation(t *testing.T) {

func TestUpdateCertReqValidation(t *testing.T) {
cases := []struct {
desc string
desc string
clientID string
err error
err error
}{
{
desc: "empty client id",
desc: "empty client id",
clientID: "",
err: apiutil.ErrMissingID,
err: apiutil.ErrMissingID,
},
}

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type channelRes struct {

type viewRes struct {
ClientID string `json:"client_id,omitempty"`
CLientSecret string `json:"client,_secretomitempty"`
CLientSecret string `json:"client_secret,omitempty"`
Channels []channelRes `json:"channels,omitempty"`
ExternalID string `json:"external_id"`
ExternalKey string `json:"external_key,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/events/producer/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ func TestChangeState(t *testing.T) {
for _, tc := range cases {
tc.session = mgauthn.Session{UserID: validID, DomainID: tc.domainID, DomainUserID: validID}
repoCall := tv.boot.On("RetrieveByID", context.Background(), tc.domainID, tc.id).Return(config, tc.retrieveErr)
sdkCall1 := tv.sdk.On("Connect", mock.Anything, mock.Anything, mock.Anything).Return(errors.NewSDKError(tc.connectErr))
sdkCall1 := tv.sdk.On("ConnectClient", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.NewSDKError(tc.connectErr))
repoCall1 := tv.boot.On("ChangeState", context.Background(), mock.Anything, mock.Anything, mock.Anything).Return(tc.stateErr)
err := tv.svc.ChangeState(context.Background(), tc.session, tc.token, tc.id, tc.state)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
Expand Down
15 changes: 6 additions & 9 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (bs bootstrapService) UpdateConnections(ctx context.Context, session mgauth
}

for _, c := range disconnect {
if err := bs.sdk.DisconnectClient(id, c, session.DomainID, token); err != nil {
if err := bs.sdk.DisconnectClient(id, c, []string{"Publish", "Subscribe"}, session.DomainID, token); err != nil {
if errors.Contains(err, repoerr.ErrNotFound) {
continue
}
Expand All @@ -248,8 +248,9 @@ func (bs bootstrapService) UpdateConnections(ctx context.Context, session mgauth

for _, c := range connect {
conIDs := mgsdk.Connection{
ChannelID: c,
ClientID: id,
ChannelIDs: []string{c},
ClientIDs: []string{id},
Types: []string{"Publish", "Subscribe"},
}
if err := bs.sdk.Connect(conIDs, session.DomainID, token); err != nil {
return ErrClients
Expand Down Expand Up @@ -336,11 +337,7 @@ func (bs bootstrapService) ChangeState(ctx context.Context, session mgauthn.Sess
switch state {
case Active:
for _, c := range cfg.Channels {
conIDs := mgsdk.Connection{
ChannelID: c.ID,
ClientID: cfg.ClientID,
}
if err := bs.sdk.Connect(conIDs, session.DomainID, token); err != nil {
if err := bs.sdk.ConnectClient(cfg.ClientID, c.ID, []string{"Publish", "Subscribe"}, session.DomainID, token); err != nil {
// Ignore conflict errors as they indicate the connection already exists.
if errors.Contains(err, svcerr.ErrConflict) {
continue
Expand All @@ -350,7 +347,7 @@ func (bs bootstrapService) ChangeState(ctx context.Context, session mgauthn.Sess
}
case Inactive:
for _, c := range cfg.Channels {
if err := bs.sdk.DisconnectClient(cfg.ClientID, c.ID, session.DomainID, token); err != nil {
if err := bs.sdk.DisconnectClient(cfg.ClientID, c.ID, []string{"Publish", "Subscribe"}, session.DomainID, token); err != nil {
if errors.Contains(err, repoerr.ErrNotFound) {
continue
}
Expand Down
Loading

0 comments on commit 4daa02b

Please sign in to comment.