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

     1  package teams
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/protocol/keybase1"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestTeamNameFromString(t *testing.T) {
    11  	cases := []struct {
    12  		str      string
    13  		ok       bool
    14  		implicit bool
    15  	}{
    16  		{"", false, false},
    17  		{"a", false, false},
    18  		{"a.", false, false},
    19  		{"aaaa.", false, false},
    20  		{"aaaa.bbbbb.", false, false},
    21  		{".aaaa", false, false},
    22  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaa", false, false},
    23  
    24  		{"aaa.bbb.ccc", true, false},
    25  		{"aaa.ccc", true, false},
    26  		{"__keybase_implicit_team__", false, false},
    27  		{"__keybase_implicit_team__.x", false, false},
    28  		{"__keybase_implicit_team__9f6d31062cf8efbca45f9b193b24d724", true, true},
    29  		{"__keybase_implicit_team__bbbb26367512347c55154f14d436fb8b", true, true},
    30  		{"__keybase_implicit_team_", false, true},
    31  		{"__keybase_implicit_team__", false, true},
    32  		{"__keybase_implicit_team___", false, true},
    33  		{"__keybase_implicit_team__bbbb26367512347c55154f14d436fb8", false, true},  // too short
    34  		{"__keybase_implicit_team__bbbb26367512347c55154f14d436fb8z", false, true}, // non-hex
    35  	}
    36  
    37  	for i, c := range cases {
    38  		t.Logf("--> [%v] %+v", i, c)
    39  		name, err := keybase1.TeamNameFromString(c.str)
    40  		if c.ok {
    41  			require.NoError(t, err)
    42  		} else {
    43  			require.Error(t, err)
    44  			continue
    45  		}
    46  		require.True(t, len(name.Parts) > 0)
    47  		require.Equal(t, c.str, name.String())
    48  		require.Equal(t, c.implicit, name.IsImplicit())
    49  	}
    50  }
    51  
    52  func TestParseImplicitTeamBackingName(t *testing.T) {
    53  	tc := SetupTest(t, "team", 1)
    54  	defer tc.Cleanup()
    55  
    56  	badNames := []string{
    57  		"__keybase_implicit_team__",
    58  		"__keybase_implicit_team__12345678901234567801234567890q",
    59  		"__keybase_implicit_team__12345678901234567801234567890",
    60  	}
    61  	for _, badName := range badNames {
    62  		_, err := keybase1.TeamNameFromString(badName)
    63  		require.Error(t, err)
    64  	}
    65  	goodName := "__keybase_implicit_team__0123456789abcdef0123456789abcdef"
    66  	name, err := keybase1.TeamNameFromString(goodName)
    67  	require.NoError(t, err)
    68  	require.Equal(t, string(name.Parts[0]), "__keybase_implicit_team__0123456789abcdef0123456789abcdef")
    69  }
    70  
    71  func TestIsAncestorOf(t *testing.T) {
    72  	test := func(a, b string) bool {
    73  		aName, err := keybase1.TeamNameFromString(a)
    74  		require.NoError(t, err)
    75  		bName, err := keybase1.TeamNameFromString(b)
    76  		require.NoError(t, err)
    77  		return aName.IsAncestorOf(bName)
    78  	}
    79  
    80  	require.True(t, test("aaa", "aaa.bbb"))
    81  	require.True(t, test("aaa", "aaa.bbb.ccc"))
    82  	require.False(t, test("ccc", "aaa.bbb"))
    83  
    84  	require.True(t, test("aaa.bbb", "aaa.bbb.ccc"))
    85  
    86  	require.False(t, test("aaa.bbb.ccc", "aaa.bbb.ccc"))
    87  	require.False(t, test("aaa.bbb.ccc", "aaa.bbb"))
    88  }