github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/unfurl/settings_test.go (about)

     1  package unfurl
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"sync"
     7  	"testing"
     8  
     9  	"github.com/keybase/client/go/chat/globals"
    10  	"github.com/keybase/client/go/externalstest"
    11  	"github.com/keybase/client/go/protocol/chat1"
    12  	"github.com/keybase/client/go/protocol/gregor1"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  type memConversationBackedStorage struct {
    17  	sync.Mutex
    18  	storage map[string]string
    19  }
    20  
    21  func newMemConversationBackedStorage() *memConversationBackedStorage {
    22  	return &memConversationBackedStorage{
    23  		storage: make(map[string]string),
    24  	}
    25  }
    26  
    27  func (s *memConversationBackedStorage) Get(ctx context.Context, uid gregor1.UID, name string, res interface{}) (bool, error) {
    28  	s.Lock()
    29  	defer s.Unlock()
    30  	dat, ok := s.storage[name]
    31  	if !ok {
    32  		return false, nil
    33  	}
    34  	err := json.Unmarshal([]byte(dat), res)
    35  	return true, err
    36  }
    37  
    38  func (s *memConversationBackedStorage) Put(ctx context.Context, uid gregor1.UID, name string, src interface{}) error {
    39  	s.Lock()
    40  	defer s.Unlock()
    41  	dat, err := json.Marshal(src)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	s.storage[name] = string(dat)
    46  	return nil
    47  }
    48  
    49  func TestUnfurlSetting(t *testing.T) {
    50  	tc := externalstest.SetupTest(t, "chat_settings", 1)
    51  	defer tc.Cleanup()
    52  	g := globals.NewContext(tc.G, &globals.ChatContext{})
    53  
    54  	uid := gregor1.UID([]byte{0, 1})
    55  	settings := NewSettings(g, newMemConversationBackedStorage())
    56  	res, err := settings.Get(context.TODO(), uid)
    57  	require.NoError(t, err)
    58  	require.Equal(t, chat1.UnfurlMode_WHITELISTED, res.Mode)
    59  	require.Zero(t, len(res.Whitelist))
    60  	require.NoError(t, settings.WhitelistAdd(context.TODO(), uid, "yahoo.com"))
    61  	res, err = settings.Get(context.TODO(), uid)
    62  	require.NoError(t, err)
    63  	require.Equal(t, chat1.UnfurlMode_WHITELISTED, res.Mode)
    64  	require.Equal(t, 1, len(res.Whitelist))
    65  	require.True(t, res.Whitelist["yahoo.com"])
    66  	require.NoError(t, settings.WhitelistAdd(context.TODO(), uid, "google.com"))
    67  	res, err = settings.Get(context.TODO(), uid)
    68  	require.NoError(t, err)
    69  	require.Equal(t, chat1.UnfurlMode_WHITELISTED, res.Mode)
    70  	require.Equal(t, 2, len(res.Whitelist))
    71  	require.True(t, res.Whitelist["google.com"])
    72  	require.True(t, res.Whitelist["yahoo.com"])
    73  	require.NoError(t, settings.SetMode(context.TODO(), uid, chat1.UnfurlMode_NEVER))
    74  	res, err = settings.Get(context.TODO(), uid)
    75  	require.NoError(t, err)
    76  	require.Equal(t, chat1.UnfurlMode_NEVER, res.Mode)
    77  	require.Equal(t, 2, len(res.Whitelist))
    78  	require.True(t, res.Whitelist["google.com"])
    79  	require.True(t, res.Whitelist["yahoo.com"])
    80  	require.NoError(t, settings.WhitelistRemove(context.TODO(), uid, "google.com"))
    81  	res, err = settings.Get(context.TODO(), uid)
    82  	require.NoError(t, err)
    83  	require.Equal(t, 1, len(res.Whitelist))
    84  	require.True(t, res.Whitelist["yahoo.com"])
    85  }