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

     1  package teams
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/keybase/client/go/kbtest"
     8  
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestAccessRequestAccept(t *testing.T) {
    17  	tc, owner, u1, _, teamName := memberSetupMultiple(t)
    18  	defer tc.Cleanup()
    19  
    20  	// owner is logged in and created teamName
    21  	err := tc.Logout()
    22  	require.NoError(t, err)
    23  
    24  	// u1 requests access to the team
    25  	err = u1.Login(tc.G)
    26  	require.NoError(t, err)
    27  	_, err = RequestAccess(context.Background(), tc.G, teamName)
    28  	require.NoError(t, err)
    29  
    30  	myReqs, err := ListMyAccessRequests(context.Background(), tc.G, &teamName)
    31  	require.NoError(t, err)
    32  	require.Equal(t, 1, len(myReqs))
    33  	require.Equal(t, teamName, myReqs[0].String())
    34  
    35  	// teamName is optional, if not given, all pending requests will be returned.
    36  	myReqs, err = ListMyAccessRequests(context.Background(), tc.G, nil)
    37  	require.NoError(t, err)
    38  	require.Equal(t, 1, len(myReqs))
    39  	require.Equal(t, teamName, myReqs[0].String())
    40  
    41  	// owner lists requests, sees u1 request
    42  	err = tc.Logout()
    43  	require.NoError(t, err)
    44  	err = owner.Login(tc.G)
    45  	require.NoError(t, err)
    46  
    47  	reqs, err := ListRequests(context.Background(), tc.G, nil)
    48  	require.NoError(t, err)
    49  	require.Equal(t, 1, len(reqs))
    50  	require.Equal(t, teamName, reqs[0].Name)
    51  	require.Equal(t, u1.Username, reqs[0].Username)
    52  	require.True(t, reqs[0].Ctime.Time().After(time.Now().Add(-1*time.Minute)))
    53  	require.Equal(t, "", reqs[0].FullName.String()) // no fullname in this case
    54  
    55  	// owner add u1 to team
    56  	_, err = AddMember(context.Background(), tc.G, teamName, u1.Username, keybase1.TeamRole_WRITER, nil)
    57  	require.NoError(t, err)
    58  
    59  	// owner lists requests, sees no requests
    60  	assertNoRequests(tc)
    61  
    62  	// u1 requests access to the team again
    63  	err = tc.Logout()
    64  	require.NoError(t, err)
    65  	err = u1.Login(tc.G)
    66  	require.NoError(t, err)
    67  
    68  	_, err = RequestAccess(context.Background(), tc.G, teamName)
    69  	require.Error(t, err)
    70  	aerr, ok := err.(libkb.AppStatusError)
    71  	if !ok {
    72  		t.Fatalf("error %s (%T), expected libkb.AppStatusError", err, err)
    73  	}
    74  	if aerr.Code != libkb.SCTeamMemberExists {
    75  		t.Errorf("status code: %d, expected %d", aerr.Code, libkb.SCTeamMemberExists)
    76  	}
    77  	err = tc.Logout()
    78  	require.NoError(t, err)
    79  
    80  	// owner lists requests, sees no requests
    81  	err = owner.Login(tc.G)
    82  	require.NoError(t, err)
    83  	assertNoRequests(tc)
    84  }
    85  
    86  func TestAccessRequestIgnore(t *testing.T) {
    87  	tc, owner, u1, _, teamName := memberSetupMultiple(t)
    88  	defer tc.Cleanup()
    89  
    90  	// owner is logged in and created teamName
    91  	err := tc.Logout()
    92  	require.NoError(t, err)
    93  
    94  	// u1 requests access to the team
    95  	err = u1.Login(tc.G)
    96  	require.NoError(t, err)
    97  
    98  	_, err = RequestAccess(context.Background(), tc.G, teamName)
    99  	require.NoError(t, err)
   100  
   101  	// Set no caching mode. If we change our full name and ask UidMapper about
   102  	// it quickly, we might still be getting the old version because of pubsub
   103  	// delay.
   104  	tc.G.UIDMapper.SetTestingNoCachingMode(true)
   105  
   106  	// Change full name
   107  	fullName, err := libkb.RandString("test", 5)
   108  	require.NoError(t, err)
   109  	err = kbtest.EditProfile(tc.MetaContext(), keybase1.ProfileEditArg{
   110  		FullName: fullName,
   111  	})
   112  	require.NoError(t, err)
   113  
   114  	// owner lists requests, sees u1 request
   115  	err = tc.Logout()
   116  	require.NoError(t, err)
   117  	err = owner.Login(tc.G)
   118  	require.NoError(t, err)
   119  
   120  	reqs, err := ListRequests(context.Background(), tc.G, nil)
   121  	require.NoError(t, err)
   122  
   123  	require.Len(t, reqs, 1)
   124  	require.Equal(t, teamName, reqs[0].Name)
   125  	require.Equal(t, u1.Username, reqs[0].Username)
   126  	require.True(t, reqs[0].Ctime.Time().After(time.Now().Add(-1*time.Minute)), "ctime within last minute")
   127  	require.Equal(t, fullName, reqs[0].FullName.String())
   128  
   129  	// owner ignores u1 request
   130  	err = IgnoreRequest(context.Background(), tc.G, reqs[0].Name, reqs[0].Username)
   131  	require.NoError(t, err)
   132  
   133  	// owner lists requests, sees no requests
   134  	assertNoRequests(tc)
   135  
   136  	// u1 requests access to the team again
   137  	err = tc.Logout()
   138  	require.NoError(t, err)
   139  
   140  	err = u1.Login(tc.G)
   141  	require.NoError(t, err)
   142  
   143  	_, err = RequestAccess(context.Background(), tc.G, teamName)
   144  	require.Error(t, err)
   145  	aerr, ok := err.(libkb.AppStatusError)
   146  	require.True(t, ok, "error is libkb.AppStatusError")
   147  	require.Equal(t, libkb.SCTeamTarDuplicate, aerr.Code, "expected status code")
   148  
   149  	err = tc.Logout()
   150  	require.NoError(t, err)
   151  
   152  	// owner lists requests, sees no requests
   153  	err = owner.Login(tc.G)
   154  	require.NoError(t, err)
   155  	assertNoRequests(tc)
   156  }
   157  
   158  func assertNoRequests(tc libkb.TestContext) {
   159  	reqs, err := ListRequests(context.Background(), tc.G, nil /* teamName */)
   160  	require.NoError(tc.T, err)
   161  	require.Len(tc.T, reqs, 0)
   162  }