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

     1  package systests
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/keybase/client/go/client"
    10  	"github.com/keybase/client/go/libkb"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestTeamAPI(t *testing.T) {
    15  	tt := newTeamTester(t)
    16  	defer tt.cleanup()
    17  
    18  	tt.addUser("onr")
    19  	tt.addUser("wtr")
    20  	tt.addUser("botua")
    21  	tt.addUser("rbot")
    22  
    23  	assertTeamAPIOutput(t, tt.users[0],
    24  		`{"method": "list-self-memberships"}`,
    25  		`{"result":{"teams":null}}`)
    26  
    27  	teamName, err := libkb.RandHexString("t", 6)
    28  	require.NoError(t, err)
    29  
    30  	assertTeamAPIOutputRegexp(t, tt.users[0],
    31  		`{"method": "create-team", "params": {"options": {"team": "`+teamName+`"}}}`,
    32  		`{"result":{"teamID":"[a-z0-9]{32}","chatSent":true,"creatorAdded":true}}`)
    33  
    34  	assertTeamAPIOutput(t, tt.users[0],
    35  		`{"method": "add-members", "params": {"options": {"team": "`+teamName+`", "usernames": [{"username": "`+tt.users[1].username+`", "role": "reader"}]}}}`,
    36  		`{"result":[{"invited":false,"user":{"uid":"`+tt.users[1].uid.String()+`","username":"`+tt.users[1].username+`"},"chatSending":false}]}`)
    37  	assertTeamAPIOutput(t, tt.users[0],
    38  		`{"method": "add-members", "params": {"options": {"team": "`+teamName+`", "usernames": [{"username": "`+tt.users[2].username+`", "role": "bot"}]}}}`,
    39  		`{"result":[{"invited":false,"user":{"uid":"`+tt.users[2].uid.String()+`","username":"`+tt.users[2].username+`"},"chatSending":false}]}`)
    40  	// TODO HOTPOT-227 reenable
    41  	// assertTeamAPIOutput(t, tt.users[0],
    42  	//	`{"method": "add-members", "params": {"options": {"team": "`+teamName+`", "usernames": [{"username": "`+tt.users[3].username+`", "role": "restrictedbot"}]}}}`,
    43  	//	`{"result":[{"invited":false,"user":{"uid":"`+tt.users[3].uid.String()+`","username":"`+tt.users[3].username+`"},"emailSent":false,"chatSending":false}]}`)
    44  
    45  	assertTeamAPIOutputRegexp(t, tt.users[0],
    46  		`{"method": "create-team", "params": {"options": {"team": "`+teamName+`.sub"}}}`,
    47  		`{"result":{"teamID":"[a-z0-9]{32}","chatSent":true,"creatorAdded":false}}`)
    48  
    49  	assertTeamAPIOutput(t, tt.users[0],
    50  		`{"method": "rename-subteam", "params": {"options": {"team": "`+teamName+`.sub", "new-team-name": "`+teamName+`.sub2"}}}`,
    51  		`{}`)
    52  
    53  	assertTeamAPIOutput(t, tt.users[0],
    54  		`{"method": "edit-member", "params": {"options": {"team": "`+teamName+`", "username": "`+tt.users[1].username+`", "role": "writer"}}}`,
    55  		`{}`)
    56  
    57  	assertTeamAPIOutput(t, tt.users[0],
    58  		`{"method": "remove-member", "params": {"options": {"team": "`+teamName+`", "username": "`+tt.users[1].username+`"}}}`,
    59  		`{}`)
    60  	assertTeamAPIOutput(t, tt.users[0],
    61  		`{"method": "remove-member", "params": {"options": {"team": "`+teamName+`", "username": "`+tt.users[2].username+`"}}}`,
    62  		`{}`)
    63  	// TODO HOTPOT-227 reenable
    64  	// assertTeamAPIOutput(t, tt.users[0],
    65  	//	`{"method": "remove-member", "params": {"options": {"team": "`+teamName+`", "username": "`+tt.users[3].username+`"}}}`,
    66  	//	`{}`)
    67  }
    68  
    69  // Compares `in` to `expectedOut`, treating `expectedOut` as a regex to match.
    70  func assertTeamAPIOutputRegexp(t *testing.T, u *userPlusDevice, in, expectedOut string) {
    71  	out, err := runTeamAPI(t, u, in)
    72  	require.NoError(t, err)
    73  
    74  	out = strings.TrimSpace(out)
    75  	require.Regexp(t, expectedOut, out)
    76  }
    77  
    78  func assertTeamAPIOutput(t *testing.T, u *userPlusDevice, in, expectedOut string) {
    79  	out, err := runTeamAPI(t, u, in)
    80  	require.NoError(t, err)
    81  
    82  	out = strings.TrimSpace(out)
    83  	require.Equal(t, expectedOut, out)
    84  }
    85  
    86  func runTeamAPI(t *testing.T, u *userPlusDevice, json string) (string, error) {
    87  	cmd := client.NewCmdTeamAPIRunner(u.tc.G)
    88  	cmd.SetMessage(json)
    89  	r, err := libkb.RandString("teamapi", 4)
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  	filename := filepath.Join(os.TempDir(), r+".json")
    94  	cmd.SetOutputFile(filename)
    95  	defer os.Remove(filename)
    96  	if err := cmd.Run(); err != nil {
    97  		return "", err
    98  	}
    99  	out, err := os.ReadFile(filename)
   100  	if err != nil {
   101  		return "", err
   102  	}
   103  	return string(out), nil
   104  }