github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/common_test.go (about) 1 package teams 2 3 import ( 4 "testing" 5 6 "golang.org/x/net/context" 7 8 "github.com/keybase/client/go/externalstest" 9 "github.com/keybase/client/go/kbtest" 10 "github.com/keybase/client/go/libkb" 11 "github.com/keybase/client/go/protocol/keybase1" 12 insecureTriplesec "github.com/keybase/go-triplesec-insecure" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func SetupTest(tb testing.TB, name string, depth int) (tc libkb.TestContext) { 17 tc = externalstest.SetupTest(tb, name, depth+1) 18 19 // use an insecure triplesec in tests 20 tc.G.NewTriplesec = func(passphrase []byte, salt []byte) (libkb.Triplesec, error) { 21 warner := func() { tc.G.Log.Warning("Installing insecure Triplesec with weak stretch parameters") } 22 isProduction := func() bool { 23 return tc.G.Env.GetRunMode() == libkb.ProductionRunMode 24 } 25 return insecureTriplesec.NewCipher(passphrase, salt, libkb.ClientTriplesecVersion, warner, isProduction) 26 } 27 ServiceInit(tc.G) 28 return tc 29 } 30 31 func GetForTestByStringName(ctx context.Context, g *libkb.GlobalContext, name string) (*Team, error) { 32 return Load(ctx, g, keybase1.LoadTeamArg{ 33 Name: name, 34 ForceRepoll: true, 35 }) 36 } 37 38 func GetForTestByID(ctx context.Context, g *libkb.GlobalContext, id keybase1.TeamID) (*Team, error) { 39 return Load(ctx, g, keybase1.LoadTeamArg{ 40 ID: id, 41 Public: id.IsPublic(), 42 ForceRepoll: true, 43 }) 44 } 45 46 func createTeamName(t *testing.T, root string, parts ...string) keybase1.TeamName { 47 name, err := keybase1.TeamNameFromString(root) 48 require.NoError(t, err) 49 require.True(t, name.IsRootTeam(), "team name must be root %v", root) 50 for _, part := range parts { 51 name, err = name.Append(part) 52 require.NoError(t, err) 53 } 54 return name 55 } 56 57 // Create n TestContexts with logged in users 58 // Returns (FakeUsers, TestContexts, CleanupFunction) 59 func setupNTests(t *testing.T, n int) ([]*kbtest.FakeUser, []*libkb.TestContext, func()) { 60 return setupNTestsWithPukless(t, n, 0) 61 } 62 63 // nPukless is how many users start out with no PUK. 64 // Those users appear at the end of the list 65 func setupNTestsWithPukless(t *testing.T, n, nPukless int) ([]*kbtest.FakeUser, []*libkb.TestContext, func()) { 66 require.True(t, n > 0, "must create at least 1 tc") 67 require.True(t, n >= nPukless, "more pukless users than total users requested") 68 var fus []*kbtest.FakeUser 69 var tcs []*libkb.TestContext 70 for i := 0; i < n; i++ { 71 tc := SetupTest(t, "team", 1) 72 tcs = append(tcs, &tc) 73 if i >= n-nPukless { 74 tc.Tp.DisableUpgradePerUserKey = true 75 } 76 fu, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 77 require.NoError(t, err) 78 fus = append(fus, fu) 79 } 80 cleanup := func() { 81 for _, tc := range tcs { 82 tc.Cleanup() 83 } 84 } 85 for i, fu := range fus { 86 t.Logf("U%d: %v %v", i, fu.Username, fu.GetUserVersion()) 87 } 88 return fus, tcs, cleanup 89 } 90 91 func runMany(t *testing.T, f func(implicit, public bool)) { 92 for _, implicit := range []bool{false, true} { 93 for _, public := range []bool{false, true} { 94 if !implicit && public { 95 // public teams not supported 96 continue 97 } 98 t.Logf("running test with implicit:%v public:%v", implicit, public) 99 f(implicit, public) 100 } 101 } 102 }