Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions cmd/datastore/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,55 @@ func prepareExportMockData(cm *shared.ClientsMock, numberOfItems int, maxItemsTo
}
return data, nil
}

func Test_getExpressionPatterns(t *testing.T) {
tests := map[string]struct {
expression string
wantAttrs int
wantVals int
}{
"expression with attributes and values": {
expression: "#name = :name AND #status = :status",
wantAttrs: 2,
wantVals: 2,
},
"empty expression": {
expression: "",
wantAttrs: 0,
wantVals: 0,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
attrs, vals := getExpressionPatterns(tc.expression)
assert.Len(t, attrs, tc.wantAttrs)
assert.Len(t, vals, tc.wantVals)
})
}
}

func Test_mapAttributeFlag(t *testing.T) {
tests := map[string]struct {
flag string
wantErr bool
}{
"valid JSON": {
flag: `{"#name":"name"}`,
},
"invalid JSON": {
flag: `not json`,
wantErr: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
result, err := mapAttributeFlag(tc.flag)
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, result)
}
})
}
}
48 changes: 48 additions & 0 deletions internal/api/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,51 @@ func TestClient_DeveloperAppInstall_RequestAppApproval(t *testing.T) {
})
}
}

func TestClient_GetAppStatus_Ok(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: appStatusMethod,
Response: `{"ok":true,"apps":[{"app_id":"A123","status":"installed"}]}`,
})
defer teardown()
result, err := c.GetAppStatus(ctx, "token", []string{"A123"}, "T123")
require.NoError(t, err)
require.NotNil(t, result)
}

func TestClient_GetAppStatus_Error(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: appStatusMethod,
Response: `{"ok":false,"error":"invalid_app"}`,
})
defer teardown()
_, err := c.GetAppStatus(ctx, "token", []string{"A123"}, "T123")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid_app")
}

func TestClient_ConnectionsOpen_Ok(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: appConnectionsOpenMethod,
Response: `{"ok":true,"url":"wss://example.com/ws"}`,
})
defer teardown()
result, err := c.ConnectionsOpen(ctx, "token")
require.NoError(t, err)
require.Equal(t, "wss://example.com/ws", result.URL)
}

func TestClient_ConnectionsOpen_Error(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: appConnectionsOpenMethod,
Response: `{"ok":false,"error":"token_revoked"}`,
})
defer teardown()
_, err := c.ConnectionsOpen(ctx, "token")
require.Error(t, err)
require.Contains(t, err.Error(), "token_revoked")
}
52 changes: 52 additions & 0 deletions internal/api/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package api

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHost(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 TestSetHost(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())
})
}
}
115 changes: 115 additions & 0 deletions internal/api/collaborators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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 TestClient_AddCollaborator_Ok(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsAddMethod,
Response: `{"ok":true}`,
})
defer teardown()
err := c.AddCollaborator(ctx, "token", "A123", types.SlackUser{Email: "user@example.com", PermissionType: "owner"})
require.NoError(t, err)
}

func TestClient_AddCollaborator_WithUserID(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsAddMethod,
Response: `{"ok":true}`,
})
defer teardown()
err := c.AddCollaborator(ctx, "token", "A123", types.SlackUser{ID: "U123", PermissionType: "owner"})
require.NoError(t, err)
}

func TestClient_AddCollaborator_Error(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsAddMethod,
Response: `{"ok":false,"error":"user_not_found"}`,
})
defer teardown()
err := c.AddCollaborator(ctx, "token", "A123", types.SlackUser{Email: "bad@example.com"})
require.Error(t, err)
require.Contains(t, err.Error(), "user_not_found")
}

func TestClient_ListCollaborators_Ok(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsListMethod,
Response: `{"ok":true,"owners":[{"user_id":"U123","username":"Test User"}]}`,
})
defer teardown()
users, err := c.ListCollaborators(ctx, "token", "A123")
require.NoError(t, err)
require.Len(t, users, 1)
require.Equal(t, "U123", users[0].ID)
}

func TestClient_ListCollaborators_Error(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsListMethod,
Response: `{"ok":false,"error":"app_not_found"}`,
})
defer teardown()
_, err := c.ListCollaborators(ctx, "token", "A123")
require.Error(t, err)
require.Contains(t, err.Error(), "app_not_found")
}

func TestClient_RemoveCollaborator_Ok(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsRemoveMethod,
Response: `{"ok":true}`,
})
defer teardown()
warnings, err := c.RemoveCollaborator(ctx, "token", "A123", types.SlackUser{ID: "U123"})
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestClient_RemoveCollaborator_Error(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsRemoveMethod,
Response: `{"ok":false,"error":"cannot_remove_owner"}`,
})
defer teardown()
_, err := c.RemoveCollaborator(ctx, "token", "A123", types.SlackUser{Email: "owner@example.com"})
require.Error(t, err)
require.Contains(t, err.Error(), "cannot_remove_owner")
}

func TestClient_UpdateCollaborator_Ok(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsUpdateMethod,
Response: `{"ok":true}`,
})
defer teardown()
err := c.UpdateCollaborator(ctx, "token", "A123", types.SlackUser{ID: "U123", PermissionType: "collaborator"})
require.NoError(t, err)
}

func TestClient_UpdateCollaborator_Error(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
c, teardown := NewFakeClient(t, FakeClientParams{
ExpectedMethod: collaboratorsUpdateMethod,
Response: `{"ok":false,"error":"invalid_permission"}`,
})
defer teardown()
err := c.UpdateCollaborator(ctx, "token", "A123", types.SlackUser{ID: "U123", PermissionType: "invalid"})
require.Error(t, err)
require.Contains(t, err.Error(), "invalid_permission")
}
Loading
Loading