|
| 1 | +// Copyright 2022-2026 Salesforce, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sandbox |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/slackapi/slack-cli/internal/experiment" |
| 23 | + "github.com/slackapi/slack-cli/internal/shared" |
| 24 | + "github.com/slackapi/slack-cli/internal/shared/types" |
| 25 | + "github.com/slackapi/slack-cli/test/testutil" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "github.com/stretchr/testify/mock" |
| 28 | +) |
| 29 | + |
| 30 | +func TestListCommand(t *testing.T) { |
| 31 | + testutil.TableTestCommand(t, testutil.CommandTests{ |
| 32 | + "empty list": { |
| 33 | + CmdArgs: []string{"--experiment=sandboxes", "--token", "xoxb-test-token"}, |
| 34 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 35 | + testToken := "xoxb-test-token" |
| 36 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 37 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 38 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 39 | + cm.API.On("ListSandboxes", mock.Anything, testToken, "").Return([]types.Sandbox{}, nil) |
| 40 | + cm.API.On("UsersInfo", mock.Anything, mock.Anything, mock.Anything).Return(&types.UserInfo{Profile: types.UserProfile{}}, nil) |
| 41 | + |
| 42 | + cm.AddDefaultMocks() |
| 43 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 44 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 45 | + }, |
| 46 | + ExpectedStdoutOutputs: []string{"No sandboxes found"}, |
| 47 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 48 | + cm.Auth.AssertCalled(t, "AuthWithToken", mock.Anything, "xoxb-test-token") |
| 49 | + cm.API.AssertCalled(t, "ListSandboxes", mock.Anything, "xoxb-test-token", "") |
| 50 | + }, |
| 51 | + }, |
| 52 | + "with sandboxes": { |
| 53 | + CmdArgs: []string{"--experiment=sandboxes", "--token", "xoxb-test-token"}, |
| 54 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 55 | + testToken := "xoxb-test-token" |
| 56 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 57 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 58 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 59 | + sandboxes := []types.Sandbox{ |
| 60 | + { |
| 61 | + SandboxTeamID: "T123", |
| 62 | + SandboxName: "my-sandbox", |
| 63 | + SandboxDomain: "my-sandbox", |
| 64 | + Status: "active", |
| 65 | + DateCreated: 1700000000, |
| 66 | + DateArchived: 0, |
| 67 | + }, |
| 68 | + } |
| 69 | + cm.API.On("ListSandboxes", mock.Anything, testToken, "").Return(sandboxes, nil) |
| 70 | + cm.API.On("UsersInfo", mock.Anything, mock.Anything, mock.Anything).Return(&types.UserInfo{Profile: types.UserProfile{}}, nil) |
| 71 | + |
| 72 | + cm.AddDefaultMocks() |
| 73 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 74 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 75 | + }, |
| 76 | + ExpectedStdoutOutputs: []string{"my-sandbox", "T123", "https://my-sandbox.slack.com"}, |
| 77 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 78 | + cm.API.AssertCalled(t, "ListSandboxes", mock.Anything, "xoxb-test-token", "") |
| 79 | + }, |
| 80 | + }, |
| 81 | + "with status": { |
| 82 | + CmdArgs: []string{"--experiment=sandboxes", "--token", "xoxb-test-token", "--status", "active"}, |
| 83 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 84 | + testToken := "xoxb-test-token" |
| 85 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 86 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 87 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 88 | + cm.API.On("ListSandboxes", mock.Anything, testToken, "active").Return([]types.Sandbox{}, nil) |
| 89 | + cm.API.On("UsersInfo", mock.Anything, mock.Anything, mock.Anything).Return(&types.UserInfo{Profile: types.UserProfile{}}, nil) |
| 90 | + |
| 91 | + cm.AddDefaultMocks() |
| 92 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 93 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 94 | + }, |
| 95 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 96 | + cm.API.AssertCalled(t, "ListSandboxes", mock.Anything, "xoxb-test-token", "active") |
| 97 | + }, |
| 98 | + }, |
| 99 | + "list error": { |
| 100 | + CmdArgs: []string{"--experiment=sandboxes", "--token", "xoxb-test-token"}, |
| 101 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 102 | + testToken := "xoxb-test-token" |
| 103 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 104 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 105 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 106 | + cm.API.On("ListSandboxes", mock.Anything, testToken, ""). |
| 107 | + Return([]types.Sandbox(nil), errors.New("api_error")) |
| 108 | + |
| 109 | + cm.AddDefaultMocks() |
| 110 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 111 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 112 | + }, |
| 113 | + ExpectedErrorStrings: []string{"api_error"}, |
| 114 | + }, |
| 115 | + "experiment required": { |
| 116 | + CmdArgs: []string{}, |
| 117 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 118 | + cm.AddDefaultMocks() |
| 119 | + // Do NOT enable sandboxes experiment |
| 120 | + }, |
| 121 | + ExpectedErrorStrings: []string{"sandbox"}, |
| 122 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 123 | + cm.API.AssertNotCalled(t, "ListSandboxes", mock.Anything, mock.Anything, mock.Anything) |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, func(cf *shared.ClientFactory) *cobra.Command { |
| 127 | + return NewListCommand(cf) |
| 128 | + }) |
| 129 | +} |
0 commit comments