github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/user_blocks_test.go (about)

     1  package engine
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/keybase1"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestUserBlocking(t *testing.T) {
    13  	tc1 := SetupEngineTest(t, "blocking")
    14  	defer tc1.Cleanup()
    15  	u1 := CreateAndSignupFakeUser(tc1, "ublk")
    16  	m1 := NewMetaContextForTest(tc1).WithUIs(libkb.UIs{
    17  		LogUI: tc1.G.UI.GetLogUI(),
    18  	})
    19  
    20  	tc2 := SetupEngineTest(t, "blocking")
    21  	defer tc2.Cleanup()
    22  	u2 := CreateAndSignupFakeUser(tc2, "ublk")
    23  	m2 := NewMetaContextForTest(tc2).WithUIs(libkb.UIs{
    24  		LogUI: tc2.G.UI.GetLogUI(),
    25  	})
    26  
    27  	// Both users should have 0 blocks
    28  	e1 := NewUserBlocksGet(tc1.G, keybase1.GetUserBlocksArg{})
    29  	require.NoError(t, RunEngine2(m1, e1))
    30  	require.Len(t, e1.Blocks(), 0)
    31  
    32  	e1 = NewUserBlocksGet(tc2.G, keybase1.GetUserBlocksArg{})
    33  	require.NoError(t, RunEngine2(m2, e1))
    34  	require.Len(t, e1.Blocks(), 0)
    35  
    36  	// Chat block
    37  	yes := true
    38  	e2 := NewUserBlocksSet(tc1.G, keybase1.SetUserBlocksArg{
    39  		Blocks: []keybase1.UserBlockArg{{
    40  			Username:     u2.Username,
    41  			SetChatBlock: &yes,
    42  		}},
    43  	})
    44  	require.NoError(t, RunEngine2(m1, e2))
    45  
    46  	e1 = NewUserBlocksGet(tc1.G, keybase1.GetUserBlocksArg{})
    47  	require.NoError(t, RunEngine2(m1, e1))
    48  	blocks := e1.Blocks()
    49  	require.Len(t, blocks, 1)
    50  	require.Equal(t, u2.Username, blocks[0].Username)
    51  	require.Equal(t, true, blocks[0].ChatBlocked)
    52  	require.Equal(t, false, blocks[0].FollowBlocked)
    53  	require.NotNil(t, blocks[0].CreateTime)
    54  	require.Nil(t, blocks[0].ModifyTime)
    55  
    56  	// Follow block
    57  	e2 = NewUserBlocksSet(tc1.G, keybase1.SetUserBlocksArg{
    58  		Blocks: []keybase1.UserBlockArg{{
    59  			Username:       u2.Username,
    60  			SetFollowBlock: &yes,
    61  		}},
    62  	})
    63  	require.NoError(t, RunEngine2(m1, e2))
    64  
    65  	e1 = NewUserBlocksGet(tc1.G, keybase1.GetUserBlocksArg{})
    66  	require.NoError(t, RunEngine2(m1, e1))
    67  	blocks = e1.Blocks()
    68  	require.Len(t, blocks, 1)
    69  	require.Equal(t, u2.Username, blocks[0].Username)
    70  	require.Equal(t, true, blocks[0].ChatBlocked)
    71  	require.Equal(t, true, blocks[0].FollowBlocked)
    72  	require.NotNil(t, blocks[0].CreateTime)
    73  	require.NotNil(t, blocks[0].ModifyTime)
    74  
    75  	// Unblocking
    76  	no := false
    77  	e2 = NewUserBlocksSet(tc1.G, keybase1.SetUserBlocksArg{
    78  		Blocks: []keybase1.UserBlockArg{{
    79  			Username:     u2.Username,
    80  			SetChatBlock: &no,
    81  		}, {
    82  			Username:       u2.Username,
    83  			SetFollowBlock: &no,
    84  		}},
    85  	})
    86  	require.NoError(t, RunEngine2(m1, e2))
    87  
    88  	e1 = NewUserBlocksGet(tc1.G, keybase1.GetUserBlocksArg{})
    89  	require.NoError(t, RunEngine2(m1, e1))
    90  	blocks = e1.Blocks()
    91  	require.Len(t, blocks, 1)
    92  	require.Equal(t, u2.Username, blocks[0].Username)
    93  	require.Equal(t, false, blocks[0].ChatBlocked)
    94  	require.Equal(t, false, blocks[0].FollowBlocked)
    95  	require.NotNil(t, blocks[0].CreateTime)
    96  	require.NotNil(t, blocks[0].ModifyTime)
    97  
    98  	// Try to get blocks with username filter.
    99  	e1 = NewUserBlocksGet(tc1.G, keybase1.GetUserBlocksArg{
   100  		Usernames: []string{u2.Username},
   101  	})
   102  	require.NoError(t, RunEngine2(m1, e1))
   103  	blocks2 := e1.Blocks()
   104  
   105  	// Expect to see same things as in unfiltered call.
   106  	require.Equal(t, blocks, blocks2)
   107  
   108  	// Try to get blocks with username filter for different username than tc2.
   109  	e1 = NewUserBlocksGet(tc1.G, keybase1.GetUserBlocksArg{
   110  		Usernames: []string{u1.Username},
   111  	})
   112  	require.NoError(t, RunEngine2(m1, e1))
   113  	blocks2 = e1.Blocks()
   114  	require.Len(t, blocks2, 0)
   115  }