github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/create_test.go (about)

     1  // Copyright 2017 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package teams
     5  
     6  import (
     7  	"context"
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/keybase/client/go/kbtest"
    12  	"github.com/keybase/client/go/libkb"
    13  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func createUserAndRootTeam(t *testing.T) (fu *kbtest.FakeUser, nm keybase1.TeamName, g *libkb.GlobalContext, cleanup func()) {
    18  	tc := SetupTest(t, "team", 1)
    19  	cleanup = func() { tc.Cleanup() }
    20  
    21  	// Note that the length limit for a team name, with the additional suffix
    22  	// below, is 16 characters. We have 5 to play with, including the implicit
    23  	// underscore after the prefix.
    24  	u, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    25  	require.NoError(t, err)
    26  
    27  	teamName := u.Username + "t"
    28  
    29  	_, err = CreateRootTeam(context.TODO(), tc.G, teamName, keybase1.TeamSettings{})
    30  	require.NoError(t, err)
    31  
    32  	nm, err = keybase1.TeamNameFromString(teamName)
    33  	require.NoError(t, err)
    34  	require.Equal(t, nm.String(), teamName)
    35  
    36  	return u, nm, tc.G, cleanup
    37  }
    38  
    39  func TestCreateTeam(t *testing.T) {
    40  	_, _, _, cleanup := createUserAndRootTeam(t)
    41  	cleanup()
    42  }
    43  
    44  func TestCreateTeamAfterAccountReset(t *testing.T) {
    45  	tc := SetupTest(t, "team", 1)
    46  	defer tc.Cleanup()
    47  
    48  	// Note that the length limit for a team name, with the additional suffix
    49  	// below, is 16 characters. We have 5 to play with, including the implicit
    50  	// underscore after the prefix.
    51  	u, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    52  	require.NoError(t, err)
    53  
    54  	// Now the user's fully qualified username should be like user%seqno. If we
    55  	// don't format this properly, the server will reject the post.
    56  	kbtest.ResetAccount(tc, u)
    57  
    58  	// this will reprovision as an eldest device:
    59  	err = u.Login(tc.G)
    60  	require.NoError(t, err)
    61  	if err = kbtest.AssertProvisioned(tc); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	teamName := u.Username + "T"
    66  	_, err = CreateRootTeam(context.TODO(), tc.G, teamName, keybase1.TeamSettings{})
    67  	require.NoError(t, err)
    68  }
    69  
    70  func TestCreateSubteam(t *testing.T) {
    71  	tc := SetupTest(t, "team", 1)
    72  	defer tc.Cleanup()
    73  
    74  	u, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    75  	require.NoError(t, err)
    76  
    77  	parentTeamName, err := keybase1.TeamNameFromString(u.Username + "T")
    78  	require.NoError(t, err)
    79  	_, err = CreateRootTeam(context.TODO(), tc.G, parentTeamName.String(), keybase1.TeamSettings{})
    80  	require.NoError(t, err)
    81  
    82  	subteamBasename := "mysubteam"
    83  	_, err = CreateSubteam(context.TODO(), tc.G, subteamBasename, parentTeamName, keybase1.TeamRole_NONE /* addSelfAs */)
    84  	require.NoError(t, err)
    85  
    86  	// Fetch the subteam we just created, to make sure it's there.
    87  	subteamFQName, err := parentTeamName.Append(subteamBasename)
    88  	require.NoError(t, err)
    89  	subteam, err := Load(context.TODO(), tc.G, keybase1.LoadTeamArg{
    90  		Name: subteamFQName.String(),
    91  	})
    92  	require.NoError(t, err)
    93  	require.Equal(t, subteamFQName, subteam.Name())
    94  	require.Equal(t, keybase1.Seqno(1), subteam.chain().GetLatestSeqno())
    95  
    96  	// creator of subteam should *not* be a member of the subteam, they
    97  	// need to explicitly ask to create it with themself in it.
    98  	assertRole(tc, subteamFQName.String(), u.Username, keybase1.TeamRole_NONE)
    99  
   100  	// Test joining with addSelf=true
   101  
   102  	subteamBasename = "mysubteam2"
   103  	_, err = CreateSubteam(context.TODO(), tc.G, subteamBasename, parentTeamName, keybase1.TeamRole_ADMIN /* addSelfAs */)
   104  	require.NoError(t, err)
   105  
   106  	subteamFQName, err = parentTeamName.Append(subteamBasename)
   107  	require.NoError(t, err)
   108  	assertRole(tc, subteamFQName.String(), u.Username, keybase1.TeamRole_ADMIN)
   109  }
   110  
   111  func TestCreateSubSubteam(t *testing.T) {
   112  	tc := SetupTest(t, "team", 1)
   113  	defer tc.Cleanup()
   114  
   115  	u, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
   116  	require.NoError(t, err)
   117  
   118  	parentTeamName, err := keybase1.TeamNameFromString(u.Username + "T")
   119  	require.NoError(t, err)
   120  	_, err = CreateRootTeam(context.TODO(), tc.G, parentTeamName.String(), keybase1.TeamSettings{})
   121  	require.NoError(t, err)
   122  
   123  	subteamBasename := "bbb"
   124  	_, err = CreateSubteam(context.TODO(), tc.G, subteamBasename, parentTeamName, keybase1.TeamRole_NONE /* addSelfAs */)
   125  	require.NoError(t, err)
   126  	subteamName, err := parentTeamName.Append(subteamBasename)
   127  	require.NoError(t, err)
   128  
   129  	assertRole(tc, subteamName.String(), u.Username, keybase1.TeamRole_NONE)
   130  
   131  	subsubteamBasename := "ccc"
   132  	_, err = CreateSubteam(context.TODO(), tc.G, subsubteamBasename, subteamName, keybase1.TeamRole_NONE /* addSelfAs */)
   133  	require.NoError(t, err)
   134  
   135  	subsubteamName, err := parentTeamName.Append(subteamBasename)
   136  	require.NoError(t, err)
   137  
   138  	assertRole(tc, subsubteamName.String(), u.Username, keybase1.TeamRole_NONE)
   139  }
   140  
   141  func TestCreateImplicitTeam(t *testing.T) {
   142  	tc := SetupTest(t, "team", 1)
   143  	defer tc.Cleanup()
   144  
   145  	numKBUsers := 3
   146  	var users []*kbtest.FakeUser
   147  	var uvs []keybase1.UserVersion
   148  	var impTeam keybase1.ImplicitTeamDisplayName
   149  	for i := 0; i < numKBUsers; i++ {
   150  		u, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
   151  		require.NoError(t, err)
   152  		users = append(users, u)
   153  	}
   154  
   155  	// Simple imp team
   156  	for _, u := range users {
   157  		impTeam.Writers.KeybaseUsers = append(impTeam.Writers.KeybaseUsers, u.Username)
   158  		uvs = append(uvs, u.User.ToUserVersion())
   159  	}
   160  	sort.Sort(keybase1.ByUserVersionID(uvs))
   161  	impTeam.IsPublic = false
   162  	teamID, _, err := CreateImplicitTeam(context.TODO(), tc.G, impTeam)
   163  	require.NoError(t, err)
   164  	team, err := Load(context.TODO(), tc.G, keybase1.LoadTeamArg{
   165  		ID: teamID,
   166  	})
   167  	require.NoError(t, err)
   168  	require.Equal(t, team.ID, teamID)
   169  	members, err := team.Members()
   170  	require.NoError(t, err)
   171  	sort.Sort(keybase1.ByUserVersionID(members.Owners))
   172  	require.Equal(t, members.Owners, uvs)
   173  
   174  	// Imp team with invites
   175  	impTeam.Writers.UnresolvedUsers = []keybase1.SocialAssertion{
   176  		{
   177  			User:    "mike",
   178  			Service: keybase1.SocialAssertionService("twitter"),
   179  		},
   180  		{
   181  			User:    "mike",
   182  			Service: keybase1.SocialAssertionService("github"),
   183  		},
   184  	}
   185  	teamID, _, err = CreateImplicitTeam(context.TODO(), tc.G, impTeam)
   186  	require.NoError(t, err)
   187  	team, err = Load(context.TODO(), tc.G, keybase1.LoadTeamArg{
   188  		ID: teamID,
   189  	})
   190  	require.NoError(t, err)
   191  	members, err = team.Members()
   192  	require.NoError(t, err)
   193  	sort.Sort(keybase1.ByUserVersionID(members.Owners))
   194  	require.Equal(t, members.Owners, uvs)
   195  	chainInvites := team.chain().ActiveInvites()
   196  	require.Equal(t, 2, len(chainInvites))
   197  }