github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/team_role_map_test.go (about)

     1  package teams
     2  
     3  import (
     4  	"github.com/keybase/client/go/libkb"
     5  	"github.com/keybase/client/go/protocol/keybase1"
     6  	"github.com/stretchr/testify/require"
     7  	"testing"
     8  )
     9  
    10  func TestTeamRoleMap(t *testing.T) {
    11  	fus, tcs, cleanup := setupNTests(t, 2)
    12  	defer cleanup()
    13  
    14  	t.Logf("create team")
    15  	teamName, teamID := createTeam2(*tcs[0])
    16  	m := make([]libkb.MetaContext, 2)
    17  	for i, tc := range tcs {
    18  		m[i] = libkb.NewMetaContextForTest(*tc)
    19  	}
    20  	t.Logf("add B to the team so they can load it")
    21  	_, err := AddMember(m[0].Ctx(), tcs[0].G, teamName.String(), fus[1].Username, keybase1.TeamRole_ADMIN, nil)
    22  	require.NoError(t, err)
    23  	subteamName := "abc"
    24  	subteamID, err := CreateSubteam(m[0].Ctx(), tcs[0].G, subteamName, teamName, keybase1.TeamRole_WRITER)
    25  	require.NoError(t, err)
    26  
    27  	received, err := m[1].G().GetTeamRoleMapManager().Get(m[1], false)
    28  	require.NoError(t, err)
    29  
    30  	expected := keybase1.TeamRoleMapAndVersion{
    31  		Teams:   make(map[keybase1.TeamID]keybase1.TeamRolePair),
    32  		Version: keybase1.UserTeamVersion(2),
    33  	}
    34  	expected.Teams[teamID] = keybase1.TeamRolePair{
    35  		Role:         keybase1.TeamRole_ADMIN,
    36  		ImplicitRole: keybase1.TeamRole_NONE,
    37  	}
    38  	expected.Teams[*subteamID] = keybase1.TeamRolePair{
    39  		Role:         keybase1.TeamRole_NONE,
    40  		ImplicitRole: keybase1.TeamRole_ADMIN,
    41  	}
    42  
    43  	require.Equal(t, expected, received)
    44  
    45  	_, err = AddMember(m[0].Ctx(), tcs[0].G, teamName.String()+"."+subteamName, fus[1].Username, keybase1.TeamRole_READER, nil)
    46  	require.NoError(t, err)
    47  
    48  	expected.Teams[*subteamID] = keybase1.TeamRolePair{
    49  		Role:         keybase1.TeamRole_READER,
    50  		ImplicitRole: keybase1.TeamRole_ADMIN,
    51  	}
    52  	expected.Version++
    53  
    54  	// In teams/*_test.go, we don't have gregor hooked up, so we have to mock out what happens
    55  	// when a new gregpr notification comes down from the server.
    56  	err = m[1].G().GetTeamRoleMapManager().Update(m[1], expected.Version)
    57  	require.NoError(t, err)
    58  
    59  	// Check that the state is eagerly refreshed.
    60  	pollForTrue(t, m[1].G(), func(i int) bool {
    61  		received := m[1].G().GetTeamRoleMapManager().(*TeamRoleMapManager).Query()
    62  		if received != nil && received.Data.Version == expected.Version {
    63  			require.Equal(t, expected.Teams, received.Data.Teams)
    64  			return true
    65  		}
    66  		return false
    67  	})
    68  
    69  	m[1].G().GetTeamRoleMapManager().FlushCache()
    70  
    71  	received, err = m[1].G().GetTeamRoleMapManager().Get(m[1], false)
    72  	require.NoError(t, err)
    73  	require.Equal(t, expected, received)
    74  }