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

     1  package teams
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/keybase/client/go/kbtest"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestTeamPlusApplicationKeysExim(t *testing.T) {
    15  	tc := SetupTest(t, "TestTeamPlusApplicationKeysExim", 1)
    16  	defer tc.Cleanup()
    17  
    18  	_, err := kbtest.CreateAndSignupFakeUser("team", tc.G)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	name := createTeam(tc)
    24  	team, err := Load(context.TODO(), tc.G, keybase1.LoadTeamArg{
    25  		Name: name,
    26  	})
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	exported, err := team.ExportToTeamPlusApplicationKeys(context.TODO(), keybase1.Time(0),
    32  		keybase1.TeamApplication_KBFS, true)
    33  	if err != nil {
    34  		t.Fatalf("Error during export: %s", err)
    35  	}
    36  	if exported.Name != team.Name().String() {
    37  		t.Fatalf("Got name %s, expected %s", exported.Name, team.Name())
    38  	}
    39  	if !exported.Id.Eq(team.ID) {
    40  		t.Fatalf("Got id %q, expected %q", exported.Id, team.ID)
    41  	}
    42  	expectedKeys, err := team.AllApplicationKeys(context.TODO(), keybase1.TeamApplication_KBFS)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if len(exported.ApplicationKeys) != len(expectedKeys) {
    47  		t.Fatalf("Got %v applicationKeys, expected %v", len(exported.ApplicationKeys), len(expectedKeys))
    48  	}
    49  }
    50  
    51  func TestImplicitTeamLTPAK(t *testing.T) {
    52  	tc := SetupTest(t, "team", 1)
    53  	defer tc.Cleanup()
    54  
    55  	u0, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    56  	require.NoError(t, err)
    57  	u1, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    58  	require.NoError(t, err)
    59  	u2, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    60  	require.NoError(t, err)
    61  	displayName := strings.Join([]string{u1.Username, u2.Username}, ",")
    62  
    63  	for _, public := range []bool{true, false} {
    64  		createdTeam, _, impTeamName, err := LookupOrCreateImplicitTeam(context.Background(), tc.G, displayName, public)
    65  		require.NoError(t, err)
    66  		require.Equal(t, public, impTeamName.IsPublic)
    67  
    68  		t.Logf("Created team public: %t, %s %s", public, createdTeam.ID, impTeamName)
    69  
    70  		for _, u := range []*kbtest.FakeUser{u1, u2, u0, nil} {
    71  			require.NoError(t, tc.Logout())
    72  			if u != nil {
    73  				require.NoError(t, u.Login(tc.G))
    74  				t.Logf("Testing as user %s", u.Username)
    75  			} else {
    76  				t.Logf("Testing as unlogged user")
    77  			}
    78  
    79  			ret, err := LoadTeamPlusApplicationKeys(context.Background(), tc.G, createdTeam.ID,
    80  				keybase1.TeamApplication_KBFS, keybase1.TeamRefreshers{}, true)
    81  			if !public && (u == nil || u == u0) {
    82  				require.Error(t, err)
    83  				continue
    84  			}
    85  
    86  			require.NoError(t, err)
    87  			require.True(t, ret.Implicit)
    88  			require.Equal(t, public, ret.Public)
    89  			require.Equal(t, createdTeam.ID, ret.Id)
    90  
    91  			if u == nil || u == u0 {
    92  				require.Empty(t, ret.ApplicationKeys)
    93  			} else {
    94  				require.NotEmpty(t, ret.ApplicationKeys)
    95  			}
    96  		}
    97  
    98  		require.NoError(t, tc.Logout())
    99  		require.NoError(t, u2.Login(tc.G))
   100  	}
   101  }