Skip to content

Commit 3c995d5

Browse files
authored
test: fix collaborator command test to run standalone (#14)
1 parent 69fe8b9 commit 3c995d5

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

cmd/collaborators/collaborators_test.go

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,91 @@
1515
package collaborators
1616

1717
import (
18-
"context"
18+
"fmt"
1919
"testing"
2020

21+
"github.com/slackapi/slack-cli/internal/hooks"
22+
"github.com/slackapi/slack-cli/internal/prompts"
2123
"github.com/slackapi/slack-cli/internal/shared"
2224
"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"
2526
"github.com/stretchr/testify/mock"
27+
"github.com/stretchr/testify/require"
2628
)
2729

2830
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+
},
4873
}
4974

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+
}
82105
}

0 commit comments

Comments
 (0)