github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/networkmap/register_node_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 networkmap 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/kaleido-io/firefly/internal/config" 24 "github.com/kaleido-io/firefly/mocks/broadcastmocks" 25 "github.com/kaleido-io/firefly/mocks/databasemocks" 26 "github.com/kaleido-io/firefly/mocks/dataexchangemocks" 27 "github.com/kaleido-io/firefly/mocks/identitymocks" 28 "github.com/kaleido-io/firefly/pkg/fftypes" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/mock" 31 ) 32 33 func TestRegisterNodeOk(t *testing.T) { 34 35 nm, cancel := newTestNetworkmap(t) 36 defer cancel() 37 38 config.Set(config.NodeDescription, "Node 1") 39 config.Set(config.OrgIdentity, "0x23456") 40 41 mdi := nm.database.(*databasemocks.Plugin) 42 mdi.On("GetOrganizationByIdentity", nm.ctx, "0x23456").Return(&fftypes.Organization{ 43 Identity: "0x23456", 44 Description: "owning organization", 45 }, nil) 46 47 mii := nm.identity.(*identitymocks.Plugin) 48 childID := &fftypes.Identity{OnChain: "0x12345"} 49 parentID := &fftypes.Identity{OnChain: "0x23456"} 50 mii.On("Resolve", nm.ctx, "0x12345").Return(childID, nil) 51 mii.On("Resolve", nm.ctx, "0x23456").Return(parentID, nil) 52 53 mdx := nm.exchange.(*dataexchangemocks.Plugin) 54 mdx.On("GetEndpointInfo", nm.ctx).Return("peer1", fftypes.JSONObject{"endpoint": "details"}, nil) 55 56 mockMsg := &fftypes.Message{Header: fftypes.MessageHeader{ID: fftypes.NewUUID()}} 57 mbm := nm.broadcast.(*broadcastmocks.Manager) 58 mbm.On("BroadcastDefinition", nm.ctx, mock.Anything, parentID, fftypes.SystemTagDefineNode).Return(mockMsg, nil) 59 60 msg, err := nm.RegisterNode(nm.ctx) 61 assert.NoError(t, err) 62 assert.Equal(t, mockMsg, msg) 63 64 } 65 66 func TestRegisterNodeMissingConfig(t *testing.T) { 67 68 nm, cancel := newTestNetworkmap(t) 69 defer cancel() 70 71 config.Set(config.NodeDescription, nil) 72 config.Set(config.NodeName, nil) 73 config.Set(config.OrgIdentity, nil) 74 75 _, err := nm.RegisterNode(nm.ctx) 76 assert.Regexp(t, "FF10216", err) 77 78 } 79 80 func TestRegisterNodeBadParentID(t *testing.T) { 81 82 nm, cancel := newTestNetworkmap(t) 83 defer cancel() 84 85 config.Set(config.NodeDescription, "Node 1") 86 config.Set(config.NodeName, "node1") 87 config.Set(config.OrgIdentity, "0x23456") 88 89 mdi := nm.database.(*databasemocks.Plugin) 90 mdi.On("GetOrganizationByIdentity", nm.ctx, "0x23456").Return(&fftypes.Organization{ 91 Identity: "0x23456", 92 Description: "owning organization", 93 }, nil) 94 95 mii := nm.identity.(*identitymocks.Plugin) 96 mii.On("Resolve", nm.ctx, "0x23456").Return(nil, fmt.Errorf("pop")) 97 98 mdx := nm.exchange.(*dataexchangemocks.Plugin) 99 mdx.On("GetEndpointInfo", nm.ctx).Return("peer1", fftypes.JSONObject{"endpoint": "details"}, nil) 100 101 _, err := nm.RegisterNode(nm.ctx) 102 assert.Regexp(t, "FF10215", err) 103 104 } 105 106 func TestRegisterNodeBadNodeID(t *testing.T) { 107 108 nm, cancel := newTestNetworkmap(t) 109 defer cancel() 110 111 config.Set(config.NodeDescription, "Node 1") 112 config.Set(config.NodeName, "node1") 113 config.Set(config.OrgIdentity, "0x23456") 114 115 mdi := nm.database.(*databasemocks.Plugin) 116 mdi.On("GetOrganizationByIdentity", nm.ctx, "0x23456").Return(&fftypes.Organization{ 117 Identity: "0x23456", 118 Description: "owning organization", 119 }, nil) 120 121 mii := nm.identity.(*identitymocks.Plugin) 122 mii.On("Resolve", nm.ctx, "0x23456").Return(nil, fmt.Errorf("pop")) 123 124 mdx := nm.exchange.(*dataexchangemocks.Plugin) 125 mdx.On("GetEndpointInfo", nm.ctx).Return("peer1", fftypes.JSONObject{"endpoint": "details"}, nil) 126 127 _, err := nm.RegisterNode(nm.ctx) 128 assert.Regexp(t, "pop", err) 129 130 } 131 132 func TestRegisterNodeParentNotFound(t *testing.T) { 133 134 nm, cancel := newTestNetworkmap(t) 135 defer cancel() 136 137 config.Set(config.NodeDescription, "Node 1") 138 config.Set(config.NodeName, "node1") 139 config.Set(config.OrgIdentity, "0x23456") 140 141 mdi := nm.database.(*databasemocks.Plugin) 142 mdi.On("GetOrganizationByIdentity", nm.ctx, "0x23456").Return(nil, nil) 143 144 mii := nm.identity.(*identitymocks.Plugin) 145 childID := &fftypes.Identity{OnChain: "0x12345"} 146 mii.On("Resolve", nm.ctx, "0x12345").Return(childID, nil) 147 148 mdx := nm.exchange.(*dataexchangemocks.Plugin) 149 mdx.On("GetEndpointInfo", nm.ctx).Return("peer1", fftypes.JSONObject{"endpoint": "details"}, nil) 150 151 _, err := nm.RegisterNode(nm.ctx) 152 assert.Regexp(t, "FF10214", err) 153 154 } 155 156 func TestRegisterNodeParentBadNode(t *testing.T) { 157 158 nm, cancel := newTestNetworkmap(t) 159 defer cancel() 160 161 config.Set(config.NodeDescription, string(make([]byte, 4097))) 162 config.Set(config.NodeName, "node1") 163 config.Set(config.OrgIdentity, "0x23456") 164 165 mdx := nm.exchange.(*dataexchangemocks.Plugin) 166 mdx.On("GetEndpointInfo", nm.ctx).Return("peer1", fftypes.JSONObject{"endpoint": "details"}, nil) 167 168 _, err := nm.RegisterNode(nm.ctx) 169 assert.Regexp(t, "FF10188", err) 170 171 } 172 173 func TestRegisterNodeParentDXEndpointFail(t *testing.T) { 174 175 nm, cancel := newTestNetworkmap(t) 176 defer cancel() 177 178 config.Set(config.NodeDescription, string(make([]byte, 4097))) 179 config.Set(config.NodeName, "node1") 180 config.Set(config.OrgIdentity, "0x23456") 181 182 mdx := nm.exchange.(*dataexchangemocks.Plugin) 183 mdx.On("GetEndpointInfo", nm.ctx).Return("", nil, fmt.Errorf("pop")) 184 185 _, err := nm.RegisterNode(nm.ctx) 186 assert.Regexp(t, "pop", err) 187 188 }