Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 15, 2024
1 parent b78f4a3 commit ea9ced7
Show file tree
Hide file tree
Showing 22 changed files with 481 additions and 76 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ goreleaser:

tags:
gotags -R *.go > $@

generate: | install_tools
go generate ./...
.PHONY:generate

install_tools:
go install go.uber.org/mock/mockgen@latest
go install golang.org/x/tools/cmd/stringer@latest
.PHONY: install_tools
16 changes: 13 additions & 3 deletions auth/auth_ui/auth_ui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package auth_ui

import (
"errors"
"fmt"
"net/url"
"strings"
)
Expand All @@ -20,9 +22,11 @@ const (
LCancel
)

var ErrInvalidDomain = errors.New("invalid domain")

// Sanitize takes a workspace name or URL and returns the workspace name.
func Sanitize(workspace string) (string, error) {
if !strings.Contains(workspace, ".slack.com") {
if !strings.Contains(workspace, ".slack.com") && !strings.Contains(workspace, ".") {
return workspace, nil
}
if strings.HasPrefix(workspace, "https://") {
Expand All @@ -33,6 +37,12 @@ func Sanitize(workspace string) (string, error) {
workspace = uri.Host
}
// parse
parts := strings.Split(workspace, ".")
return parts[0], nil
name, domain, found := strings.Cut(workspace, ".")
if !found {
return "", errors.New("workspace name is empty")
}
if strings.TrimRight(domain, "/") != "slack.com" {
return "", fmt.Errorf("%s: %w", domain, ErrInvalidDomain)
}
return name, nil
}
1 change: 1 addition & 0 deletions auth/auth_ui/auth_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestSanitize(t *testing.T) {
{"url no slash", args{"https://blahblah.slack.com"}, "blahblah", false},
{"url no schema slash", args{"blahblah.slack.com/"}, "blahblah", false},
{"url no schema no slash", args{"blahblah.slack.com"}, "blahblah", false},
{"not a slack domain", args{"blahblah.example.com"}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
17 changes: 9 additions & 8 deletions auth/browser/playwright_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions clienter_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/slackdump/internal/emoji/emojidl/emoji_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/slackdump/internal/list/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions cmd/slackdump/internal/ui/bubbles/datepicker/focus_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/slackdump/internal/workspace/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions cmd/slackdump/internal/workspace/workspaceui/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package workspaceui

import (
"context"
"errors"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/auth/auth_ui"
)

//go:generate mockgen -package workspaceui -destination=test_mock_manager.go -source api.go manager
type manager interface {
SaveProvider(workspace string, p auth.Provider) error
Select(workspace string) error
Expand All @@ -24,6 +26,9 @@ func createAndSelect(ctx context.Context, m manager, prov auth.Provider) (string
if err != nil {
return "", err
}
if wsp == "" {
return "", errors.New("workspace name is empty")
}
if err := m.SaveProvider(wsp, prov); err != nil {
return "", err
}
Expand Down
117 changes: 117 additions & 0 deletions cmd/slackdump/internal/workspace/workspaceui/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package workspaceui

import (
"context"
"testing"

"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/internal/fixtures"
"github.com/rusq/slackdump/v3/internal/mocks/mock_auth"
"github.com/stretchr/testify/assert"
gomock "go.uber.org/mock/gomock"
)

func Test_createAndSelect(t *testing.T) {
type args struct {
ctx context.Context
// m manager
// prov auth.Provider
}
tests := []struct {
name string
args args
expectFn func(mp *mock_auth.MockProvider, mm *Mockmanager)
want string
wantErr bool
}{
{
name: "provider test fails",
args: args{
ctx: context.Background(),
},
expectFn: func(mp *mock_auth.MockProvider, mm *Mockmanager) {
mp.EXPECT().Test(gomock.Any()).Return(nil, assert.AnError)
},
want: "",
wantErr: true,
},
{
name: "save provider fails",
args: args{
ctx: context.Background(),
},
expectFn: func(mp *mock_auth.MockProvider, mm *Mockmanager) {
mp.EXPECT().Test(gomock.Any()).Return(fixtures.LoadPtr[slack.AuthTestResponse](string(fixtures.TestAuthTestInfo)), nil)
mm.EXPECT().SaveProvider(gomock.Any(), gomock.Any()).Return(assert.AnError)
},
want: "",
wantErr: true,
},
{
name: "select fails",
args: args{
ctx: context.Background(),
},
expectFn: func(mp *mock_auth.MockProvider, mm *Mockmanager) {
mp.EXPECT().Test(gomock.Any()).Return(fixtures.LoadPtr[slack.AuthTestResponse](string(fixtures.TestAuthTestInfo)), nil)
mm.EXPECT().SaveProvider(gomock.Any(), gomock.Any()).Return(nil)
mm.EXPECT().Select(gomock.Any()).Return(assert.AnError)
},
want: "",
wantErr: true,
},
{
name: "success",
args: args{
ctx: context.Background(),
},
expectFn: func(mp *mock_auth.MockProvider, mm *Mockmanager) {
mp.EXPECT().Test(gomock.Any()).Return(fixtures.LoadPtr[slack.AuthTestResponse](string(fixtures.TestAuthTestInfo)), nil)
mm.EXPECT().SaveProvider(gomock.Any(), gomock.Any()).Return(nil)
mm.EXPECT().Select(gomock.Any()).Return(nil)
},
want: "test",
wantErr: false,
},
{
name: "url empty fails",
args: args{
ctx: context.Background(),
},
expectFn: func(mp *mock_auth.MockProvider, mm *Mockmanager) {
mp.EXPECT().Test(gomock.Any()).Return(&slack.AuthTestResponse{URL: ""}, nil)
},
want: "",
wantErr: true,
},
{
name: "url sanitize fails",
args: args{
ctx: context.Background(),
},
expectFn: func(mp *mock_auth.MockProvider, mm *Mockmanager) {
mp.EXPECT().Test(gomock.Any()).Return(&slack.AuthTestResponse{URL: "ftp://lol.example.com"}, nil)
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
mp := mock_auth.NewMockProvider(ctrl)
mm := NewMockmanager(ctrl)
if tt.expectFn != nil {
tt.expectFn(mp, mm)
}
got, err := createAndSelect(tt.args.ctx, mm, mp)
if (err != nil) != tt.wantErr {
t.Errorf("createAndSelect() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("createAndSelect() = %v, want %v", got, tt.want)
}
})
}
}
17 changes: 12 additions & 5 deletions cmd/slackdump/internal/workspace/workspaceui/filesecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func fileWithSecrets(ctx context.Context, mgr manager) error {
return nil
}
}

tok, cookie, err := parseSecretsTxt(filename)
if err != nil {
return err
Expand All @@ -54,6 +55,12 @@ func validateSecrets(filename string) error {
}

func parseSecretsTxt(filename string) (string, string, error) {
const (
tokenKey = "SLACK_TOKEN"
cookieKey = "SLACK_COOKIE"

clientTokenPrefix = "xoxc-"
)
f, err := os.Open(filename)
if err != nil {
return "", "", err
Expand All @@ -63,19 +70,19 @@ func parseSecretsTxt(filename string) (string, string, error) {
if err != nil {
return "", "", errors.New("not a secrets file")
}
token, ok := secrets["SLACK_TOKEN"]
token, ok := secrets[tokenKey]
if !ok {
return "", "", errors.New("no SLACK_TOKEN found")
return "", "", errors.New("no SLACK_TOKEN found in the file")
}
if err := structures.ValidateToken(token); err != nil {
return "", "", err
}
if !strings.HasPrefix(token, "xoxc-") {
if !strings.HasPrefix(token, clientTokenPrefix) {
return token, "", nil
}
cook, ok := secrets["SLACK_COOKIE"]
cook, ok := secrets[cookieKey]
if !ok {
return "", "", errors.New("no SLACK_COOKIE found")
return "", "", errors.New("no SLACK_COOKIE found in the file")
}
if !strings.HasPrefix(cook, "xoxd-") {
return "", "", errors.New("invalid cookie")
Expand Down
Loading

0 comments on commit ea9ced7

Please sign in to comment.