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

     1  package chat
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/protocol/chat1"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestDevConversationBackedStorage(t *testing.T) {
    11  	useRemoteMock = false
    12  	defer func() { useRemoteMock = true }()
    13  	ctc := makeChatTestContext(t, "TestDevConversationBackedStorage", 1)
    14  	defer ctc.cleanup()
    15  
    16  	users := ctc.users()
    17  	tc := ctc.world.Tcs[users[0].Username]
    18  	ri := ctc.as(t, users[0]).ri
    19  	ctx := ctc.as(t, users[0]).startCtx
    20  	uid := users[0].User.GetUID().ToBytes()
    21  
    22  	key0 := "storage0"
    23  	key1 := "storage1"
    24  	storage := NewDevConversationBackedStorage(tc.Context(), func() chat1.RemoteInterface { return ri })
    25  	settings := chat1.UnfurlSettings{
    26  		Mode:      chat1.UnfurlMode_WHITELISTED,
    27  		Whitelist: make(map[string]bool),
    28  	}
    29  	require.NoError(t, storage.Put(ctx, uid, key0, settings))
    30  	var settingsRes chat1.UnfurlSettings
    31  	found, err := storage.Get(ctx, uid, key0, &settingsRes)
    32  	require.NoError(t, err)
    33  	require.True(t, found)
    34  	require.Equal(t, chat1.UnfurlMode_WHITELISTED, settingsRes.Mode)
    35  	require.Zero(t, len(settingsRes.Whitelist))
    36  
    37  	settings.Mode = chat1.UnfurlMode_NEVER
    38  	require.NoError(t, storage.Put(ctx, uid, key0, settings))
    39  	found, err = storage.Get(ctx, uid, key0, &settingsRes)
    40  	require.NoError(t, err)
    41  	require.True(t, found)
    42  	require.Equal(t, chat1.UnfurlMode_NEVER, settingsRes.Mode)
    43  	require.Zero(t, len(settingsRes.Whitelist))
    44  
    45  	settings.Mode = chat1.UnfurlMode_WHITELISTED
    46  	settings.Whitelist["MIKE"] = true
    47  	require.NoError(t, storage.Put(ctx, uid, key1, settings))
    48  	found, err = storage.Get(ctx, uid, key0, &settingsRes)
    49  	require.NoError(t, err)
    50  	require.True(t, found)
    51  	require.Equal(t, chat1.UnfurlMode_NEVER, settingsRes.Mode)
    52  	require.Zero(t, len(settingsRes.Whitelist))
    53  	found, err = storage.Get(ctx, uid, key1, &settingsRes)
    54  	require.NoError(t, err)
    55  	require.True(t, found)
    56  	require.Equal(t, chat1.UnfurlMode_WHITELISTED, settingsRes.Mode)
    57  	require.Equal(t, 1, len(settingsRes.Whitelist))
    58  	require.True(t, settingsRes.Whitelist["MIKE"])
    59  
    60  	found, err = storage.Get(ctx, uid, "AHHHHH CANT FIND ME", &settingsRes)
    61  	require.NoError(t, err)
    62  	require.False(t, found)
    63  }