-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from rusq/v3-tests
V3 tests
- Loading branch information
Showing
51 changed files
with
1,355 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,3 @@ | ||
// Package bootstrap contains some initialisation functions that are shared | ||
// between main some other top level commands, i.e. wizard. | ||
package bootstrap | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rusq/slackdump/v3/auth" | ||
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg" | ||
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace" | ||
) | ||
|
||
// CurrentProviderCtx returns the context with the current provider. | ||
func CurrentProviderCtx(ctx context.Context) (context.Context, error) { | ||
prov, err := workspace.AuthCurrent(ctx, cfg.CacheDir(), cfg.Workspace, cfg.LegacyBrowser) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
return auth.WithContext(ctx, prov), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rusq/slackdump/v3/auth" | ||
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg" | ||
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace" | ||
) | ||
|
||
// CurrentProviderCtx returns the context with the current provider. | ||
func CurrentProviderCtx(ctx context.Context) (context.Context, error) { | ||
prov, err := workspace.AuthCurrent(ctx, cfg.CacheDir(), cfg.Workspace, cfg.LegacyBrowser) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
return auth.WithContext(ctx, prov), nil | ||
} |
11 changes: 5 additions & 6 deletions
11
cmd/slackdump/internal/cfg/slackdump.go → ...slackdump/internal/bootstrap/slackdump.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/rusq/slack" | ||
"github.com/rusq/slackdump/v3" | ||
"github.com/rusq/slackdump/v3/auth" | ||
"github.com/rusq/slackdump/v3/internal/fixtures" | ||
) | ||
|
||
func TestSlackdumpSession(t *testing.T) { | ||
t.Run("no auth in context", func(t *testing.T) { | ||
_, err := SlackdumpSession(context.Background()) | ||
if err == nil { | ||
t.Error("expected error") | ||
} | ||
}) | ||
t.Run("auth in context", func(t *testing.T) { | ||
var authJSON = `{"token":"` + strings.Replace(fixtures.TestClientToken, `xoxc`, `xoxb`, -1) + `"}` | ||
prov, err := auth.Load(strings.NewReader(authJSON)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// start fake Slack server | ||
srv := fixtures.TestAuthServer(t) | ||
defer srv.Close() | ||
s := slack.New("", slack.OptionAPIURL(srv.URL+"/")) | ||
|
||
ctx := auth.WithContext(context.Background(), prov) | ||
if _, err := SlackdumpSession(ctx, slackdump.WithSlackClient(s)); err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Package cfg contains common configuration variables. | ||
package cfg | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
"time" | ||
|
||
"github.com/rusq/slackdump/v3/auth/browser" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetBaseFlags(t *testing.T) { | ||
t.Run("all flags are set", func(t *testing.T) { | ||
fs := flag.NewFlagSet("test", flag.ExitOnError) | ||
mask := DefaultFlags | ||
|
||
SetBaseFlags(fs, mask) | ||
|
||
// Test flag parsing and assignment | ||
fs.Parse([]string{ | ||
"-trace", "trace.log", | ||
"-log", "log.txt", | ||
"-v", | ||
"-token", "slack_token", | ||
"-cookie", "slack_cookie", | ||
"-browser", "firefox", | ||
"-browser-timeout", "5s", | ||
"-autologin-timeout", "10s", | ||
"-legacy-browser", | ||
"-enterprise", | ||
"-user-agent", "Mozilla/5.0", | ||
"-files=false", | ||
"-api-config", "config.json", | ||
"-o", "output.zip", | ||
"-cache-dir", "/tmp/cache", | ||
"-workspace", "my_workspace", | ||
"-no-user-cache", | ||
"-user-cache-retention", "30m", | ||
"-no-chunk-cache", | ||
"-time-from", "2022-01-01T00:00:00", | ||
"-time-to", "2022-01-31T23:59:59", | ||
}) | ||
|
||
// Test flag values | ||
if TraceFile != "trace.log" { | ||
t.Errorf("Expected TraceFile to be 'trace.log', got '%s'", TraceFile) | ||
} | ||
if LogFile != "log.txt" { | ||
t.Errorf("Expected LogFile to be 'log.txt', got '%s'", LogFile) | ||
} | ||
if !Verbose { | ||
t.Error("Expected Verbose to be true, got false") | ||
} | ||
if SlackToken != "slack_token" { | ||
t.Errorf("Expected SlackToken to be 'slack_token', got '%s'", SlackToken) | ||
} | ||
if SlackCookie != "slack_cookie" { | ||
t.Errorf("Expected SlackCookie to be 'slack_cookie', got '%s'", SlackCookie) | ||
} | ||
if Browser != browser.Bfirefox { | ||
t.Errorf("Expected Browser to be 'chrome', got '%s'", Browser) | ||
} | ||
if LoginTimeout != 5*time.Second { | ||
t.Errorf("Expected LoginTimeout to be 5 seconds, got %s", LoginTimeout) | ||
} | ||
if HeadlessTimeout != 10*time.Second { | ||
t.Errorf("Expected HeadlessTimeout to be 10 seconds, got %s", HeadlessTimeout) | ||
} | ||
if !LegacyBrowser { | ||
t.Error("Expected LegacyBrowser to be true, got false") | ||
} | ||
if !ForceEnterprise { | ||
t.Error("Expected ForceEnterprise to be true, got false") | ||
} | ||
if RODUserAgent != "Mozilla/5.0" { | ||
t.Errorf("Expected RODUserAgent to be 'Mozilla/5.0', got '%s'", RODUserAgent) | ||
} | ||
if DownloadFiles { | ||
t.Error("Expected DownloadFiles to be false, got true") | ||
} | ||
if ConfigFile != "config.json" { | ||
t.Errorf("Expected ConfigFile to be 'config.json', got '%s'", ConfigFile) | ||
} | ||
if Output != "output.zip" { | ||
t.Errorf("Expected Output to be 'output.zip', got '%s'", Output) | ||
} | ||
if LocalCacheDir != "/tmp/cache" { | ||
t.Errorf("Expected LocalCacheDir to be '/tmp/cache', got '%s'", LocalCacheDir) | ||
} | ||
if Workspace != "my_workspace" { | ||
t.Errorf("Expected Workspace to be 'my_workspace', got '%s'", Workspace) | ||
} | ||
if !NoUserCache { | ||
t.Error("Expected NoUserCache to be true, got false") | ||
} | ||
if UserCacheRetention != 30*time.Minute { | ||
t.Errorf("Expected UserCacheRetention to be 30 minutes, got %s", UserCacheRetention) | ||
} | ||
if !NoChunkCache { | ||
t.Error("Expected NoChunkCache to be true, got false") | ||
} | ||
if Oldest.String() != "2022-01-01T00:00:00" { | ||
t.Errorf("Expected Oldest to be '2022-01-01T00:00:00Z', got '%s'", Oldest.String()) | ||
} | ||
if Latest.String() != "2022-01-31T23:59:59" { | ||
t.Errorf("Expected Latest to be '2022-01-31T23:59:59Z', got '%s'", Latest.String()) | ||
} | ||
}) | ||
t.Run("omit cache dir set", func(t *testing.T) { | ||
fs := flag.NewFlagSet("test", flag.ExitOnError) | ||
mask := OmitCacheDir | ||
|
||
SetBaseFlags(fs, mask) | ||
fs.Parse([]string{}) | ||
|
||
assert.Equal(t, LocalCacheDir, CacheDir()) | ||
}) | ||
} |
Oops, something went wrong.