Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions cmd/collaborators/collaborators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,45 @@ import (

func TestCollaboratorsCommand(t *testing.T) {
tests := map[string]struct {
App types.App
Collaborators []types.SlackUser
app types.App
collaborators []types.SlackUser
expectedOutputs []string
Comment on lines +32 to +34
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: We don't need to export these variables and the default is typically to not export unless it's intentional. Happy to hear your thoughts @zimeg so we can start to make this a standard across the tests. 🧠

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a super appreciated change - thank you for considering it! 🙏 ✨

I agree that we should aim to match test setups to this format also.

}{
"lists no collaborators if none exist": {
App: types.App{
app: types.App{
AppID: "A001",
},
Collaborators: []types.SlackUser{},
collaborators: []types.SlackUser{},
expectedOutputs: []string{
" 0 collaborators", // Include space to not match "10 collaborators"
},
},
"lists the collaborator if one exists": {
App: types.App{
app: types.App{
AppID: "A002",
},
Collaborators: []types.SlackUser{
collaborators: []types.SlackUser{
{
ID: "USLACKBOT",
UserName: "slackbot",
Email: "[email protected]",
PermissionType: types.OWNER,
},
},
expectedOutputs: []string{
"1 collaborator",
// User info: slackbot
"USLACKBOT",
"slackbot",
"[email protected]",
string(types.OWNER),
},
Comment on lines +57 to +64
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Assert 1 user is listed

},
"lists all collaborators if many exist": {
App: types.App{
app: types.App{
AppID: "A002",
},
Collaborators: []types.SlackUser{
collaborators: []types.SlackUser{
{
ID: "USLACKBOT",
UserName: "slackbot",
Expand All @@ -69,37 +81,54 @@ func TestCollaboratorsCommand(t *testing.T) {
PermissionType: types.READER,
},
},
expectedOutputs: []string{
"2 collaborators",
// User info: slackbot
"USLACKBOT",
"slackbot",
"[email protected]",
string(types.OWNER),
// User info: bookworm
"U00READER",
"bookworm",
"[email protected]",
string(types.READER),
},
Comment on lines +84 to +96
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Assert 2 users are listed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📚 🪱 🤖 ✨

What an interesting app!

},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
appSelectMock := prompts.NewAppSelectMock()
teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.App, Auth: types.SlackAuth{}}, nil)
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.app, Auth: types.SlackAuth{}}, nil)
clientsMock := shared.NewClientsMock()
clientsMock.AddDefaultMocks()
clientsMock.ApiInterface.On("ListCollaborators", mock.Anything, mock.Anything, mock.Anything).
Return(tt.Collaborators, nil)
Return(tt.collaborators, nil)
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) {
clients.SDKConfig = hooks.NewSDKConfigMock()
})

err := NewCommand(clients).Execute()
require.NoError(t, err)
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.App.AppID)
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.app.AppID)
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListSuccess, mock.Anything)
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCount, []string{
fmt.Sprintf("%d", len(tt.Collaborators)),
fmt.Sprintf("%d", len(tt.collaborators)),
})
for _, collaborator := range tt.Collaborators {
for _, collaborator := range tt.collaborators {
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCollaborator, []string{
collaborator.ID,
collaborator.UserName,
collaborator.Email,
string(collaborator.PermissionType),
})
}
output := clientsMock.GetCombinedOutput()
for _, expectedOutput := range tt.expectedOutputs {
require.Contains(t, output, expectedOutput)
}
Comment on lines +128 to +131
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: This is the meat and potatoes that implements the assertions

})
}
}
55 changes: 42 additions & 13 deletions cmd/collaborators/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,45 @@ import (

func TestListCommand(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: This files changes are identical to cmd/collaborators/collaborators_test.go

tests := map[string]struct {
App types.App
Collaborators []types.SlackUser
app types.App
collaborators []types.SlackUser
expectedOutputs []string
}{
"lists no collaborators if none exist": {
App: types.App{
app: types.App{
AppID: "A001",
},
Collaborators: []types.SlackUser{},
collaborators: []types.SlackUser{},
expectedOutputs: []string{
"0 collaborators",
},
},
"lists the collaborator if one exists": {
App: types.App{
app: types.App{
AppID: "A002",
},
Collaborators: []types.SlackUser{
collaborators: []types.SlackUser{
{
ID: "USLACKBOT",
UserName: "slackbot",
Email: "[email protected]",
PermissionType: types.OWNER,
},
},
expectedOutputs: []string{
"1 collaborator",
// User info: slackbot
"USLACKBOT",
"slackbot",
"[email protected]",
string(types.OWNER),
},
},
"lists all collaborators if many exist": {
App: types.App{
app: types.App{
AppID: "A002",
},
Collaborators: []types.SlackUser{
collaborators: []types.SlackUser{
{
ID: "USLACKBOT",
UserName: "slackbot",
Expand All @@ -69,37 +81,54 @@ func TestListCommand(t *testing.T) {
PermissionType: types.READER,
},
},
expectedOutputs: []string{
"2 collaborators",
// User info: slackbot
"USLACKBOT",
"slackbot",
"[email protected]",
string(types.OWNER),
// User info: bookworm
"U00READER",
"bookworm",
"[email protected]",
string(types.READER),
},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
appSelectMock := prompts.NewAppSelectMock()
teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.App, Auth: types.SlackAuth{}}, nil)
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.app, Auth: types.SlackAuth{}}, nil)
clientsMock := shared.NewClientsMock()
clientsMock.AddDefaultMocks()
clientsMock.ApiInterface.On("ListCollaborators", mock.Anything, mock.Anything, mock.Anything).
Return(tt.Collaborators, nil)
Return(tt.collaborators, nil)
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) {
clients.SDKConfig = hooks.NewSDKConfigMock()
})

err := NewListCommand(clients).Execute()
require.NoError(t, err)
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.App.AppID)
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.app.AppID)
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListSuccess, mock.Anything)
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCount, []string{
fmt.Sprintf("%d", len(tt.Collaborators)),
fmt.Sprintf("%d", len(tt.collaborators)),
})
for _, collaborator := range tt.Collaborators {
for _, collaborator := range tt.collaborators {
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCollaborator, []string{
collaborator.ID,
collaborator.UserName,
collaborator.Email,
string(collaborator.PermissionType),
})
}
output := clientsMock.GetCombinedOutput()
for _, expectedOutput := range tt.expectedOutputs {
require.Contains(t, output, expectedOutput)
}
})
}
}
Loading