github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/team_bot_settings_test.go (about) 1 package teams 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/keybase/client/go/libkb" 8 "github.com/keybase/client/go/protocol/chat1" 9 keybase1 "github.com/keybase/client/go/protocol/keybase1" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestTeamBotSettings(t *testing.T) { 14 fus, tcs, cleanup := setupNTests(t, 4) 15 defer cleanup() 16 17 t.Logf("create team") 18 teamName, teamID := createTeam2(*tcs[0]) 19 m := make([]libkb.MetaContext, 4) 20 for i, tc := range tcs { 21 m[i] = libkb.NewMetaContextForTest(*tc) 22 } 23 rBotua1, rBotua2 := fus[1], fus[2] 24 botua := fus[3] 25 rBotua1UV, rBotua2UV, botuaUV := rBotua1.GetUserVersion(), rBotua2.GetUserVersion(), botua.GetUserVersion() 26 27 t.Logf("empty bot state") 28 botSettings1 := keybase1.TeamBotSettings{Cmds: true} 29 _, err := AddMember(m[0].Ctx(), tcs[0].G, teamName.String(), rBotua1.Username, keybase1.TeamRole_RESTRICTEDBOT, &botSettings1) 30 require.NoError(t, err) 31 32 botSettings2 := keybase1.TeamBotSettings{Cmds: true} 33 _, err = AddMember(m[0].Ctx(), tcs[0].G, teamName.String(), rBotua2.Username, keybase1.TeamRole_RESTRICTEDBOT, &botSettings2) 34 require.NoError(t, err) 35 36 _, err = AddMember(m[0].Ctx(), tcs[0].G, teamName.String(), botua.Username, keybase1.TeamRole_BOT, nil) 37 require.NoError(t, err) 38 39 team, err := Load(context.TODO(), tcs[0].G, keybase1.LoadTeamArg{ 40 ID: teamID, 41 ForceRepoll: true, 42 }) 43 require.NoError(t, err) 44 teamBotSettings, err := team.TeamBotSettings() 45 require.NoError(t, err) 46 // Defaults without a bot settings link 47 expectedBots := map[keybase1.UserVersion]keybase1.TeamBotSettings{ 48 rBotua1UV: botSettings1, 49 rBotua2UV: botSettings2, 50 } 51 require.Len(t, teamBotSettings, 2) 52 53 // happy paths 54 botSettings1 = keybase1.TeamBotSettings{ 55 Cmds: true, 56 Mentions: true, 57 Triggers: []string{"shipit"}, 58 Convs: []string{chat1.ConversationID([]byte("convo")).String()}, 59 } 60 err = team.PostTeamBotSettings(context.TODO(), map[keybase1.UserVersion]keybase1.TeamBotSettings{ 61 rBotua1UV: botSettings1, 62 }) 63 require.NoError(t, err) 64 65 team, err = Load(context.TODO(), tcs[0].G, keybase1.LoadTeamArg{ 66 ID: teamID, 67 ForceRepoll: true, 68 }) 69 require.NoError(t, err) 70 teamBotSettings, err = team.TeamBotSettings() 71 require.NoError(t, err) 72 expectedBots[rBotua1UV] = botSettings1 73 require.Equal(t, expectedBots, teamBotSettings) 74 75 // update settings 76 botSettings1 = keybase1.TeamBotSettings{Cmds: true} 77 expectedBots[rBotua1UV] = botSettings1 78 err = team.PostTeamBotSettings(context.TODO(), expectedBots) 79 require.NoError(t, err) 80 81 team, err = Load(context.TODO(), tcs[0].G, keybase1.LoadTeamArg{ 82 ID: teamID, 83 ForceRepoll: true, 84 }) 85 require.NoError(t, err) 86 teamBotSettings, err = team.TeamBotSettings() 87 require.NoError(t, err) 88 require.Equal(t, expectedBots, teamBotSettings) 89 90 // add a second bot 91 settings2 := keybase1.TeamBotSettings{Cmds: true} 92 expectedBots = map[keybase1.UserVersion]keybase1.TeamBotSettings{ 93 rBotua2UV: settings2, 94 } 95 err = team.PostTeamBotSettings(context.TODO(), expectedBots) 96 require.NoError(t, err) 97 98 team, err = Load(context.TODO(), tcs[0].G, keybase1.LoadTeamArg{ 99 ID: teamID, 100 ForceRepoll: true, 101 }) 102 require.NoError(t, err) 103 teamBotSettings, err = team.TeamBotSettings() 104 require.NoError(t, err) 105 expectedBots[rBotua1UV] = botSettings1 106 require.Equal(t, expectedBots, teamBotSettings) 107 108 // Role change for rBotua1, they get nuked from the bot settings 109 err = SetRoleBot(context.TODO(), tcs[0].G, team.Name().String(), rBotua1.Username) 110 require.NoError(t, err) 111 team, err = Load(context.TODO(), tcs[0].G, keybase1.LoadTeamArg{ 112 ID: teamID, 113 ForceRepoll: true, 114 }) 115 require.NoError(t, err) 116 teamBotSettings, err = team.TeamBotSettings() 117 require.NoError(t, err) 118 delete(expectedBots, rBotua1UV) 119 require.Equal(t, expectedBots, teamBotSettings) 120 121 // sad paths 122 // must specify a restricted-bot member but botua is a BOT 123 expectedBots = map[keybase1.UserVersion]keybase1.TeamBotSettings{ 124 botuaUV: {}, 125 } 126 err = team.PostTeamBotSettings(context.TODO(), expectedBots) 127 require.Error(t, err) 128 require.IsType(t, PrecheckAppendError{}, err) 129 130 // bad trigger regex 131 expectedBots[rBotua1UV] = keybase1.TeamBotSettings{Triggers: []string{"*"}} 132 err = team.PostTeamBotSettings(context.TODO(), expectedBots) 133 require.Error(t, err) 134 135 // bad conv ids 136 expectedBots[rBotua1UV] = keybase1.TeamBotSettings{Convs: []string{"not hex"}} 137 err = team.PostTeamBotSettings(context.TODO(), expectedBots) 138 require.Error(t, err) 139 }