github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/service_helper_test.go (about) 1 package teams 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/keybase/client/go/kbtest" 8 "github.com/keybase/client/go/protocol/keybase1" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func countMember(t *testing.T, members []keybase1.TeamMemberDetails, username string, needsPuk bool, status keybase1.TeamMemberStatus) int { 13 c := 0 14 for _, member := range members { 15 if member.Username == username { 16 require.Equal(t, needsPuk, member.NeedsPUK) 17 require.Equal(t, status, member.Status) 18 c++ 19 } 20 } 21 return c 22 } 23 24 // TestGetAnnotatedTeamIdiosyncrasies tests behavior of GetAnnotatedTeam that in some way alters the 25 // view of the teamchain, for the sake of presentation in the UI. These cases mainly involve 26 // members, invites, keybase-type member invites, resets, and deletions. 27 func TestGetAnnotatedTeamIdiosyncrasies(t *testing.T) { 28 tc := SetupTest(t, "team", 1) 29 30 tc.G.UIDMapper.SetTestingNoCachingMode(true) 31 32 ali, err := kbtest.CreateAndSignupFakeUser("alih", tc.G) 33 require.NoError(t, err) 34 del, err := kbtest.CreateAndSignupFakeUser("delh", tc.G) 35 require.NoError(t, err) 36 37 adm, err := kbtest.CreateAndSignupFakeUser("admh", tc.G) 38 require.NoError(t, err) 39 name, ID := createTeam2(tc) 40 41 ctx := context.TODO() 42 43 _, err = AddMember(ctx, tc.G, name.String(), ali.Username, keybase1.TeamRole_OWNER, nil) 44 require.NoError(t, err) 45 _, err = AddMember(ctx, tc.G, name.String(), del.Username, keybase1.TeamRole_ADMIN, nil) 46 require.NoError(t, err) 47 48 tm, err := GetAnnotatedTeam(ctx, tc.G, ID) 49 require.NoError(t, err) 50 require.Len(t, tm.Members, 3) 51 require.Equal(t, 1, countMember(t, tm.Members, adm.Username, false, keybase1.TeamMemberStatus_ACTIVE)) 52 require.Equal(t, 1, countMember(t, tm.Members, ali.Username, false, keybase1.TeamMemberStatus_ACTIVE)) 53 require.Equal(t, 1, countMember(t, tm.Members, del.Username, false, keybase1.TeamMemberStatus_ACTIVE)) 54 55 t.Logf("ali resets but doesnt log back in") 56 kbtest.Logout(tc) 57 err = ali.Login(tc.G) 58 require.NoError(t, err) 59 kbtest.ResetAccount(tc, ali) 60 61 kbtest.Logout(tc) 62 err = del.Login(tc.G) 63 require.NoError(t, err) 64 65 err = reAddMemberAfterResetInner(ctx, tc.G, ID, ali.Username) 66 require.NoError(t, err) 67 team, err := GetForTestByStringName(context.TODO(), tc.G, name.String()) 68 require.NoError(t, err) 69 require.True(t, team.IsMember(ctx, ali.GetUserVersion()), "ali is a cryptomember, though reset") 70 resetUV := keybase1.UserVersion{ 71 Uid: ali.GetUID(), 72 EldestSeqno: 0, 73 } 74 t.Logf("assert that ali is also invited as an admin") 75 assertInvite(tc, name.String(), resetUV.String(), "keybase", keybase1.TeamRole_ADMIN) 76 tm, err = GetAnnotatedTeam(ctx, tc.G, ID) 77 require.NoError(t, err) 78 require.Len(t, tm.Members, 3, "GetAnnotatedTeam returns only one result for Ali anyway, filtering out inactive owners") 79 require.Equal(t, 1, countMember(t, tm.Members, ali.Username, true, keybase1.TeamMemberStatus_ACTIVE), "ali added as a kb invite") 80 } 81 82 func TestGetAnnotatedTeamKeybaseInvites(t *testing.T) { 83 tc := SetupTest(t, "team", 1) 84 85 tc.G.UIDMapper.SetTestingNoCachingMode(true) 86 87 ali, err := kbtest.CreateAndSignupFakeUser("alih", tc.G) 88 require.NoError(t, err) 89 tc.Tp.DisableUpgradePerUserKey = true 90 91 cha, err := kbtest.CreateAndSignupFakeUser("chah", tc.G) 92 require.NoError(t, err) 93 94 kbtest.Logout(tc) 95 err = ali.Login(tc.G) 96 require.NoError(t, err) 97 98 ctx := context.TODO() 99 name, ID := createTeam2(tc) 100 101 _, err = AddMember(ctx, tc.G, name.String(), cha.Username, keybase1.TeamRole_WRITER, nil) 102 require.NoError(t, err) 103 104 tc.Tp.DisableUpgradePerUserKey = false 105 kbtest.Logout(tc) 106 err = cha.Login(tc.G) 107 require.NoError(t, err) 108 perUserKeyUpgradeSoft(ctx, tc.G, "test") 109 110 kbtest.Logout(tc) 111 err = ali.Login(tc.G) 112 require.NoError(t, err) 113 114 tm, err := GetAnnotatedTeam(ctx, tc.G, ID) 115 require.NoError(t, err) 116 require.Len(t, tm.Members, 2) 117 require.Equal(t, 1, countMember(t, tm.Members, cha.Username, true, keybase1.TeamMemberStatus_ACTIVE)) 118 119 _, err = AddMember(ctx, tc.G, name.String(), cha.Username, keybase1.TeamRole_WRITER, nil) 120 require.NoError(t, err) 121 122 tm, err = GetAnnotatedTeam(ctx, tc.G, ID) 123 require.NoError(t, err) 124 require.Len(t, tm.Members, 2) 125 require.Equal(t, 1, countMember(t, tm.Members, cha.Username, false, keybase1.TeamMemberStatus_ACTIVE)) 126 }