github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/manager_test.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package identity
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"github.com/mysteriumnetwork/node/eventbus"
    27  )
    28  
    29  func Test_IdentityManager(t *testing.T) {
    30  	ks := NewMockKeystoreWith(MockKeys)
    31  	idm := &identityManager{
    32  		keystoreManager: ks,
    33  		eventBus:        eventbus.New(),
    34  		unlocked:        map[string]bool{},
    35  	}
    36  
    37  	t.Run("gets existing identities", func(t *testing.T) {
    38  		identities := idm.GetIdentities()
    39  		assert.Len(t, identities, 1)
    40  		assert.Equal(t, FromAddress("0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"), identities[0])
    41  	})
    42  
    43  	var newID Identity
    44  	t.Run("creates a new identity", func(t *testing.T) {
    45  		id, err := idm.CreateNewIdentity("")
    46  		assert.NoError(t, err)
    47  		assert.Len(t, idm.keystoreManager.Accounts(), 2)
    48  		assert.True(t, common.IsHexAddress(id.Address))
    49  		newID = id
    50  	})
    51  
    52  	t.Run("gets identity", func(t *testing.T) {
    53  		identity, err := idm.GetIdentity("0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68")
    54  		assert.NoError(t, err)
    55  		assert.Exactly(t, Identity{"0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"}, identity)
    56  
    57  		identity, err = idm.GetIdentity(newID.Address)
    58  		assert.NoError(t, err)
    59  		assert.Exactly(t, newID, identity)
    60  
    61  		identity, err = idm.GetIdentity("0x000000000000000000000000000000000000000B")
    62  		assert.EqualError(t, err, "identity not found")
    63  		assert.Exactly(t, Identity{}, identity)
    64  	})
    65  
    66  	t.Run("has identity", func(t *testing.T) {
    67  		assert.True(t, idm.HasIdentity("0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"))
    68  		assert.True(t, idm.HasIdentity(newID.Address))
    69  		assert.False(t, idm.HasIdentity("0x000000000000000000000000000000000000000B"))
    70  	})
    71  }