config_test.go raw

   1  // see ./tests/config_test.go for config tests with DB coverage for both SQlite & Postgres
   2  package config
   3  
   4  import (
   5  	"strconv"
   6  	"testing"
   7  
   8  	"github.com/getAlby/hub/db/migrations"
   9  	"github.com/getAlby/hub/logger"
  10  	"github.com/sirupsen/logrus"
  11  	"github.com/stretchr/testify/assert"
  12  	"github.com/stretchr/testify/require"
  13  	"gorm.io/driver/sqlite"
  14  	"gorm.io/gorm"
  15  )
  16  
  17  func TestCheckCache_NoEncryptionKey(t *testing.T) {
  18  	logger.Init(strconv.Itoa(int(logrus.DebugLevel)))
  19  
  20  	db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
  21  	err = migrations.Migrate(db)
  22  	require.NoError(t, err)
  23  
  24  	cfg, err := NewConfig(&AppConfig{
  25  		Workdir: ".test",
  26  	}, db)
  27  	require.NoError(t, err)
  28  
  29  	err = cfg.SetUpdate("key", "value", "")
  30  	require.NoError(t, err)
  31  
  32  	require.Equal(t, cfg.cache["key"][""], "")
  33  
  34  	value, err := cfg.Get("key", "")
  35  	require.NoError(t, err)
  36  	require.Equal(t, "value", value)
  37  
  38  	require.Equal(t, cfg.cache["key"][""], "value")
  39  
  40  	// test we can access the cached value without the db
  41  	cfg.db = nil
  42  	value, err = cfg.Get("key", "")
  43  	require.NoError(t, err)
  44  	require.Equal(t, "value", value)
  45  }
  46  
  47  func TestCheckUnlockPasswordCache(t *testing.T) {
  48  	logger.Init(strconv.Itoa(int(logrus.DebugLevel)))
  49  	unlockPassword := "123"
  50  
  51  	db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
  52  	err = migrations.Migrate(db)
  53  	require.NoError(t, err)
  54  
  55  	cfg, err := NewConfig(&AppConfig{
  56  		Workdir: ".test",
  57  	}, db)
  58  	require.NoError(t, err)
  59  	err = cfg.SaveUnlockPasswordCheck(unlockPassword)
  60  	require.NoError(t, err)
  61  
  62  	// check cache
  63  	assert.Nil(t, cfg.cache["UnlockPasswordCheck"])
  64  	// check password
  65  	assert.True(t, cfg.CheckUnlockPassword(unlockPassword))
  66  	assert.NotNil(t, cfg.cache["UnlockPasswordCheck"])
  67  
  68  	// check hash cache
  69  	cacheValue, ok := cfg.cache["UnlockPasswordCheck"]
  70  	require.True(t, ok)
  71  	assert.Equal(t, 1, len(cacheValue))
  72  
  73  	assert.False(t, cfg.CheckUnlockPassword(unlockPassword+"1"))
  74  
  75  	// check hash cache - length should not have changed because decrypt failed with invalid password
  76  	cacheValue2, ok := cfg.cache["UnlockPasswordCheck"]
  77  	require.True(t, ok)
  78  	assert.Equal(t, 1, len(cacheValue2))
  79  	require.Equal(t, cacheValue, cacheValue2)
  80  
  81  	// change the password
  82  	newUnlockPassword := unlockPassword + "1"
  83  	err = cfg.ChangeUnlockPassword(unlockPassword, newUnlockPassword)
  84  	require.NoError(t, err)
  85  	assert.Equal(t, 0, len(cfg.cache["UnlockPasswordCheck"]))
  86  
  87  	// test we can access the cached value without the db
  88  	assert.True(t, cfg.CheckUnlockPassword(newUnlockPassword))
  89  	assert.NotNil(t, cfg.cache["UnlockPasswordCheck"])
  90  	cfg.db = nil
  91  	assert.True(t, cfg.CheckUnlockPassword(newUnlockPassword))
  92  
  93  	// should panic when trying to access the db for an uncached value
  94  	hitPanic := false
  95  	func() {
  96  		defer func() {
  97  			// ensure the app cannot panic if firing events to Alby API fails
  98  			if r := recover(); r != nil {
  99  				hitPanic = true
 100  			}
 101  		}()
 102  		assert.False(t, cfg.CheckUnlockPassword(unlockPassword))
 103  	}()
 104  	assert.True(t, hitPanic)
 105  }
 106