From 861c73cd2dda5bd2f9f8e4f39a5ff249ea57b973 Mon Sep 17 00:00:00 2001 From: Elton Minetto Date: Tue, 2 Oct 2018 21:18:47 -0300 Subject: [PATCH 1/2] Add new functions --- client.go | 25 +++++++++------ user.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 14 deletions(-) diff --git a/client.go b/client.go index c993950..0c59a41 100644 --- a/client.go +++ b/client.go @@ -7,8 +7,6 @@ import ( "net/http" "net/url" "strings" - - "github.com/Sirupsen/logrus" ) // Requester interface allowing testing further down the line @@ -16,6 +14,7 @@ type Requester interface { do(*http.Request) ([]byte, int, error) Get(string) ([]byte, int, error) Post(string, []byte) ([]byte, int, error) + Put(string, []byte) ([]byte, int, error) } // Client struct to keep track of new client @@ -48,7 +47,6 @@ func NewClient(ClientEndpoint string, ClientKey string, ClientUser string) (*Cli // Get resource string func (c *Client) Get(resource string) ([]byte, int, error) { url := fmt.Sprintf("%s%s", c.domain, resource) - logrus.Debugf("URL: %v", url) req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, 0, err @@ -67,25 +65,19 @@ func (c *Client) do(req *http.Request) ([]byte, int, error) { if err != nil { return nil, 0, err } - logrus.Debugf("req Body: %v", body) - logrus.Debugf("Status: %v", resp.StatusCode) - logrus.Debugf("Err: %v", err) if resp.StatusCode != 200 && resp.StatusCode != 404 { err = fmt.Errorf("Received unexpected status %d while trying to retrieve the server data with \"%s\"", resp.StatusCode, string(body)) return nil, resp.StatusCode, err } - logrus.Debug(string(body)) return body, resp.StatusCode, nil } // Post to resource string the data provided func (c *Client) Post(resource string, data []byte) ([]byte, int, error) { - logrus.Debugf("POST Data: %v", string(data)) apiAuth := url.Values{} apiAuth.Set("api_key", c.key) apiAuth.Add("api_username", c.user) url := fmt.Sprintf("%s%s?%s", c.domain, resource, apiAuth.Encode()) - logrus.Debugf("URL: %v", url) req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { return nil, 0, err @@ -94,3 +86,18 @@ func (c *Client) Post(resource string, data []byte) ([]byte, int, error) { return c.do(req) } + +// Put to resource string the data provided +func (c *Client) Put(resource string, data []byte) ([]byte, int, error) { + apiAuth := url.Values{} + apiAuth.Set("api_key", c.key) + apiAuth.Add("api_username", c.user) + url := fmt.Sprintf("%s%s?%s", c.domain, resource, apiAuth.Encode()) + req, err := http.NewRequest("PUT", url, bytes.NewBuffer(data)) + if err != nil { + return nil, 0, err + } + req.Header.Set("Content-Type", "application/json") + + return c.do(req) +} diff --git a/user.go b/user.go index 1ae1686..15854ef 100644 --- a/user.go +++ b/user.go @@ -43,12 +43,13 @@ type UserResponse struct { Moderator bool `json:"moderator"` Admin bool `json:"admin"` } `json:"users,omitempty"` - User userInfo `json:"user"` + User UserInfo `json:"user"` Errors []string `json:"errors,omitempty"` ErrorType string `json:"error_type,omitempty"` } -type userInfo struct { +//UserInfo user information +type UserInfo struct { ID int `json:"id"` Username string `json:"username"` Avatar string `json:"avatar_template"` @@ -88,8 +89,8 @@ type userInfo struct { Messages bool `json:"has_messages"` // false, Mentionable bool `json:"mentionable"` // false } `json:"groups"` // - Featured []string `json:"featured_user_badge_ids"` // [], - Card string `json:"card_badge"` // null + Featured []int64 `json:"featured_user_badge_ids"` // [], + Card string `json:"card_badge"` // null } type user struct { @@ -98,6 +99,13 @@ type user struct { Email string `json:"email"` Password string `json:"password"` Active bool `json:"active"` + Approved bool `json:"approved"` +} + +type invite struct { + Email string `json:"email"` + GroupNames string `json:"group_names"` + CustomMessage string `json:"custom_message"` } // CreateResp typical response after user update @@ -111,6 +119,12 @@ type CreateResp struct { Developer bool `json:"is_developer"` } +// InviteResp typical response after invite created +type InviteResp struct { + Success bool `json:"success"` + Message string `json:"message,omitempty"` +} + // GetUser sends a request for information about the user func GetUser(req Requester, user string) (userInfo *UserResponse, err error) { endpoint := fmt.Sprintf("/users/%s.json", user) @@ -123,13 +137,14 @@ func GetUser(req Requester, user string) (userInfo *UserResponse, err error) { } // CreateUser creates a new user based on details provided -func CreateUser(req Requester, name string, username string, email string, password string, active bool) (response *CreateResp, err error) { +func CreateUser(req Requester, name string, username string, email string, password string, active bool, approved bool) (response *CreateResp, err error) { update := &user{ Name: name, Username: username, Email: email, Password: password, Active: active, + Approved: approved, } data, err := json.Marshal(update) endpoint := "/users" @@ -140,3 +155,73 @@ func CreateUser(req Requester, name string, username string, email string, passw err = json.Unmarshal(body, &response) return response, err } + +//DeactivateUser user +func DeactivateUser(req Requester, userID int) error { + endpoint := fmt.Sprintf("/admin/users/%d/deactivate.json", userID) + var data []byte + _, _, err := req.Put(endpoint, data) + return err +} + +//ActivateUser user +func ActivateUser(req Requester, userID int) error { + endpoint := fmt.Sprintf("/admin/users/%d/activate.json", userID) + var data []byte + _, _, err := req.Put(endpoint, data) + return err +} + +// InviteUser invite a new user +func InviteUser(req Requester, email string, message string) (*InviteResp, error) { + i := &invite{ + Email: email, + CustomMessage: message, + } + data, err := json.Marshal(i) + endpoint := "/invites" + body, _, err := req.Post(endpoint, data) + if err != nil { + return nil, err + } + type response struct { + Success string `json:"success"` + } + var r response + err = json.Unmarshal(body, &r) + invR := &InviteResp{} + if r.Success == "OK" { + invR.Success = true + } + invR.Message = r.Success + return invR, err +} + +// SendPasswordResetEmail Send password reset email +func SendPasswordResetEmail(req Requester, user string) error { + type parameters struct { + Login string `json:"login"` + } + type response struct { + Result string `json:"result"` + UserFound bool `json:"user_found"` + } + p := parameters{ + Login: user, + } + data, err := json.Marshal(p) + endpoint := "/session/forgot_password" + body, _, err := req.Post(endpoint, data) + if err != nil { + return err + } + var r response + err = json.Unmarshal(body, &r) + if err != nil { + return err + } + if !r.UserFound { + return fmt.Errorf("User %s not found", user) + } + return err +} From b9792c36f4ed4c16fccbbef37b4acddc15a49dc7 Mon Sep 17 00:00:00 2001 From: Elton Minetto Date: Tue, 16 Oct 2018 09:32:44 -0300 Subject: [PATCH 2/2] WIP --- client.go | 8 ++++++++ client_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ user_test.go | 15 ++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 client_test.go create mode 100644 user_test.go diff --git a/client.go b/client.go index 0c59a41..0ac0321 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,7 @@ package godisco import ( "bytes" "fmt" + "github.com/Sirupsen/logrus" "io/ioutil" "net/http" "net/url" @@ -47,6 +48,7 @@ func NewClient(ClientEndpoint string, ClientKey string, ClientUser string) (*Cli // Get resource string func (c *Client) Get(resource string) ([]byte, int, error) { url := fmt.Sprintf("%s%s", c.domain, resource) + logrus.Debugf("URL: %v", url) req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, 0, err @@ -65,19 +67,25 @@ func (c *Client) do(req *http.Request) ([]byte, int, error) { if err != nil { return nil, 0, err } + logrus.Debugf("req Body: %v", body) + logrus.Debugf("Status: %v", resp.StatusCode) + logrus.Debugf("Err: %v", err) if resp.StatusCode != 200 && resp.StatusCode != 404 { err = fmt.Errorf("Received unexpected status %d while trying to retrieve the server data with \"%s\"", resp.StatusCode, string(body)) return nil, resp.StatusCode, err } + logrus.Debug(string(body)) return body, resp.StatusCode, nil } // Post to resource string the data provided func (c *Client) Post(resource string, data []byte) ([]byte, int, error) { + logrus.Debugf("POST Data: %v", string(data)) apiAuth := url.Values{} apiAuth.Set("api_key", c.key) apiAuth.Add("api_username", c.user) url := fmt.Sprintf("%s%s?%s", c.domain, resource, apiAuth.Encode()) + logrus.Debugf("URL: %v", url) req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { return nil, 0, err diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..1e18a28 --- /dev/null +++ b/client_test.go @@ -0,0 +1,56 @@ +package godisco + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +type testClient struct { + client *http.Client + domain string + key string + user string +} + +func newTestClient() *testClient { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.Write([]byte(`OK`)) + })) + client := &testClient{ + client: server.Client(), + domain: server.URL, + key: "key", + user: "user", + } + return client +} + +// Get resource string +func (c *testClient) Get(resource string) ([]byte, int, error) { + return []byte{}, 0, nil +} + +func (c *testClient) do(req *http.Request) ([]byte, int, error) { + return []byte{}, 0, nil +} + +// Post to resource string the data provided +func (c *testClient) Post(resource string, data []byte) ([]byte, int, error) { + return []byte{}, 0, nil +} + +// Put to resource string the data provided +func (c *testClient) Put(resource string, data []byte) ([]byte, int, error) { + return []byte{}, 0, nil +} + +func TestPut(t *testing.T) { + client := newTestClient() + _, _, err := client.Put("/resource", []byte{}) + + if err != nil { + t.Errorf("expected: %v actual: %v", nil, err) + } + +} diff --git a/user_test.go b/user_test.go new file mode 100644 index 0000000..af6cecf --- /dev/null +++ b/user_test.go @@ -0,0 +1,15 @@ +package godisco + +import ( + "testing" +) + +func TestDeactivateUser(t *testing.T) { + client := newTestClient() + err := DeactivateUser(client, 1) + + if err != nil { + t.Errorf("expected: %v actual: %v", nil, err) + } + +}