github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/networkmap/register_org_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/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 TestRegisterOrganizationChildOk(t *testing.T) {
    33  
    34  	nm, cancel := newTestNetworkmap(t)
    35  	defer cancel()
    36  
    37  	mdi := nm.database.(*databasemocks.Plugin)
    38  	mdi.On("GetOrganizationByIdentity", nm.ctx, "0x23456").Return(&fftypes.Organization{
    39  		Identity:    "0x23456",
    40  		Description: "parent organization",
    41  	}, nil)
    42  
    43  	mii := nm.identity.(*identitymocks.Plugin)
    44  	childID := &fftypes.Identity{OnChain: "0x12345"}
    45  	parentID := &fftypes.Identity{OnChain: "0x23456"}
    46  	mii.On("Resolve", nm.ctx, "0x12345").Return(childID, nil)
    47  	mii.On("Resolve", nm.ctx, "0x23456").Return(parentID, nil)
    48  
    49  	mockMsg := &fftypes.Message{Header: fftypes.MessageHeader{ID: fftypes.NewUUID()}}
    50  	mbm := nm.broadcast.(*broadcastmocks.Manager)
    51  	mbm.On("BroadcastDefinition", nm.ctx, mock.Anything, parentID, fftypes.SystemTagDefineOrganization).Return(mockMsg, nil)
    52  
    53  	msg, err := nm.RegisterOrganization(nm.ctx, &fftypes.Organization{
    54  		Name:        "org1",
    55  		Identity:    "0x12345",
    56  		Parent:      "0x23456",
    57  		Description: "my organization",
    58  	})
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, mockMsg, msg)
    61  
    62  }
    63  
    64  func TestRegisterNodeOrganizationRootOk(t *testing.T) {
    65  
    66  	nm, cancel := newTestNetworkmap(t)
    67  	defer cancel()
    68  
    69  	config.Set(config.OrgName, "org1")
    70  	config.Set(config.OrgIdentity, "0x12345")
    71  	config.Set(config.OrgDescription, "my organization")
    72  
    73  	mii := nm.identity.(*identitymocks.Plugin)
    74  	rootID := &fftypes.Identity{OnChain: "0x12345"}
    75  	mii.On("Resolve", nm.ctx, "0x12345").Return(rootID, nil)
    76  
    77  	mockMsg := &fftypes.Message{Header: fftypes.MessageHeader{ID: fftypes.NewUUID()}}
    78  	mbm := nm.broadcast.(*broadcastmocks.Manager)
    79  	mbm.On("BroadcastDefinition", nm.ctx, mock.Anything, rootID, fftypes.SystemTagDefineOrganization).Return(mockMsg, nil)
    80  
    81  	msg, err := nm.RegisterNodeOrganization(nm.ctx)
    82  	assert.NoError(t, err)
    83  	assert.Equal(t, mockMsg, msg)
    84  
    85  }
    86  
    87  func TestRegisterNodeOrganizationMissingConfig(t *testing.T) {
    88  
    89  	nm, cancel := newTestNetworkmap(t)
    90  	defer cancel()
    91  
    92  	config.Set(config.OrgIdentity, nil)
    93  
    94  	_, err := nm.RegisterNodeOrganization(nm.ctx)
    95  	assert.Regexp(t, "FF10216", err)
    96  
    97  }
    98  
    99  func TestRegisterOrganizationBadObject(t *testing.T) {
   100  
   101  	nm, cancel := newTestNetworkmap(t)
   102  	defer cancel()
   103  
   104  	_, err := nm.RegisterOrganization(nm.ctx, &fftypes.Organization{
   105  		Name:        "org1",
   106  		Description: string(make([]byte, 4097)),
   107  	})
   108  	assert.Regexp(t, "FF10188", err)
   109  
   110  }
   111  
   112  func TestRegisterOrganizationBadIdentity(t *testing.T) {
   113  
   114  	nm, cancel := newTestNetworkmap(t)
   115  	defer cancel()
   116  
   117  	mii := nm.identity.(*identitymocks.Plugin)
   118  	mii.On("Resolve", nm.ctx, "!wrong").Return(nil, fmt.Errorf("pop"))
   119  
   120  	_, err := nm.RegisterOrganization(nm.ctx, &fftypes.Organization{
   121  		Name:     "org1",
   122  		Identity: "!wrong",
   123  	})
   124  	assert.Regexp(t, "FF10215.*pop", err)
   125  
   126  }
   127  
   128  func TestRegisterOrganizationBadParent(t *testing.T) {
   129  
   130  	nm, cancel := newTestNetworkmap(t)
   131  	defer cancel()
   132  
   133  	mii := nm.identity.(*identitymocks.Plugin)
   134  	childID := &fftypes.Identity{OnChain: "0x12345"}
   135  	mii.On("Resolve", nm.ctx, "0x12345").Return(childID, nil)
   136  	mdi := nm.database.(*databasemocks.Plugin)
   137  	mdi.On("GetOrganizationByIdentity", nm.ctx, "wrongun").Return(nil, nil)
   138  
   139  	_, err := nm.RegisterOrganization(nm.ctx, &fftypes.Organization{
   140  		Name:     "org1",
   141  		Identity: "0x12345",
   142  		Parent:   "wrongun",
   143  	})
   144  	assert.Regexp(t, "FF10214", err)
   145  
   146  }
   147  
   148  func TestRegisterOrganizationChildResolveFail(t *testing.T) {
   149  
   150  	nm, cancel := newTestNetworkmap(t)
   151  	defer cancel()
   152  
   153  	mii := nm.identity.(*identitymocks.Plugin)
   154  	mii.On("Resolve", nm.ctx, "0x12345").Return(nil, fmt.Errorf("pop"))
   155  
   156  	_, err := nm.RegisterOrganization(nm.ctx, &fftypes.Organization{
   157  		Name:     "org1",
   158  		Identity: "0x12345",
   159  		Parent:   "0x23456",
   160  	})
   161  	assert.Regexp(t, "pop", err)
   162  
   163  }
   164  
   165  func TestRegisterOrganizationParentLookupFail(t *testing.T) {
   166  
   167  	nm, cancel := newTestNetworkmap(t)
   168  	defer cancel()
   169  
   170  	mii := nm.identity.(*identitymocks.Plugin)
   171  	childID := &fftypes.Identity{OnChain: "0x12345"}
   172  	mii.On("Resolve", nm.ctx, "0x12345").Return(childID, nil)
   173  	mdi := nm.database.(*databasemocks.Plugin)
   174  	mdi.On("GetOrganizationByIdentity", nm.ctx, "0x23456").Return(nil, fmt.Errorf("pop"))
   175  
   176  	_, err := nm.RegisterOrganization(nm.ctx, &fftypes.Organization{
   177  		Name:     "org1",
   178  		Identity: "0x12345",
   179  		Parent:   "0x23456",
   180  	})
   181  	assert.Regexp(t, "pop", err)
   182  
   183  }