github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/get_test.go (about) 1 package teams 2 3 import ( 4 "encoding/hex" 5 "testing" 6 "time" 7 8 "golang.org/x/net/context" 9 10 "github.com/keybase/client/go/kbtest" 11 "github.com/keybase/client/go/libkb" 12 "github.com/keybase/client/go/protocol/keybase1" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestTeamGet(t *testing.T) { 17 tc := SetupTest(t, "team", 1) 18 defer tc.Cleanup() 19 20 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 21 require.NoError(t, err) 22 23 name := createTeam(tc) 24 25 _, err = GetForTestByStringName(context.TODO(), tc.G, name) 26 if err != nil { 27 t.Fatal(err) 28 } 29 } 30 31 func TestTeamApplicationKey(t *testing.T) { 32 tc := SetupTest(t, "team", 1) 33 defer tc.Cleanup() 34 35 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 36 require.NoError(t, err) 37 38 name := createTeam(tc) 39 40 team, err := Load(context.TODO(), tc.G, keybase1.LoadTeamArg{ 41 Name: name, 42 }) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 chatKey, err := team.ChatKey(context.TODO()) 48 if err != nil { 49 t.Fatal(err) 50 } 51 if chatKey.Application != keybase1.TeamApplication_CHAT { 52 t.Errorf("key application: %d, expected %d", chatKey.Application, keybase1.TeamApplication_CHAT) 53 } 54 if chatKey.Generation() != 1 { 55 t.Errorf("key generation: %d, expected 1", chatKey.Generation()) 56 } 57 if len(chatKey.Key) != 32 { 58 t.Errorf("key length: %d, expected 32", len(chatKey.Key)) 59 } 60 } 61 62 func TestTeamGetRepeat(t *testing.T) { 63 t.Skip("not needed") 64 // in order to try to repro in CI, run this 10 times 65 for i := 0; i < 10; i++ { 66 tc := SetupTest(t, "team", 1) 67 defer tc.Cleanup() 68 69 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 70 require.NoError(t, err) 71 72 name := createTeam(tc) 73 74 _, err = GetForTestByStringName(context.TODO(), tc.G, name) 75 if err != nil { 76 t.Fatal(err) 77 } 78 } 79 } 80 81 func TestTeamGetWhileCreate(t *testing.T) { 82 t.Skip("this found create team bug") 83 tc := SetupTest(t, "team", 1) 84 defer tc.Cleanup() 85 86 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 87 require.NoError(t, err) 88 89 name := createTeam(tc) 90 91 for i := 0; i < 100; i++ { 92 go createTeam(tc) 93 time.Sleep(10 * time.Millisecond) 94 } 95 96 for i := 0; i < 100; i++ { 97 _, err := GetForTestByStringName(context.TODO(), tc.G, name) 98 if err != nil { 99 t.Fatal(err) 100 } 101 } 102 } 103 104 func TestTeamGetConcurrent(t *testing.T) { 105 t.Skip("this is slow but it passes") 106 work := make(chan bool) 107 108 for i := 0; i < 10; i++ { 109 go func() { 110 for x := range work { 111 _ = x 112 teamGet(t) 113 } 114 }() 115 } 116 117 for j := 0; j < 100; j++ { 118 work <- true 119 } 120 } 121 122 // Test TeamGet on a team that you implicitly admin but 123 // are not an explicit member of. 124 func TestTeamDetailsAsImplicitAdmin(t *testing.T) { 125 _, tcs, cleanup := setupNTests(t, 1) 126 defer cleanup() 127 128 t.Logf("creates a team") 129 teamName, _ := createTeam2(*tcs[0]) 130 131 t.Logf("creates a subteam") 132 _, err := CreateSubteam(context.Background(), tcs[0].G, "bbb", teamName, keybase1.TeamRole_NONE /* addSelfAs */) 133 require.NoError(t, err) 134 135 t.Logf("loads the subteam") 136 team, err := GetAnnotatedTeamByName(context.Background(), tcs[0].G, teamName.String()+".bbb") 137 require.NoError(t, err) 138 require.Len(t, team.Members, 0, "should be no team members in subteam") 139 } 140 141 // Test loading when you have become an admin after 142 // having already cached the team as a non-admin. 143 func TestGetMaybeAdminByStringName(t *testing.T) { 144 fus, tcs, cleanup := setupNTests(t, 2) 145 defer cleanup() 146 147 t.Logf("U0 creates a team") 148 teamName, _ := createTeam2(*tcs[0]) 149 150 t.Logf("U0 creates a subteam") 151 _, err := CreateSubteam(context.TODO(), tcs[0].G, "abc", teamName, keybase1.TeamRole_NONE /* addSelfAs */) 152 require.NoError(t, err) 153 154 t.Logf("U0 adds U1 as a reader") 155 _, err = AddMember(context.TODO(), tcs[0].G, teamName.String(), fus[1].Username, keybase1.TeamRole_READER, nil) 156 require.NoError(t, err) 157 158 t.Logf("U1 loads and is a reader") 159 team, err := Load(context.TODO(), tcs[1].G, keybase1.LoadTeamArg{ 160 Name: teamName.String(), 161 }) 162 require.NoError(t, err) 163 role, err := team.MemberRole(context.TODO(), fus[1].GetUserVersion()) 164 require.NoError(t, err) 165 require.Equal(t, keybase1.TeamRole_READER, role, "still a reader") 166 require.Equal(t, 0, len(team.chain().inner.SubteamLog), "doesn't know about any subteams") 167 168 t.Logf("U0 makes U1 an admin") 169 err = SetRoleAdmin(context.TODO(), tcs[0].G, teamName.String(), fus[1].Username) 170 require.NoError(t, err) 171 172 t.Logf("U1 loads from the cache, and doesn't realize they're an admin") 173 team, err = Load(context.TODO(), tcs[1].G, keybase1.LoadTeamArg{ 174 Name: teamName.String(), 175 }) 176 require.NoError(t, err) 177 role, err = team.MemberRole(context.TODO(), fus[1].GetUserVersion()) 178 require.NoError(t, err) 179 require.Equal(t, keybase1.TeamRole_READER, role, "cached as a reader") 180 require.Equal(t, 0, len(team.chain().inner.SubteamLog), "still doesn't know about any subteams") 181 182 t.Logf("U1 loads and realizes they're an admin") 183 team, err = GetMaybeAdminByStringName(context.TODO(), tcs[1].G, teamName.String(), false /*isPublic*/) 184 require.NoError(t, err) 185 role, err = team.MemberRole(context.TODO(), fus[1].GetUserVersion()) 186 require.NoError(t, err) 187 require.Equal(t, keybase1.TeamRole_ADMIN, role, "still an admin") 188 require.Equal(t, 1, len(team.chain().inner.SubteamLog), "has loaded previously-stubbed admin links") 189 } 190 191 func TestGetTeamIDByName(t *testing.T) { 192 fus, tcs, cleanup := setupNTests(t, 2) 193 defer cleanup() 194 195 teamName, teamID := createTeam2(*tcs[0]) 196 subteamName, subteamID := createSubteam(tcs[0], teamName, "hello") 197 198 // Test as owner of team and subteam 199 mctx := libkb.NewMetaContextForTest(*tcs[0]) 200 res, err := GetTeamIDByNameRPC(mctx, teamName.String()) 201 require.NoError(t, err) 202 require.Equal(t, teamID, res) 203 204 res, err = GetTeamIDByNameRPC(mctx, subteamName.String()) 205 require.NoError(t, err) 206 require.Equal(t, subteamID, res) 207 208 // Test as unrelated user 209 mctx = libkb.NewMetaContextForTest(*tcs[1]) 210 _, err = GetTeamIDByNameRPC(mctx, teamName.String()) 211 require.Error(t, err) 212 213 _, err = GetTeamIDByNameRPC(mctx, subteamName.String()) 214 require.Error(t, err) 215 216 // Add user 1 as a reader to root team 217 _, err = AddMember(context.Background(), tcs[0].G, teamName.String(), fus[1].Username, keybase1.TeamRole_READER, nil) 218 require.NoError(t, err) 219 220 res, err = GetTeamIDByNameRPC(mctx, teamName.String()) 221 require.NoError(t, err) 222 require.Equal(t, teamID, res) 223 224 // Try to get subteam id, should still fail. 225 _, err = GetTeamIDByNameRPC(mctx, subteamName.String()) 226 require.Error(t, err) 227 } 228 229 func teamGet(t *testing.T) { 230 tc := SetupTest(t, "team", 1) 231 defer tc.Cleanup() 232 233 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 234 require.NoError(t, err) 235 236 name := createTeam(tc) 237 238 _, err = GetForTestByStringName(context.TODO(), tc.G, name) 239 if err != nil { 240 t.Fatal(err) 241 } 242 } 243 244 func createTeam(tc libkb.TestContext) string { 245 b, err := libkb.RandBytes(4) 246 require.NoError(tc.T, err) 247 248 name := hex.EncodeToString(b) 249 _, err = CreateRootTeam(context.TODO(), tc.G, name, keybase1.TeamSettings{}) 250 require.NoError(tc.T, err) 251 252 return name 253 } 254 255 func createTeam2(tc libkb.TestContext) (keybase1.TeamName, keybase1.TeamID) { 256 teamNameS := createTeam(tc) 257 teamName, err := keybase1.TeamNameFromString(teamNameS) 258 require.NoError(tc.T, err) 259 id := teamName.ToPrivateTeamID() 260 tc.T.Logf("created team %s: %s", id, teamName) 261 return teamName, id 262 } 263 264 func createSubteam(tc *libkb.TestContext, parent keybase1.TeamName, subteamNamePart string) (keybase1.TeamName, keybase1.TeamID) { 265 subteamName, err := parent.Append(subteamNamePart) 266 require.NoError(tc.T, err) 267 subteamID, err := CreateSubteam(context.TODO(), tc.G, subteamNamePart, parent, keybase1.TeamRole_NONE /* addSelfAs */) 268 require.NoError(tc.T, err) 269 return subteamName, *subteamID 270 }