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
27 changes: 3 additions & 24 deletions cmd/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,16 @@
package auth

import (
"context"
"testing"

"github.com/slackapi/slack-cli/internal/hooks"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/slackapi/slack-cli/test/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type listMockObject struct {
mock.Mock
}

func (m *listMockObject) MockListFunction(ctx context.Context, clients *shared.ClientFactory) ([]types.SlackAuth, error) {
args := m.Called()
return args.Get(0).([]types.SlackAuth), args.Error(1)
}

func TestAuthCommand(t *testing.T) {
// Create mocks
ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
clientsMock.AddDefaultMocks()
Expand All @@ -49,16 +36,8 @@ func TestAuthCommand(t *testing.T) {
cmd := NewCommand(clients)
testutil.MockCmdIO(clients.IO, cmd)

mock := new(listMockObject)
listFunc = mock.MockListFunction
mock.On("MockListFunction").Return([]types.SlackAuth{}, nil)

// Execute test
err := cmd.ExecuteContext(ctx)
if err != nil {
assert.Fail(t, "cmd.Execute had unexpected error")
}

// Check result
mock.AssertCalled(t, "MockListFunction")
assert.NoError(t, err)
// Verify the auth command delegates to the list command; detailed list output testing is in TestListCommand
assert.Contains(t, clientsMock.GetCombinedOutput(), "logged in")
}
8 changes: 2 additions & 6 deletions cmd/auth/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"

"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/pkg/auth"
authpkg "github.com/slackapi/slack-cli/internal/pkg/auth"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slacktrace"
Expand All @@ -28,10 +28,6 @@ import (
"golang.org/x/text/language"
)

// Create handle to the function for testing
// TODO - Stopgap until we learn the correct way to structure our code for testing.
var listFunc = auth.List

Comment on lines -31 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.

🪓

Copy link
Member

Choose a reason for hiding this comment

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

🪵 praise: These TODO have been unsolved for some time I recall! Thanks for finding good pattern.

// NewListCommand creates the Cobra command for listing authorized accounts
func NewListCommand(clients *shared.ClientFactory) *cobra.Command {
return &cobra.Command{
Expand All @@ -51,7 +47,7 @@ func NewListCommand(clients *shared.ClientFactory) *cobra.Command {
// runListCommand will execute the list command
func runListCommand(cmd *cobra.Command, clients *shared.ClientFactory) error {
ctx := cmd.Context()
userAuthList, err := listFunc(ctx, clients)
userAuthList, err := authpkg.List(ctx, clients)
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 important line that removes the listFunc stub.

if err != nil {
return err
}
Expand Down
96 changes: 68 additions & 28 deletions cmd/auth/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package auth

import (
"context"
"testing"
"time"

"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/shared/types"
Expand All @@ -26,37 +26,77 @@ import (
"github.com/stretchr/testify/mock"
)

// Setup a mock for the List package
type ListPkgMock struct {
mock.Mock
}

func (m *ListPkgMock) List(ctx context.Context, clients *shared.ClientFactory) ([]types.SlackAuth, error) {
m.Called()
return []types.SlackAuth{}, nil
}

func TestListCommand(t *testing.T) {
// Create mocks
ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
clientsMock.AddDefaultMocks()
tests := map[string]struct {
auths []types.SlackAuth
expected []string
}{
"no authorized accounts": {
auths: []types.SlackAuth{},
expected: []string{
"You are not logged in to any Slack accounts",
"login",
},
},
"a single authorized account": {
auths: []types.SlackAuth{
{
TeamDomain: "test-workspace",
TeamID: "T12345",
UserID: "U67890",
LastUpdated: time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC),
},
},
expected: []string{
"test-workspace",
"T12345",
"U67890",
},
},
"multiple authorized accounts": {
auths: []types.SlackAuth{
{
TeamDomain: "alpha-workspace",
TeamID: "T11111",
UserID: "U11111",
LastUpdated: time.Date(2025, 1, 10, 8, 0, 0, 0, time.UTC),
},
{
TeamDomain: "beta-workspace",
TeamID: "T22222",
UserID: "U22222",
LastUpdated: time.Date(2025, 2, 20, 16, 0, 0, 0, time.UTC),
},
},
expected: []string{
"alpha-workspace",
"T11111",
"U11111",
"beta-workspace",
"T22222",
"U22222",
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
clientsMock.Auth.On("Auths", mock.Anything).Return(tc.auths, nil)
Copy link
Member

Choose a reason for hiding this comment

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

🌟 praise: Love to see this! I understand this has significant increase to test coverage:

+0.10%

clientsMock.AddDefaultMocks()

// Create clients that is mocked for testing
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
clients := shared.NewClientFactory(clientsMock.MockClientFactory())

// Create the command
cmd := NewListCommand(clients)
testutil.MockCmdIO(clients.IO, cmd)
cmd := NewListCommand(clients)
testutil.MockCmdIO(clients.IO, cmd)

listPkgMock := new(ListPkgMock)
listFunc = listPkgMock.List
err := cmd.ExecuteContext(ctx)
assert.NoError(t, err)

listPkgMock.On("List").Return([]types.SlackAuth{}, nil)
err := cmd.ExecuteContext(ctx)
if err != nil {
assert.Fail(t, "cmd.Execute had unexpected error")
output := clientsMock.GetCombinedOutput()
for _, expected := range tc.expected {
assert.Contains(t, output, expected)
}
})
}

listPkgMock.AssertCalled(t, "List")
}
Loading