Skip to content

Commit

Permalink
add edge search test
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 1, 2024
1 parent 62dfff9 commit 236e79c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 10 deletions.
87 changes: 87 additions & 0 deletions internal/edge/assets/search.module.channels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"ok": true,
"module": "channels",
"query": "",
"filters": {},
"pagination": {
"total_count": 6,
"page": 1,
"per_page": 50,
"first": 1,
"last": 6
},
"items": [
{
"iid": "87914a28-8d36-4751-8fd1-7b30cad64ce3",
"id": "CHY5HUESG",
"is_member": false,
"member_count": 2,
"name": "everyone",
"purpose": {
"value": "This channel is for workspace-wide communication and announcements. All members are in this channel.",
"creator": "UHSD97ZA5",
"last_set": 1555493778
}
},
{
"iid": "cd71827f-1d55-4791-a1a2-33d70bffa616",
"id": "C045TUGSSTW",
"is_member": false,
"member_count": 1,
"name": "adapt_w_3dビューア",
"purpose": {
"value": "Issue 44 test",
"creator": "UHSD97ZA5",
"last_set": 1665307423
}
},
{
"iid": "7b596f2f-03c4-4c0f-b528-e13cc993c6ff",
"id": "C04BJATRQRL",
"is_member": false,
"member_count": 1,
"name": "slackdump",
"purpose": {
"value": "",
"creator": "",
"last_set": 0
}
},
{
"iid": "57c2302a-fbe2-48f8-9bc7-3be9c5cdd4f4",
"id": "CHYLGDP0D",
"is_member": false,
"member_count": 3,
"name": "random",
"purpose": {
"value": "A place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber you'd prefer to keep out of more focused work-related channels.",
"creator": "UHSD97ZA5",
"last_set": 1555493778
}
},
{
"iid": "7e91cbe8-e7af-47d9-a9f8-3173cee7977c",
"id": "CHM82GF99",
"is_member": false,
"member_count": 2,
"name": "everything",
"purpose": {
"value": "",
"creator": "",
"last_set": 0
}
},
{
"iid": "7fd17a45-514d-4c40-91be-5dd8afb4b110",
"id": "C011D885FP0",
"is_member": false,
"member_count": 1,
"name": "wakatime",
"purpose": {
"value": "Timesheet",
"creator": "UHSD97ZA5",
"last_set": 1586035666
}
}
]
}
6 changes: 1 addition & 5 deletions internal/edge/client_boot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
_ "embed"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,10 +14,7 @@ import (
var clientUserBootJSON []byte

func TestClient_ClientUserBoot(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(clientUserBootJSON)
}))
srv := testServer(http.StatusOK, clientUserBootJSON)
defer srv.Close()

cl := Client{
Expand Down
4 changes: 3 additions & 1 deletion internal/edge/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func (cl *Client) Raw() *http.Client {
func (cl *Client) Close() error {
if cl.tape != nil {
return cl.tape.Close()

}
return nil
}
Expand Down Expand Up @@ -172,6 +171,9 @@ const (
hdrContentType = "Content-Type"
)

// PostJSON posts a request to the edge API. The request is marshalled to
// JSON and the response is unmarshalled to the req, which must be a pointer
// to a struct.
func (cl *Client) PostJSON(ctx context.Context, path string, req PostRequest) (*http.Response, error) {
if !req.IsTokenSet() {
req.SetToken(cl.token)
Expand Down
9 changes: 9 additions & 0 deletions internal/edge/edge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package edge

import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"

Expand All @@ -19,6 +21,13 @@ var (
testCookie = os.Getenv("EDGE_COOKIE")
)

func testServer(status int, payload []byte) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
w.Write(payload)
}))
}

func TestNew(t *testing.T) {
if testToken == "" {
t.Skip("test token not set")
Expand Down
28 changes: 28 additions & 0 deletions internal/edge/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edge

import (
"context"
_ "embed"
"net/http"
"testing"

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

//go:embed assets/search.module.channels.json
var searchChannelsJSON []byte

func TestClient_SearchChannels(t *testing.T) {
srv := testServer(http.StatusOK, searchChannelsJSON)
defer srv.Close()

cl := Client{
cl: http.DefaultClient,
edgeAPI: srv.URL + "/",
webclientAPI: srv.URL + "/",
}
r, err := cl.SearchChannels(context.Background(), "test")
require.NoError(t, err)
assert.Len(t, r, 6)
}
9 changes: 5 additions & 4 deletions internal/edge/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (cl *Client) GetUsers(ctx context.Context, userID ...string) ([]UserInfo, e
// called again to get the actual user info (see [Client.GetUsers]).
func (cl *Client) UsersInfo(ctx context.Context, req *UsersInfoRequest) (*UserInfoResponse, error) {
var ui UserInfoResponse
if err := cl.callAPI(ctx, &ui, "users/info", req); err != nil {
if err := cl.callEdgeAPI(ctx, &ui, "users/info", req); err != nil {
return nil, err
}
return &ui, nil
Expand All @@ -175,13 +175,14 @@ type ChannelsMembershipResponse struct {
// ChannelsMembership calls channels/membership endpoint.
func (cl *Client) ChannelsMembership(ctx context.Context, req *ChannelsMembershipRequest) (*ChannelsMembershipResponse, error) {
var um ChannelsMembershipResponse
if err := cl.callAPI(ctx, &um, "channels/membership", req); err != nil {
if err := cl.callEdgeAPI(ctx, &um, "channels/membership", req); err != nil {
return nil, err
}
return &um, nil
}

func (cl *Client) callAPI(ctx context.Context, v any, endpoint string, req PostRequest) error {
// callEdgeAPI calls the edge API.
func (cl *Client) callEdgeAPI(ctx context.Context, v any, endpoint string, req PostRequest) error {
r, err := cl.PostJSON(ctx, endpoint, req)
if err != nil && !errors.Is(err, io.EOF) {
return err
Expand Down Expand Up @@ -216,7 +217,7 @@ func (cl *Client) UsersList(ctx context.Context, channelIDs ...string) ([]User,
var uu = make([]User, 0, perRequest)
for {
var ur UsersListResponse
if err := cl.callAPI(ctx, &ur, "users/list", &req); err != nil {
if err := cl.callEdgeAPI(ctx, &ur, "users/list", &req); err != nil {
return nil, err
}
if len(ur.Results) == 0 {
Expand Down

0 comments on commit 236e79c

Please sign in to comment.