github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/cache_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  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var file = "/tmp/cache.json"
    28  
    29  func TestIdentityCache_StoreIdentity(t *testing.T) {
    30  	identity := FromAddress("0x000000000000000000000000000000000000000A")
    31  	cache := IdentityCache{
    32  		File: file,
    33  	}
    34  
    35  	err := cache.StoreIdentity(identity)
    36  	assert.Nil(t, err)
    37  }
    38  
    39  func TestIdentityCache_GetIdentity(t *testing.T) {
    40  	identity := FromAddress("0x000000000000000000000000000000000000000A")
    41  	cache := IdentityCache{
    42  		File: file,
    43  	}
    44  
    45  	err := cache.StoreIdentity(identity)
    46  	assert.Nil(t, err)
    47  	id, err := cache.GetIdentity()
    48  
    49  	assert.Equal(t, id, identity)
    50  	assert.Nil(t, err)
    51  }
    52  
    53  func TestIdentityCache_GetIdentityWithNoCache(t *testing.T) {
    54  	cache := IdentityCache{
    55  		File: "does-not-exist",
    56  	}
    57  
    58  	_, err := cache.GetIdentity()
    59  
    60  	assert.EqualError(t, err, "cache file does not exist")
    61  }
    62  
    63  func TestIdentityCache_cacheExists(t *testing.T) {
    64  	identity := FromAddress("0x000000000000000000000000000000000000000A")
    65  	cache := IdentityCache{
    66  		File: file,
    67  	}
    68  
    69  	err := cache.StoreIdentity(identity)
    70  	assert.Nil(t, err)
    71  
    72  	assert.True(t, cache.cacheExists())
    73  
    74  	_, err = os.Stat(file)
    75  	assert.True(t, err == nil && !os.IsNotExist(err))
    76  }