Skip to content

Commit 82230b1

Browse files
committed
feat(users): add users status api
1 parent dcf027e commit 82230b1

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

users/users.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package users
33

44
import (
55
"encoding/json"
6+
"fmt"
67

78
drycc "github.com/drycc/controller-sdk-go"
89
"github.com/drycc/controller-sdk-go/api"
@@ -23,3 +24,25 @@ func List(c *drycc.Client, results int) (api.Users, int, error) {
2324

2425
return users, count, reqErr
2526
}
27+
28+
//Enable user with the controller.
29+
func Enable(c *drycc.Client, username string) error {
30+
u := fmt.Sprintf("/v2/users/%s/enable/", username)
31+
res, err := c.Request("PATCH", u, nil)
32+
33+
if err == nil {
34+
return res.Body.Close()
35+
}
36+
return err
37+
}
38+
39+
//Disable user with the controller.
40+
func Disable(c *drycc.Client, username string) error {
41+
u := fmt.Sprintf("/v2/users/%s/disable/", username)
42+
res, err := c.Request("PATCH", u, nil)
43+
44+
if err == nil {
45+
return res.Body.Close()
46+
}
47+
return err
48+
}

users/users_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
4343
res.Write([]byte(usersFixture))
4444
return
4545
}
46+
if req.URL.Path == "/v2/users/test/enable/" && req.Method == "PATCH" {
47+
res.WriteHeader(http.StatusNoContent)
48+
return
49+
}
50+
if req.URL.Path == "/v2/users/test/disable/" && req.Method == "PATCH" {
51+
res.WriteHeader(http.StatusNoContent)
52+
return
53+
}
4654

4755
fmt.Printf("Unrecongized URL %s\n", req.URL)
4856
res.WriteHeader(http.StatusNotFound)
@@ -86,3 +94,35 @@ func TestUsersList(t *testing.T) {
8694
t.Errorf("Expected %v, Got %v", expected, actual)
8795
}
8896
}
97+
98+
func TestUsersEnable(t *testing.T) {
99+
t.Parallel()
100+
handler := fakeHTTPServer{}
101+
server := httptest.NewServer(handler)
102+
defer server.Close()
103+
104+
drycc, err := drycc.New(false, server.URL, "abc")
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
err = Enable(drycc, "test")
109+
if err != nil {
110+
t.Fatal(err)
111+
}
112+
}
113+
114+
func TestUsersDisable(t *testing.T) {
115+
t.Parallel()
116+
handler := fakeHTTPServer{}
117+
server := httptest.NewServer(handler)
118+
defer server.Close()
119+
120+
drycc, err := drycc.New(false, server.URL, "abc")
121+
if err != nil {
122+
t.Fatal(err)
123+
}
124+
err = Enable(drycc, "test")
125+
if err != nil {
126+
t.Fatal(err)
127+
}
128+
}

0 commit comments

Comments
 (0)