Skip to content

Commit

Permalink
add tests for workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 15, 2025
1 parent 626d2d4 commit 365fcd0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 10 deletions.
21 changes: 15 additions & 6 deletions cmd/slackdump/internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"runtime/trace"
"strings"
Expand All @@ -23,17 +24,18 @@ var CmdWorkspace = &base.Command{
Run: nil,
Wizard: nil,
UsageLine: baseCommand,
Short: "add or choose already existing workspace to run on",
Short: "manage Slack Workspaces",
Long: `
# Workspace Command
Slackdump supports working with multiple Slack Workspaces without the need
to authenticate again (unless login credentials are expired or became invalid
due to some other reason).
**Workspace** command allows to **add** a new Slack Workspace, **list** already
**Workspace** command allows to add a **new** Slack Workspace, **list** already
authenticated workspaces, **select** a workspace that you have previously
logged in to, or **del**ete an existing workspace.
logged in to, **del**ete an existing workspace, or **import** credentials from
an environment file.
To learn more about different login options, run:
Expand Down Expand Up @@ -92,6 +94,7 @@ func AuthCurrent(ctx context.Context, cacheDir string, overrideWsp string, usePl
return nil, err
}
trace.Logf(ctx, "AuthCurrent", "current workspace=%s", wsp)
slog.DebugContext(ctx, "current", "workspace", wsp)

prov, err := authWsp(ctx, cacheDir, wsp, usePlaywright)
if err != nil {
Expand Down Expand Up @@ -163,17 +166,23 @@ func CacheMgr(opts ...cache.Option) (*cache.Manager, error) {
return cache.NewManager(cfg.CacheDir(), opts...)
}

// exported for testing
var (
authCurrent = AuthCurrent
showUI = workspaceui.ShowUI
)

func CurrentOrNewProviderCtx(ctx context.Context) (context.Context, error) {
cachedir := cfg.CacheDir()
prov, err := AuthCurrent(ctx, cachedir, cfg.Workspace, cfg.LegacyBrowser)
prov, err := authCurrent(ctx, cachedir, cfg.Workspace, cfg.LegacyBrowser)
if err != nil {
if errors.Is(err, cache.ErrNoWorkspaces) {
// ask to create a new workspace
if err := workspaceui.ShowUI(ctx, workspaceui.WithQuickLogin(), workspaceui.WithTitle("No workspaces, please choose a login method")); err != nil {
if err := showUI(ctx, workspaceui.WithQuickLogin(), workspaceui.WithTitle("No workspaces, please choose a login method")); err != nil {
return ctx, fmt.Errorf("auth error: %w", err)
}
// one more time...
prov, err = AuthCurrent(ctx, cachedir, cfg.Workspace, cfg.LegacyBrowser)
prov, err = authCurrent(ctx, cachedir, cfg.Workspace, cfg.LegacyBrowser)
if err != nil {
return ctx, err
}
Expand Down
101 changes: 97 additions & 4 deletions cmd/slackdump/internal/workspace/workspace_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package workspace

import (
"context"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace/workspaceui"
"github.com/rusq/slackdump/v3/internal/cache"
fx "github.com/rusq/slackdump/v3/internal/fixtures"
)

Expand Down Expand Up @@ -54,7 +61,7 @@ func Test_argsWorkspace(t *testing.T) {
}

func TestCurrent(t *testing.T) {
//fixtures
// fixtures

empty := t.TempDir()

Expand All @@ -65,17 +72,17 @@ func TestCurrent(t *testing.T) {
// case2 has files, and a pointer to the current workspace.
case2 := t.TempDir()
fx.PrepareDir(t, case2, "dummy", fx.WorkspaceFiles...)
os.WriteFile(filepath.Join(case2, "workspace.txt"), []byte(fx.StripExt(fx.WorkspaceFiles[0])+"\n"), 0644)
os.WriteFile(filepath.Join(case2, "workspace.txt"), []byte(fx.StripExt(fx.WorkspaceFiles[0])+"\n"), 0o644)

// case3 has a file, which is specified as a directory to the function
// so that manager fails to initialise.
case3 := t.TempDir()
os.WriteFile(filepath.Join(case3, "cache_dir"), []byte(""), 0644)
os.WriteFile(filepath.Join(case3, "cache_dir"), []byte(""), 0o644)

// case4 workspace pointer points to non-existing file.
case4 := t.TempDir()
fx.PrepareDir(t, case4, "dummy", fx.WorkspaceFiles...)
os.WriteFile(filepath.Join(case4, "workspace.txt"), []byte("doesnotexist\n"), 0644)
os.WriteFile(filepath.Join(case4, "workspace.txt"), []byte("doesnotexist\n"), 0o644)

// tests
type args struct {
Expand Down Expand Up @@ -150,3 +157,89 @@ func TestCurrent(t *testing.T) {
})
}
}

type recorder struct {
authCurrentCalledTimes int
authCurrentRetProv auth.Provider
authCurrentRetErr error

showUICalledTimes int
showUIRetErr error
}

func (r *recorder) AuthCurrent(ctx context.Context, cacheDir string, overrideWsp string, usePlaywright bool) (auth.Provider, error) {
r.authCurrentCalledTimes++
return r.authCurrentRetProv, r.authCurrentRetErr
}

func (r *recorder) ShowUI(ctx context.Context, opts ...workspaceui.UIOption) error {
r.showUICalledTimes++
return r.showUIRetErr
}

func TestCurrentOrNewProviderCtx(t *testing.T) {
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
rec *recorder
want context.Context
wantErr bool
wantAuthCurrentCalledTimes int
wantShowUICalledTimes int
}{
{
"authCurrent fails",
args{context.Background()},
&recorder{
authCurrentRetErr: assert.AnError,
},
context.Background(),
true,
1,
0,
},
{
"authCurrent doesn't find workspace",
args{context.Background()},
&recorder{
authCurrentRetErr: cache.ErrNoWorkspaces,
},
context.Background(),
true,
2, // attempts to call authCurrent twice
1, // after showing the UI
},
{
"authCurrent succeeds",
args{context.Background()},
&recorder{
authCurrentRetProv: auth.ValueAuth{},
},
auth.WithContext(context.Background(), auth.ValueAuth{}),
false,
1,
0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := tt.rec
authCurrent = rec.AuthCurrent
showUI = rec.ShowUI

got, err := CurrentOrNewProviderCtx(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("CurrentOrNewProviderCtx() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CurrentOrNewProviderCtx() = %v, want %v", got, tt.want)
}
assert.Equal(t, rec.authCurrentCalledTimes, tt.wantAuthCurrentCalledTimes)
assert.Equal(t, rec.showUICalledTimes, tt.wantShowUICalledTimes)
})
}
}

0 comments on commit 365fcd0

Please sign in to comment.