github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/showcase_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/kbtest"
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestShowcaseTeam(t *testing.T) {
    16  	tc := SetupTest(t, "team", 1)
    17  	defer tc.Cleanup()
    18  
    19  	user, err := kbtest.CreateAndSignupFakeUser("team", tc.G)
    20  	require.NoError(t, err)
    21  
    22  	notifications := kbtest.NewTeamNotifyListener()
    23  	tc.G.SetService()
    24  	tc.G.NotifyRouter.AddListener(notifications)
    25  
    26  	namex, teamID := createTeam2(tc)
    27  	name := namex.String()
    28  	t.Logf("Created team %q", name)
    29  
    30  	isShowcased := true
    31  	err = SetTeamShowcase(context.TODO(), tc.G, teamID, &isShowcased, nil, nil)
    32  	require.NoError(t, err)
    33  	kbtest.CheckTeamMiscNotifications(tc, notifications)
    34  
    35  	showcase, err := GetTeamShowcase(context.TODO(), tc.G, teamID)
    36  	require.NoError(t, err)
    37  	require.Equal(t, true, showcase.IsShowcased)
    38  	require.NotNil(t, showcase.SetByUID)
    39  	require.Equal(t, user.User.GetUID(), *showcase.SetByUID)
    40  	require.Nil(t, nil, showcase.Description)
    41  
    42  	description := "Hello world"
    43  	err = SetTeamShowcase(context.TODO(), tc.G, teamID, nil, &description, nil)
    44  	require.NoError(t, err)
    45  	kbtest.CheckTeamMiscNotifications(tc, notifications)
    46  
    47  	showcase, err = GetTeamShowcase(context.TODO(), tc.G, teamID)
    48  	require.NoError(t, err)
    49  	require.Equal(t, true, showcase.IsShowcased)
    50  	require.NotNil(t, showcase.SetByUID)
    51  	require.Equal(t, user.User.GetUID(), *showcase.SetByUID)
    52  	require.NotNil(t, showcase.Description)
    53  	require.Equal(t, "Hello world", *showcase.Description)
    54  
    55  	isShowcased = false
    56  	err = SetTeamShowcase(context.TODO(), tc.G, teamID, &isShowcased, nil, nil)
    57  	require.NoError(t, err)
    58  	kbtest.CheckTeamMiscNotifications(tc, notifications)
    59  
    60  	showcase, err = GetTeamShowcase(context.TODO(), tc.G, teamID)
    61  	require.NoError(t, err)
    62  
    63  	require.Equal(t, false, showcase.IsShowcased)
    64  	require.NotNil(t, showcase.SetByUID)
    65  	require.Equal(t, user.User.GetUID(), *showcase.SetByUID)
    66  	require.NotNil(t, showcase.Description)
    67  	require.Equal(t, "Hello world", *showcase.Description)
    68  
    69  	tmShowcase, err := GetTeamAndMemberShowcase(context.TODO(), tc.G, teamID)
    70  	require.NoError(t, err)
    71  	require.Equal(t, showcase, tmShowcase.TeamShowcase)
    72  	require.Equal(t, false, tmShowcase.IsMemberShowcased)
    73  }
    74  
    75  func TestShowcaseMember(t *testing.T) {
    76  	tc := SetupTest(t, "team", 1)
    77  	defer tc.Cleanup()
    78  
    79  	user, err := kbtest.CreateAndSignupFakeUser("team", tc.G)
    80  	require.NoError(t, err)
    81  
    82  	notifications := kbtest.NewTeamNotifyListener()
    83  	tc.G.SetService()
    84  	tc.G.NotifyRouter.AddListener(notifications)
    85  
    86  	name, id := createTeam2(tc)
    87  	t.Logf("Created team %q", name)
    88  
    89  	var defaultTeamShowcase = keybase1.TeamShowcase{IsShowcased: false, Description: nil, SetByUID: nil, AnyMemberShowcase: true}
    90  
    91  	tmShowcase, err := GetTeamAndMemberShowcase(context.TODO(), tc.G, id)
    92  	require.NoError(t, err)
    93  	require.Equal(t, false, tmShowcase.IsMemberShowcased)
    94  	require.Equal(t, defaultTeamShowcase, tmShowcase.TeamShowcase)
    95  
    96  	err = SetTeamMemberShowcase(context.TODO(), tc.G, id, true)
    97  	require.NoError(t, err)
    98  
    99  	tmShowcase, err = GetTeamAndMemberShowcase(context.TODO(), tc.G, id)
   100  	require.NoError(t, err)
   101  	require.Equal(t, true, tmShowcase.IsMemberShowcased)
   102  	require.Equal(t, defaultTeamShowcase, tmShowcase.TeamShowcase)
   103  
   104  	isShowcased := true
   105  	description := "Hello Team!"
   106  	err = SetTeamShowcase(context.TODO(), tc.G, id, &isShowcased, &description, nil)
   107  	require.NoError(t, err)
   108  	kbtest.CheckTeamMiscNotifications(tc, notifications)
   109  
   110  	tmShowcase, err = GetTeamAndMemberShowcase(context.TODO(), tc.G, id)
   111  	require.NoError(t, err)
   112  	require.Equal(t, true, tmShowcase.IsMemberShowcased)
   113  
   114  	showcase := tmShowcase.TeamShowcase
   115  	require.Equal(t, true, showcase.IsShowcased)
   116  	require.NotNil(t, showcase.SetByUID)
   117  	require.Equal(t, user.User.GetUID(), *showcase.SetByUID)
   118  	require.NotNil(t, showcase.Description)
   119  	require.Equal(t, "Hello Team!", *showcase.Description)
   120  }
   121  
   122  func TestShowcasePermissions(t *testing.T) {
   123  	tc := SetupTest(t, "team", 1)
   124  	defer tc.Cleanup()
   125  
   126  	user, err := kbtest.CreateAndSignupFakeUser("team", tc.G)
   127  	require.NoError(t, err)
   128  
   129  	notifications := kbtest.NewTeamNotifyListener()
   130  	tc.G.SetService()
   131  	tc.G.NotifyRouter.AddListener(notifications)
   132  
   133  	_, err = kbtest.CreateAndSignupFakeUser("team", tc.G)
   134  	require.NoError(t, err)
   135  
   136  	namex, teamID := createTeam2(tc)
   137  	team := namex.String()
   138  	t.Logf("Created team %q", team)
   139  
   140  	isShowcased := true
   141  	description := "This team is showcased"
   142  	anyMemberShowcase := false
   143  	err = SetTeamShowcase(context.TODO(), tc.G, teamID, &isShowcased, &description, &anyMemberShowcase)
   144  	require.NoError(t, err)
   145  	kbtest.CheckTeamMiscNotifications(tc, notifications)
   146  
   147  	err = SetTeamMemberShowcase(context.TODO(), tc.G, teamID, true)
   148  	require.NoError(t, err)
   149  
   150  	_, err = AddMember(context.TODO(), tc.G, team, user.Username, keybase1.TeamRole_WRITER, nil)
   151  	require.NoError(t, err)
   152  
   153  	err = tc.Logout()
   154  	require.NoError(t, err)
   155  	err = user.Login(tc.G)
   156  	require.NoError(t, err)
   157  
   158  	// Set- functions should check membership and not issue request
   159  	// when user is not an admin or higher.
   160  	isShowcased = false
   161  	err = SetTeamShowcase(context.TODO(), tc.G, teamID, &isShowcased, nil, nil)
   162  	require.Error(t, err)
   163  
   164  	// AppStatusErrors means we bounced off server instead of being
   165  	// stopped by API helper code.
   166  	_, ok := err.(libkb.AppStatusError)
   167  	require.False(t, ok)
   168  
   169  	// But we expect to hit server error here: because server checks
   170  	// anyMemberShowcase, not us.
   171  	err = SetTeamMemberShowcase(context.TODO(), tc.G, teamID, true)
   172  	require.Error(t, err)
   173  	_, ok = err.(libkb.AppStatusError)
   174  	require.True(t, ok)
   175  
   176  	// Get- functions should still work.
   177  	ret, err := GetTeamAndMemberShowcase(context.TODO(), tc.G, teamID)
   178  	require.NoError(t, err)
   179  	require.False(t, ret.IsMemberShowcased) // false by default
   180  	require.NotNil(t, ret.TeamShowcase.Description)
   181  	require.Equal(t, description, *ret.TeamShowcase.Description)
   182  	require.True(t, ret.TeamShowcase.IsShowcased)
   183  
   184  	ret2, err := GetTeamShowcase(context.TODO(), tc.G, teamID)
   185  	require.NoError(t, err)
   186  	require.Equal(t, ret.TeamShowcase, ret2)
   187  }
   188  
   189  func TestShowcaseAnyMember(t *testing.T) {
   190  	tc := SetupTest(t, "team", 1)
   191  	defer tc.Cleanup()
   192  
   193  	user, err := kbtest.CreateAndSignupFakeUser("team", tc.G)
   194  	require.NoError(t, err)
   195  
   196  	_, err = kbtest.CreateAndSignupFakeUser("team", tc.G)
   197  	require.NoError(t, err)
   198  
   199  	team, teamID := createTeam2(tc)
   200  	t.Logf("Created team %q", team)
   201  
   202  	_, err = AddMember(context.TODO(), tc.G, team.String(), user.Username, keybase1.TeamRole_READER, nil)
   203  	require.NoError(t, err)
   204  
   205  	err = tc.Logout()
   206  	require.NoError(t, err)
   207  	err = user.Login(tc.G)
   208  	require.NoError(t, err)
   209  
   210  	t.Logf("Logged in as %q (reader)", user.Username)
   211  
   212  	ret, err := GetTeamAndMemberShowcase(context.TODO(), tc.G, teamID)
   213  	require.NoError(t, err)
   214  	require.False(t, ret.IsMemberShowcased) // false by default
   215  
   216  	err = SetTeamMemberShowcase(context.TODO(), tc.G, teamID, true)
   217  	require.NoError(t, err)
   218  
   219  	ret, err = GetTeamAndMemberShowcase(context.TODO(), tc.G, teamID)
   220  	require.NoError(t, err)
   221  	require.True(t, ret.IsMemberShowcased) // membership is true and we are a reader
   222  }