github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/convdevstorage_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 TestDevConversationBackedStorageTeamAdminOnly(t *testing.T) { 11 useRemoteMock = false 12 defer func() { useRemoteMock = true }() 13 ctc := makeChatTestContext(t, t.Name(), 2) 14 defer ctc.cleanup() 15 16 users := ctc.users() 17 18 alice := users[0] 19 tc := ctc.world.Tcs[alice.Username] 20 ri := ctc.as(t, alice).ri 21 ctx := ctc.as(t, alice).startCtx 22 uid := alice.User.GetUID().ToBytes() 23 storage := NewConvDevConversationBackedStorage(tc.Context(), 24 chat1.TopicType_DEV, true /* adminOnly */, func() chat1.RemoteInterface { return ri }) 25 var msg string 26 27 bob := users[1] 28 readeruid := bob.User.GetUID().ToBytes() 29 readertc := ctc.world.Tcs[bob.Username] 30 readerri := ctc.as(t, bob).ri 31 readerctx := ctc.as(t, bob).startCtx 32 readerstorage := NewConvDevConversationBackedStorage(readertc.Context(), 33 chat1.TopicType_DEV, true /* adminOnly */, func() chat1.RemoteInterface { return readerri }) 34 var readermsg string 35 36 conv := mustCreateChannelForTest(t, ctc, alice, chat1.TopicType_CHAT, nil, 37 chat1.ConversationMembersType_TEAM, bob) 38 key0 := "mykey" 39 40 found, _, err := storage.Get(ctx, uid, conv.Id, key0, &msg, false) 41 require.NoError(t, err) 42 require.False(t, found) 43 found, _, err = readerstorage.Get(readerctx, readeruid, conv.Id, key0, &readermsg, false) 44 require.NoError(t, err) 45 require.False(t, found) 46 47 err = storage.Put(ctx, uid, conv.Id, key0, "hello") 48 require.NoError(t, err) 49 50 err = readerstorage.Put(readerctx, readeruid, conv.Id, key0, "hello") 51 require.Error(t, err) 52 require.IsType(t, &DevStoragePermissionDeniedError{}, err, "got right error") 53 54 found, _, err = storage.Get(ctx, uid, conv.Id, key0, &msg, false) 55 require.NoError(t, err) 56 require.True(t, found) 57 require.Equal(t, msg, "hello") 58 found, _, err = readerstorage.Get(readerctx, readeruid, conv.Id, key0, &readermsg, false) 59 require.NoError(t, err) 60 require.True(t, found) 61 require.Equal(t, msg, "hello") 62 } 63 64 func TestDevConversationBackedStorageTeamAdminOnlyReaderMisbehavior(t *testing.T) { 65 useRemoteMock = false 66 defer func() { useRemoteMock = true }() 67 ctc := makeChatTestContext(t, t.Name(), 2) 68 defer ctc.cleanup() 69 70 users := ctc.users() 71 72 alice := users[0] 73 tc := ctc.world.Tcs[alice.Username] 74 ri := ctc.as(t, alice).ri 75 ctx := ctc.as(t, alice).startCtx 76 uid := alice.User.GetUID().ToBytes() 77 storage := NewConvDevConversationBackedStorage(tc.Context(), 78 chat1.TopicType_DEV, true /* adminOnly */, func() chat1.RemoteInterface { return ri }) 79 var msg string 80 81 bob := users[1] 82 readertc := ctc.world.Tcs[bob.Username] 83 readerri := ctc.as(t, bob).ri 84 readerctx := ctc.as(t, bob).startCtx 85 readeruid := bob.User.GetUID().ToBytes() 86 readerstorage := NewConvDevConversationBackedStorage(readertc.Context(), 87 chat1.TopicType_DEV, true /* adminOnly */, func() chat1.RemoteInterface { return readerri }) 88 var readermsg string 89 90 conv := mustCreateChannelForTest(t, ctc, alice, chat1.TopicType_CHAT, nil, 91 chat1.ConversationMembersType_TEAM, bob) 92 key0 := "mykey" 93 94 found, _, err := storage.Get(ctx, uid, conv.Id, key0, &msg, false) 95 require.NoError(t, err) 96 require.False(t, found) 97 found, _, err = readerstorage.Get(readerctx, readeruid, conv.Id, key0, &readermsg, false) 98 require.NoError(t, err) 99 require.False(t, found) 100 101 err = readerstorage.Put(readerctx, readeruid, conv.Id, key0, "evil") 102 require.Error(t, err) 103 require.IsType(t, &DevStoragePermissionDeniedError{}, err, "got right error") 104 105 // work around client-side protection and make dev channel/msg anyway 106 devconv := mustCreateChannelForTest(t, ctc, bob, chat1.TopicType_DEV, &key0, 107 chat1.ConversationMembersType_TEAM, alice) 108 larg := chat1.PostLocalArg{ 109 ConversationID: devconv.Id, 110 Msg: chat1.MessagePlaintext{ 111 ClientHeader: chat1.MessageClientHeader{ 112 Conv: devconv.Triple, 113 TlfName: devconv.TlfName, 114 MessageType: chat1.MessageType_TEXT, 115 }, 116 MessageBody: chat1.NewMessageBodyWithText(chat1.MessageText{Body: "reallyevil"}), 117 }, 118 } 119 _, err = ctc.as(t, bob).chatLocalHandler().PostLocal(ctc.as(t, bob).startCtx, larg) 120 require.NoError(t, err) 121 122 _, _, err = storage.Get(ctx, uid, conv.Id, key0, &msg, false) 123 require.Error(t, err, "got an error after misbehavior") 124 require.IsType(t, &DevStorageAdminOnlyError{}, err, "got a permission error") 125 126 _, _, err = readerstorage.Get(readerctx, readeruid, conv.Id, key0, &readermsg, false) 127 require.Error(t, err, "got an error after misbehavior") 128 require.IsType(t, &DevStorageAdminOnlyError{}, err, "got a permission error") 129 }