-
Notifications
You must be signed in to change notification settings - Fork 29
test: add test coverage for untested files #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
84aab1f
b6df3ac
c77463f
7986821
d88ed1f
e3340c9
57362fb
0be8c06
8bee097
6e3c728
758ce76
26d5fbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright 2022-2026 Salesforce, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func Test_Client_Host(t *testing.T) { | ||
| tests := map[string]struct { | ||
| host string | ||
| expected string | ||
| }{ | ||
| "returns the configured host": { | ||
| host: "https://slack.com", | ||
| expected: "https://slack.com", | ||
| }, | ||
| "returns empty when unset": { | ||
| host: "", | ||
| expected: "", | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| c := &Client{host: tc.host} | ||
| assert.Equal(t, tc.expected, c.Host()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_Client_SetHost(t *testing.T) { | ||
| tests := map[string]struct { | ||
| initial string | ||
| newHost string | ||
| }{ | ||
| "sets a new host": { | ||
| initial: "", | ||
| newHost: "https://dev.slack.com", | ||
| }, | ||
| "overwrites existing host": { | ||
| initial: "https://slack.com", | ||
| newHost: "https://dev.slack.com", | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| c := &Client{host: tc.initial} | ||
| c.SetHost(tc.newHost) | ||
| assert.Equal(t, tc.newHost, c.Host()) | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // Copyright 2022-2026 Salesforce, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/slackapi/slack-cli/internal/shared/types" | ||
| "github.com/slackapi/slack-cli/internal/slackcontext" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_Client_AddCollaborator(t *testing.T) { | ||
| tests := map[string]struct { | ||
| response string | ||
| user types.SlackUser | ||
| expectErr string | ||
| }{ | ||
| "adds a collaborator by email": { | ||
| response: `{"ok":true}`, | ||
| user: types.SlackUser{Email: "user@example.com", PermissionType: "owner"}, | ||
| }, | ||
| "adds a collaborator by user ID": { | ||
| response: `{"ok":true}`, | ||
| user: types.SlackUser{ID: "U123", PermissionType: "owner"}, | ||
| }, | ||
| "returns error when user not found": { | ||
| response: `{"ok":false,"error":"user_not_found"}`, | ||
| user: types.SlackUser{Email: "bad@example.com"}, | ||
| expectErr: "user_not_found", | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| c, teardown := NewFakeClient(t, FakeClientParams{ | ||
| ExpectedMethod: collaboratorsAddMethod, | ||
| Response: tc.response, | ||
| }) | ||
| defer teardown() | ||
| err := c.AddCollaborator(ctx, "token", "A123", tc.user) | ||
| if tc.expectErr != "" { | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), tc.expectErr) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_Client_ListCollaborators(t *testing.T) { | ||
| tests := map[string]struct { | ||
| response string | ||
| expectedCount int | ||
| expectedID string | ||
| expectErr string | ||
| }{ | ||
| "returns a list of collaborators": { | ||
| response: `{"ok":true,"owners":[{"user_id":"U123","username":"Test User"}]}`, | ||
| expectedCount: 1, | ||
| expectedID: "U123", | ||
| }, | ||
| "returns error when app not found": { | ||
| response: `{"ok":false,"error":"app_not_found"}`, | ||
| expectErr: "app_not_found", | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| c, teardown := NewFakeClient(t, FakeClientParams{ | ||
| ExpectedMethod: collaboratorsListMethod, | ||
| Response: tc.response, | ||
| }) | ||
| defer teardown() | ||
| users, err := c.ListCollaborators(ctx, "token", "A123") | ||
| if tc.expectErr != "" { | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), tc.expectErr) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Len(t, users, tc.expectedCount) | ||
| require.Equal(t, tc.expectedID, users[0].ID) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_Client_RemoveCollaborator(t *testing.T) { | ||
| tests := map[string]struct { | ||
| response string | ||
| user types.SlackUser | ||
| expectErr string | ||
| }{ | ||
| "removes a collaborator": { | ||
| response: `{"ok":true}`, | ||
| user: types.SlackUser{ID: "U123"}, | ||
| }, | ||
| "returns error when removing owner": { | ||
| response: `{"ok":false,"error":"cannot_remove_owner"}`, | ||
| user: types.SlackUser{Email: "owner@example.com"}, | ||
| expectErr: "cannot_remove_owner", | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| c, teardown := NewFakeClient(t, FakeClientParams{ | ||
| ExpectedMethod: collaboratorsRemoveMethod, | ||
| Response: tc.response, | ||
| }) | ||
| defer teardown() | ||
| warnings, err := c.RemoveCollaborator(ctx, "token", "A123", tc.user) | ||
| if tc.expectErr != "" { | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), tc.expectErr) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Empty(t, warnings) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_Client_UpdateCollaborator(t *testing.T) { | ||
| tests := map[string]struct { | ||
| response string | ||
| user types.SlackUser | ||
| expectErr string | ||
| }{ | ||
| "updates a collaborator": { | ||
| response: `{"ok":true}`, | ||
| user: types.SlackUser{ID: "U123", PermissionType: "collaborator"}, | ||
| }, | ||
| "returns error for invalid permission": { | ||
| response: `{"ok":false,"error":"invalid_permission"}`, | ||
| user: types.SlackUser{ID: "U123", PermissionType: "invalid"}, | ||
| expectErr: "invalid_permission", | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| c, teardown := NewFakeClient(t, FakeClientParams{ | ||
| ExpectedMethod: collaboratorsUpdateMethod, | ||
| Response: tc.response, | ||
| }) | ||
| defer teardown() | ||
| err := c.UpdateCollaborator(ctx, "token", "A123", tc.user) | ||
| if tc.expectErr != "" { | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), tc.expectErr) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // Copyright 2022-2026 Salesforce, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package archiveutil | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "archive/zip" | ||
| "compress/gzip" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TODO: Refactor to use afero.Fs once ExtractAndWriteFile accepts it. Currently uses t.TempDir() which is safe. | ||
| func Test_ExtractAndWriteFile(t *testing.T) { | ||
|
Comment on lines
+30
to
+31
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: I've decided to implement these tests now using the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mwbrooks Amazing callout! Having these tests in place will make that change much more confident I think! 🧪 ✨
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to hear that you agree. I was on the fence, since we are trying to avoid I/O during testing. But I'll be sure to follow-up and remove these in an upcoming PR. |
||
| tests := map[string]struct { | ||
| fileName string | ||
| content string | ||
| isDir bool | ||
| }{ | ||
| "extracts a regular file": { | ||
| fileName: "hello.txt", | ||
| content: "hello world", | ||
| isDir: false, | ||
| }, | ||
| "creates a directory": { | ||
| fileName: "subdir/", | ||
| isDir: true, | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| destDir := t.TempDir() | ||
| reader := strings.NewReader(tc.content) | ||
|
|
||
| path, err := ExtractAndWriteFile(reader, destDir, tc.fileName, tc.isDir, 0644) | ||
| require.NoError(t, err) | ||
| assert.True(t, strings.HasPrefix(path, destDir)) | ||
|
|
||
| if tc.isDir { | ||
| info, err := os.Stat(path) | ||
| require.NoError(t, err) | ||
| assert.True(t, info.IsDir()) | ||
| } else { | ||
| content, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tc.content, string(content)) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| t.Run("rejects path traversal", func(t *testing.T) { | ||
| destDir := t.TempDir() | ||
| reader := strings.NewReader("malicious") | ||
| _, err := ExtractAndWriteFile(reader, destDir, "../../../etc/passwd", false, 0644) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "illegal file path") | ||
| }) | ||
| } | ||
|
|
||
| // TODO: Refactor to use afero.Fs once Unzip accepts it. Currently uses t.TempDir() which is safe. | ||
| func Test_Unzip(t *testing.T) { | ||
| t.Run("extracts a zip archive", func(t *testing.T) { | ||
| srcDir := t.TempDir() | ||
| destDir := t.TempDir() | ||
|
|
||
| // Create a zip file | ||
| zipPath := filepath.Join(srcDir, "test.zip") | ||
| zipFile, err := os.Create(zipPath) | ||
| require.NoError(t, err) | ||
|
|
||
| w := zip.NewWriter(zipFile) | ||
| f, err := w.Create("test.txt") | ||
| require.NoError(t, err) | ||
| _, err = f.Write([]byte("zip content")) | ||
| require.NoError(t, err) | ||
| err = w.Close() | ||
| require.NoError(t, err) | ||
| err = zipFile.Close() | ||
| require.NoError(t, err) | ||
|
|
||
| files, err := Unzip(zipPath, destDir) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, files) | ||
|
|
||
| content, err := os.ReadFile(filepath.Join(destDir, "test.txt")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "zip content", string(content)) | ||
| }) | ||
|
|
||
| t.Run("returns error for non-existent file", func(t *testing.T) { | ||
| _, err := Unzip("/nonexistent.zip", t.TempDir()) | ||
| assert.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| // TODO: Refactor to use afero.Fs once UntarGzip accepts it. Currently uses t.TempDir() which is safe. | ||
| func Test_UntarGzip(t *testing.T) { | ||
| t.Run("extracts a tar.gz archive", func(t *testing.T) { | ||
| srcDir := t.TempDir() | ||
| destDir := t.TempDir() | ||
|
|
||
| // Create a tar.gz file | ||
| tgzPath := filepath.Join(srcDir, "test.tar.gz") | ||
| tgzFile, err := os.Create(tgzPath) | ||
| require.NoError(t, err) | ||
|
|
||
| gw := gzip.NewWriter(tgzFile) | ||
| tw := tar.NewWriter(gw) | ||
|
|
||
| content := []byte("tar content") | ||
| hdr := &tar.Header{ | ||
| Name: "test.txt", | ||
| Mode: 0644, | ||
| Size: int64(len(content)), | ||
| Typeflag: tar.TypeReg, | ||
| } | ||
| err = tw.WriteHeader(hdr) | ||
| require.NoError(t, err) | ||
| _, err = tw.Write(content) | ||
| require.NoError(t, err) | ||
|
|
||
| err = tw.Close() | ||
| require.NoError(t, err) | ||
| err = gw.Close() | ||
| require.NoError(t, err) | ||
| err = tgzFile.Close() | ||
| require.NoError(t, err) | ||
|
|
||
| files, err := UntarGzip(tgzPath, destDir) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, files) | ||
|
|
||
| data, err := os.ReadFile(filepath.Join(destDir, "test.txt")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "tar content", string(data)) | ||
| }) | ||
|
|
||
| t.Run("returns error for non-existent file", func(t *testing.T) { | ||
| _, err := UntarGzip("/nonexistent.tar.gz", t.TempDir()) | ||
| assert.Error(t, err) | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⭐ praise: These tests aren't so complicated but that's amazing I think! It gives me confidence that existing API methods are gathering expected responses as we expect!