-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
601 additions
and
260 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,79 @@ | ||
package export | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/rusq/fsadapter" | ||
"github.com/rusq/slackdump/v2" | ||
"github.com/rusq/slackdump/v2/export" | ||
"github.com/rusq/slackdump/v2/internal/chunk/chunktest" | ||
"github.com/rusq/slackdump/v2/internal/structures" | ||
"github.com/slack-go/slack" | ||
) | ||
|
||
const ( | ||
baseDir = "../../../../" | ||
chunkDir = baseDir + "tmp/2/" | ||
largeFile = chunkDir + "C0BBSGYFN.json.gz" | ||
) | ||
|
||
func Test_exportV3(t *testing.T) { | ||
// TODO: this is manual | ||
t.Run("large file", func(t *testing.T) { | ||
srv := chunktest.NewDirServer(chunkDir, "U0BBSGYFN") | ||
defer srv.Close() | ||
cl := slack.New("", slack.OptionAPIURL(srv.URL+"/api/")) | ||
|
||
ctx := context.Background() | ||
cl.AuthTestContext(ctx) | ||
prov := &chunktest.TestAuth{ | ||
FakeToken: "xoxp-1234567890-1234567890-1234567890-1234567890", | ||
WantHTTPClient: http.DefaultClient, | ||
} | ||
sess, err := slackdump.New(ctx, prov, slackdump.WithSlackClient(cl), slackdump.WithLimits(slackdump.NoLimits)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
output := filepath.Join(baseDir, "output.zip") | ||
fsa, err := fsadapter.New(output) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer fsa.Close() | ||
|
||
list := &structures.EntityList{Include: []string{"C0BBSGYFN"}} | ||
if err := exportV3(ctx, sess, fsa, list, export.Config{List: list}); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} | ||
|
||
func load(t *testing.T, filename string) io.ReadSeeker { | ||
absPath, err := filepath.Abs(largeFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log("test file", absPath) | ||
f, err := os.Open(absPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer f.Close() | ||
gz, err := gzip.NewReader(f) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var buf bytes.Buffer | ||
_, err = io.Copy(&buf, gz) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return bytes.NewReader(buf.Bytes()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package chunktest | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/rusq/slackdump/v2/auth" | ||
) | ||
|
||
// TestAuth to use with the chunktest server. | ||
type TestAuth struct { | ||
FakeToken string | ||
FakeCookies []*http.Cookie | ||
WantValidateError error | ||
WantTestError error | ||
WantHTTPClient *http.Client | ||
WantHTTPClientErr error | ||
} | ||
|
||
// SlackToken should return the Slack Token value. | ||
func (a *TestAuth) SlackToken() string { | ||
return a.FakeToken | ||
} | ||
|
||
// Cookies should return a set of Slack Session cookies. | ||
func (a *TestAuth) Cookies() []*http.Cookie { | ||
return nil | ||
} | ||
|
||
// Type returns the auth type. | ||
func (a *TestAuth) Type() auth.Type { | ||
return auth.Type(255) | ||
} | ||
|
||
// Validate should return error, in case the token or cookies cannot be | ||
// retrieved. | ||
func (a *TestAuth) Validate() error { | ||
// chur | ||
return a.WantValidateError | ||
} | ||
|
||
// Test tests if credentials are valid. | ||
func (a *TestAuth) Test(ctx context.Context) error { | ||
return a.WantTestError | ||
} | ||
|
||
// Client returns an authenticated HTTP client | ||
func (a *TestAuth) HTTPClient() (*http.Client, error) { | ||
return a.WantHTTPClient, a.WantHTTPClientErr | ||
} |
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,90 @@ | ||
package chunktest | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"sync" | ||
|
||
"github.com/rusq/slackdump/v2/internal/chunk" | ||
) | ||
|
||
// DirServer is a test server that serves files from a chunk.Directory. | ||
type DirServer struct { | ||
*httptest.Server | ||
cd *chunk.Directory | ||
userID string | ||
|
||
mu sync.Mutex | ||
ptrs map[string]map[chunk.GroupID]int | ||
} | ||
|
||
func NewDirServer(dir string, currentUserID string) *DirServer { | ||
cd, err := chunk.OpenDir(dir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ds := &DirServer{ | ||
cd: cd, | ||
userID: currentUserID, | ||
ptrs: make(map[string]map[chunk.GroupID]int), | ||
} | ||
ds.init() | ||
return ds | ||
} | ||
|
||
func (s *DirServer) init() { | ||
s.Server = httptest.NewServer(s.dirRouter()) | ||
} | ||
|
||
func (s *DirServer) dirRouter() *http.ServeMux { | ||
mux := http.NewServeMux() | ||
mux.Handle("/api/auth.test", authHandler{s.userID}) | ||
|
||
mux.Handle("/api/conversations.info", s.chunkWrapper(handleConversationsInfo)) | ||
mux.Handle("/api/conversations.history", s.chunkWrapper(handleConversationsHistory)) | ||
mux.Handle("/api/conversations.replies", s.chunkWrapper(handleConversationsReplies)) | ||
mux.Handle("/api/conversations.list", s.chunkWrapper(handleConversationsList)) | ||
mux.Handle("/api/users.list", s.chunkfileWrapper("users", handleUsersList)) | ||
return mux | ||
} | ||
|
||
func (s *DirServer) chunkWrapper(fn func(p *chunk.Player) http.HandlerFunc) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
channel := r.FormValue("channel") | ||
rs, err := s.cd.Open(channel) | ||
if err != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
p := chunk.NewPlayerFromFile(rs) | ||
|
||
s.mu.Lock() | ||
if state, ok := s.ptrs[channel]; ok { | ||
p.SetState(state) | ||
} else { | ||
s.ptrs[channel] = make(map[chunk.GroupID]int) | ||
} | ||
s.mu.Unlock() | ||
fn(p)(w, r) | ||
s.mu.Lock() | ||
s.ptrs[channel] = p.State() | ||
s.mu.Unlock() | ||
}) | ||
} | ||
|
||
func (s *DirServer) chunkfileWrapper(name string, fn func(p *chunk.Player) http.HandlerFunc) http.Handler { | ||
rs, err := s.cd.Open(name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
p := chunk.NewPlayerFromFile(rs) | ||
s.mu.Lock() | ||
if state, ok := s.ptrs["$"+name]; ok { | ||
p.SetState(state) | ||
} else { | ||
s.ptrs["$"+name] = make(map[chunk.GroupID]int) | ||
} | ||
s.mu.Unlock() | ||
|
||
return fn(p) | ||
} |
Oops, something went wrong.