Skip to content

Commit

Permalink
refactor to ease testing
Browse files Browse the repository at this point in the history
BREAKING:
- APIURL is now const, can no longer set it. use OptionAPIURL to
adjust the endpoint for a client.
WARNINGS:
- Take special care if you rely on File upload/download and
SetUserPhotoContext

- refactor internal api to support per client APIURL configuration
- adds a slacktest package (copied and modified from
lusis/slack-test)
  • Loading branch information
James Lawrence authored and james-lawrence committed Apr 4, 2019
1 parent 9c98723 commit 95b04ee
Show file tree
Hide file tree
Showing 59 changed files with 1,868 additions and 417 deletions.
20 changes: 17 additions & 3 deletions Gopkg.lock

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

4 changes: 1 addition & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
ignored = ["github.com/lusis/slack-test"]

[[constraint]]
name = "github.com/gorilla/websocket"
version = "1.2.0"
Expand All @@ -10,7 +8,7 @@ ignored = ["github.com/lusis/slack-test"]

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
version = "0.8.1"

[prune]
go-tests = true
Expand Down
41 changes: 15 additions & 26 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@ package slack

import (
"context"
"errors"
"fmt"
"net/url"
"strings"
)

type adminResponse struct {
OK bool `json:"ok"`
Error string `json:"error"`
}

func adminRequest(ctx context.Context, client httpClient, method string, teamName string, values url.Values, d debug) (*adminResponse, error) {
adminResponse := &adminResponse{}
err := parseAdminResponse(ctx, client, method, teamName, values, adminResponse, d)
func (api *Client) adminRequest(ctx context.Context, method string, teamName string, values url.Values) error {
resp := &SlackResponse{}
err := parseAdminResponse(ctx, api.httpclient, method, teamName, values, resp, api)
if err != nil {
return nil, err
return err
}

if !adminResponse.OK {
return nil, errors.New(adminResponse.Error)
}

return adminResponse, nil
return resp.Err()
}

// DisableUser disabled a user account, given a user ID
Expand All @@ -41,9 +31,8 @@ func (api *Client) DisableUserContext(ctx context.Context, teamName string, uid
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "setInactive", teamName, values, api)
if err != nil {
return fmt.Errorf("Failed to disable user with id '%s': %s", uid, err)
if err := api.adminRequest(ctx, "setInactive", teamName, values); err != nil {
return fmt.Errorf("failed to disable user with id '%s': %s", uid, err)
}

return nil
Expand All @@ -68,7 +57,7 @@ func (api *Client) InviteGuestContext(ctx context.Context, teamName, channel, fi
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "invite", teamName, values, api)
err := api.adminRequest(ctx, "invite", teamName, values)
if err != nil {
return fmt.Errorf("Failed to invite single-channel guest: %s", err)
}
Expand All @@ -95,7 +84,7 @@ func (api *Client) InviteRestrictedContext(ctx context.Context, teamName, channe
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "invite", teamName, values, api)
err := api.adminRequest(ctx, "invite", teamName, values)
if err != nil {
return fmt.Errorf("Failed to restricted account: %s", err)
}
Expand All @@ -119,7 +108,7 @@ func (api *Client) InviteToTeamContext(ctx context.Context, teamName, firstName,
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "invite", teamName, values, api)
err := api.adminRequest(ctx, "invite", teamName, values)
if err != nil {
return fmt.Errorf("Failed to invite to team: %s", err)
}
Expand All @@ -141,7 +130,7 @@ func (api *Client) SetRegularContext(ctx context.Context, teamName, user string)
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "setRegular", teamName, values, api)
err := api.adminRequest(ctx, "setRegular", teamName, values)
if err != nil {
return fmt.Errorf("Failed to change the user (%s) to a regular user: %s", user, err)
}
Expand All @@ -163,7 +152,7 @@ func (api *Client) SendSSOBindingEmailContext(ctx context.Context, teamName, use
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "sendSSOBind", teamName, values, api)
err := api.adminRequest(ctx, "sendSSOBind", teamName, values)
if err != nil {
return fmt.Errorf("Failed to send SSO binding email for user (%s): %s", user, err)
}
Expand All @@ -186,7 +175,7 @@ func (api *Client) SetUltraRestrictedContext(ctx context.Context, teamName, uid,
"_attempts": {"1"},
}

_, err := adminRequest(ctx, api.httpclient, "setUltraRestricted", teamName, values, api)
err := api.adminRequest(ctx, "setUltraRestricted", teamName, values)
if err != nil {
return fmt.Errorf("Failed to ultra-restrict account: %s", err)
}
Expand All @@ -209,9 +198,9 @@ func (api *Client) SetRestrictedContext(ctx context.Context, teamName, uid strin
"channels": {strings.Join(channelIds, ",")},
}

_, err := adminRequest(ctx, api.httpclient, "setRestricted", teamName, values, api)
err := api.adminRequest(ctx, "setRestricted", teamName, values)
if err != nil {
return fmt.Errorf("Failed to restrict account: %s", err)
return fmt.Errorf("failed to restrict account: %s", err)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type AuthRevokeResponse struct {
}

// authRequest sends the actual request, and unmarshals the response
func authRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (*AuthRevokeResponse, error) {
func (api *Client) authRequest(ctx context.Context, path string, values url.Values) (*AuthRevokeResponse, error) {
response := &AuthRevokeResponse{}
err := postSlackMethod(ctx, client, path, values, response, d)
err := api.postMethod(ctx, path, values, response)
if err != nil {
return nil, err
}
Expand All @@ -36,5 +36,5 @@ func (api *Client) SendAuthRevokeContext(ctx context.Context, token string) (*Au
"token": {token},
}

return authRequest(ctx, api.httpclient, "auth.revoke", values, api)
return api.authRequest(ctx, "auth.revoke", values)
}
13 changes: 7 additions & 6 deletions bots.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package slack

import (
"context"
"errors"
"net/url"
)

Expand All @@ -19,15 +18,17 @@ type botResponseFull struct {
SlackResponse
}

func botRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (*botResponseFull, error) {
func (api *Client) botRequest(ctx context.Context, path string, values url.Values) (*botResponseFull, error) {
response := &botResponseFull{}
err := postSlackMethod(ctx, client, path, values, response, d)
err := api.postMethod(ctx, path, values, response)
if err != nil {
return nil, err
}
if !response.Ok {
return nil, errors.New(response.Error)

if err := response.Err(); err != nil {
return nil, err
}

return response, nil
}

Expand All @@ -43,7 +44,7 @@ func (api *Client) GetBotInfoContext(ctx context.Context, bot string) (*Bot, err
"bot": {bot},
}

response, err := botRequest(ctx, api.httpclient, "bots.info", values, api)
response, err := api.botRequest(ctx, "bots.info", values)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func TestGetBotInfo(t *testing.T) {
http.HandleFunc("/bots.info", getBotInfo)

once.Do(startServer)
APIURL = "http://" + serverAddr + "/"
api := New("testing-token")
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

bot, err := api.GetBotInfo("B02875YLA")
if err != nil {
Expand Down
Loading

0 comments on commit 95b04ee

Please sign in to comment.