github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/privatemessaging/message_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 privatemessaging 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 "github.com/kaleido-io/firefly/mocks/databasemocks" 25 "github.com/kaleido-io/firefly/mocks/datamocks" 26 "github.com/kaleido-io/firefly/mocks/identitymocks" 27 "github.com/kaleido-io/firefly/pkg/fftypes" 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/mock" 30 ) 31 32 func TestSendMessageE2EOk(t *testing.T) { 33 34 pm, cancel := newTestPrivateMessaging(t) 35 defer cancel() 36 37 mii := pm.identity.(*identitymocks.Plugin) 38 mii.On("Resolve", pm.ctx, "localorg").Return(&fftypes.Identity{ 39 Identifier: "localorg", 40 OnChain: "0x12345", 41 }, nil) 42 43 dataID := fftypes.NewUUID() 44 mdm := pm.data.(*datamocks.Manager) 45 mdm.On("ResolveInputData", pm.ctx, "ns1", mock.Anything).Return(fftypes.DataRefs{ 46 {ID: dataID, Hash: fftypes.NewRandB32()}, 47 }, nil) 48 49 mdi := pm.database.(*databasemocks.Plugin) 50 rag := mdi.On("RunAsGroup", pm.ctx, mock.Anything).Return(nil) 51 rag.RunFn = func(a mock.Arguments) { 52 err := a[1].(func(context.Context) error)(a[0].(context.Context)) 53 rag.ReturnArguments = mock.Arguments{err} 54 } 55 mdi.On("GetOrganizationByName", pm.ctx, "localorg").Return(&fftypes.Organization{ 56 ID: fftypes.NewUUID(), 57 }, nil) 58 mdi.On("GetNodes", pm.ctx, mock.Anything).Return([]*fftypes.Node{ 59 {ID: fftypes.NewUUID(), Name: "node1", Owner: "localorg"}, 60 }, nil).Once() 61 mdi.On("GetOrganizationByName", pm.ctx, "org1").Return(&fftypes.Organization{ 62 ID: fftypes.NewUUID(), 63 }, nil) 64 mdi.On("GetNodes", pm.ctx, mock.Anything).Return([]*fftypes.Node{ 65 {ID: fftypes.NewUUID(), Name: "node1", Owner: "org1"}, 66 }, nil).Once() 67 mdi.On("GetGroups", pm.ctx, mock.Anything).Return([]*fftypes.Group{ 68 {Hash: fftypes.NewRandB32()}, 69 }, nil).Once() 70 mdi.On("InsertMessageLocal", pm.ctx, mock.Anything).Return(nil).Once() 71 72 msg, err := pm.SendMessage(pm.ctx, "ns1", &fftypes.MessageInput{ 73 InputData: fftypes.InputData{ 74 {Value: fftypes.Byteable(`{"some": "data"}`)}, 75 }, 76 Group: &fftypes.InputGroup{ 77 Members: []fftypes.MemberInput{ 78 {Identity: "org1"}, 79 }, 80 }, 81 }) 82 assert.NoError(t, err) 83 assert.Equal(t, *dataID, *msg.Data[0].ID) 84 assert.NotNil(t, msg.Header.Group) 85 86 } 87 88 func TestSendMessageBadIdentity(t *testing.T) { 89 90 pm, cancel := newTestPrivateMessaging(t) 91 defer cancel() 92 93 mii := pm.identity.(*identitymocks.Plugin) 94 mii.On("Resolve", pm.ctx, "localorg").Return(nil, fmt.Errorf("pop")) 95 96 _, err := pm.SendMessage(pm.ctx, "ns1", &fftypes.MessageInput{ 97 InputData: fftypes.InputData{ 98 {Value: fftypes.Byteable(`{"some": "data"}`)}, 99 }, 100 Group: &fftypes.InputGroup{ 101 Members: []fftypes.MemberInput{ 102 {Identity: "org1"}, 103 }, 104 }, 105 }) 106 assert.Regexp(t, "FF10206.*pop", err) 107 108 } 109 110 func TestSendMessageFail(t *testing.T) { 111 112 pm, cancel := newTestPrivateMessaging(t) 113 defer cancel() 114 115 mii := pm.identity.(*identitymocks.Plugin) 116 mii.On("Resolve", pm.ctx, "localorg").Return(&fftypes.Identity{ 117 Identifier: "localorg", 118 OnChain: "0x12345", 119 }, nil) 120 121 dataID := fftypes.NewUUID() 122 mdm := pm.data.(*datamocks.Manager) 123 mdm.On("ResolveInputData", pm.ctx, "ns1", mock.Anything).Return(fftypes.DataRefs{ 124 {ID: dataID, Hash: fftypes.NewRandB32()}, 125 }, nil) 126 127 mdi := pm.database.(*databasemocks.Plugin) 128 mdi.On("RunAsGroup", pm.ctx, mock.Anything).Return(fmt.Errorf("pop")) 129 _, err := pm.SendMessage(pm.ctx, "ns1", &fftypes.MessageInput{ 130 InputData: fftypes.InputData{ 131 {Value: fftypes.Byteable(`{"some": "data"}`)}, 132 }, 133 Group: &fftypes.InputGroup{ 134 Members: []fftypes.MemberInput{ 135 {Identity: "org1"}, 136 }, 137 }, 138 }) 139 assert.EqualError(t, err, "pop") 140 141 } 142 143 func TestResolveAndSendBadMembers(t *testing.T) { 144 145 pm, cancel := newTestPrivateMessaging(t) 146 defer cancel() 147 148 err := pm.resolveAndSend(pm.ctx, &fftypes.Identity{}, &fftypes.MessageInput{ 149 InputData: fftypes.InputData{ 150 {Value: fftypes.Byteable(`{"some": "data"}`)}, 151 }, 152 }) 153 assert.Regexp(t, "FF10219", err) 154 155 } 156 157 func TestResolveAndSendBadInputData(t *testing.T) { 158 159 pm, cancel := newTestPrivateMessaging(t) 160 defer cancel() 161 162 mdi := pm.database.(*databasemocks.Plugin) 163 mdi.On("GetOrganizationByName", pm.ctx, "localorg").Return(&fftypes.Organization{ 164 ID: fftypes.NewUUID(), 165 }, nil) 166 mdi.On("GetNodes", pm.ctx, mock.Anything).Return([]*fftypes.Node{ 167 {ID: fftypes.NewUUID(), Name: "node1", Owner: "localorg"}, 168 }, nil).Once() 169 mdi.On("GetGroups", pm.ctx, mock.Anything).Return([]*fftypes.Group{ 170 {Hash: fftypes.NewRandB32()}, 171 }, nil).Once() 172 173 mdm := pm.data.(*datamocks.Manager) 174 mdm.On("ResolveInputData", pm.ctx, "ns1", mock.Anything).Return(nil, fmt.Errorf("pop")) 175 176 err := pm.resolveAndSend(pm.ctx, &fftypes.Identity{}, &fftypes.MessageInput{ 177 Message: fftypes.Message{Header: fftypes.MessageHeader{Namespace: "ns1"}}, 178 Group: &fftypes.InputGroup{ 179 Members: []fftypes.MemberInput{ 180 {Identity: "localorg"}, 181 }, 182 }, 183 }) 184 assert.Regexp(t, "pop", err) 185 186 } 187 188 func TestResolveAndSendSealFail(t *testing.T) { 189 190 pm, cancel := newTestPrivateMessaging(t) 191 defer cancel() 192 193 mdi := pm.database.(*databasemocks.Plugin) 194 mdi.On("GetOrganizationByName", pm.ctx, "localorg").Return(&fftypes.Organization{ 195 ID: fftypes.NewUUID(), 196 }, nil) 197 mdi.On("GetNodes", pm.ctx, mock.Anything).Return([]*fftypes.Node{ 198 {ID: fftypes.NewUUID(), Name: "node1", Owner: "localorg"}, 199 }, nil).Once() 200 mdi.On("GetGroups", pm.ctx, mock.Anything).Return([]*fftypes.Group{ 201 {Hash: fftypes.NewRandB32()}, 202 }, nil).Once() 203 204 mdm := pm.data.(*datamocks.Manager) 205 mdm.On("ResolveInputData", pm.ctx, "ns1", mock.Anything).Return(fftypes.DataRefs{ 206 { /* missing */ }, 207 }, nil) 208 209 err := pm.resolveAndSend(pm.ctx, &fftypes.Identity{}, &fftypes.MessageInput{ 210 Message: fftypes.Message{Header: fftypes.MessageHeader{Namespace: "ns1"}}, 211 Group: &fftypes.InputGroup{ 212 Members: []fftypes.MemberInput{ 213 {Identity: "localorg"}, 214 }, 215 }, 216 }) 217 assert.Regexp(t, "FF10144", err) 218 219 }