github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/manager_fake.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 "github.com/pkg/errors"
    21  
    22  type idmFake struct {
    23  	LastUnlockAddress    string
    24  	LastUnlockPassphrase string
    25  	LastUnlockChainID    int64
    26  	existingIdentities   []Identity
    27  	newIdentity          Identity
    28  	unlockFails          bool
    29  	isUnlocked           bool
    30  }
    31  
    32  // NewIdentityManagerFake creates fake identity manager for testing purposes
    33  // TODO each caller should use it's own mocked manager part instead of global one
    34  func NewIdentityManagerFake(existingIdentities []Identity, newIdentity Identity) *idmFake {
    35  	return &idmFake{"", "", 0, existingIdentities, newIdentity, false, true}
    36  }
    37  
    38  func (fakeIdm *idmFake) IsUnlocked(id string) bool {
    39  	return fakeIdm.isUnlocked
    40  }
    41  
    42  func (fakeIdm *idmFake) MarkUnlockToFail() {
    43  	fakeIdm.unlockFails = true
    44  }
    45  
    46  func (fakeIdm *idmFake) CreateNewIdentity(_ string) (Identity, error) {
    47  	return fakeIdm.newIdentity, nil
    48  }
    49  func (fakeIdm *idmFake) GetIdentities() []Identity {
    50  	return fakeIdm.existingIdentities
    51  }
    52  
    53  func (fakeIdm *idmFake) GetUnlockedIdentity() (Identity, bool) {
    54  	return fakeIdm.newIdentity, false
    55  }
    56  
    57  func (fakeIdm *idmFake) GetIdentity(address string) (Identity, error) {
    58  	for _, fakeIdentity := range fakeIdm.existingIdentities {
    59  		if address == fakeIdentity.Address {
    60  			return fakeIdentity, nil
    61  		}
    62  	}
    63  	return Identity{}, errors.New("Identity not found")
    64  }
    65  func (fakeIdm *idmFake) HasIdentity(_ string) bool {
    66  	return true
    67  }
    68  
    69  func (fakeIdm *idmFake) Unlock(chainID int64, address string, passphrase string) error {
    70  	fakeIdm.LastUnlockAddress = address
    71  	fakeIdm.LastUnlockPassphrase = passphrase
    72  	fakeIdm.LastUnlockChainID = chainID
    73  	if fakeIdm.unlockFails {
    74  		return errors.New("Unlock failed")
    75  	}
    76  	return nil
    77  }