github.com/braveheart12/just@v0.8.7/functest/new_api_test.go (about) 1 // +build functest 2 3 /* 4 * Copyright 2019 Insolar Technologies 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package functest 20 21 import ( 22 "context" 23 "encoding/json" 24 "errors" 25 "io/ioutil" 26 "net/http" 27 "strings" 28 "testing" 29 30 "github.com/insolar/insolar/api/requester" 31 "github.com/insolar/insolar/core" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func contractError(body []byte) error { 36 var t map[string]interface{} 37 err := json.Unmarshal(body, &t) 38 if err != nil { 39 return err 40 } 41 if e, ok := t["error"]; ok { 42 if ee, ok := e.(string); ok && ee != "" { 43 return errors.New(ee) 44 } 45 } 46 return nil 47 } 48 49 func TestBadSeed(t *testing.T) { 50 ctx := context.TODO() 51 rootCfg, err := requester.CreateUserConfig(root.ref, root.privKey) 52 require.NoError(t, err) 53 res, err := requester.SendWithSeed(ctx, TestCallUrl, rootCfg, &requester.RequestConfigJSON{ 54 Method: "CreateMember", 55 Params: nil, 56 }, []byte("111")) 57 require.NoError(t, err) 58 require.EqualError(t, contractError(res), "[ checkSeed ] Bad seed param") 59 } 60 61 func TestIncorrectSeed(t *testing.T) { 62 ctx := context.TODO() 63 rootCfg, err := requester.CreateUserConfig(root.ref, root.privKey) 64 require.NoError(t, err) 65 res, err := requester.SendWithSeed(ctx, TestCallUrl, rootCfg, &requester.RequestConfigJSON{ 66 Method: "CreateMember", 67 Params: nil, 68 }, []byte("12345678901234567890123456789012")) 69 require.NoError(t, err) 70 require.EqualError(t, contractError(res), "[ checkSeed ] Incorrect seed") 71 } 72 73 func customSend(data string) (map[string]interface{}, error) { 74 req, err := http.NewRequest("POST", TestCallUrl, strings.NewReader(data)) 75 if err != nil { 76 return nil, err 77 } 78 req.Header.Set("Content-Type", "application/json") 79 c := http.Client{} 80 res, err := c.Do(req) 81 if err != nil { 82 return nil, err 83 } 84 defer res.Body.Close() 85 body, err := ioutil.ReadAll(res.Body) 86 if err != nil { 87 return nil, err 88 } 89 var out map[string]interface{} 90 err = json.Unmarshal(body, &out) 91 return out, err 92 } 93 94 func TestEmptyBody(t *testing.T) { 95 res, err := customSend("") 96 require.NoError(t, err) 97 require.Equal(t, "[ UnmarshalRequest ] Empty body", res["error"]) 98 } 99 100 func TestCrazyJSON(t *testing.T) { 101 res, err := customSend("[dh") 102 require.NoError(t, err) 103 require.Contains(t, res["error"], "[ UnmarshalRequest ] Can't unmarshal input params: invalid") 104 } 105 106 func TestIncorrectSign(t *testing.T) { 107 args, err := core.MarshalArgs(nil) 108 require.NoError(t, err) 109 seed, err := requester.GetSeed(TestAPIURL) 110 require.NoError(t, err) 111 body, err := requester.GetResponseBody(TestCallUrl, requester.PostParams{ 112 "params": args, 113 "method": "SomeMethod", 114 "reference": root.ref, 115 "seed": seed, 116 "signature": []byte("1234567890"), 117 }) 118 require.NoError(t, err) 119 var res map[string]interface{} 120 err = json.Unmarshal(body, &res) 121 require.NoError(t, err) 122 require.Contains(t, res["error"], "Incorrect signature") 123 }