github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/authentication_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/cockroachdb/errors" 8 9 "github.com/golang/protobuf/proto" 10 "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 11 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestGrpcClient_CreateCredential(t *testing.T) { 16 ctx := context.Background() 17 c := testClient(ctx, t) 18 19 t.Run("create credential normal", func(t *testing.T) { 20 mockServer.SetInjection(MCreateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) { 21 s, err := SuccessStatus() 22 return s, err 23 }) 24 defer mockServer.DelInjection(MCreateCredential) 25 err := c.CreateCredential(ctx, testUsername, testPassword) 26 assert.Nil(t, err) 27 }) 28 29 t.Run("create credential invalid name", func(t *testing.T) { 30 mockServer.SetInjection(MCreateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) { 31 s, err := BadRequestStatus() 32 return s, err 33 }) 34 defer mockServer.DelInjection(MCreateCredential) 35 err := c.CreateCredential(ctx, "123", testPassword) 36 assert.Error(t, err) 37 }) 38 39 t.Run("create credential grpc error", func(t *testing.T) { 40 mockServer.SetInjection(MCreateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 41 return &commonpb.Status{}, errors.New("mockServer.d grpc error") 42 }) 43 defer mockServer.DelInjection(MCreateCredential) 44 err := c.CreateCredential(ctx, testUsername, testPassword) 45 assert.Error(t, err) 46 }) 47 48 t.Run("create credential milvuspb.error", func(t *testing.T) { 49 mockServer.SetInjection(MCreateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 50 return &commonpb.Status{ 51 ErrorCode: commonpb.ErrorCode_UnexpectedError, 52 Reason: "Service is not healthy", 53 }, nil 54 }) 55 defer mockServer.DelInjection(MCreateCredential) 56 err := c.CreateCredential(ctx, testUsername, testPassword) 57 assert.Error(t, err) 58 }) 59 } 60 61 func TestGrpcClient_UpdateCredential(t *testing.T) { 62 ctx := context.Background() 63 c := testClient(ctx, t) 64 65 t.Run("update credential normal", func(t *testing.T) { 66 mockServer.SetInjection(MUpdateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) { 67 s, err := SuccessStatus() 68 return s, err 69 }) 70 defer mockServer.DelInjection(MUpdateCredential) 71 err := c.UpdateCredential(ctx, testUsername, testPassword, testPassword) 72 assert.Nil(t, err) 73 }) 74 75 t.Run("update credential grpc error", func(t *testing.T) { 76 mockServer.SetInjection(MUpdateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 77 return &commonpb.Status{}, errors.New("mockServer.d grpc error") 78 }) 79 defer mockServer.DelInjection(MUpdateCredential) 80 err := c.UpdateCredential(ctx, testUsername, testPassword, testPassword) 81 assert.Error(t, err) 82 }) 83 84 t.Run("update credential milvuspb.error", func(t *testing.T) { 85 mockServer.SetInjection(MUpdateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 86 return &commonpb.Status{ 87 ErrorCode: commonpb.ErrorCode_UnexpectedError, 88 Reason: "Service is not healthy", 89 }, nil 90 }) 91 defer mockServer.DelInjection(MUpdateCredential) 92 err := c.UpdateCredential(ctx, testUsername, testPassword, testPassword) 93 assert.Error(t, err) 94 }) 95 } 96 97 func TestGrpcClient_DeleteCredential(t *testing.T) { 98 ctx := context.Background() 99 c := testClient(ctx, t) 100 101 t.Run("delete credential normal", func(t *testing.T) { 102 mockServer.SetInjection(MDeleteCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) { 103 s, err := SuccessStatus() 104 return s, err 105 }) 106 defer mockServer.DelInjection(MDeleteCredential) 107 err := c.DeleteCredential(ctx, testUsername) 108 assert.Nil(t, err) 109 }) 110 111 t.Run("delete credential grpc error", func(t *testing.T) { 112 mockServer.SetInjection(MDeleteCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 113 return &commonpb.Status{}, errors.New("mockServer.d grpc error") 114 }) 115 defer mockServer.DelInjection(MDeleteCredential) 116 err := c.DeleteCredential(ctx, testUsername) 117 assert.Error(t, err) 118 }) 119 120 t.Run("delete credential milvuspb.error", func(t *testing.T) { 121 mockServer.SetInjection(MDeleteCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 122 return &commonpb.Status{ 123 ErrorCode: commonpb.ErrorCode_UnexpectedError, 124 Reason: "Service is not healthy", 125 }, nil 126 }) 127 defer mockServer.DelInjection(MDeleteCredential) 128 err := c.DeleteCredential(ctx, testUsername) 129 assert.Error(t, err) 130 }) 131 } 132 133 func TestGrpcClient_ListCredUsers(t *testing.T) { 134 ctx := context.Background() 135 c := testClient(ctx, t) 136 137 t.Run("list credential users normal", func(t *testing.T) { 138 mockServer.SetInjection(MListCredUsers, func(ctx context.Context, _ proto.Message) (proto.Message, error) { 139 resp := &milvuspb.ListCredUsersResponse{ 140 Usernames: []string{testUsername}, 141 } 142 s, err := SuccessStatus() 143 resp.Status = s 144 return resp, err 145 }) 146 defer mockServer.DelInjection(MListCredUsers) 147 names, err := c.ListCredUsers(ctx) 148 assert.Nil(t, err) 149 assert.Equal(t, []string{testUsername}, names) 150 }) 151 152 t.Run("list credential users grpc error", func(t *testing.T) { 153 mockServer.SetInjection(MListCredUsers, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 154 return &milvuspb.ListCredUsersResponse{}, errors.New("mockServer.d grpc error") 155 }) 156 defer mockServer.DelInjection(MListCredUsers) 157 _, err := c.ListCredUsers(ctx) 158 assert.Error(t, err) 159 }) 160 161 t.Run("list credential users milvuspb.error", func(t *testing.T) { 162 mockServer.SetInjection(MListCredUsers, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 163 return &milvuspb.ListCredUsersResponse{ 164 Status: &commonpb.Status{ 165 ErrorCode: commonpb.ErrorCode_UnexpectedError, 166 Reason: "Service is not healthy", 167 }, 168 }, nil 169 }) 170 defer mockServer.DelInjection(MListCredUsers) 171 _, err := c.ListCredUsers(ctx) 172 assert.Error(t, err) 173 }) 174 }