|
| 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/assert" |
| 28 | + "github.com/stretchr/testify/mock" |
| 29 | +) |
| 30 | + |
| 31 | +func TestCreateCommand(t *testing.T) { |
| 32 | + testutil.TableTestCommand(t, testutil.CommandTests{ |
| 33 | + "create success": { |
| 34 | + CmdArgs: []string{ |
| 35 | + "--experiment=sandboxes", |
| 36 | + "--token", "xoxb-test-token", |
| 37 | + "--name", "test-box", |
| 38 | + "--domain", "test-box", |
| 39 | + "--password", "mypass", |
| 40 | + }, |
| 41 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 42 | + testToken := "xoxb-test-token" |
| 43 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 44 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 45 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 46 | + cm.API.On("CreateSandbox", mock.Anything, testToken, "test-box", "test-box", "mypass", "", "", "", "", int64(0)). |
| 47 | + Return("T123", "https://test-box.slack.com", nil) |
| 48 | + cm.API.On("UsersInfo", mock.Anything, mock.Anything, mock.Anything).Return(&types.UserInfo{Profile: types.UserProfile{}}, nil) |
| 49 | + |
| 50 | + cm.AddDefaultMocks() |
| 51 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 52 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 53 | + }, |
| 54 | + ExpectedStdoutOutputs: []string{"T123", "https://test-box.slack.com", "Sandbox Created"}, |
| 55 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 56 | + cm.Auth.AssertCalled(t, "AuthWithToken", mock.Anything, "xoxb-test-token") |
| 57 | + cm.API.AssertCalled(t, "CreateSandbox", mock.Anything, "xoxb-test-token", "test-box", "test-box", "mypass", "", "", "", "", int64(0)) |
| 58 | + }, |
| 59 | + }, |
| 60 | + "create with JSON output": { |
| 61 | + CmdArgs: []string{ |
| 62 | + "--experiment=sandboxes", |
| 63 | + "--token", "xoxb-test-token", |
| 64 | + "--name", "json-box", |
| 65 | + "--domain", "json-box", |
| 66 | + "--password", "secret", |
| 67 | + "--output", "json", |
| 68 | + }, |
| 69 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 70 | + testToken := "xoxb-test-token" |
| 71 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 72 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 73 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 74 | + cm.API.On("CreateSandbox", mock.Anything, testToken, "json-box", "json-box", "secret", "", "", "", "", int64(0)). |
| 75 | + Return("T456", "https://json-box.slack.com", nil) |
| 76 | + |
| 77 | + cm.AddDefaultMocks() |
| 78 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 79 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 80 | + }, |
| 81 | + ExpectedStdoutOutputs: []string{`"team_id": "T456"`, `"url": "https://json-box.slack.com"`}, |
| 82 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 83 | + cm.API.AssertCalled(t, "CreateSandbox", mock.Anything, "xoxb-test-token", "json-box", "json-box", "secret", "", "", "", "", int64(0)) |
| 84 | + }, |
| 85 | + }, |
| 86 | + "create with derived domain": { |
| 87 | + CmdArgs: []string{ |
| 88 | + "--experiment=sandboxes", |
| 89 | + "--token", "xoxb-test-token", |
| 90 | + "--name", "My Test Box", |
| 91 | + "--domain", "my-test-box", |
| 92 | + "--password", "pass", |
| 93 | + }, |
| 94 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 95 | + testToken := "xoxb-test-token" |
| 96 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 97 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 98 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 99 | + cm.API.On("CreateSandbox", mock.Anything, testToken, "My Test Box", "my-test-box", "pass", "", "", "", "", int64(0)). |
| 100 | + Return("T789", "https://my-test-box.slack.com", nil) |
| 101 | + |
| 102 | + cm.AddDefaultMocks() |
| 103 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 104 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 105 | + }, |
| 106 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 107 | + cm.API.AssertCalled(t, "CreateSandbox", mock.Anything, "xoxb-test-token", "My Test Box", "my-test-box", "pass", "", "", "", "", int64(0)) |
| 108 | + }, |
| 109 | + }, |
| 110 | + "create with TTL": { |
| 111 | + CmdArgs: []string{ |
| 112 | + "--experiment=sandboxes", |
| 113 | + "--token", "xoxb-test-token", |
| 114 | + "--name", "ttl-box", |
| 115 | + "--domain", "ttl-box", |
| 116 | + "--password", "pass", |
| 117 | + "--ttl", "24h", |
| 118 | + }, |
| 119 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 120 | + testToken := "xoxb-test-token" |
| 121 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 122 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 123 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 124 | + cm.API.On("CreateSandbox", mock.Anything, testToken, "ttl-box", "ttl-box", "pass", "", "", "", "", mock.MatchedBy(func(v int64) bool { return v > 0 })). |
| 125 | + Return("T111", "https://ttl-box.slack.com", nil) |
| 126 | + |
| 127 | + cm.AddDefaultMocks() |
| 128 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 129 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 130 | + }, |
| 131 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 132 | + cm.API.AssertCalled(t, "CreateSandbox", mock.Anything, "xoxb-test-token", "ttl-box", "ttl-box", "pass", "", "", "", "", mock.MatchedBy(func(v int64) bool { return v > 0 })) |
| 133 | + }, |
| 134 | + }, |
| 135 | + "create API error": { |
| 136 | + CmdArgs: []string{ |
| 137 | + "--experiment=sandboxes", |
| 138 | + "--token", "xoxb-test-token", |
| 139 | + "--name", "err-box", |
| 140 | + "--domain", "err-box", |
| 141 | + "--password", "pass", |
| 142 | + }, |
| 143 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 144 | + testToken := "xoxb-test-token" |
| 145 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 146 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 147 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 148 | + cm.API.On("CreateSandbox", mock.Anything, testToken, "err-box", "err-box", "pass", "", "", "", "", int64(0)). |
| 149 | + Return("", "", errors.New("api_error")) |
| 150 | + |
| 151 | + cm.AddDefaultMocks() |
| 152 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 153 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 154 | + }, |
| 155 | + ExpectedErrorStrings: []string{"api_error"}, |
| 156 | + }, |
| 157 | + "invalid TTL": { |
| 158 | + CmdArgs: []string{ |
| 159 | + "--experiment=sandboxes", |
| 160 | + "--token", "xoxb-test-token", |
| 161 | + "--name", "ttl-box", |
| 162 | + "--domain", "ttl-box", |
| 163 | + "--password", "pass", |
| 164 | + "--ttl", "invalid", |
| 165 | + }, |
| 166 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 167 | + testToken := "xoxb-test-token" |
| 168 | + cm.Auth.On("AuthWithToken", mock.Anything, testToken).Return(types.SlackAuth{Token: testToken}, nil) |
| 169 | + cm.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).Return("https://api.slack.com") |
| 170 | + cm.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).Return("https://slackb.com/events/cli") |
| 171 | + |
| 172 | + cm.AddDefaultMocks() |
| 173 | + cm.Config.ExperimentsFlag = []string{string(experiment.Sandboxes)} |
| 174 | + cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug) |
| 175 | + }, |
| 176 | + ExpectedErrorStrings: []string{"Invalid TTL"}, |
| 177 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 178 | + cm.API.AssertNotCalled(t, "CreateSandbox", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) |
| 179 | + }, |
| 180 | + }, |
| 181 | + "experiment required": { |
| 182 | + CmdArgs: []string{ |
| 183 | + "--name", "test-box", |
| 184 | + "--domain", "test-box", |
| 185 | + "--password", "pass", |
| 186 | + }, |
| 187 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 188 | + cm.AddDefaultMocks() |
| 189 | + }, |
| 190 | + ExpectedErrorStrings: []string{"sandbox"}, |
| 191 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 192 | + cm.API.AssertNotCalled(t, "CreateSandbox", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, func(cf *shared.ClientFactory) *cobra.Command { |
| 196 | + return NewCreateCommand(cf) |
| 197 | + }) |
| 198 | +} |
| 199 | + |
| 200 | +func Test_ttlToArchiveDate(t *testing.T) { |
| 201 | + tests := []struct { |
| 202 | + name string |
| 203 | + ttl string |
| 204 | + wantErr bool |
| 205 | + }{ |
| 206 | + {"empty", "", false}, |
| 207 | + {"24h", "24h", false}, |
| 208 | + {"1d", "1d", false}, |
| 209 | + {"7d", "7d", false}, |
| 210 | + {"invalid", "invalid", true}, |
| 211 | + {"exceeds max", "200d", true}, |
| 212 | + } |
| 213 | + for _, tt := range tests { |
| 214 | + t.Run(tt.name, func(t *testing.T) { |
| 215 | + got, err := ttlToArchiveDate(tt.ttl) |
| 216 | + if tt.wantErr { |
| 217 | + assert.Error(t, err) |
| 218 | + return |
| 219 | + } |
| 220 | + assert.NoError(t, err) |
| 221 | + if tt.ttl == "" { |
| 222 | + assert.Equal(t, int64(0), got) |
| 223 | + } else { |
| 224 | + assert.Greater(t, got, int64(0), "archive date should be in the future") |
| 225 | + } |
| 226 | + }) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +func Test_slugFromsandboxName(t *testing.T) { |
| 231 | + tests := []struct { |
| 232 | + name string |
| 233 | + in string |
| 234 | + want string |
| 235 | + }{ |
| 236 | + {"simple", "test-box", "test-box"}, |
| 237 | + {"spaces", "My Test Box", "my-test-box"}, |
| 238 | + {"uppercase", "MyBox", "mybox"}, |
| 239 | + {"mixed", "Hello_World 123", "hello-world-123"}, |
| 240 | + {"hyphens", "a--b", "a-b"}, |
| 241 | + {"leading trailing", "-test-", "test"}, |
| 242 | + {"empty", "", "sandbox"}, |
| 243 | + } |
| 244 | + for _, tt := range tests { |
| 245 | + t.Run(tt.name, func(t *testing.T) { |
| 246 | + got := slugFromsandboxName(tt.in) |
| 247 | + assert.Equal(t, tt.want, got) |
| 248 | + }) |
| 249 | + } |
| 250 | +} |
0 commit comments