github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/appregistry/appregistry_test.go (about) 1 // Copyright 2018-2021 CERN 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package appregistry 20 21 import ( 22 "context" 23 "sort" 24 "testing" 25 26 registrypb "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1" 27 rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" 28 providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" 29 "github.com/cs3org/reva/v2/pkg/app/registry/static" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 type ByAddress []*registrypb.ProviderInfo 34 35 func (a ByAddress) Len() int { return len(a) } 36 func (a ByAddress) Less(i, j int) bool { return a[i].Address < a[j].Address } 37 func (a ByAddress) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 38 39 func Test_ListAppProviders(t *testing.T) { 40 tests := []struct { 41 name string 42 providers []map[string]interface{} 43 mimeTypes []map[string]interface{} 44 want *registrypb.ListAppProvidersResponse 45 }{ 46 { 47 name: "simple test", 48 providers: []map[string]interface{}{ 49 { 50 "address": "some Address", 51 "mimetypes": []string{"text/json"}, 52 }, 53 { 54 "address": "another address", 55 "mimetypes": []string{"currently/ignored"}, 56 }, 57 }, 58 mimeTypes: []map[string]interface{}{ 59 { 60 "mime_type": "text/json", 61 "extension": "json", 62 "name": "JSON File", 63 "icon": "https://example.org/icons&file=json.png", 64 "default_app": "some Address", 65 }, 66 { 67 "mime_type": "currently/ignored", 68 "extension": "unknown", 69 "name": "Ignored file", 70 "icon": "https://example.org/icons&file=unknown.png", 71 "default_app": "some Address", 72 }, 73 }, 74 75 // only Status and Providers will be asserted in the tests 76 want: ®istrypb.ListAppProvidersResponse{ 77 Status: &rpcv1beta1.Status{ 78 Code: 1, 79 Trace: "00000000000000000000000000000000", 80 Message: "", 81 }, 82 Providers: []*registrypb.ProviderInfo{ 83 { 84 Address: "some Address", 85 MimeTypes: []string{"text/json"}, 86 }, 87 { 88 Address: "another address", 89 MimeTypes: []string{"currently/ignored"}, 90 }, 91 }, 92 }, 93 }, 94 { 95 name: "providers is nil", 96 providers: nil, 97 mimeTypes: nil, 98 want: ®istrypb.ListAppProvidersResponse{ 99 Status: &rpcv1beta1.Status{ 100 Code: 1, 101 Trace: "00000000000000000000000000000000", 102 }, 103 Providers: []*registrypb.ProviderInfo{}, 104 }, 105 }, 106 { 107 name: "empty providers", 108 providers: []map[string]interface{}{}, 109 mimeTypes: []map[string]interface{}{}, 110 111 // only Status and Providers will be asserted in the tests 112 want: ®istrypb.ListAppProvidersResponse{ 113 Status: &rpcv1beta1.Status{ 114 Code: 1, 115 Trace: "00000000000000000000000000000000", 116 Message: "", 117 }, 118 Providers: []*registrypb.ProviderInfo{}, 119 }, 120 }, 121 } 122 123 for _, tt := range tests { 124 t.Run(tt.name, func(t *testing.T) { 125 rr, err := static.New(map[string]interface{}{"providers": tt.providers, "mime_types": tt.mimeTypes}) 126 if err != nil { 127 t.Errorf("could not create registry error = %v", err) 128 return 129 } 130 131 ss := &svc{ 132 reg: rr, 133 } 134 got, err := ss.ListAppProviders(context.Background(), nil) 135 136 if err != nil { 137 t.Errorf("ListAppProviders() error = %v", err) 138 return 139 } 140 assert.Equal(t, tt.want.Status, got.Status) 141 sort.Sort(ByAddress(tt.want.Providers)) 142 sort.Sort(ByAddress(got.Providers)) 143 assert.Equal(t, tt.want.Providers, got.Providers) 144 }) 145 } 146 } 147 148 func Test_GetAppProviders(t *testing.T) { 149 providers := []map[string]interface{}{ 150 { 151 "address": "text appprovider addr", 152 "mimetypes": []string{"text/json", "text/xml"}, 153 }, 154 { 155 "address": "image appprovider addr", 156 "mimetypes": []string{"image/bmp"}, 157 }, 158 { 159 "address": "misc appprovider addr", 160 "mimetypes": []string{"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.oasis.opendocument.presentation", "application/vnd.apple.installer+xml"}, 161 }, 162 } 163 164 mimeTypes := []map[string]string{ 165 { 166 "mime_type": "text/json", 167 "extension": "json", 168 "name": "JSON File", 169 "icon": "https://example.org/icons&file=json.png", 170 "default_app": "some Address", 171 }, 172 { 173 "mime_type": "text/xml", 174 "extension": "xml", 175 "name": "XML File", 176 "icon": "https://example.org/icons&file=xml.png", 177 "default_app": "some Address", 178 }, 179 { 180 "mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 181 "extension": "doc", 182 "name": "Word File", 183 "icon": "https://example.org/icons&file=doc.png", 184 "default_app": "some Address", 185 }, 186 { 187 "mime_type": "application/vnd.oasis.opendocument.presentation", 188 "extension": "odf", 189 "name": "OpenDocument File", 190 "icon": "https://example.org/icons&file=odf.png", 191 "default_app": "some Address", 192 }, 193 { 194 "mime_type": "application/vnd.apple.installer+xml", 195 "extension": "mpkg", 196 "name": "Mpkg File", 197 "icon": "https://example.org/icons&file=mpkg.png", 198 "default_app": "some Address", 199 }, 200 { 201 "mime_type": "image/bmp", 202 "extension": "bmp", 203 "name": "Image File", 204 "icon": "https://example.org/icons&file=bmp.png", 205 "default_app": "some Address", 206 }, 207 } 208 209 tests := []struct { 210 name string 211 search *providerv1beta1.ResourceInfo 212 want *registrypb.GetAppProvidersResponse 213 }{ 214 { 215 name: "simple", 216 search: &providerv1beta1.ResourceInfo{MimeType: "text/json"}, 217 // only Status and Providers will be asserted in the tests 218 want: ®istrypb.GetAppProvidersResponse{ 219 Status: &rpcv1beta1.Status{ 220 Code: 1, 221 Trace: "00000000000000000000000000000000", 222 Message: "", 223 }, 224 Providers: []*registrypb.ProviderInfo{ 225 { 226 Address: "text appprovider addr", 227 MimeTypes: []string{"text/json", "text/xml"}, 228 }, 229 }, 230 }, 231 }, 232 { 233 name: "more obscure MimeType", 234 search: &providerv1beta1.ResourceInfo{MimeType: "application/vnd.apple.installer+xml"}, 235 want: ®istrypb.GetAppProvidersResponse{ 236 Status: &rpcv1beta1.Status{ 237 Code: 1, 238 Trace: "00000000000000000000000000000000", 239 Message: "", 240 }, 241 Providers: []*registrypb.ProviderInfo{ 242 { 243 Address: "misc appprovider addr", 244 MimeTypes: []string{"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.oasis.opendocument.presentation", "application/vnd.apple.installer+xml"}, 245 }, 246 }, 247 }, 248 }, 249 { 250 name: "not existing MimeType", 251 search: &providerv1beta1.ResourceInfo{MimeType: "doesnot/exist"}, 252 want: ®istrypb.GetAppProvidersResponse{ 253 Status: &rpcv1beta1.Status{ 254 Code: 15, 255 Trace: "00000000000000000000000000000000", 256 Message: "error looking for the app provider", 257 }, 258 Providers: nil, 259 }, 260 }, 261 { 262 name: "empty MimeType", 263 search: &providerv1beta1.ResourceInfo{MimeType: ""}, 264 want: ®istrypb.GetAppProvidersResponse{ 265 Status: &rpcv1beta1.Status{ 266 Code: 15, 267 Trace: "00000000000000000000000000000000", 268 Message: "error looking for the app provider", 269 }, 270 Providers: nil, 271 }, 272 }, 273 { 274 name: "no data in resource info", 275 search: &providerv1beta1.ResourceInfo{}, 276 want: ®istrypb.GetAppProvidersResponse{ 277 Status: &rpcv1beta1.Status{ 278 Code: 15, 279 Trace: "00000000000000000000000000000000", 280 Message: "error looking for the app provider", 281 }, 282 Providers: nil, 283 }, 284 }, 285 { 286 name: "not valid MimeType", 287 search: &providerv1beta1.ResourceInfo{MimeType: "this/type\\IS.not?VALID@all"}, 288 want: ®istrypb.GetAppProvidersResponse{ 289 Status: &rpcv1beta1.Status{ 290 Code: 15, 291 Trace: "00000000000000000000000000000000", 292 Message: "error looking for the app provider", 293 }, 294 Providers: nil, 295 }, 296 }, 297 } 298 299 rr, err := static.New(map[string]interface{}{"providers": providers, "mime_types": mimeTypes}) 300 if err != nil { 301 t.Errorf("could not create registry error = %v", err) 302 return 303 } 304 305 ss := &svc{ 306 reg: rr, 307 } 308 309 for _, tt := range tests { 310 t.Run(tt.name, func(t *testing.T) { 311 req := registrypb.GetAppProvidersRequest{ResourceInfo: tt.search} 312 got, err := ss.GetAppProviders(context.Background(), &req) 313 314 if err != nil { 315 t.Errorf("GetAppProviders() error = %v", err) 316 return 317 } 318 assert.Equal(t, tt.want.Status, got.Status) 319 sort.Sort(ByAddress(tt.want.Providers)) 320 sort.Sort(ByAddress(got.Providers)) 321 assert.Equal(t, tt.want.Providers, got.Providers) 322 }) 323 } 324 } 325 326 func TestNew(t *testing.T) { 327 328 tests := []struct { 329 name string 330 m map[string]interface{} 331 providers map[string]interface{} 332 want svc 333 wantErr interface{} 334 }{ 335 { 336 name: "no error", 337 m: map[string]interface{}{"Driver": "static"}, 338 wantErr: nil, 339 }, 340 { 341 name: "not existing driver", 342 m: map[string]interface{}{"Driver": "doesnotexist"}, 343 wantErr: "error: not found: appregistrysvc: driver not found: doesnotexist", 344 }, 345 { 346 name: "empty", 347 m: map[string]interface{}{}, 348 wantErr: nil, 349 }, 350 { 351 name: "extra not existing field in setting", 352 m: map[string]interface{}{"Driver": "static", "doesnotexist": "doesnotexist"}, 353 wantErr: nil, 354 }, 355 } 356 357 for _, tt := range tests { 358 t.Run(tt.name, func(t *testing.T) { 359 got, err := New(tt.m, nil, nil) 360 if err != nil { 361 assert.Equal(t, tt.wantErr, err.Error()) 362 assert.Nil(t, got) 363 } else { 364 assert.Equal(t, tt.wantErr, err) 365 } 366 }) 367 } 368 }