github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/apiserver/route_post_data_test.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package apiserver 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "mime/multipart" 23 "net/http/httptest" 24 "testing" 25 26 "github.com/kaleido-io/firefly/internal/log" 27 "github.com/kaleido-io/firefly/mocks/datamocks" 28 "github.com/kaleido-io/firefly/mocks/orchestratormocks" 29 "github.com/kaleido-io/firefly/pkg/fftypes" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/mock" 32 ) 33 34 func TestPostDataJSON(t *testing.T) { 35 o := &orchestratormocks.Orchestrator{} 36 mdm := &datamocks.Manager{} 37 o.On("Data").Return(mdm) 38 r := createMuxRouter(o) 39 input := fftypes.Data{} 40 var buf bytes.Buffer 41 json.NewEncoder(&buf).Encode(&input) 42 req := httptest.NewRequest("POST", "/api/v1/namespaces/ns1/data", &buf) 43 req.Header.Set("Content-Type", "application/json; charset=utf-8") 44 res := httptest.NewRecorder() 45 46 mdm.On("UploadJSON", mock.Anything, "ns1", mock.AnythingOfType("*fftypes.Data")). 47 Return(&fftypes.Data{}, nil) 48 r.ServeHTTP(res, req) 49 50 assert.Equal(t, 201, res.Result().StatusCode) 51 } 52 53 func TestPostDataBinary(t *testing.T) { 54 log.SetLevel("debug") 55 56 o := &orchestratormocks.Orchestrator{} 57 mdm := &datamocks.Manager{} 58 o.On("Data").Return(mdm) 59 r := createMuxRouter(o) 60 61 var b bytes.Buffer 62 w := multipart.NewWriter(&b) 63 writer, err := w.CreateFormFile("file", "filename.ext") 64 assert.NoError(t, err) 65 writer.Write([]byte(`some data`)) 66 w.Close() 67 req := httptest.NewRequest("POST", "/api/v1/namespaces/ns1/data", &b) 68 req.Header.Set("Content-Type", w.FormDataContentType()) 69 70 res := httptest.NewRecorder() 71 72 mdm.On("UploadBLOB", mock.Anything, "ns1", mock.AnythingOfType("*multipart.Part")). 73 Return(&fftypes.Data{}, nil) 74 r.ServeHTTP(res, req) 75 76 assert.Equal(t, 201, res.Result().StatusCode) 77 } 78 79 func TestPostDataBinaryMissing(t *testing.T) { 80 log.SetLevel("debug") 81 82 o := &orchestratormocks.Orchestrator{} 83 mdm := &datamocks.Manager{} 84 o.On("Data").Return(mdm) 85 r := createMuxRouter(o) 86 87 var b bytes.Buffer 88 w := multipart.NewWriter(&b) 89 formField, _ := w.CreateFormField("Not a file") 90 formField.Write([]byte(`some value`)) 91 w.Close() 92 req := httptest.NewRequest("POST", "/api/v1/namespaces/ns1/data", &b) 93 req.Header.Set("Content-Type", w.FormDataContentType()) 94 95 res := httptest.NewRecorder() 96 97 mdm.On("UploadBLOB", mock.Anything, "ns1", mock.AnythingOfType("*multipart.Part")). 98 Return(&fftypes.Data{}, nil) 99 r.ServeHTTP(res, req) 100 101 assert.Equal(t, 400, res.Result().StatusCode) 102 } 103 104 func TestPostDataBadForm(t *testing.T) { 105 log.SetLevel("debug") 106 107 o := &orchestratormocks.Orchestrator{} 108 mdm := &datamocks.Manager{} 109 o.On("Data").Return(mdm) 110 r := createMuxRouter(o) 111 112 var b bytes.Buffer 113 req := httptest.NewRequest("POST", "/api/v1/namespaces/ns1/data", &b) 114 req.Header.Set("Content-Type", "multipart/form-data") 115 116 res := httptest.NewRecorder() 117 118 mdm.On("UploadBLOB", mock.Anything, "ns1", mock.AnythingOfType("*multipart.Part")). 119 Return(&fftypes.Data{}, nil) 120 r.ServeHTTP(res, req) 121 122 assert.Equal(t, 400, res.Result().StatusCode) 123 }