|
| 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 env |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/slackapi/slack-cli/internal/app" |
| 22 | + "github.com/slackapi/slack-cli/internal/hooks" |
| 23 | + "github.com/slackapi/slack-cli/internal/prompts" |
| 24 | + "github.com/slackapi/slack-cli/internal/shared" |
| 25 | + "github.com/slackapi/slack-cli/internal/shared/types" |
| 26 | + "github.com/slackapi/slack-cli/internal/slackerror" |
| 27 | + "github.com/slackapi/slack-cli/internal/slacktrace" |
| 28 | + "github.com/slackapi/slack-cli/test/testutil" |
| 29 | + "github.com/spf13/afero" |
| 30 | + "github.com/spf13/cobra" |
| 31 | + "github.com/stretchr/testify/assert" |
| 32 | + "github.com/stretchr/testify/mock" |
| 33 | +) |
| 34 | + |
| 35 | +func Test_Env_InitCommandPreRun(t *testing.T) { |
| 36 | + tests := map[string]struct { |
| 37 | + mockWorkingDirectory string |
| 38 | + expectedError error |
| 39 | + }{ |
| 40 | + "continues if the command is run in a project": { |
| 41 | + mockWorkingDirectory: "/slack/path/to/project", |
| 42 | + expectedError: nil, |
| 43 | + }, |
| 44 | + "errors if the command is not run in a project": { |
| 45 | + mockWorkingDirectory: "", |
| 46 | + expectedError: slackerror.New(slackerror.ErrInvalidAppDirectory), |
| 47 | + }, |
| 48 | + } |
| 49 | + for name, tc := range tests { |
| 50 | + t.Run(name, func(t *testing.T) { |
| 51 | + clientsMock := shared.NewClientsMock() |
| 52 | + clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(cf *shared.ClientFactory) { |
| 53 | + cf.SDKConfig.WorkingDirectory = tc.mockWorkingDirectory |
| 54 | + }) |
| 55 | + cmd := NewEnvInitCommand(clients) |
| 56 | + err := cmd.PreRunE(cmd, nil) |
| 57 | + if tc.expectedError != nil { |
| 58 | + assert.Equal(t, slackerror.ToSlackError(tc.expectedError).Code, slackerror.ToSlackError(err).Code) |
| 59 | + } else { |
| 60 | + assert.NoError(t, err) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func Test_Env_InitCommand(t *testing.T) { |
| 67 | + testutil.TableTestCommand(t, testutil.CommandTests{ |
| 68 | + "copies .env.sample to .env": { |
| 69 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 70 | + cm.AddDefaultMocks() |
| 71 | + manifestMock := &app.ManifestMockObject{} |
| 72 | + manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{}, slackerror.New(slackerror.ErrSDKHookNotFound)) |
| 73 | + cm.AppClient.Manifest = manifestMock |
| 74 | + err := afero.WriteFile(cf.Fs, ".env.sample", []byte("SECRET=placeholder\n"), 0600) |
| 75 | + assert.NoError(t, err) |
| 76 | + }, |
| 77 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 78 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.EnvInitSuccess, mock.Anything) |
| 79 | + content, err := afero.ReadFile(cm.Fs, ".env") |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.Equal(t, "SECRET=placeholder\n", string(content)) |
| 82 | + }, |
| 83 | + }, |
| 84 | + "copies .env.example to .env": { |
| 85 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 86 | + cm.AddDefaultMocks() |
| 87 | + manifestMock := &app.ManifestMockObject{} |
| 88 | + manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{}, slackerror.New(slackerror.ErrSDKHookNotFound)) |
| 89 | + cm.AppClient.Manifest = manifestMock |
| 90 | + err := afero.WriteFile(cf.Fs, ".env.example", []byte("TOKEN=example\n"), 0600) |
| 91 | + assert.NoError(t, err) |
| 92 | + }, |
| 93 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 94 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.EnvInitSuccess, mock.Anything) |
| 95 | + content, err := afero.ReadFile(cm.Fs, ".env") |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.Equal(t, "TOKEN=example\n", string(content)) |
| 98 | + }, |
| 99 | + }, |
| 100 | + "prints message when .env already exists": { |
| 101 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 102 | + cm.AddDefaultMocks() |
| 103 | + manifestMock := &app.ManifestMockObject{} |
| 104 | + manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{}, slackerror.New(slackerror.ErrSDKHookNotFound)) |
| 105 | + cm.AppClient.Manifest = manifestMock |
| 106 | + err := afero.WriteFile(cf.Fs, ".env", []byte("EXISTING=value\n"), 0600) |
| 107 | + assert.NoError(t, err) |
| 108 | + err = afero.WriteFile(cf.Fs, ".env.sample", []byte("NEW=value\n"), 0600) |
| 109 | + assert.NoError(t, err) |
| 110 | + }, |
| 111 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 112 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.EnvInitSuccess, mock.Anything) |
| 113 | + content, err := afero.ReadFile(cm.Fs, ".env") |
| 114 | + assert.NoError(t, err) |
| 115 | + assert.Equal(t, "EXISTING=value\n", string(content)) |
| 116 | + }, |
| 117 | + }, |
| 118 | + "prints message when no sample file exists": { |
| 119 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 120 | + cm.AddDefaultMocks() |
| 121 | + manifestMock := &app.ManifestMockObject{} |
| 122 | + manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{}, slackerror.New(slackerror.ErrSDKHookNotFound)) |
| 123 | + cm.AppClient.Manifest = manifestMock |
| 124 | + }, |
| 125 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 126 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.EnvInitSuccess, mock.Anything) |
| 127 | + }, |
| 128 | + }, |
| 129 | + "prints ROSI message for hosted non-dev app": { |
| 130 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 131 | + cf.SDKConfig = hooks.NewSDKConfigMock() |
| 132 | + cm.AddDefaultMocks() |
| 133 | + _ = cf.AppClient().SaveDeployed(ctx, mockApp) |
| 134 | + |
| 135 | + appSelectMock := prompts.NewAppSelectMock() |
| 136 | + appSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 137 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAppsOnly).Return(prompts.SelectedApp{Auth: mockAuth, App: mockApp}, nil) |
| 138 | + |
| 139 | + manifestMock := &app.ManifestMockObject{} |
| 140 | + manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return( |
| 141 | + types.SlackYaml{ |
| 142 | + AppManifest: types.AppManifest{ |
| 143 | + Settings: &types.AppSettings{ |
| 144 | + FunctionRuntime: types.SlackHosted, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + nil, |
| 149 | + ) |
| 150 | + cm.AppClient.Manifest = manifestMock |
| 151 | + }, |
| 152 | + ExpectedStdoutOutputs: []string{ |
| 153 | + "ROSI features", |
| 154 | + "env set", |
| 155 | + }, |
| 156 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 157 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.EnvInitSuccess, mock.Anything) |
| 158 | + _, err := afero.ReadFile(cm.Fs, ".env") |
| 159 | + assert.Error(t, err) |
| 160 | + }, |
| 161 | + }, |
| 162 | + "copies placeholder for hosted dev app": { |
| 163 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 164 | + cf.SDKConfig = hooks.NewSDKConfigMock() |
| 165 | + cm.AddDefaultMocks() |
| 166 | + |
| 167 | + devApp := types.App{ |
| 168 | + TeamID: "T1", |
| 169 | + TeamDomain: "team1", |
| 170 | + AppID: "A0123456789", |
| 171 | + IsDev: true, |
| 172 | + } |
| 173 | + appSelectMock := prompts.NewAppSelectMock() |
| 174 | + appSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 175 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAppsOnly).Return(prompts.SelectedApp{Auth: mockAuth, App: devApp}, nil) |
| 176 | + |
| 177 | + manifestMock := &app.ManifestMockObject{} |
| 178 | + manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return( |
| 179 | + types.SlackYaml{ |
| 180 | + AppManifest: types.AppManifest{ |
| 181 | + Settings: &types.AppSettings{ |
| 182 | + FunctionRuntime: types.SlackHosted, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + nil, |
| 187 | + ) |
| 188 | + cm.AppClient.Manifest = manifestMock |
| 189 | + |
| 190 | + err := afero.WriteFile(cf.Fs, ".env.sample", []byte("SECRET=placeholder\n"), 0600) |
| 191 | + assert.NoError(t, err) |
| 192 | + }, |
| 193 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 194 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.EnvInitSuccess, mock.Anything) |
| 195 | + content, err := afero.ReadFile(cm.Fs, ".env") |
| 196 | + assert.NoError(t, err) |
| 197 | + assert.Equal(t, "SECRET=placeholder\n", string(content)) |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, func(cf *shared.ClientFactory) *cobra.Command { |
| 201 | + cmd := NewEnvInitCommand(cf) |
| 202 | + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil } |
| 203 | + return cmd |
| 204 | + }) |
| 205 | +} |
0 commit comments