Skip to content

Commit 2cf691e

Browse files
authored
refactor: remove listFunc stub from cmd/auth/list (#435)
* refactor: remove listFunc stub from cmd/auth/list Remove the package-level `listFunc` variable that was used to stub `auth.List` in tests. The command now calls `auth.List` directly, and tests mock at the dependency layer (`Auth().Auths()`) instead, exercising the full command code path. * test: add multiple accounts test case for auth list * test: add minimal output assertion to auth command test
1 parent a3187a9 commit 2cf691e

3 files changed

Lines changed: 73 additions & 58 deletions

File tree

cmd/auth/auth_test.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,16 @@
1515
package auth
1616

1717
import (
18-
"context"
1918
"testing"
2019

2120
"github.com/slackapi/slack-cli/internal/hooks"
2221
"github.com/slackapi/slack-cli/internal/shared"
23-
"github.com/slackapi/slack-cli/internal/shared/types"
2422
"github.com/slackapi/slack-cli/internal/slackcontext"
2523
"github.com/slackapi/slack-cli/test/testutil"
2624
"github.com/stretchr/testify/assert"
27-
"github.com/stretchr/testify/mock"
2825
)
2926

30-
type listMockObject struct {
31-
mock.Mock
32-
}
33-
34-
func (m *listMockObject) MockListFunction(ctx context.Context, clients *shared.ClientFactory) ([]types.SlackAuth, error) {
35-
args := m.Called()
36-
return args.Get(0).([]types.SlackAuth), args.Error(1)
37-
}
38-
3927
func TestAuthCommand(t *testing.T) {
40-
// Create mocks
4128
ctx := slackcontext.MockContext(t.Context())
4229
clientsMock := shared.NewClientsMock()
4330
clientsMock.AddDefaultMocks()
@@ -49,16 +36,8 @@ func TestAuthCommand(t *testing.T) {
4936
cmd := NewCommand(clients)
5037
testutil.MockCmdIO(clients.IO, cmd)
5138

52-
mock := new(listMockObject)
53-
listFunc = mock.MockListFunction
54-
mock.On("MockListFunction").Return([]types.SlackAuth{}, nil)
55-
56-
// Execute test
5739
err := cmd.ExecuteContext(ctx)
58-
if err != nil {
59-
assert.Fail(t, "cmd.Execute had unexpected error")
60-
}
61-
62-
// Check result
63-
mock.AssertCalled(t, "MockListFunction")
40+
assert.NoError(t, err)
41+
// Verify the auth command delegates to the list command; detailed list output testing is in TestListCommand
42+
assert.Contains(t, clientsMock.GetCombinedOutput(), "logged in")
6443
}

cmd/auth/list.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"fmt"
1919

2020
"github.com/slackapi/slack-cli/internal/iostreams"
21-
"github.com/slackapi/slack-cli/internal/pkg/auth"
21+
authpkg "github.com/slackapi/slack-cli/internal/pkg/auth"
2222
"github.com/slackapi/slack-cli/internal/shared"
2323
"github.com/slackapi/slack-cli/internal/shared/types"
2424
"github.com/slackapi/slack-cli/internal/slacktrace"
@@ -28,10 +28,6 @@ import (
2828
"golang.org/x/text/language"
2929
)
3030

31-
// Create handle to the function for testing
32-
// TODO - Stopgap until we learn the correct way to structure our code for testing.
33-
var listFunc = auth.List
34-
3531
// NewListCommand creates the Cobra command for listing authorized accounts
3632
func NewListCommand(clients *shared.ClientFactory) *cobra.Command {
3733
return &cobra.Command{
@@ -51,7 +47,7 @@ func NewListCommand(clients *shared.ClientFactory) *cobra.Command {
5147
// runListCommand will execute the list command
5248
func runListCommand(cmd *cobra.Command, clients *shared.ClientFactory) error {
5349
ctx := cmd.Context()
54-
userAuthList, err := listFunc(ctx, clients)
50+
userAuthList, err := authpkg.List(ctx, clients)
5551
if err != nil {
5652
return err
5753
}

cmd/auth/list_test.go

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package auth
1616

1717
import (
18-
"context"
1918
"testing"
19+
"time"
2020

2121
"github.com/slackapi/slack-cli/internal/shared"
2222
"github.com/slackapi/slack-cli/internal/shared/types"
@@ -26,37 +26,77 @@ import (
2626
"github.com/stretchr/testify/mock"
2727
)
2828

29-
// Setup a mock for the List package
30-
type ListPkgMock struct {
31-
mock.Mock
32-
}
33-
34-
func (m *ListPkgMock) List(ctx context.Context, clients *shared.ClientFactory) ([]types.SlackAuth, error) {
35-
m.Called()
36-
return []types.SlackAuth{}, nil
37-
}
38-
3929
func TestListCommand(t *testing.T) {
40-
// Create mocks
41-
ctx := slackcontext.MockContext(t.Context())
42-
clientsMock := shared.NewClientsMock()
43-
clientsMock.AddDefaultMocks()
30+
tests := map[string]struct {
31+
auths []types.SlackAuth
32+
expected []string
33+
}{
34+
"no authorized accounts": {
35+
auths: []types.SlackAuth{},
36+
expected: []string{
37+
"You are not logged in to any Slack accounts",
38+
"login",
39+
},
40+
},
41+
"a single authorized account": {
42+
auths: []types.SlackAuth{
43+
{
44+
TeamDomain: "test-workspace",
45+
TeamID: "T12345",
46+
UserID: "U67890",
47+
LastUpdated: time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC),
48+
},
49+
},
50+
expected: []string{
51+
"test-workspace",
52+
"T12345",
53+
"U67890",
54+
},
55+
},
56+
"multiple authorized accounts": {
57+
auths: []types.SlackAuth{
58+
{
59+
TeamDomain: "alpha-workspace",
60+
TeamID: "T11111",
61+
UserID: "U11111",
62+
LastUpdated: time.Date(2025, 1, 10, 8, 0, 0, 0, time.UTC),
63+
},
64+
{
65+
TeamDomain: "beta-workspace",
66+
TeamID: "T22222",
67+
UserID: "U22222",
68+
LastUpdated: time.Date(2025, 2, 20, 16, 0, 0, 0, time.UTC),
69+
},
70+
},
71+
expected: []string{
72+
"alpha-workspace",
73+
"T11111",
74+
"U11111",
75+
"beta-workspace",
76+
"T22222",
77+
"U22222",
78+
},
79+
},
80+
}
81+
for name, tc := range tests {
82+
t.Run(name, func(t *testing.T) {
83+
ctx := slackcontext.MockContext(t.Context())
84+
clientsMock := shared.NewClientsMock()
85+
clientsMock.Auth.On("Auths", mock.Anything).Return(tc.auths, nil)
86+
clientsMock.AddDefaultMocks()
4487

45-
// Create clients that is mocked for testing
46-
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
88+
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
4789

48-
// Create the command
49-
cmd := NewListCommand(clients)
50-
testutil.MockCmdIO(clients.IO, cmd)
90+
cmd := NewListCommand(clients)
91+
testutil.MockCmdIO(clients.IO, cmd)
5192

52-
listPkgMock := new(ListPkgMock)
53-
listFunc = listPkgMock.List
93+
err := cmd.ExecuteContext(ctx)
94+
assert.NoError(t, err)
5495

55-
listPkgMock.On("List").Return([]types.SlackAuth{}, nil)
56-
err := cmd.ExecuteContext(ctx)
57-
if err != nil {
58-
assert.Fail(t, "cmd.Execute had unexpected error")
96+
output := clientsMock.GetCombinedOutput()
97+
for _, expected := range tc.expected {
98+
assert.Contains(t, output, expected)
99+
}
100+
})
59101
}
60-
61-
listPkgMock.AssertCalled(t, "List")
62102
}

0 commit comments

Comments
 (0)