|
15 | 15 | package collaborators |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "context" |
| 18 | + "fmt" |
19 | 19 | "testing" |
20 | 20 |
|
| 21 | + "github.com/slackapi/slack-cli/internal/hooks" |
| 22 | + "github.com/slackapi/slack-cli/internal/prompts" |
21 | 23 | "github.com/slackapi/slack-cli/internal/shared" |
22 | 24 | "github.com/slackapi/slack-cli/internal/shared/types" |
23 | | - "github.com/slackapi/slack-cli/test/testutil" |
24 | | - "github.com/stretchr/testify/assert" |
| 25 | + "github.com/slackapi/slack-cli/internal/slacktrace" |
25 | 26 | "github.com/stretchr/testify/mock" |
| 27 | + "github.com/stretchr/testify/require" |
26 | 28 | ) |
27 | 29 |
|
28 | 30 | func TestCollaboratorsCommand(t *testing.T) { |
29 | | - // Create mocks |
30 | | - clientsMock := shared.NewClientsMock() |
31 | | - clientsMock.AddDefaultMocks() |
32 | | - |
33 | | - clientsMock.ApiInterface.On("ListCollaborators", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]types.SlackUser{}, nil) |
34 | | - |
35 | | - // Create clients that is mocked for testing |
36 | | - clients := shared.NewClientFactory(clientsMock.MockClientFactory()) |
37 | | - mockAuths := []types.SlackAuth{} |
38 | | - clientsMock.AuthInterface.On("Auths", mock.Anything).Return(mockAuths, nil) |
39 | | - |
40 | | - // Create the command |
41 | | - cmd := NewCommand(clients) |
42 | | - testutil.MockCmdIO(clients.IO, cmd) |
43 | | - |
44 | | - // Execute test |
45 | | - err := cmd.Execute() |
46 | | - if err != nil { |
47 | | - assert.Fail(t, "cmd.Execute had unexpected error") |
| 31 | + tests := map[string]struct { |
| 32 | + App types.App |
| 33 | + Collaborators []types.SlackUser |
| 34 | + }{ |
| 35 | + "lists no collaborators if none exist": { |
| 36 | + App: types.App{ |
| 37 | + AppID: "A001", |
| 38 | + }, |
| 39 | + Collaborators: []types.SlackUser{}, |
| 40 | + }, |
| 41 | + "lists the collaborator if one exists": { |
| 42 | + App: types.App{ |
| 43 | + AppID: "A002", |
| 44 | + }, |
| 45 | + Collaborators: []types.SlackUser{ |
| 46 | + { |
| 47 | + ID: "USLACKBOT", |
| 48 | + UserName: "slackbot", |
| 49 | + |
| 50 | + PermissionType: types.OWNER, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + "lists all collaborators if many exist": { |
| 55 | + App: types.App{ |
| 56 | + AppID: "A002", |
| 57 | + }, |
| 58 | + Collaborators: []types.SlackUser{ |
| 59 | + { |
| 60 | + ID: "USLACKBOT", |
| 61 | + UserName: "slackbot", |
| 62 | + |
| 63 | + PermissionType: types.OWNER, |
| 64 | + }, |
| 65 | + { |
| 66 | + ID: "U00READER", |
| 67 | + UserName: "bookworm", |
| 68 | + |
| 69 | + PermissionType: types.READER, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
48 | 73 | } |
49 | 74 |
|
50 | | - // Check result |
51 | | - clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, mock.Anything, mock.Anything) |
52 | | -} |
53 | | - |
54 | | -func TestCollaboratorsCommand_PrintSuccess(t *testing.T) { |
55 | | - |
56 | | - // Setup |
57 | | - |
58 | | - ctx := context.Background() |
59 | | - clientsMock := shared.NewClientsMock() |
60 | | - clients := shared.NewClientFactory(clientsMock.MockClientFactory()) |
61 | | - |
62 | | - // Execute tests |
63 | | - |
64 | | - t.Run("Username will be used if present", func(t *testing.T) { |
65 | | - user := types. SlackUser{ Email: "[email protected]", ID: "U1234", PermissionType: types. OWNER} |
66 | | - printSuccess(ctx, clients.IO, user, "added") |
67 | | - assert. Contains( t, clientsMock. GetStdoutOutput(), "[email protected] successfully added as an owner collaborator on this app") |
68 | | - }) |
69 | | - |
70 | | - t.Run("User has no email set; fall back on user ID", func(t *testing.T) { |
71 | | - user := types.SlackUser{ID: "U1234", PermissionType: types.OWNER} |
72 | | - printSuccess(ctx, clients.IO, user, "removed") |
73 | | - assert.Contains(t, clientsMock.GetStdoutOutput(), "\nU1234 successfully removed as an owner collaborator on this app\n\n") |
74 | | - }) |
75 | | - |
76 | | - t.Run("Reader-type collaborator", func(t *testing.T) { |
77 | | - user := types. SlackUser{ Email: "[email protected]", ID: "U1234", PermissionType: types. READER} |
78 | | - printSuccess(ctx, clients.IO, user, "updated") |
79 | | - assert. Contains( t, clientsMock. GetStdoutOutput(), "\n[email protected] successfully updated as a reader collaborator on this app\n\n") |
80 | | - }) |
81 | | - |
| 75 | + for name, tt := range tests { |
| 76 | + t.Run(name, func(t *testing.T) { |
| 77 | + appSelectMock := prompts.NewAppSelectMock() |
| 78 | + teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt |
| 79 | + appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.App, Auth: types.SlackAuth{}}, nil) |
| 80 | + clientsMock := shared.NewClientsMock() |
| 81 | + clientsMock.AddDefaultMocks() |
| 82 | + clientsMock.ApiInterface.On("ListCollaborators", mock.Anything, mock.Anything, mock.Anything). |
| 83 | + Return(tt.Collaborators, nil) |
| 84 | + clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) { |
| 85 | + clients.SDKConfig = hooks.NewSDKConfigMock() |
| 86 | + }) |
| 87 | + |
| 88 | + err := NewCommand(clients).Execute() |
| 89 | + require.NoError(t, err) |
| 90 | + clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.App.AppID) |
| 91 | + clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListSuccess, mock.Anything) |
| 92 | + clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCount, []string{ |
| 93 | + fmt.Sprintf("%d", len(tt.Collaborators)), |
| 94 | + }) |
| 95 | + for _, collaborator := range tt.Collaborators { |
| 96 | + clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCollaborator, []string{ |
| 97 | + collaborator.ID, |
| 98 | + collaborator.UserName, |
| 99 | + collaborator.Email, |
| 100 | + string(collaborator.PermissionType), |
| 101 | + }) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
82 | 105 | } |
0 commit comments