grpc_client_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package mysterybox
  14. import (
  15. "bytes"
  16. "context"
  17. "errors"
  18. "maps"
  19. "testing"
  20. mbox "github.com/nebius/gosdk/proto/nebius/mysterybox/v1"
  21. tassert "github.com/stretchr/testify/assert"
  22. "github.com/stretchr/testify/require"
  23. "google.golang.org/grpc"
  24. )
  25. const (
  26. defaultVersion = "version1"
  27. alternativeVersion = "version2"
  28. notFoundError = "not found"
  29. )
  30. type FakePayloadService struct {
  31. data map[string]map[string]*mbox.SecretPayload
  32. }
  33. func (f *FakePayloadService) Get(_ context.Context, r *mbox.GetPayloadRequest, _ ...grpc.CallOption) (*mbox.SecretPayload, error) {
  34. version := extractVersionFromRequest(r.VersionId)
  35. val, ok := f.data[r.GetSecretId()][version]
  36. if !ok {
  37. return nil, errors.New("secret not found")
  38. }
  39. return val, nil
  40. }
  41. func (f *FakePayloadService) GetByKey(_ context.Context, r *mbox.GetPayloadByKeyRequest, _ ...grpc.CallOption) (*mbox.SecretPayloadEntry, error) {
  42. version := extractVersionFromRequest(r.VersionId)
  43. payload, ok := f.data[r.GetSecretId()][version]
  44. if !ok {
  45. return nil, errors.New("secret not found")
  46. }
  47. for _, p := range payload.GetData() {
  48. if p.Key == r.GetKey() {
  49. return &mbox.SecretPayloadEntry{VersionId: payload.VersionId, Data: &mbox.Payload{Key: p.Key, Payload: p.Payload}}, nil
  50. }
  51. }
  52. return nil, errors.New(notFoundError)
  53. }
  54. func InitFakePayloadService() *FakePayloadService {
  55. mysteryboxData := map[string]map[string]*mbox.SecretPayload{}
  56. mysteryboxData["secret1Id"] = make(map[string]*mbox.SecretPayload)
  57. mysteryboxData["secret1Id"][defaultVersion] = &mbox.SecretPayload{
  58. VersionId: defaultVersion,
  59. Data: []*mbox.Payload{
  60. {
  61. Key: "key1",
  62. Payload: &mbox.Payload_StringValue{StringValue: "test string secret"},
  63. }, {
  64. Key: "key2",
  65. Payload: &mbox.Payload_BinaryValue{BinaryValue: []byte("test byte secret")},
  66. },
  67. },
  68. }
  69. mysteryboxData["secret2Id"] = make(map[string]*mbox.SecretPayload)
  70. mysteryboxData["secret2Id"][defaultVersion] = &mbox.SecretPayload{
  71. VersionId: defaultVersion,
  72. Data: []*mbox.Payload{
  73. {
  74. Key: "key3",
  75. Payload: &mbox.Payload_StringValue{StringValue: "test string secret"},
  76. },
  77. },
  78. }
  79. mysteryboxData["secret2Id"][alternativeVersion] = &mbox.SecretPayload{
  80. VersionId: alternativeVersion,
  81. Data: []*mbox.Payload{
  82. {
  83. Key: "key3",
  84. Payload: &mbox.Payload_StringValue{StringValue: "test string secret alternative"},
  85. },
  86. },
  87. }
  88. return &FakePayloadService{
  89. data: mysteryboxData,
  90. }
  91. }
  92. func TestGetSecret(t *testing.T) {
  93. t.Parallel()
  94. client := &GrpcClient{PayloadService: InitFakePayloadService()}
  95. tests := []struct {
  96. name string
  97. secretID string
  98. expected map[string][]byte
  99. version string
  100. wantErr string
  101. }{
  102. {
  103. name: "Get secret's payload",
  104. secretID: "secret1Id",
  105. expected: map[string][]byte{
  106. "key1": []byte("test string secret"),
  107. "key2": []byte("test byte secret"),
  108. }},
  109. {
  110. name: "Get secret's payload by version",
  111. secretID: "secret2Id",
  112. expected: map[string][]byte{
  113. "key3": []byte("test string secret alternative"),
  114. },
  115. version: alternativeVersion,
  116. },
  117. {
  118. name: "Get secret's payload by version not found",
  119. secretID: "secret2Id",
  120. wantErr: notFoundError,
  121. version: "another-version",
  122. },
  123. {
  124. name: "Not found secret",
  125. secretID: "nope",
  126. wantErr: notFoundError,
  127. },
  128. }
  129. for _, tt := range tests {
  130. t.Run(tt.name, func(t *testing.T) {
  131. t.Parallel()
  132. payload, err := client.GetSecret(context.Background(), "token", tt.secretID, tt.version)
  133. if tt.wantErr != "" {
  134. tassert.Error(t, err)
  135. tassert.Contains(t, err.Error(), tt.wantErr)
  136. return
  137. }
  138. require.NoError(t, err)
  139. if tt.version == "" {
  140. tassert.Equal(t, defaultVersion, payload.VersionID, "Payload version must be default")
  141. } else {
  142. tassert.Equal(t, tt.version, payload.VersionID)
  143. }
  144. expected := make(map[string][]byte, len(tt.expected))
  145. maps.Copy(expected, tt.expected)
  146. tassert.Equal(t, len(payload.Entries), len(expected))
  147. for _, entry := range payload.Entries {
  148. value, _ := expected[entry.Key]
  149. if (entry.BinaryValue != nil && bytes.Equal(value, entry.BinaryValue)) || (entry.StringValue != "" && bytes.Equal(value, []byte(entry.StringValue))) {
  150. delete(expected, entry.Key)
  151. continue
  152. }
  153. }
  154. tassert.Empty(t, expected, "not all expected entries found: %+v", expected)
  155. })
  156. }
  157. }
  158. func TestGetSecretByKey(t *testing.T) {
  159. t.Parallel()
  160. client := &GrpcClient{PayloadService: InitFakePayloadService()}
  161. tests := []struct {
  162. name string
  163. secretID string
  164. key string
  165. wantStr string
  166. wantBin []byte
  167. wantErr string
  168. }{
  169. {name: "Get secret's string payload", secretID: "secret1Id", key: "key1", wantStr: "test string secret"},
  170. {name: "Get secret's binary payload", secretID: "secret1Id", key: "key2", wantBin: []byte("test byte secret")},
  171. {name: "Not found key", secretID: "secret1Id", key: "missing", wantErr: notFoundError},
  172. {name: "Not found secret", secretID: "nope", key: "any", wantErr: notFoundError},
  173. }
  174. for _, tt := range tests {
  175. t.Run(tt.name, func(t *testing.T) {
  176. t.Parallel()
  177. payload, err := client.GetSecretByKey(context.Background(), "token", tt.secretID, "", tt.key)
  178. if tt.wantErr != "" {
  179. tassert.Error(t, err)
  180. tassert.Contains(t, err.Error(), tt.wantErr)
  181. return
  182. }
  183. require.NoError(t, err)
  184. require.NotNil(t, payload)
  185. require.NotNil(t, payload.Entry)
  186. if tt.wantStr != "" {
  187. tassert.Nil(t, payload.Entry.BinaryValue)
  188. tassert.Equal(t, tt.wantStr, payload.Entry.StringValue)
  189. }
  190. if tt.wantBin != nil {
  191. tassert.Empty(t, payload.Entry.StringValue)
  192. tassert.Equal(t, tt.wantBin, payload.Entry.BinaryValue)
  193. }
  194. })
  195. }
  196. }
  197. func extractVersionFromRequest(requestVersion string) string {
  198. if requestVersion == "" {
  199. return defaultVersion
  200. }
  201. return requestVersion
  202. }